David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 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 | */ |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 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> |
Hans de Goede | 086cb4a | 2017-02-10 11:27:56 +0100 | [diff] [blame] | 19 | #include <linux/pm_qos.h> |
Andy Shevchenko | c8e043e | 2015-02-10 19:06:08 +0200 | [diff] [blame] | 20 | |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 21 | #include <asm/iosf_mbi.h> |
Andy Shevchenko | c8e043e | 2015-02-10 19:06:08 +0200 | [diff] [blame] | 22 | |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 23 | #include "i2c-designware-core.h" |
| 24 | |
| 25 | #define SEMAPHORE_TIMEOUT 100 |
| 26 | #define PUNIT_SEMAPHORE 0x7 |
Andy Shevchenko | 9b5c9f0 | 2015-02-10 19:06:06 +0200 | [diff] [blame] | 27 | #define PUNIT_SEMAPHORE_BIT BIT(0) |
| 28 | #define PUNIT_SEMAPHORE_ACQUIRE BIT(1) |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 29 | |
| 30 | static unsigned long acquired; |
| 31 | |
Hans de Goede | 62aee93 | 2017-02-10 11:27:54 +0100 | [diff] [blame] | 32 | static int get_sem(struct dw_i2c_dev *dev, u32 *sem) |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 33 | { |
Andy Shevchenko | 9b5c9f0 | 2015-02-10 19:06:06 +0200 | [diff] [blame] | 34 | u32 data; |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 35 | int ret; |
| 36 | |
Andy Shevchenko | 4077a38 | 2015-11-11 19:59:29 +0200 | [diff] [blame] | 37 | ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, PUNIT_SEMAPHORE, &data); |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 38 | if (ret) { |
Hans de Goede | 62aee93 | 2017-02-10 11:27:54 +0100 | [diff] [blame] | 39 | dev_err(dev->dev, "iosf failed to read punit semaphore\n"); |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 40 | return ret; |
| 41 | } |
| 42 | |
Andy Shevchenko | 9b5c9f0 | 2015-02-10 19:06:06 +0200 | [diff] [blame] | 43 | *sem = data & PUNIT_SEMAPHORE_BIT; |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Hans de Goede | 62aee93 | 2017-02-10 11:27:54 +0100 | [diff] [blame] | 48 | static void reset_semaphore(struct dw_i2c_dev *dev) |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 49 | { |
Hans de Goede | 519e23a | 2017-02-10 11:27:57 +0100 | [diff] [blame^] | 50 | if (iosf_mbi_modify(BT_MBI_UNIT_PMC, MBI_REG_READ, PUNIT_SEMAPHORE, |
| 51 | 0, PUNIT_SEMAPHORE_BIT)) |
Hans de Goede | 62aee93 | 2017-02-10 11:27:54 +0100 | [diff] [blame] | 52 | dev_err(dev->dev, "iosf failed to reset punit semaphore during write\n"); |
Hans de Goede | 086cb4a | 2017-02-10 11:27:56 +0100 | [diff] [blame] | 53 | |
| 54 | pm_qos_update_request(&dev->pm_qos, PM_QOS_DEFAULT_VALUE); |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Andy Shevchenko | c8e043e | 2015-02-10 19:06:08 +0200 | [diff] [blame] | 57 | static int baytrail_i2c_acquire(struct dw_i2c_dev *dev) |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 58 | { |
Andy Shevchenko | 4077a38 | 2015-11-11 19:59:29 +0200 | [diff] [blame] | 59 | u32 sem = PUNIT_SEMAPHORE_ACQUIRE; |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 60 | int ret; |
| 61 | unsigned long start, end; |
| 62 | |
Andy Shevchenko | ebf2ef8 | 2015-02-10 19:06:10 +0200 | [diff] [blame] | 63 | might_sleep(); |
| 64 | |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 65 | if (!dev || !dev->dev) |
| 66 | return -ENODEV; |
| 67 | |
Andy Shevchenko | 30be774 | 2015-02-10 19:06:09 +0200 | [diff] [blame] | 68 | if (!dev->release_lock) |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 69 | return 0; |
| 70 | |
Hans de Goede | 086cb4a | 2017-02-10 11:27:56 +0100 | [diff] [blame] | 71 | /* |
| 72 | * Disallow the CPU to enter C6 or C7 state, entering these states |
| 73 | * requires the punit to talk to the pmic and if this happens while |
| 74 | * we're holding the semaphore, the SoC hangs. |
| 75 | */ |
| 76 | pm_qos_update_request(&dev->pm_qos, 0); |
| 77 | |
Andy Shevchenko | 9b5c9f0 | 2015-02-10 19:06:06 +0200 | [diff] [blame] | 78 | /* host driver writes to side band semaphore register */ |
Andy Shevchenko | 4077a38 | 2015-11-11 19:59:29 +0200 | [diff] [blame] | 79 | ret = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, PUNIT_SEMAPHORE, sem); |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 80 | if (ret) { |
| 81 | dev_err(dev->dev, "iosf punit semaphore request failed\n"); |
Hans de Goede | 086cb4a | 2017-02-10 11:27:56 +0100 | [diff] [blame] | 82 | goto out; |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* host driver waits for bit 0 to be set in semaphore register */ |
| 86 | start = jiffies; |
| 87 | end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT); |
Andy Shevchenko | ebf2ef8 | 2015-02-10 19:06:10 +0200 | [diff] [blame] | 88 | do { |
Hans de Goede | 62aee93 | 2017-02-10 11:27:54 +0100 | [diff] [blame] | 89 | ret = get_sem(dev, &sem); |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 90 | if (!ret && sem) { |
| 91 | acquired = jiffies; |
| 92 | dev_dbg(dev->dev, "punit semaphore acquired after %ums\n", |
| 93 | jiffies_to_msecs(jiffies - start)); |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | usleep_range(1000, 2000); |
Andy Shevchenko | ebf2ef8 | 2015-02-10 19:06:10 +0200 | [diff] [blame] | 98 | } while (time_before(jiffies, end)); |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 99 | |
| 100 | dev_err(dev->dev, "punit semaphore timed out, resetting\n"); |
Hans de Goede | 086cb4a | 2017-02-10 11:27:56 +0100 | [diff] [blame] | 101 | out: |
Hans de Goede | 62aee93 | 2017-02-10 11:27:54 +0100 | [diff] [blame] | 102 | reset_semaphore(dev); |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 103 | |
Andy Shevchenko | 4077a38 | 2015-11-11 19:59:29 +0200 | [diff] [blame] | 104 | ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, PUNIT_SEMAPHORE, &sem); |
Andy Shevchenko | 259aada | 2015-02-10 19:06:07 +0200 | [diff] [blame] | 105 | if (ret) |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 106 | dev_err(dev->dev, "iosf failed to read punit semaphore\n"); |
| 107 | else |
| 108 | dev_err(dev->dev, "PUNIT SEM: %d\n", sem); |
| 109 | |
| 110 | WARN_ON(1); |
| 111 | |
| 112 | return -ETIMEDOUT; |
| 113 | } |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 114 | |
Andy Shevchenko | c8e043e | 2015-02-10 19:06:08 +0200 | [diff] [blame] | 115 | static void baytrail_i2c_release(struct dw_i2c_dev *dev) |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 116 | { |
| 117 | if (!dev || !dev->dev) |
| 118 | return; |
| 119 | |
| 120 | if (!dev->acquire_lock) |
| 121 | return; |
| 122 | |
Hans de Goede | 62aee93 | 2017-02-10 11:27:54 +0100 | [diff] [blame] | 123 | reset_semaphore(dev); |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 124 | dev_dbg(dev->dev, "punit semaphore held for %ums\n", |
| 125 | jiffies_to_msecs(jiffies - acquired)); |
| 126 | } |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 127 | |
Hans de Goede | 086cb4a | 2017-02-10 11:27:56 +0100 | [diff] [blame] | 128 | int i2c_dw_probe_lock_support(struct dw_i2c_dev *dev) |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 129 | { |
| 130 | acpi_status status; |
| 131 | unsigned long long shared_host = 0; |
| 132 | acpi_handle handle; |
| 133 | |
| 134 | if (!dev || !dev->dev) |
| 135 | return 0; |
| 136 | |
| 137 | handle = ACPI_HANDLE(dev->dev); |
| 138 | if (!handle) |
| 139 | return 0; |
| 140 | |
| 141 | status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host); |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 142 | if (ACPI_FAILURE(status)) |
| 143 | return 0; |
| 144 | |
Hans de Goede | e234ed2 | 2017-02-10 11:27:55 +0100 | [diff] [blame] | 145 | if (!shared_host) |
| 146 | return 0; |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 147 | |
| 148 | if (!iosf_mbi_available()) |
| 149 | return -EPROBE_DEFER; |
| 150 | |
Hans de Goede | e234ed2 | 2017-02-10 11:27:55 +0100 | [diff] [blame] | 151 | dev_info(dev->dev, "I2C bus managed by PUNIT\n"); |
| 152 | dev->acquire_lock = baytrail_i2c_acquire; |
| 153 | dev->release_lock = baytrail_i2c_release; |
| 154 | dev->pm_runtime_disabled = true; |
| 155 | |
Hans de Goede | 086cb4a | 2017-02-10 11:27:56 +0100 | [diff] [blame] | 156 | pm_qos_add_request(&dev->pm_qos, PM_QOS_CPU_DMA_LATENCY, |
| 157 | PM_QOS_DEFAULT_VALUE); |
| 158 | |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 159 | return 0; |
| 160 | } |
Hans de Goede | 086cb4a | 2017-02-10 11:27:56 +0100 | [diff] [blame] | 161 | |
| 162 | void i2c_dw_remove_lock_support(struct dw_i2c_dev *dev) |
| 163 | { |
| 164 | if (dev->acquire_lock) |
| 165 | pm_qos_remove_request(&dev->pm_qos); |
| 166 | } |