1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 20ae797a8SArto Merilainen /* 30ae797a8SArto Merilainen * Copyright (c) 2015, NVIDIA Corporation. 40ae797a8SArto Merilainen */ 50ae797a8SArto Merilainen 60ae797a8SArto Merilainen #include <linux/clk.h> 7eb1df694SSam Ravnborg #include <linux/delay.h> 80ae797a8SArto Merilainen #include <linux/host1x.h> 90ae797a8SArto Merilainen #include <linux/iommu.h> 100ae797a8SArto Merilainen #include <linux/module.h> 110ae797a8SArto Merilainen #include <linux/of.h> 120ae797a8SArto Merilainen #include <linux/of_device.h> 130ae797a8SArto Merilainen #include <linux/of_platform.h> 140ae797a8SArto Merilainen #include <linux/platform_device.h> 150ae797a8SArto Merilainen #include <linux/pm_runtime.h> 160ae797a8SArto Merilainen #include <linux/reset.h> 170ae797a8SArto Merilainen 180ae797a8SArto Merilainen #include <soc/tegra/pmc.h> 190ae797a8SArto Merilainen 200ae797a8SArto Merilainen #include "drm.h" 210ae797a8SArto Merilainen #include "falcon.h" 220ae797a8SArto Merilainen #include "vic.h" 230ae797a8SArto Merilainen 240ae797a8SArto Merilainen struct vic_config { 250ae797a8SArto Merilainen const char *firmware; 26acae8a9dSThierry Reding unsigned int version; 27f3779cb1SThierry Reding bool supports_sid; 280ae797a8SArto Merilainen }; 290ae797a8SArto Merilainen 300ae797a8SArto Merilainen struct vic { 310ae797a8SArto Merilainen struct falcon falcon; 320ae797a8SArto Merilainen bool booted; 330ae797a8SArto Merilainen 340ae797a8SArto Merilainen void __iomem *regs; 350ae797a8SArto Merilainen struct tegra_drm_client client; 360ae797a8SArto Merilainen struct host1x_channel *channel; 370ae797a8SArto Merilainen struct device *dev; 380ae797a8SArto Merilainen struct clk *clk; 390dc34e19SThierry Reding struct reset_control *rst; 400ae797a8SArto Merilainen 410ae797a8SArto Merilainen /* Platform configuration */ 420ae797a8SArto Merilainen const struct vic_config *config; 430ae797a8SArto Merilainen }; 440ae797a8SArto Merilainen 450ae797a8SArto Merilainen static inline struct vic *to_vic(struct tegra_drm_client *client) 460ae797a8SArto Merilainen { 470ae797a8SArto Merilainen return container_of(client, struct vic, client); 480ae797a8SArto Merilainen } 490ae797a8SArto Merilainen 500ae797a8SArto Merilainen static void vic_writel(struct vic *vic, u32 value, unsigned int offset) 510ae797a8SArto Merilainen { 520ae797a8SArto Merilainen writel(value, vic->regs + offset); 530ae797a8SArto Merilainen } 540ae797a8SArto Merilainen 550ae797a8SArto Merilainen static int vic_runtime_resume(struct device *dev) 560ae797a8SArto Merilainen { 570ae797a8SArto Merilainen struct vic *vic = dev_get_drvdata(dev); 580dc34e19SThierry Reding int err; 590ae797a8SArto Merilainen 600dc34e19SThierry Reding err = clk_prepare_enable(vic->clk); 610dc34e19SThierry Reding if (err < 0) 620dc34e19SThierry Reding return err; 630dc34e19SThierry Reding 640dc34e19SThierry Reding usleep_range(10, 20); 650dc34e19SThierry Reding 660dc34e19SThierry Reding err = reset_control_deassert(vic->rst); 670dc34e19SThierry Reding if (err < 0) 680dc34e19SThierry Reding goto disable; 690dc34e19SThierry Reding 700dc34e19SThierry Reding usleep_range(10, 20); 710dc34e19SThierry Reding 720dc34e19SThierry Reding return 0; 730dc34e19SThierry Reding 740dc34e19SThierry Reding disable: 750dc34e19SThierry Reding clk_disable_unprepare(vic->clk); 760dc34e19SThierry Reding return err; 770ae797a8SArto Merilainen } 780ae797a8SArto Merilainen 790ae797a8SArto Merilainen static int vic_runtime_suspend(struct device *dev) 800ae797a8SArto Merilainen { 810ae797a8SArto Merilainen struct vic *vic = dev_get_drvdata(dev); 820dc34e19SThierry Reding int err; 830dc34e19SThierry Reding 840dc34e19SThierry Reding err = reset_control_assert(vic->rst); 850dc34e19SThierry Reding if (err < 0) 860dc34e19SThierry Reding return err; 870dc34e19SThierry Reding 880dc34e19SThierry Reding usleep_range(2000, 4000); 890ae797a8SArto Merilainen 900ae797a8SArto Merilainen clk_disable_unprepare(vic->clk); 910ae797a8SArto Merilainen 920ae797a8SArto Merilainen vic->booted = false; 930ae797a8SArto Merilainen 940ae797a8SArto Merilainen return 0; 950ae797a8SArto Merilainen } 960ae797a8SArto Merilainen 970ae797a8SArto Merilainen static int vic_boot(struct vic *vic) 980ae797a8SArto Merilainen { 99dd631e8aSThierry Reding #ifdef CONFIG_IOMMU_API 100dd631e8aSThierry Reding struct iommu_fwspec *spec = dev_iommu_fwspec_get(vic->dev); 101dd631e8aSThierry Reding #endif 1020ae797a8SArto Merilainen u32 fce_ucode_size, fce_bin_data_offset; 1030ae797a8SArto Merilainen void *hdr; 1040ae797a8SArto Merilainen int err = 0; 1050ae797a8SArto Merilainen 1060ae797a8SArto Merilainen if (vic->booted) 1070ae797a8SArto Merilainen return 0; 1080ae797a8SArto Merilainen 109509869a2SAnders Roxell #ifdef CONFIG_IOMMU_API 110dd631e8aSThierry Reding if (vic->config->supports_sid && spec) { 111f3779cb1SThierry Reding u32 value; 112f3779cb1SThierry Reding 113f3779cb1SThierry Reding value = TRANSCFG_ATT(1, TRANSCFG_SID_FALCON) | 114f3779cb1SThierry Reding TRANSCFG_ATT(0, TRANSCFG_SID_HW); 115f3779cb1SThierry Reding vic_writel(vic, value, VIC_TFBIF_TRANSCFG); 116f3779cb1SThierry Reding 117dd631e8aSThierry Reding if (spec->num_ids > 0) { 118f3779cb1SThierry Reding value = spec->ids[0] & 0xffff; 119f3779cb1SThierry Reding 120f3779cb1SThierry Reding vic_writel(vic, value, VIC_THI_STREAMID0); 121f3779cb1SThierry Reding vic_writel(vic, value, VIC_THI_STREAMID1); 122f3779cb1SThierry Reding } 123f3779cb1SThierry Reding } 124509869a2SAnders Roxell #endif 125f3779cb1SThierry Reding 1260ae797a8SArto Merilainen /* setup clockgating registers */ 1270ae797a8SArto Merilainen vic_writel(vic, CG_IDLE_CG_DLY_CNT(4) | 1280ae797a8SArto Merilainen CG_IDLE_CG_EN | 1290ae797a8SArto Merilainen CG_WAKEUP_DLY_CNT(4), 1300ae797a8SArto Merilainen NV_PVIC_MISC_PRI_VIC_CG); 1310ae797a8SArto Merilainen 1320ae797a8SArto Merilainen err = falcon_boot(&vic->falcon); 1330ae797a8SArto Merilainen if (err < 0) 1340ae797a8SArto Merilainen return err; 1350ae797a8SArto Merilainen 136d972d624SThierry Reding hdr = vic->falcon.firmware.virt; 1370ae797a8SArto Merilainen fce_bin_data_offset = *(u32 *)(hdr + VIC_UCODE_FCE_DATA_OFFSET); 138*58ef3aebSMikko Perttunen 139*58ef3aebSMikko Perttunen falcon_execute_method(&vic->falcon, VIC_SET_APPLICATION_ID, 1); 140*58ef3aebSMikko Perttunen 141*58ef3aebSMikko Perttunen /* Old VIC firmware needs kernel help with setting up FCE microcode. */ 142*58ef3aebSMikko Perttunen if (fce_bin_data_offset != 0x0 && fce_bin_data_offset != 0xa5a5a5a5) { 143d972d624SThierry Reding hdr = vic->falcon.firmware.virt + 1440ae797a8SArto Merilainen *(u32 *)(hdr + VIC_UCODE_FCE_HEADER_OFFSET); 1450ae797a8SArto Merilainen fce_ucode_size = *(u32 *)(hdr + FCE_UCODE_SIZE_OFFSET); 1460ae797a8SArto Merilainen 1470ae797a8SArto Merilainen falcon_execute_method(&vic->falcon, VIC_SET_FCE_UCODE_SIZE, 1480ae797a8SArto Merilainen fce_ucode_size); 149*58ef3aebSMikko Perttunen falcon_execute_method( 150*58ef3aebSMikko Perttunen &vic->falcon, VIC_SET_FCE_UCODE_OFFSET, 151*58ef3aebSMikko Perttunen (vic->falcon.firmware.iova + fce_bin_data_offset) >> 8); 152*58ef3aebSMikko Perttunen } 1530ae797a8SArto Merilainen 1540ae797a8SArto Merilainen err = falcon_wait_idle(&vic->falcon); 1550ae797a8SArto Merilainen if (err < 0) { 1560ae797a8SArto Merilainen dev_err(vic->dev, 1570ae797a8SArto Merilainen "failed to set application ID and FCE base\n"); 1580ae797a8SArto Merilainen return err; 1590ae797a8SArto Merilainen } 1600ae797a8SArto Merilainen 1610ae797a8SArto Merilainen vic->booted = true; 1620ae797a8SArto Merilainen 1630ae797a8SArto Merilainen return 0; 1640ae797a8SArto Merilainen } 1650ae797a8SArto Merilainen 1660ae797a8SArto Merilainen static int vic_init(struct host1x_client *client) 1670ae797a8SArto Merilainen { 1680ae797a8SArto Merilainen struct tegra_drm_client *drm = host1x_to_drm_client(client); 169608f43adSThierry Reding struct drm_device *dev = dev_get_drvdata(client->host); 1700ae797a8SArto Merilainen struct tegra_drm *tegra = dev->dev_private; 1710ae797a8SArto Merilainen struct vic *vic = to_vic(drm); 1720ae797a8SArto Merilainen int err; 1730ae797a8SArto Merilainen 1747edd7961SThierry Reding err = host1x_client_iommu_attach(client); 175a8817489SThierry Reding if (err < 0 && err != -ENODEV) { 1767baa943eSThierry Reding dev_err(vic->dev, "failed to attach to domain: %d\n", err); 1770ae797a8SArto Merilainen return err; 1780ae797a8SArto Merilainen } 1790ae797a8SArto Merilainen 180caccddcfSThierry Reding vic->channel = host1x_channel_request(client); 1810ae797a8SArto Merilainen if (!vic->channel) { 1820ae797a8SArto Merilainen err = -ENOMEM; 183bc8828bdSThierry Reding goto detach; 1840ae797a8SArto Merilainen } 1850ae797a8SArto Merilainen 186617dd7ccSThierry Reding client->syncpts[0] = host1x_syncpt_request(client, 0); 1870ae797a8SArto Merilainen if (!client->syncpts[0]) { 1880ae797a8SArto Merilainen err = -ENOMEM; 1890ae797a8SArto Merilainen goto free_channel; 1900ae797a8SArto Merilainen } 1910ae797a8SArto Merilainen 1920ae797a8SArto Merilainen err = tegra_drm_register_client(tegra, drm); 1930ae797a8SArto Merilainen if (err < 0) 1940ae797a8SArto Merilainen goto free_syncpt; 1950ae797a8SArto Merilainen 19647b15779SThierry Reding /* 19747b15779SThierry Reding * Inherit the DMA parameters (such as maximum segment size) from the 198608f43adSThierry Reding * parent host1x device. 19947b15779SThierry Reding */ 200608f43adSThierry Reding client->dev->dma_parms = client->host->dma_parms; 20147b15779SThierry Reding 2020ae797a8SArto Merilainen return 0; 2030ae797a8SArto Merilainen 2040ae797a8SArto Merilainen free_syncpt: 2050ae797a8SArto Merilainen host1x_syncpt_free(client->syncpts[0]); 2060ae797a8SArto Merilainen free_channel: 2078474b025SMikko Perttunen host1x_channel_put(vic->channel); 208bc8828bdSThierry Reding detach: 209aacdf198SThierry Reding host1x_client_iommu_detach(client); 2100ae797a8SArto Merilainen 2110ae797a8SArto Merilainen return err; 2120ae797a8SArto Merilainen } 2130ae797a8SArto Merilainen 2140ae797a8SArto Merilainen static int vic_exit(struct host1x_client *client) 2150ae797a8SArto Merilainen { 2160ae797a8SArto Merilainen struct tegra_drm_client *drm = host1x_to_drm_client(client); 217608f43adSThierry Reding struct drm_device *dev = dev_get_drvdata(client->host); 2180ae797a8SArto Merilainen struct tegra_drm *tegra = dev->dev_private; 2190ae797a8SArto Merilainen struct vic *vic = to_vic(drm); 2200ae797a8SArto Merilainen int err; 2210ae797a8SArto Merilainen 22247b15779SThierry Reding /* avoid a dangling pointer just in case this disappears */ 22347b15779SThierry Reding client->dev->dma_parms = NULL; 22447b15779SThierry Reding 2250ae797a8SArto Merilainen err = tegra_drm_unregister_client(tegra, drm); 2260ae797a8SArto Merilainen if (err < 0) 2270ae797a8SArto Merilainen return err; 2280ae797a8SArto Merilainen 2290ae797a8SArto Merilainen host1x_syncpt_free(client->syncpts[0]); 2308474b025SMikko Perttunen host1x_channel_put(vic->channel); 231aacdf198SThierry Reding host1x_client_iommu_detach(client); 2320ae797a8SArto Merilainen 233d972d624SThierry Reding if (client->group) { 234d972d624SThierry Reding dma_unmap_single(vic->dev, vic->falcon.firmware.phys, 235d972d624SThierry Reding vic->falcon.firmware.size, DMA_TO_DEVICE); 23620e7dce2SThierry Reding tegra_drm_free(tegra, vic->falcon.firmware.size, 237d972d624SThierry Reding vic->falcon.firmware.virt, 238d972d624SThierry Reding vic->falcon.firmware.iova); 239d972d624SThierry Reding } else { 24020e7dce2SThierry Reding dma_free_coherent(vic->dev, vic->falcon.firmware.size, 241d972d624SThierry Reding vic->falcon.firmware.virt, 242d972d624SThierry Reding vic->falcon.firmware.iova); 243d972d624SThierry Reding } 24420e7dce2SThierry Reding 2450ae797a8SArto Merilainen return 0; 2460ae797a8SArto Merilainen } 2470ae797a8SArto Merilainen 2480ae797a8SArto Merilainen static const struct host1x_client_ops vic_client_ops = { 2490ae797a8SArto Merilainen .init = vic_init, 2500ae797a8SArto Merilainen .exit = vic_exit, 2510ae797a8SArto Merilainen }; 2520ae797a8SArto Merilainen 25377a0b09dSThierry Reding static int vic_load_firmware(struct vic *vic) 25477a0b09dSThierry Reding { 25520e7dce2SThierry Reding struct host1x_client *client = &vic->client.base; 25620e7dce2SThierry Reding struct tegra_drm *tegra = vic->client.drm; 257d972d624SThierry Reding dma_addr_t iova; 25820e7dce2SThierry Reding size_t size; 25920e7dce2SThierry Reding void *virt; 26077a0b09dSThierry Reding int err; 26177a0b09dSThierry Reding 262d972d624SThierry Reding if (vic->falcon.firmware.virt) 26377a0b09dSThierry Reding return 0; 26477a0b09dSThierry Reding 26577a0b09dSThierry Reding err = falcon_read_firmware(&vic->falcon, vic->config->firmware); 26677a0b09dSThierry Reding if (err < 0) 26720e7dce2SThierry Reding return err; 26820e7dce2SThierry Reding 26920e7dce2SThierry Reding size = vic->falcon.firmware.size; 27020e7dce2SThierry Reding 27120e7dce2SThierry Reding if (!client->group) { 272d972d624SThierry Reding virt = dma_alloc_coherent(vic->dev, size, &iova, GFP_KERNEL); 27320e7dce2SThierry Reding 274d972d624SThierry Reding err = dma_mapping_error(vic->dev, iova); 27520e7dce2SThierry Reding if (err < 0) 27620e7dce2SThierry Reding return err; 27720e7dce2SThierry Reding } else { 278d972d624SThierry Reding virt = tegra_drm_alloc(tegra, size, &iova); 27920e7dce2SThierry Reding } 28020e7dce2SThierry Reding 281d972d624SThierry Reding vic->falcon.firmware.virt = virt; 282d972d624SThierry Reding vic->falcon.firmware.iova = iova; 28377a0b09dSThierry Reding 28477a0b09dSThierry Reding err = falcon_load_firmware(&vic->falcon); 28577a0b09dSThierry Reding if (err < 0) 28677a0b09dSThierry Reding goto cleanup; 28777a0b09dSThierry Reding 28820e7dce2SThierry Reding /* 28920e7dce2SThierry Reding * In this case we have received an IOVA from the shared domain, so we 29020e7dce2SThierry Reding * need to make sure to get the physical address so that the DMA API 29120e7dce2SThierry Reding * knows what memory pages to flush the cache for. 29220e7dce2SThierry Reding */ 29320e7dce2SThierry Reding if (client->group) { 294d972d624SThierry Reding dma_addr_t phys; 295d972d624SThierry Reding 29620e7dce2SThierry Reding phys = dma_map_single(vic->dev, virt, size, DMA_TO_DEVICE); 29720e7dce2SThierry Reding 29820e7dce2SThierry Reding err = dma_mapping_error(vic->dev, phys); 29920e7dce2SThierry Reding if (err < 0) 30020e7dce2SThierry Reding goto cleanup; 30120e7dce2SThierry Reding 302d972d624SThierry Reding vic->falcon.firmware.phys = phys; 30320e7dce2SThierry Reding } 30420e7dce2SThierry Reding 30577a0b09dSThierry Reding return 0; 30677a0b09dSThierry Reding 30777a0b09dSThierry Reding cleanup: 30820e7dce2SThierry Reding if (!client->group) 309d972d624SThierry Reding dma_free_coherent(vic->dev, size, virt, iova); 31020e7dce2SThierry Reding else 311d972d624SThierry Reding tegra_drm_free(tegra, size, virt, iova); 31220e7dce2SThierry Reding 31377a0b09dSThierry Reding return err; 31477a0b09dSThierry Reding } 31577a0b09dSThierry Reding 3160ae797a8SArto Merilainen static int vic_open_channel(struct tegra_drm_client *client, 3170ae797a8SArto Merilainen struct tegra_drm_context *context) 3180ae797a8SArto Merilainen { 3190ae797a8SArto Merilainen struct vic *vic = to_vic(client); 3200ae797a8SArto Merilainen int err; 3210ae797a8SArto Merilainen 3220ae797a8SArto Merilainen err = pm_runtime_get_sync(vic->dev); 3230ae797a8SArto Merilainen if (err < 0) 3240ae797a8SArto Merilainen return err; 3250ae797a8SArto Merilainen 32677a0b09dSThierry Reding err = vic_load_firmware(vic); 32777a0b09dSThierry Reding if (err < 0) 32877a0b09dSThierry Reding goto rpm_put; 32977a0b09dSThierry Reding 3300ae797a8SArto Merilainen err = vic_boot(vic); 33177a0b09dSThierry Reding if (err < 0) 33277a0b09dSThierry Reding goto rpm_put; 3330ae797a8SArto Merilainen 3340ae797a8SArto Merilainen context->channel = host1x_channel_get(vic->channel); 3350ae797a8SArto Merilainen if (!context->channel) { 33677a0b09dSThierry Reding err = -ENOMEM; 33777a0b09dSThierry Reding goto rpm_put; 3380ae797a8SArto Merilainen } 3390ae797a8SArto Merilainen 3400ae797a8SArto Merilainen return 0; 34177a0b09dSThierry Reding 34277a0b09dSThierry Reding rpm_put: 34377a0b09dSThierry Reding pm_runtime_put(vic->dev); 34477a0b09dSThierry Reding return err; 3450ae797a8SArto Merilainen } 3460ae797a8SArto Merilainen 3470ae797a8SArto Merilainen static void vic_close_channel(struct tegra_drm_context *context) 3480ae797a8SArto Merilainen { 3490ae797a8SArto Merilainen struct vic *vic = to_vic(context->client); 3500ae797a8SArto Merilainen 3510ae797a8SArto Merilainen host1x_channel_put(context->channel); 3520ae797a8SArto Merilainen 3530ae797a8SArto Merilainen pm_runtime_put(vic->dev); 3540ae797a8SArto Merilainen } 3550ae797a8SArto Merilainen 3560ae797a8SArto Merilainen static const struct tegra_drm_client_ops vic_ops = { 3570ae797a8SArto Merilainen .open_channel = vic_open_channel, 3580ae797a8SArto Merilainen .close_channel = vic_close_channel, 3590ae797a8SArto Merilainen .submit = tegra_drm_submit, 3600ae797a8SArto Merilainen }; 3610ae797a8SArto Merilainen 362788ff4b6SNicolas Chauvet #define NVIDIA_TEGRA_124_VIC_FIRMWARE "nvidia/tegra124/vic03_ucode.bin" 363788ff4b6SNicolas Chauvet 3640ae797a8SArto Merilainen static const struct vic_config vic_t124_config = { 365788ff4b6SNicolas Chauvet .firmware = NVIDIA_TEGRA_124_VIC_FIRMWARE, 366acae8a9dSThierry Reding .version = 0x40, 367f3779cb1SThierry Reding .supports_sid = false, 3680ae797a8SArto Merilainen }; 3690ae797a8SArto Merilainen 370788ff4b6SNicolas Chauvet #define NVIDIA_TEGRA_210_VIC_FIRMWARE "nvidia/tegra210/vic04_ucode.bin" 371788ff4b6SNicolas Chauvet 3720ae797a8SArto Merilainen static const struct vic_config vic_t210_config = { 373788ff4b6SNicolas Chauvet .firmware = NVIDIA_TEGRA_210_VIC_FIRMWARE, 374acae8a9dSThierry Reding .version = 0x21, 375f3779cb1SThierry Reding .supports_sid = false, 3760ae797a8SArto Merilainen }; 3770ae797a8SArto Merilainen 3786e44b9adSMikko Perttunen #define NVIDIA_TEGRA_186_VIC_FIRMWARE "nvidia/tegra186/vic04_ucode.bin" 3796e44b9adSMikko Perttunen 3806e44b9adSMikko Perttunen static const struct vic_config vic_t186_config = { 3816e44b9adSMikko Perttunen .firmware = NVIDIA_TEGRA_186_VIC_FIRMWARE, 382acae8a9dSThierry Reding .version = 0x18, 383f3779cb1SThierry Reding .supports_sid = true, 3846e44b9adSMikko Perttunen }; 3856e44b9adSMikko Perttunen 386d6b9bc02SThierry Reding #define NVIDIA_TEGRA_194_VIC_FIRMWARE "nvidia/tegra194/vic.bin" 387d6b9bc02SThierry Reding 388d6b9bc02SThierry Reding static const struct vic_config vic_t194_config = { 389d6b9bc02SThierry Reding .firmware = NVIDIA_TEGRA_194_VIC_FIRMWARE, 390d6b9bc02SThierry Reding .version = 0x19, 391f3779cb1SThierry Reding .supports_sid = true, 392d6b9bc02SThierry Reding }; 393d6b9bc02SThierry Reding 39482d73874SThierry Reding static const struct of_device_id tegra_vic_of_match[] = { 3950ae797a8SArto Merilainen { .compatible = "nvidia,tegra124-vic", .data = &vic_t124_config }, 3960ae797a8SArto Merilainen { .compatible = "nvidia,tegra210-vic", .data = &vic_t210_config }, 3976e44b9adSMikko Perttunen { .compatible = "nvidia,tegra186-vic", .data = &vic_t186_config }, 398d6b9bc02SThierry Reding { .compatible = "nvidia,tegra194-vic", .data = &vic_t194_config }, 3990ae797a8SArto Merilainen { }, 4000ae797a8SArto Merilainen }; 40182d73874SThierry Reding MODULE_DEVICE_TABLE(of, tegra_vic_of_match); 4020ae797a8SArto Merilainen 4030ae797a8SArto Merilainen static int vic_probe(struct platform_device *pdev) 4040ae797a8SArto Merilainen { 4050ae797a8SArto Merilainen struct device *dev = &pdev->dev; 4060ae797a8SArto Merilainen struct host1x_syncpt **syncpts; 4070ae797a8SArto Merilainen struct resource *regs; 4080ae797a8SArto Merilainen struct vic *vic; 4090ae797a8SArto Merilainen int err; 4100ae797a8SArto Merilainen 411d5ad0e3dSThierry Reding /* inherit DMA mask from host1x parent */ 412d5ad0e3dSThierry Reding err = dma_coerce_mask_and_coherent(dev, *dev->parent->dma_mask); 413d5ad0e3dSThierry Reding if (err < 0) { 414d5ad0e3dSThierry Reding dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err); 415d5ad0e3dSThierry Reding return err; 416d5ad0e3dSThierry Reding } 417d5ad0e3dSThierry Reding 4180ae797a8SArto Merilainen vic = devm_kzalloc(dev, sizeof(*vic), GFP_KERNEL); 4190ae797a8SArto Merilainen if (!vic) 4200ae797a8SArto Merilainen return -ENOMEM; 4210ae797a8SArto Merilainen 422829ce7a6SThierry Reding vic->config = of_device_get_match_data(dev); 423829ce7a6SThierry Reding 4240ae797a8SArto Merilainen syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL); 4250ae797a8SArto Merilainen if (!syncpts) 4260ae797a8SArto Merilainen return -ENOMEM; 4270ae797a8SArto Merilainen 4280ae797a8SArto Merilainen regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); 4290ae797a8SArto Merilainen if (!regs) { 4300ae797a8SArto Merilainen dev_err(&pdev->dev, "failed to get registers\n"); 4310ae797a8SArto Merilainen return -ENXIO; 4320ae797a8SArto Merilainen } 4330ae797a8SArto Merilainen 4340ae797a8SArto Merilainen vic->regs = devm_ioremap_resource(dev, regs); 4350ae797a8SArto Merilainen if (IS_ERR(vic->regs)) 4360ae797a8SArto Merilainen return PTR_ERR(vic->regs); 4370ae797a8SArto Merilainen 4380ae797a8SArto Merilainen vic->clk = devm_clk_get(dev, NULL); 4390ae797a8SArto Merilainen if (IS_ERR(vic->clk)) { 4400ae797a8SArto Merilainen dev_err(&pdev->dev, "failed to get clock\n"); 4410ae797a8SArto Merilainen return PTR_ERR(vic->clk); 4420ae797a8SArto Merilainen } 4430ae797a8SArto Merilainen 4440dc34e19SThierry Reding if (!dev->pm_domain) { 4450dc34e19SThierry Reding vic->rst = devm_reset_control_get(dev, "vic"); 4460dc34e19SThierry Reding if (IS_ERR(vic->rst)) { 4470dc34e19SThierry Reding dev_err(&pdev->dev, "failed to get reset\n"); 4480dc34e19SThierry Reding return PTR_ERR(vic->rst); 4490dc34e19SThierry Reding } 4500dc34e19SThierry Reding } 4510dc34e19SThierry Reding 4520ae797a8SArto Merilainen vic->falcon.dev = dev; 4530ae797a8SArto Merilainen vic->falcon.regs = vic->regs; 4540ae797a8SArto Merilainen 4550ae797a8SArto Merilainen err = falcon_init(&vic->falcon); 4560ae797a8SArto Merilainen if (err < 0) 4570ae797a8SArto Merilainen return err; 4580ae797a8SArto Merilainen 4590ae797a8SArto Merilainen platform_set_drvdata(pdev, vic); 4600ae797a8SArto Merilainen 4610ae797a8SArto Merilainen INIT_LIST_HEAD(&vic->client.base.list); 4620ae797a8SArto Merilainen vic->client.base.ops = &vic_client_ops; 4630ae797a8SArto Merilainen vic->client.base.dev = dev; 4640ae797a8SArto Merilainen vic->client.base.class = HOST1X_CLASS_VIC; 4650ae797a8SArto Merilainen vic->client.base.syncpts = syncpts; 4660ae797a8SArto Merilainen vic->client.base.num_syncpts = 1; 4670ae797a8SArto Merilainen vic->dev = dev; 4680ae797a8SArto Merilainen 4690ae797a8SArto Merilainen INIT_LIST_HEAD(&vic->client.list); 470acae8a9dSThierry Reding vic->client.version = vic->config->version; 4710ae797a8SArto Merilainen vic->client.ops = &vic_ops; 4720ae797a8SArto Merilainen 4730ae797a8SArto Merilainen err = host1x_client_register(&vic->client.base); 4740ae797a8SArto Merilainen if (err < 0) { 4750ae797a8SArto Merilainen dev_err(dev, "failed to register host1x client: %d\n", err); 4760ae797a8SArto Merilainen goto exit_falcon; 4770ae797a8SArto Merilainen } 4780ae797a8SArto Merilainen 4790ae797a8SArto Merilainen pm_runtime_enable(&pdev->dev); 4800ae797a8SArto Merilainen if (!pm_runtime_enabled(&pdev->dev)) { 4810ae797a8SArto Merilainen err = vic_runtime_resume(&pdev->dev); 4820ae797a8SArto Merilainen if (err < 0) 4830ae797a8SArto Merilainen goto unregister_client; 4840ae797a8SArto Merilainen } 4850ae797a8SArto Merilainen 4860ae797a8SArto Merilainen return 0; 4870ae797a8SArto Merilainen 4880ae797a8SArto Merilainen unregister_client: 4890ae797a8SArto Merilainen host1x_client_unregister(&vic->client.base); 4900ae797a8SArto Merilainen exit_falcon: 4910ae797a8SArto Merilainen falcon_exit(&vic->falcon); 4920ae797a8SArto Merilainen 4930ae797a8SArto Merilainen return err; 4940ae797a8SArto Merilainen } 4950ae797a8SArto Merilainen 4960ae797a8SArto Merilainen static int vic_remove(struct platform_device *pdev) 4970ae797a8SArto Merilainen { 4980ae797a8SArto Merilainen struct vic *vic = platform_get_drvdata(pdev); 4990ae797a8SArto Merilainen int err; 5000ae797a8SArto Merilainen 5010ae797a8SArto Merilainen err = host1x_client_unregister(&vic->client.base); 5020ae797a8SArto Merilainen if (err < 0) { 5030ae797a8SArto Merilainen dev_err(&pdev->dev, "failed to unregister host1x client: %d\n", 5040ae797a8SArto Merilainen err); 5050ae797a8SArto Merilainen return err; 5060ae797a8SArto Merilainen } 5070ae797a8SArto Merilainen 5080ae797a8SArto Merilainen if (pm_runtime_enabled(&pdev->dev)) 5090ae797a8SArto Merilainen pm_runtime_disable(&pdev->dev); 5100ae797a8SArto Merilainen else 5110ae797a8SArto Merilainen vic_runtime_suspend(&pdev->dev); 5120ae797a8SArto Merilainen 5130ae797a8SArto Merilainen falcon_exit(&vic->falcon); 5140ae797a8SArto Merilainen 5150ae797a8SArto Merilainen return 0; 5160ae797a8SArto Merilainen } 5170ae797a8SArto Merilainen 5180ae797a8SArto Merilainen static const struct dev_pm_ops vic_pm_ops = { 5190ae797a8SArto Merilainen SET_RUNTIME_PM_OPS(vic_runtime_suspend, vic_runtime_resume, NULL) 5200ae797a8SArto Merilainen }; 5210ae797a8SArto Merilainen 5220ae797a8SArto Merilainen struct platform_driver tegra_vic_driver = { 5230ae797a8SArto Merilainen .driver = { 5240ae797a8SArto Merilainen .name = "tegra-vic", 52582d73874SThierry Reding .of_match_table = tegra_vic_of_match, 5260ae797a8SArto Merilainen .pm = &vic_pm_ops 5270ae797a8SArto Merilainen }, 5280ae797a8SArto Merilainen .probe = vic_probe, 5290ae797a8SArto Merilainen .remove = vic_remove, 5300ae797a8SArto Merilainen }; 531788ff4b6SNicolas Chauvet 532788ff4b6SNicolas Chauvet #if IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC) 533788ff4b6SNicolas Chauvet MODULE_FIRMWARE(NVIDIA_TEGRA_124_VIC_FIRMWARE); 534788ff4b6SNicolas Chauvet #endif 535788ff4b6SNicolas Chauvet #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC) 536788ff4b6SNicolas Chauvet MODULE_FIRMWARE(NVIDIA_TEGRA_210_VIC_FIRMWARE); 537788ff4b6SNicolas Chauvet #endif 5386e44b9adSMikko Perttunen #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC) 5396e44b9adSMikko Perttunen MODULE_FIRMWARE(NVIDIA_TEGRA_186_VIC_FIRMWARE); 5406e44b9adSMikko Perttunen #endif 541d6b9bc02SThierry Reding #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC) 542d6b9bc02SThierry Reding MODULE_FIRMWARE(NVIDIA_TEGRA_194_VIC_FIRMWARE); 543d6b9bc02SThierry Reding #endif 544