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 | */ |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/acpi.h> |
| 18 | #include <linux/i2c.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <asm/iosf_mbi.h> |
| 21 | #include "i2c-designware-core.h" |
| 22 | |
| 23 | #define SEMAPHORE_TIMEOUT 100 |
| 24 | #define PUNIT_SEMAPHORE 0x7 |
Andy Shevchenko | 9b5c9f0 | 2015-02-10 19:06:06 +0200 | [diff] [blame^] | 25 | #define PUNIT_SEMAPHORE_BIT BIT(0) |
| 26 | #define PUNIT_SEMAPHORE_ACQUIRE BIT(1) |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 27 | |
| 28 | static unsigned long acquired; |
| 29 | |
| 30 | static int get_sem(struct device *dev, u32 *sem) |
| 31 | { |
Andy Shevchenko | 9b5c9f0 | 2015-02-10 19:06:06 +0200 | [diff] [blame^] | 32 | u32 data; |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 33 | int ret; |
| 34 | |
| 35 | ret = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ, PUNIT_SEMAPHORE, |
Andy Shevchenko | 9b5c9f0 | 2015-02-10 19:06:06 +0200 | [diff] [blame^] | 36 | &data); |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 37 | if (ret) { |
| 38 | dev_err(dev, "iosf failed to read punit semaphore\n"); |
| 39 | return ret; |
| 40 | } |
| 41 | |
Andy Shevchenko | 9b5c9f0 | 2015-02-10 19:06:06 +0200 | [diff] [blame^] | 42 | *sem = data & PUNIT_SEMAPHORE_BIT; |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static void reset_semaphore(struct device *dev) |
| 48 | { |
| 49 | u32 data; |
| 50 | |
| 51 | if (iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ, |
| 52 | PUNIT_SEMAPHORE, &data)) { |
| 53 | dev_err(dev, "iosf failed to reset punit semaphore during read\n"); |
| 54 | return; |
| 55 | } |
| 56 | |
Andy Shevchenko | 9b5c9f0 | 2015-02-10 19:06:06 +0200 | [diff] [blame^] | 57 | data &= ~PUNIT_SEMAPHORE_BIT; |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 58 | if (iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE, |
Andy Shevchenko | 9b5c9f0 | 2015-02-10 19:06:06 +0200 | [diff] [blame^] | 59 | PUNIT_SEMAPHORE, data)) |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 60 | dev_err(dev, "iosf failed to reset punit semaphore during write\n"); |
| 61 | } |
| 62 | |
| 63 | int baytrail_i2c_acquire(struct dw_i2c_dev *dev) |
| 64 | { |
| 65 | u32 sem = 0; |
| 66 | int ret; |
| 67 | unsigned long start, end; |
| 68 | |
| 69 | if (!dev || !dev->dev) |
| 70 | return -ENODEV; |
| 71 | |
| 72 | if (!dev->acquire_lock) |
| 73 | return 0; |
| 74 | |
Andy Shevchenko | 9b5c9f0 | 2015-02-10 19:06:06 +0200 | [diff] [blame^] | 75 | /* host driver writes to side band semaphore register */ |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 76 | ret = iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE, |
Andy Shevchenko | 9b5c9f0 | 2015-02-10 19:06:06 +0200 | [diff] [blame^] | 77 | PUNIT_SEMAPHORE, PUNIT_SEMAPHORE_ACQUIRE); |
David Box | 894acb2 | 2015-01-15 01:12:17 -0800 | [diff] [blame] | 78 | if (ret) { |
| 79 | dev_err(dev->dev, "iosf punit semaphore request failed\n"); |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | /* host driver waits for bit 0 to be set in semaphore register */ |
| 84 | start = jiffies; |
| 85 | end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT); |
| 86 | while (!time_after(jiffies, end)) { |
| 87 | ret = get_sem(dev->dev, &sem); |
| 88 | if (!ret && sem) { |
| 89 | acquired = jiffies; |
| 90 | dev_dbg(dev->dev, "punit semaphore acquired after %ums\n", |
| 91 | jiffies_to_msecs(jiffies - start)); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | usleep_range(1000, 2000); |
| 96 | } |
| 97 | |
| 98 | dev_err(dev->dev, "punit semaphore timed out, resetting\n"); |
| 99 | reset_semaphore(dev->dev); |
| 100 | |
| 101 | ret = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ, |
| 102 | PUNIT_SEMAPHORE, &sem); |
| 103 | if (!ret) |
| 104 | dev_err(dev->dev, "iosf failed to read punit semaphore\n"); |
| 105 | else |
| 106 | dev_err(dev->dev, "PUNIT SEM: %d\n", sem); |
| 107 | |
| 108 | WARN_ON(1); |
| 109 | |
| 110 | return -ETIMEDOUT; |
| 111 | } |
| 112 | EXPORT_SYMBOL(baytrail_i2c_acquire); |
| 113 | |
| 114 | void baytrail_i2c_release(struct dw_i2c_dev *dev) |
| 115 | { |
| 116 | if (!dev || !dev->dev) |
| 117 | return; |
| 118 | |
| 119 | if (!dev->acquire_lock) |
| 120 | return; |
| 121 | |
| 122 | reset_semaphore(dev->dev); |
| 123 | dev_dbg(dev->dev, "punit semaphore held for %ums\n", |
| 124 | jiffies_to_msecs(jiffies - acquired)); |
| 125 | } |
| 126 | EXPORT_SYMBOL(baytrail_i2c_release); |
| 127 | |
| 128 | int i2c_dw_eval_lock_support(struct dw_i2c_dev *dev) |
| 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); |
| 142 | |
| 143 | if (ACPI_FAILURE(status)) |
| 144 | return 0; |
| 145 | |
| 146 | if (shared_host) { |
| 147 | dev_info(dev->dev, "I2C bus managed by PUNIT\n"); |
| 148 | dev->acquire_lock = baytrail_i2c_acquire; |
| 149 | dev->release_lock = baytrail_i2c_release; |
| 150 | dev->pm_runtime_disabled = true; |
| 151 | } |
| 152 | |
| 153 | if (!iosf_mbi_available()) |
| 154 | return -EPROBE_DEFER; |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | EXPORT_SYMBOL(i2c_dw_eval_lock_support); |
| 159 | |
| 160 | MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>"); |
| 161 | MODULE_DESCRIPTION("Baytrail I2C Semaphore driver"); |
| 162 | MODULE_LICENSE("GPL v2"); |