2 * Copyright (c) 2015, NVIDIA Corporation.
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.
10 #include <linux/host1x.h>
11 #include <linux/iommu.h>
12 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/reset.h>
20 #include <soc/tegra/pmc.h>
36 struct tegra_drm_client client;
37 struct host1x_channel *channel;
38 struct iommu_domain *domain;
41 struct reset_control *rst;
43 /* Platform configuration */
44 const struct vic_config *config;
47 static inline struct vic *to_vic(struct tegra_drm_client *client)
49 return container_of(client, struct vic, client);
52 static void vic_writel(struct vic *vic, u32 value, unsigned int offset)
54 writel(value, vic->regs + offset);
57 static int vic_runtime_resume(struct device *dev)
59 struct vic *vic = dev_get_drvdata(dev);
62 err = clk_prepare_enable(vic->clk);
68 err = reset_control_deassert(vic->rst);
77 clk_disable_unprepare(vic->clk);
81 static int vic_runtime_suspend(struct device *dev)
83 struct vic *vic = dev_get_drvdata(dev);
86 err = reset_control_assert(vic->rst);
90 usleep_range(2000, 4000);
92 clk_disable_unprepare(vic->clk);
99 static int vic_boot(struct vic *vic)
101 u32 fce_ucode_size, fce_bin_data_offset;
108 /* setup clockgating registers */
109 vic_writel(vic, CG_IDLE_CG_DLY_CNT(4) |
111 CG_WAKEUP_DLY_CNT(4),
112 NV_PVIC_MISC_PRI_VIC_CG);
114 err = falcon_boot(&vic->falcon);
118 hdr = vic->falcon.firmware.vaddr;
119 fce_bin_data_offset = *(u32 *)(hdr + VIC_UCODE_FCE_DATA_OFFSET);
120 hdr = vic->falcon.firmware.vaddr +
121 *(u32 *)(hdr + VIC_UCODE_FCE_HEADER_OFFSET);
122 fce_ucode_size = *(u32 *)(hdr + FCE_UCODE_SIZE_OFFSET);
124 falcon_execute_method(&vic->falcon, VIC_SET_APPLICATION_ID, 1);
125 falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_SIZE,
127 falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_OFFSET,
128 (vic->falcon.firmware.paddr + fce_bin_data_offset)
131 err = falcon_wait_idle(&vic->falcon);
134 "failed to set application ID and FCE base\n");
143 static void *vic_falcon_alloc(struct falcon *falcon, size_t size,
146 struct tegra_drm *tegra = falcon->data;
148 return tegra_drm_alloc(tegra, size, iova);
151 static void vic_falcon_free(struct falcon *falcon, size_t size,
152 dma_addr_t iova, void *va)
154 struct tegra_drm *tegra = falcon->data;
156 return tegra_drm_free(tegra, size, va, iova);
159 static const struct falcon_ops vic_falcon_ops = {
160 .alloc = vic_falcon_alloc,
161 .free = vic_falcon_free
164 static int vic_init(struct host1x_client *client)
166 struct tegra_drm_client *drm = host1x_to_drm_client(client);
167 struct iommu_group *group = iommu_group_get(client->dev);
168 struct drm_device *dev = dev_get_drvdata(client->parent);
169 struct tegra_drm *tegra = dev->dev_private;
170 struct vic *vic = to_vic(drm);
173 if (group && tegra->domain) {
174 err = iommu_attach_group(tegra->domain, group);
176 dev_err(vic->dev, "failed to attach to domain: %d\n",
181 vic->domain = tegra->domain;
184 if (!vic->falcon.data) {
185 vic->falcon.data = tegra;
186 err = falcon_load_firmware(&vic->falcon);
191 vic->channel = host1x_channel_request(client->dev);
197 client->syncpts[0] = host1x_syncpt_request(client, 0);
198 if (!client->syncpts[0]) {
203 err = tegra_drm_register_client(tegra, drm);
210 host1x_syncpt_free(client->syncpts[0]);
212 host1x_channel_put(vic->channel);
214 if (group && tegra->domain)
215 iommu_detach_group(tegra->domain, group);
220 static int vic_exit(struct host1x_client *client)
222 struct tegra_drm_client *drm = host1x_to_drm_client(client);
223 struct iommu_group *group = iommu_group_get(client->dev);
224 struct drm_device *dev = dev_get_drvdata(client->parent);
225 struct tegra_drm *tegra = dev->dev_private;
226 struct vic *vic = to_vic(drm);
229 err = tegra_drm_unregister_client(tegra, drm);
233 host1x_syncpt_free(client->syncpts[0]);
234 host1x_channel_put(vic->channel);
237 iommu_detach_group(vic->domain, group);
244 static const struct host1x_client_ops vic_client_ops = {
249 static int vic_open_channel(struct tegra_drm_client *client,
250 struct tegra_drm_context *context)
252 struct vic *vic = to_vic(client);
255 err = pm_runtime_get_sync(vic->dev);
261 pm_runtime_put(vic->dev);
265 context->channel = host1x_channel_get(vic->channel);
266 if (!context->channel) {
267 pm_runtime_put(vic->dev);
274 static void vic_close_channel(struct tegra_drm_context *context)
276 struct vic *vic = to_vic(context->client);
278 host1x_channel_put(context->channel);
280 pm_runtime_put(vic->dev);
283 static const struct tegra_drm_client_ops vic_ops = {
284 .open_channel = vic_open_channel,
285 .close_channel = vic_close_channel,
286 .submit = tegra_drm_submit,
289 #define NVIDIA_TEGRA_124_VIC_FIRMWARE "nvidia/tegra124/vic03_ucode.bin"
291 static const struct vic_config vic_t124_config = {
292 .firmware = NVIDIA_TEGRA_124_VIC_FIRMWARE,
296 #define NVIDIA_TEGRA_210_VIC_FIRMWARE "nvidia/tegra210/vic04_ucode.bin"
298 static const struct vic_config vic_t210_config = {
299 .firmware = NVIDIA_TEGRA_210_VIC_FIRMWARE,
303 #define NVIDIA_TEGRA_186_VIC_FIRMWARE "nvidia/tegra186/vic04_ucode.bin"
305 static const struct vic_config vic_t186_config = {
306 .firmware = NVIDIA_TEGRA_186_VIC_FIRMWARE,
310 #define NVIDIA_TEGRA_194_VIC_FIRMWARE "nvidia/tegra194/vic.bin"
312 static const struct vic_config vic_t194_config = {
313 .firmware = NVIDIA_TEGRA_194_VIC_FIRMWARE,
317 static const struct of_device_id vic_match[] = {
318 { .compatible = "nvidia,tegra124-vic", .data = &vic_t124_config },
319 { .compatible = "nvidia,tegra210-vic", .data = &vic_t210_config },
320 { .compatible = "nvidia,tegra186-vic", .data = &vic_t186_config },
321 { .compatible = "nvidia,tegra194-vic", .data = &vic_t194_config },
325 static int vic_probe(struct platform_device *pdev)
327 struct device *dev = &pdev->dev;
328 struct host1x_syncpt **syncpts;
329 struct resource *regs;
333 vic = devm_kzalloc(dev, sizeof(*vic), GFP_KERNEL);
337 vic->config = of_device_get_match_data(dev);
339 syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
343 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
345 dev_err(&pdev->dev, "failed to get registers\n");
349 vic->regs = devm_ioremap_resource(dev, regs);
350 if (IS_ERR(vic->regs))
351 return PTR_ERR(vic->regs);
353 vic->clk = devm_clk_get(dev, NULL);
354 if (IS_ERR(vic->clk)) {
355 dev_err(&pdev->dev, "failed to get clock\n");
356 return PTR_ERR(vic->clk);
359 if (!dev->pm_domain) {
360 vic->rst = devm_reset_control_get(dev, "vic");
361 if (IS_ERR(vic->rst)) {
362 dev_err(&pdev->dev, "failed to get reset\n");
363 return PTR_ERR(vic->rst);
367 vic->falcon.dev = dev;
368 vic->falcon.regs = vic->regs;
369 vic->falcon.ops = &vic_falcon_ops;
371 err = falcon_init(&vic->falcon);
375 err = falcon_read_firmware(&vic->falcon, vic->config->firmware);
379 platform_set_drvdata(pdev, vic);
381 INIT_LIST_HEAD(&vic->client.base.list);
382 vic->client.base.ops = &vic_client_ops;
383 vic->client.base.dev = dev;
384 vic->client.base.class = HOST1X_CLASS_VIC;
385 vic->client.base.syncpts = syncpts;
386 vic->client.base.num_syncpts = 1;
389 INIT_LIST_HEAD(&vic->client.list);
390 vic->client.version = vic->config->version;
391 vic->client.ops = &vic_ops;
393 err = host1x_client_register(&vic->client.base);
395 dev_err(dev, "failed to register host1x client: %d\n", err);
396 platform_set_drvdata(pdev, NULL);
400 pm_runtime_enable(&pdev->dev);
401 if (!pm_runtime_enabled(&pdev->dev)) {
402 err = vic_runtime_resume(&pdev->dev);
404 goto unregister_client;
410 host1x_client_unregister(&vic->client.base);
412 falcon_exit(&vic->falcon);
417 static int vic_remove(struct platform_device *pdev)
419 struct vic *vic = platform_get_drvdata(pdev);
422 err = host1x_client_unregister(&vic->client.base);
424 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
429 if (pm_runtime_enabled(&pdev->dev))
430 pm_runtime_disable(&pdev->dev);
432 vic_runtime_suspend(&pdev->dev);
434 falcon_exit(&vic->falcon);
439 static const struct dev_pm_ops vic_pm_ops = {
440 SET_RUNTIME_PM_OPS(vic_runtime_suspend, vic_runtime_resume, NULL)
443 struct platform_driver tegra_vic_driver = {
446 .of_match_table = vic_match,
450 .remove = vic_remove,
453 #if IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC)
454 MODULE_FIRMWARE(NVIDIA_TEGRA_124_VIC_FIRMWARE);
456 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC)
457 MODULE_FIRMWARE(NVIDIA_TEGRA_210_VIC_FIRMWARE);
459 #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC)
460 MODULE_FIRMWARE(NVIDIA_TEGRA_186_VIC_FIRMWARE);
462 #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC)
463 MODULE_FIRMWARE(NVIDIA_TEGRA_194_VIC_FIRMWARE);