blob: 93c20bbd7b7e413b9c901beee3f338f512cf3b50 [file] [log] [blame]
Suman Anna3846a3b92019-08-07 11:26:45 +03001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * OMAP IOMMU quirks for various TI SoCs
4 *
Alexander A. Klimov83bf6db2020-07-19 12:30:33 +02005 * Copyright (C) 2015-2019 Texas Instruments Incorporated - https://www.ti.com/
Suman Anna3846a3b92019-08-07 11:26:45 +03006 * Suman Anna <s-anna@ti.com>
7 */
8
9#include <linux/platform_device.h>
10#include <linux/err.h>
Tero Kristo4ea37112019-12-12 15:05:40 +020011#include <linux/clk.h>
12#include <linux/list.h>
Suman Anna3846a3b92019-08-07 11:26:45 +030013
Suman Anna2f141012019-12-12 15:05:39 +020014#include "clockdomain.h"
Suman Anna3846a3b92019-08-07 11:26:45 +030015#include "powerdomain.h"
16
Tero Kristo4ea37112019-12-12 15:05:40 +020017struct pwrdm_link {
18 struct device *dev;
19 struct powerdomain *pwrdm;
20 struct list_head node;
21};
22
23static DEFINE_SPINLOCK(iommu_lock);
24static struct clockdomain *emu_clkdm;
25static atomic_t emu_count;
26
Suman Anna2f141012019-12-12 15:05:39 +020027static void omap_iommu_dra7_emu_swsup_config(struct platform_device *pdev,
28 bool enable)
29{
Suman Anna2f141012019-12-12 15:05:39 +020030 struct device_node *np = pdev->dev.of_node;
Tero Kristo4ea37112019-12-12 15:05:40 +020031 unsigned long flags;
Suman Anna2f141012019-12-12 15:05:39 +020032
33 if (!of_device_is_compatible(np, "ti,dra7-dsp-iommu"))
34 return;
35
36 if (!emu_clkdm) {
37 emu_clkdm = clkdm_lookup("emu_clkdm");
38 if (WARN_ON_ONCE(!emu_clkdm))
39 return;
40 }
41
Tero Kristo4ea37112019-12-12 15:05:40 +020042 spin_lock_irqsave(&iommu_lock, flags);
Suman Anna2f141012019-12-12 15:05:39 +020043
Tero Kristo4ea37112019-12-12 15:05:40 +020044 if (enable && (atomic_inc_return(&emu_count) == 1))
Suman Anna2f141012019-12-12 15:05:39 +020045 clkdm_deny_idle(emu_clkdm);
Tero Kristo4ea37112019-12-12 15:05:40 +020046 else if (!enable && (atomic_dec_return(&emu_count) == 0))
Suman Anna2f141012019-12-12 15:05:39 +020047 clkdm_allow_idle(emu_clkdm);
48
Tero Kristo4ea37112019-12-12 15:05:40 +020049 spin_unlock_irqrestore(&iommu_lock, flags);
50}
51
52static struct powerdomain *_get_pwrdm(struct device *dev)
53{
54 struct clk *clk;
55 struct clk_hw_omap *hwclk;
56 struct clockdomain *clkdm;
57 struct powerdomain *pwrdm = NULL;
58 struct pwrdm_link *entry;
59 unsigned long flags;
60 static LIST_HEAD(cache);
61
62 spin_lock_irqsave(&iommu_lock, flags);
63
64 list_for_each_entry(entry, &cache, node) {
65 if (entry->dev == dev) {
66 pwrdm = entry->pwrdm;
67 break;
68 }
69 }
70
71 spin_unlock_irqrestore(&iommu_lock, flags);
72
73 if (pwrdm)
74 return pwrdm;
75
76 clk = of_clk_get(dev->of_node->parent, 0);
Jing Xiangfenga58cfdb2020-07-24 11:54:30 +080077 if (IS_ERR(clk)) {
Tero Kristo4ea37112019-12-12 15:05:40 +020078 dev_err(dev, "no fck found\n");
79 return NULL;
80 }
81
82 hwclk = to_clk_hw_omap(__clk_get_hw(clk));
83 clk_put(clk);
84 if (!hwclk || !hwclk->clkdm_name) {
85 dev_err(dev, "no hwclk data\n");
86 return NULL;
87 }
88
89 clkdm = clkdm_lookup(hwclk->clkdm_name);
90 if (!clkdm) {
91 dev_err(dev, "clkdm not found: %s\n", hwclk->clkdm_name);
92 return NULL;
93 }
94
95 pwrdm = clkdm_get_pwrdm(clkdm);
96 if (!pwrdm) {
97 dev_err(dev, "pwrdm not found: %s\n", clkdm->name);
98 return NULL;
99 }
100
101 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
102 if (entry) {
103 entry->dev = dev;
104 entry->pwrdm = pwrdm;
105 spin_lock_irqsave(&iommu_lock, flags);
106 list_add(&entry->node, &cache);
107 spin_unlock_irqrestore(&iommu_lock, flags);
108 }
109
110 return pwrdm;
Suman Anna2f141012019-12-12 15:05:39 +0200111}
112
Suman Anna3846a3b92019-08-07 11:26:45 +0300113int omap_iommu_set_pwrdm_constraint(struct platform_device *pdev, bool request,
114 u8 *pwrst)
115{
116 struct powerdomain *pwrdm;
Suman Anna3846a3b92019-08-07 11:26:45 +0300117 u8 next_pwrst;
Suman Anna2f141012019-12-12 15:05:39 +0200118 int ret = 0;
Suman Anna3846a3b92019-08-07 11:26:45 +0300119
Tero Kristo4ea37112019-12-12 15:05:40 +0200120 pwrdm = _get_pwrdm(&pdev->dev);
Suman Anna3846a3b92019-08-07 11:26:45 +0300121 if (!pwrdm)
Tero Kristo4ea37112019-12-12 15:05:40 +0200122 return -ENODEV;
Suman Anna3846a3b92019-08-07 11:26:45 +0300123
Suman Anna2f141012019-12-12 15:05:39 +0200124 if (request) {
Suman Anna3846a3b92019-08-07 11:26:45 +0300125 *pwrst = pwrdm_read_next_pwrst(pwrdm);
Suman Anna2f141012019-12-12 15:05:39 +0200126 omap_iommu_dra7_emu_swsup_config(pdev, true);
127 }
Suman Anna3846a3b92019-08-07 11:26:45 +0300128
129 if (*pwrst > PWRDM_POWER_RET)
Suman Anna2f141012019-12-12 15:05:39 +0200130 goto out;
Suman Anna3846a3b92019-08-07 11:26:45 +0300131
132 next_pwrst = request ? PWRDM_POWER_ON : *pwrst;
133
Suman Anna2f141012019-12-12 15:05:39 +0200134 ret = pwrdm_set_next_pwrst(pwrdm, next_pwrst);
135
136out:
137 if (!request)
138 omap_iommu_dra7_emu_swsup_config(pdev, false);
139
140 return ret;
Suman Anna3846a3b92019-08-07 11:26:45 +0300141}