]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/i2c/busses/i2c-designware-baytrail.c
x86/speculation/mds: Add mitigation control for MDS
[mirror_ubuntu-bionic-kernel.git] / drivers / i2c / busses / i2c-designware-baytrail.c
CommitLineData
894acb2f
DB
1/*
2 * Intel BayTrail PMIC I2C bus semaphore implementaion
3 * Copyright (c) 2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
894acb2f
DB
14#include <linux/delay.h>
15#include <linux/device.h>
16#include <linux/acpi.h>
17#include <linux/i2c.h>
18#include <linux/interrupt.h>
086cb4af 19#include <linux/pm_qos.h>
c8e043e6 20
894acb2f 21#include <asm/iosf_mbi.h>
c8e043e6 22
894acb2f
DB
23#include "i2c-designware-core.h"
24
78c43af2 25#define SEMAPHORE_TIMEOUT 500
894acb2f 26#define PUNIT_SEMAPHORE 0x7
fd476fa2 27#define PUNIT_SEMAPHORE_CHT 0x10e
9b5c9f04
AS
28#define PUNIT_SEMAPHORE_BIT BIT(0)
29#define PUNIT_SEMAPHORE_ACQUIRE BIT(1)
894acb2f
DB
30
31static unsigned long acquired;
32
fd476fa2
HG
33static u32 get_sem_addr(struct dw_i2c_dev *dev)
34{
35 if (dev->flags & MODEL_CHERRYTRAIL)
36 return PUNIT_SEMAPHORE_CHT;
37 else
38 return PUNIT_SEMAPHORE;
39}
40
62aee937 41static int get_sem(struct dw_i2c_dev *dev, u32 *sem)
894acb2f 42{
fd476fa2 43 u32 addr = get_sem_addr(dev);
9b5c9f04 44 u32 data;
894acb2f
DB
45 int ret;
46
fd476fa2 47 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, addr, &data);
894acb2f 48 if (ret) {
62aee937 49 dev_err(dev->dev, "iosf failed to read punit semaphore\n");
894acb2f
DB
50 return ret;
51 }
52
9b5c9f04 53 *sem = data & PUNIT_SEMAPHORE_BIT;
894acb2f
DB
54
55 return 0;
56}
57
62aee937 58static void reset_semaphore(struct dw_i2c_dev *dev)
894acb2f 59{
fd476fa2 60 if (iosf_mbi_modify(BT_MBI_UNIT_PMC, MBI_REG_READ, get_sem_addr(dev),
519e23a7 61 0, PUNIT_SEMAPHORE_BIT))
62aee937 62 dev_err(dev->dev, "iosf failed to reset punit semaphore during write\n");
086cb4af
HG
63
64 pm_qos_update_request(&dev->pm_qos, PM_QOS_DEFAULT_VALUE);
5b2cacce 65
d93a6ed3
HG
66 iosf_mbi_call_pmic_bus_access_notifier_chain(MBI_PMIC_BUS_ACCESS_END,
67 NULL);
5b2cacce 68 iosf_mbi_punit_release();
894acb2f
DB
69}
70
c8e043e6 71static int baytrail_i2c_acquire(struct dw_i2c_dev *dev)
894acb2f 72{
c7f82ea8 73 u32 addr;
4077a387 74 u32 sem = PUNIT_SEMAPHORE_ACQUIRE;
894acb2f
DB
75 int ret;
76 unsigned long start, end;
77
ebf2ef8f
AS
78 might_sleep();
79
894acb2f
DB
80 if (!dev || !dev->dev)
81 return -ENODEV;
82
30be774b 83 if (!dev->release_lock)
894acb2f
DB
84 return 0;
85
5b2cacce 86 iosf_mbi_punit_acquire();
d93a6ed3
HG
87 iosf_mbi_call_pmic_bus_access_notifier_chain(MBI_PMIC_BUS_ACCESS_BEGIN,
88 NULL);
5b2cacce 89
086cb4af
HG
90 /*
91 * Disallow the CPU to enter C6 or C7 state, entering these states
92 * requires the punit to talk to the pmic and if this happens while
93 * we're holding the semaphore, the SoC hangs.
94 */
95 pm_qos_update_request(&dev->pm_qos, 0);
96
c7f82ea8
CIK
97 addr = get_sem_addr(dev);
98
9b5c9f04 99 /* host driver writes to side band semaphore register */
fd476fa2 100 ret = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, addr, sem);
894acb2f
DB
101 if (ret) {
102 dev_err(dev->dev, "iosf punit semaphore request failed\n");
086cb4af 103 goto out;
894acb2f
DB
104 }
105
106 /* host driver waits for bit 0 to be set in semaphore register */
107 start = jiffies;
108 end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT);
ebf2ef8f 109 do {
62aee937 110 ret = get_sem(dev, &sem);
894acb2f
DB
111 if (!ret && sem) {
112 acquired = jiffies;
113 dev_dbg(dev->dev, "punit semaphore acquired after %ums\n",
114 jiffies_to_msecs(jiffies - start));
115 return 0;
116 }
117
118 usleep_range(1000, 2000);
ebf2ef8f 119 } while (time_before(jiffies, end));
894acb2f
DB
120
121 dev_err(dev->dev, "punit semaphore timed out, resetting\n");
086cb4af 122out:
62aee937 123 reset_semaphore(dev);
894acb2f 124
fd476fa2 125 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, addr, &sem);
259aada4 126 if (ret)
894acb2f
DB
127 dev_err(dev->dev, "iosf failed to read punit semaphore\n");
128 else
129 dev_err(dev->dev, "PUNIT SEM: %d\n", sem);
130
131 WARN_ON(1);
132
133 return -ETIMEDOUT;
134}
894acb2f 135
c8e043e6 136static void baytrail_i2c_release(struct dw_i2c_dev *dev)
894acb2f
DB
137{
138 if (!dev || !dev->dev)
139 return;
140
141 if (!dev->acquire_lock)
142 return;
143
62aee937 144 reset_semaphore(dev);
894acb2f
DB
145 dev_dbg(dev->dev, "punit semaphore held for %ums\n",
146 jiffies_to_msecs(jiffies - acquired));
147}
894acb2f 148
086cb4af 149int i2c_dw_probe_lock_support(struct dw_i2c_dev *dev)
894acb2f
DB
150{
151 acpi_status status;
152 unsigned long long shared_host = 0;
153 acpi_handle handle;
154
155 if (!dev || !dev->dev)
156 return 0;
157
158 handle = ACPI_HANDLE(dev->dev);
159 if (!handle)
160 return 0;
161
162 status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host);
894acb2f
DB
163 if (ACPI_FAILURE(status))
164 return 0;
165
e234ed2f
HG
166 if (!shared_host)
167 return 0;
894acb2f
DB
168
169 if (!iosf_mbi_available())
170 return -EPROBE_DEFER;
171
e234ed2f
HG
172 dev_info(dev->dev, "I2C bus managed by PUNIT\n");
173 dev->acquire_lock = baytrail_i2c_acquire;
174 dev->release_lock = baytrail_i2c_release;
41c80b8a 175 dev->pm_disabled = true;
e234ed2f 176
086cb4af
HG
177 pm_qos_add_request(&dev->pm_qos, PM_QOS_CPU_DMA_LATENCY,
178 PM_QOS_DEFAULT_VALUE);
179
894acb2f
DB
180 return 0;
181}
086cb4af
HG
182
183void i2c_dw_remove_lock_support(struct dw_i2c_dev *dev)
184{
185 if (dev->acquire_lock)
186 pm_qos_remove_request(&dev->pm_qos);
187}