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  */
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>
19 #include <linux/pm_qos.h>
20 
21 #include <asm/iosf_mbi.h>
22 
23 #include "i2c-designware-core.h"
24 
25 #define SEMAPHORE_TIMEOUT	500
26 #define PUNIT_SEMAPHORE		0x7
27 #define PUNIT_SEMAPHORE_CHT	0x10e
28 #define PUNIT_SEMAPHORE_BIT	BIT(0)
29 #define PUNIT_SEMAPHORE_ACQUIRE	BIT(1)
30 
31 static unsigned long acquired;
32 
33 static 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 
41 static int get_sem(struct dw_i2c_dev *dev, u32 *sem)
42 {
43 	u32 addr = get_sem_addr(dev);
44 	u32 data;
45 	int ret;
46 
47 	ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, addr, &data);
48 	if (ret) {
49 		dev_err(dev->dev, "iosf failed to read punit semaphore\n");
50 		return ret;
51 	}
52 
53 	*sem = data & PUNIT_SEMAPHORE_BIT;
54 
55 	return 0;
56 }
57 
58 static void reset_semaphore(struct dw_i2c_dev *dev)
59 {
60 	if (iosf_mbi_modify(BT_MBI_UNIT_PMC, MBI_REG_READ, get_sem_addr(dev),
61 			    0, PUNIT_SEMAPHORE_BIT))
62 		dev_err(dev->dev, "iosf failed to reset punit semaphore during write\n");
63 
64 	pm_qos_update_request(&dev->pm_qos, PM_QOS_DEFAULT_VALUE);
65 
66 	iosf_mbi_call_pmic_bus_access_notifier_chain(MBI_PMIC_BUS_ACCESS_END,
67 						     NULL);
68 	iosf_mbi_punit_release();
69 }
70 
71 static int baytrail_i2c_acquire(struct dw_i2c_dev *dev)
72 {
73 	u32 addr = get_sem_addr(dev);
74 	u32 sem = PUNIT_SEMAPHORE_ACQUIRE;
75 	int ret;
76 	unsigned long start, end;
77 
78 	might_sleep();
79 
80 	if (!dev || !dev->dev)
81 		return -ENODEV;
82 
83 	if (!dev->release_lock)
84 		return 0;
85 
86 	iosf_mbi_punit_acquire();
87 	iosf_mbi_call_pmic_bus_access_notifier_chain(MBI_PMIC_BUS_ACCESS_BEGIN,
88 						     NULL);
89 
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 
97 	/* host driver writes to side band semaphore register */
98 	ret = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, addr, sem);
99 	if (ret) {
100 		dev_err(dev->dev, "iosf punit semaphore request failed\n");
101 		goto out;
102 	}
103 
104 	/* host driver waits for bit 0 to be set in semaphore register */
105 	start = jiffies;
106 	end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT);
107 	do {
108 		ret = get_sem(dev, &sem);
109 		if (!ret && sem) {
110 			acquired = jiffies;
111 			dev_dbg(dev->dev, "punit semaphore acquired after %ums\n",
112 				jiffies_to_msecs(jiffies - start));
113 			return 0;
114 		}
115 
116 		usleep_range(1000, 2000);
117 	} while (time_before(jiffies, end));
118 
119 	dev_err(dev->dev, "punit semaphore timed out, resetting\n");
120 out:
121 	reset_semaphore(dev);
122 
123 	ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, addr, &sem);
124 	if (ret)
125 		dev_err(dev->dev, "iosf failed to read punit semaphore\n");
126 	else
127 		dev_err(dev->dev, "PUNIT SEM: %d\n", sem);
128 
129 	WARN_ON(1);
130 
131 	return -ETIMEDOUT;
132 }
133 
134 static void baytrail_i2c_release(struct dw_i2c_dev *dev)
135 {
136 	if (!dev || !dev->dev)
137 		return;
138 
139 	if (!dev->acquire_lock)
140 		return;
141 
142 	reset_semaphore(dev);
143 	dev_dbg(dev->dev, "punit semaphore held for %ums\n",
144 		jiffies_to_msecs(jiffies - acquired));
145 }
146 
147 int i2c_dw_probe_lock_support(struct dw_i2c_dev *dev)
148 {
149 	acpi_status status;
150 	unsigned long long shared_host = 0;
151 	acpi_handle handle;
152 
153 	if (!dev || !dev->dev)
154 		return 0;
155 
156 	handle = ACPI_HANDLE(dev->dev);
157 	if (!handle)
158 		return 0;
159 
160 	status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host);
161 	if (ACPI_FAILURE(status))
162 		return 0;
163 
164 	if (!shared_host)
165 		return 0;
166 
167 	if (!iosf_mbi_available())
168 		return -EPROBE_DEFER;
169 
170 	dev_info(dev->dev, "I2C bus managed by PUNIT\n");
171 	dev->acquire_lock = baytrail_i2c_acquire;
172 	dev->release_lock = baytrail_i2c_release;
173 	dev->pm_runtime_disabled = true;
174 
175 	pm_qos_add_request(&dev->pm_qos, PM_QOS_CPU_DMA_LATENCY,
176 			   PM_QOS_DEFAULT_VALUE);
177 
178 	return 0;
179 }
180 
181 void i2c_dw_remove_lock_support(struct dw_i2c_dev *dev)
182 {
183 	if (dev->acquire_lock)
184 		pm_qos_remove_request(&dev->pm_qos);
185 }
186