13846a3b9SSuman Anna // SPDX-License-Identifier: GPL-2.0-only
23846a3b9SSuman Anna /*
33846a3b9SSuman Anna * OMAP IOMMU quirks for various TI SoCs
43846a3b9SSuman Anna *
583bf6db0SAlexander A. Klimov * Copyright (C) 2015-2019 Texas Instruments Incorporated - https://www.ti.com/
63846a3b9SSuman Anna * Suman Anna <s-anna@ti.com>
73846a3b9SSuman Anna */
83846a3b9SSuman Anna
93846a3b9SSuman Anna #include <linux/platform_device.h>
103846a3b9SSuman Anna #include <linux/err.h>
114ea3711aSTero Kristo #include <linux/clk.h>
124ea3711aSTero Kristo #include <linux/list.h>
133846a3b9SSuman Anna
142f14101aSSuman Anna #include "clockdomain.h"
153846a3b9SSuman Anna #include "powerdomain.h"
16*e514f1fdSArnd Bergmann #include "common.h"
173846a3b9SSuman Anna
184ea3711aSTero Kristo struct pwrdm_link {
194ea3711aSTero Kristo struct device *dev;
204ea3711aSTero Kristo struct powerdomain *pwrdm;
214ea3711aSTero Kristo struct list_head node;
224ea3711aSTero Kristo };
234ea3711aSTero Kristo
244ea3711aSTero Kristo static DEFINE_SPINLOCK(iommu_lock);
254ea3711aSTero Kristo static struct clockdomain *emu_clkdm;
264ea3711aSTero Kristo static atomic_t emu_count;
274ea3711aSTero Kristo
omap_iommu_dra7_emu_swsup_config(struct platform_device * pdev,bool enable)282f14101aSSuman Anna static void omap_iommu_dra7_emu_swsup_config(struct platform_device *pdev,
292f14101aSSuman Anna bool enable)
302f14101aSSuman Anna {
312f14101aSSuman Anna struct device_node *np = pdev->dev.of_node;
324ea3711aSTero Kristo unsigned long flags;
332f14101aSSuman Anna
342f14101aSSuman Anna if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
352f14101aSSuman Anna return;
362f14101aSSuman Anna
372f14101aSSuman Anna if (!emu_clkdm) {
382f14101aSSuman Anna emu_clkdm = clkdm_lookup("emu_clkdm");
392f14101aSSuman Anna if (WARN_ON_ONCE(!emu_clkdm))
402f14101aSSuman Anna return;
412f14101aSSuman Anna }
422f14101aSSuman Anna
434ea3711aSTero Kristo spin_lock_irqsave(&iommu_lock, flags);
442f14101aSSuman Anna
454ea3711aSTero Kristo if (enable && (atomic_inc_return(&emu_count) == 1))
462f14101aSSuman Anna clkdm_deny_idle(emu_clkdm);
474ea3711aSTero Kristo else if (!enable && (atomic_dec_return(&emu_count) == 0))
482f14101aSSuman Anna clkdm_allow_idle(emu_clkdm);
492f14101aSSuman Anna
504ea3711aSTero Kristo spin_unlock_irqrestore(&iommu_lock, flags);
514ea3711aSTero Kristo }
524ea3711aSTero Kristo
_get_pwrdm(struct device * dev)534ea3711aSTero Kristo static struct powerdomain *_get_pwrdm(struct device *dev)
544ea3711aSTero Kristo {
554ea3711aSTero Kristo struct clk *clk;
564ea3711aSTero Kristo struct clk_hw_omap *hwclk;
574ea3711aSTero Kristo struct clockdomain *clkdm;
584ea3711aSTero Kristo struct powerdomain *pwrdm = NULL;
594ea3711aSTero Kristo struct pwrdm_link *entry;
604ea3711aSTero Kristo unsigned long flags;
614ea3711aSTero Kristo static LIST_HEAD(cache);
624ea3711aSTero Kristo
634ea3711aSTero Kristo spin_lock_irqsave(&iommu_lock, flags);
644ea3711aSTero Kristo
654ea3711aSTero Kristo list_for_each_entry(entry, &cache, node) {
664ea3711aSTero Kristo if (entry->dev == dev) {
674ea3711aSTero Kristo pwrdm = entry->pwrdm;
684ea3711aSTero Kristo break;
694ea3711aSTero Kristo }
704ea3711aSTero Kristo }
714ea3711aSTero Kristo
724ea3711aSTero Kristo spin_unlock_irqrestore(&iommu_lock, flags);
734ea3711aSTero Kristo
744ea3711aSTero Kristo if (pwrdm)
754ea3711aSTero Kristo return pwrdm;
764ea3711aSTero Kristo
774ea3711aSTero Kristo clk = of_clk_get(dev->of_node->parent, 0);
78a58cfdbaSJing Xiangfeng if (IS_ERR(clk)) {
794ea3711aSTero Kristo dev_err(dev, "no fck found\n");
804ea3711aSTero Kristo return NULL;
814ea3711aSTero Kristo }
824ea3711aSTero Kristo
834ea3711aSTero Kristo hwclk = to_clk_hw_omap(__clk_get_hw(clk));
844ea3711aSTero Kristo clk_put(clk);
854ea3711aSTero Kristo if (!hwclk || !hwclk->clkdm_name) {
864ea3711aSTero Kristo dev_err(dev, "no hwclk data\n");
874ea3711aSTero Kristo return NULL;
884ea3711aSTero Kristo }
894ea3711aSTero Kristo
904ea3711aSTero Kristo clkdm = clkdm_lookup(hwclk->clkdm_name);
914ea3711aSTero Kristo if (!clkdm) {
924ea3711aSTero Kristo dev_err(dev, "clkdm not found: %s\n", hwclk->clkdm_name);
934ea3711aSTero Kristo return NULL;
944ea3711aSTero Kristo }
954ea3711aSTero Kristo
964ea3711aSTero Kristo pwrdm = clkdm_get_pwrdm(clkdm);
974ea3711aSTero Kristo if (!pwrdm) {
984ea3711aSTero Kristo dev_err(dev, "pwrdm not found: %s\n", clkdm->name);
994ea3711aSTero Kristo return NULL;
1004ea3711aSTero Kristo }
1014ea3711aSTero Kristo
1024ea3711aSTero Kristo entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1034ea3711aSTero Kristo if (entry) {
1044ea3711aSTero Kristo entry->dev = dev;
1054ea3711aSTero Kristo entry->pwrdm = pwrdm;
1064ea3711aSTero Kristo spin_lock_irqsave(&iommu_lock, flags);
1074ea3711aSTero Kristo list_add(&entry->node, &cache);
1084ea3711aSTero Kristo spin_unlock_irqrestore(&iommu_lock, flags);
1094ea3711aSTero Kristo }
1104ea3711aSTero Kristo
1114ea3711aSTero Kristo return pwrdm;
1122f14101aSSuman Anna }
1132f14101aSSuman Anna
omap_iommu_set_pwrdm_constraint(struct platform_device * pdev,bool request,u8 * pwrst)1143846a3b9SSuman Anna int omap_iommu_set_pwrdm_constraint(struct platform_device *pdev, bool request,
1153846a3b9SSuman Anna u8 *pwrst)
1163846a3b9SSuman Anna {
1173846a3b9SSuman Anna struct powerdomain *pwrdm;
1183846a3b9SSuman Anna u8 next_pwrst;
1192f14101aSSuman Anna int ret = 0;
1203846a3b9SSuman Anna
1214ea3711aSTero Kristo pwrdm = _get_pwrdm(&pdev->dev);
1223846a3b9SSuman Anna if (!pwrdm)
1234ea3711aSTero Kristo return -ENODEV;
1243846a3b9SSuman Anna
1252f14101aSSuman Anna if (request) {
1263846a3b9SSuman Anna *pwrst = pwrdm_read_next_pwrst(pwrdm);
1272f14101aSSuman Anna omap_iommu_dra7_emu_swsup_config(pdev, true);
1282f14101aSSuman Anna }
1293846a3b9SSuman Anna
1303846a3b9SSuman Anna if (*pwrst > PWRDM_POWER_RET)
1312f14101aSSuman Anna goto out;
1323846a3b9SSuman Anna
1333846a3b9SSuman Anna next_pwrst = request ? PWRDM_POWER_ON : *pwrst;
1343846a3b9SSuman Anna
1352f14101aSSuman Anna ret = pwrdm_set_next_pwrst(pwrdm, next_pwrst);
1362f14101aSSuman Anna
1372f14101aSSuman Anna out:
1382f14101aSSuman Anna if (!request)
1392f14101aSSuman Anna omap_iommu_dra7_emu_swsup_config(pdev, false);
1402f14101aSSuman Anna
1412f14101aSSuman Anna return ret;
1423846a3b9SSuman Anna }
143