1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Intel BayTrail PMIC I2C bus semaphore implementaion 4 * Copyright (c) 2014, Intel Corporation. 5 */ 6 #include <linux/delay.h> 7 #include <linux/device.h> 8 #include <linux/acpi.h> 9 #include <linux/i2c.h> 10 #include <linux/interrupt.h> 11 #include <linux/pm_qos.h> 12 13 #include <asm/iosf_mbi.h> 14 15 #include "i2c-designware-core.h" 16 17 #define SEMAPHORE_TIMEOUT 500 18 #define PUNIT_SEMAPHORE 0x7 19 #define PUNIT_SEMAPHORE_CHT 0x10e 20 #define PUNIT_SEMAPHORE_BIT BIT(0) 21 #define PUNIT_SEMAPHORE_ACQUIRE BIT(1) 22 23 static unsigned long acquired; 24 25 static u32 get_sem_addr(struct dw_i2c_dev *dev) 26 { 27 if (dev->flags & MODEL_CHERRYTRAIL) 28 return PUNIT_SEMAPHORE_CHT; 29 else 30 return PUNIT_SEMAPHORE; 31 } 32 33 static int get_sem(struct dw_i2c_dev *dev, u32 *sem) 34 { 35 u32 addr = get_sem_addr(dev); 36 u32 data; 37 int ret; 38 39 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, addr, &data); 40 if (ret) { 41 dev_err(dev->dev, "iosf failed to read punit semaphore\n"); 42 return ret; 43 } 44 45 *sem = data & PUNIT_SEMAPHORE_BIT; 46 47 return 0; 48 } 49 50 static void reset_semaphore(struct dw_i2c_dev *dev) 51 { 52 if (iosf_mbi_modify(BT_MBI_UNIT_PMC, MBI_REG_READ, get_sem_addr(dev), 53 0, PUNIT_SEMAPHORE_BIT)) 54 dev_err(dev->dev, "iosf failed to reset punit semaphore during write\n"); 55 56 pm_qos_update_request(&dev->pm_qos, PM_QOS_DEFAULT_VALUE); 57 58 iosf_mbi_call_pmic_bus_access_notifier_chain(MBI_PMIC_BUS_ACCESS_END, 59 NULL); 60 iosf_mbi_punit_release(); 61 } 62 63 static int baytrail_i2c_acquire(struct dw_i2c_dev *dev) 64 { 65 u32 addr; 66 u32 sem = PUNIT_SEMAPHORE_ACQUIRE; 67 int ret; 68 unsigned long start, end; 69 70 might_sleep(); 71 72 if (!dev || !dev->dev) 73 return -ENODEV; 74 75 if (!dev->release_lock) 76 return 0; 77 78 iosf_mbi_punit_acquire(); 79 iosf_mbi_call_pmic_bus_access_notifier_chain(MBI_PMIC_BUS_ACCESS_BEGIN, 80 NULL); 81 82 /* 83 * Disallow the CPU to enter C6 or C7 state, entering these states 84 * requires the punit to talk to the pmic and if this happens while 85 * we're holding the semaphore, the SoC hangs. 86 */ 87 pm_qos_update_request(&dev->pm_qos, 0); 88 89 addr = get_sem_addr(dev); 90 91 /* host driver writes to side band semaphore register */ 92 ret = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, addr, sem); 93 if (ret) { 94 dev_err(dev->dev, "iosf punit semaphore request failed\n"); 95 goto out; 96 } 97 98 /* host driver waits for bit 0 to be set in semaphore register */ 99 start = jiffies; 100 end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT); 101 do { 102 ret = get_sem(dev, &sem); 103 if (!ret && sem) { 104 acquired = jiffies; 105 dev_dbg(dev->dev, "punit semaphore acquired after %ums\n", 106 jiffies_to_msecs(jiffies - start)); 107 return 0; 108 } 109 110 usleep_range(1000, 2000); 111 } while (time_before(jiffies, end)); 112 113 dev_err(dev->dev, "punit semaphore timed out, resetting\n"); 114 out: 115 reset_semaphore(dev); 116 117 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, addr, &sem); 118 if (ret) 119 dev_err(dev->dev, "iosf failed to read punit semaphore\n"); 120 else 121 dev_err(dev->dev, "PUNIT SEM: %d\n", sem); 122 123 WARN_ON(1); 124 125 return -ETIMEDOUT; 126 } 127 128 static void baytrail_i2c_release(struct dw_i2c_dev *dev) 129 { 130 if (!dev || !dev->dev) 131 return; 132 133 if (!dev->acquire_lock) 134 return; 135 136 reset_semaphore(dev); 137 dev_dbg(dev->dev, "punit semaphore held for %ums\n", 138 jiffies_to_msecs(jiffies - acquired)); 139 } 140 141 int i2c_dw_probe_lock_support(struct dw_i2c_dev *dev) 142 { 143 acpi_status status; 144 unsigned long long shared_host = 0; 145 acpi_handle handle; 146 147 if (!dev || !dev->dev) 148 return 0; 149 150 handle = ACPI_HANDLE(dev->dev); 151 if (!handle) 152 return 0; 153 154 status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host); 155 if (ACPI_FAILURE(status)) 156 return 0; 157 158 if (!shared_host) 159 return 0; 160 161 if (!iosf_mbi_available()) 162 return -EPROBE_DEFER; 163 164 dev_info(dev->dev, "I2C bus managed by PUNIT\n"); 165 dev->acquire_lock = baytrail_i2c_acquire; 166 dev->release_lock = baytrail_i2c_release; 167 dev->shared_with_punit = true; 168 169 pm_qos_add_request(&dev->pm_qos, PM_QOS_CPU_DMA_LATENCY, 170 PM_QOS_DEFAULT_VALUE); 171 172 return 0; 173 } 174 175 void i2c_dw_remove_lock_support(struct dw_i2c_dev *dev) 176 { 177 if (dev->acquire_lock) 178 pm_qos_remove_request(&dev->pm_qos); 179 } 180