blob: 1590ad0a80819cdfe9e59d9aea6be864ff9d01be [file] [log] [blame]
David Box894acb22015-01-15 01:12:17 -08001/*
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 Box894acb22015-01-15 01:12:17 -080014#include <linux/delay.h>
15#include <linux/device.h>
16#include <linux/acpi.h>
17#include <linux/i2c.h>
18#include <linux/interrupt.h>
Andy Shevchenkoc8e043e2015-02-10 19:06:08 +020019
David Box894acb22015-01-15 01:12:17 -080020#include <asm/iosf_mbi.h>
Andy Shevchenkoc8e043e2015-02-10 19:06:08 +020021
David Box894acb22015-01-15 01:12:17 -080022#include "i2c-designware-core.h"
23
24#define SEMAPHORE_TIMEOUT 100
25#define PUNIT_SEMAPHORE 0x7
Andy Shevchenko9b5c9f02015-02-10 19:06:06 +020026#define PUNIT_SEMAPHORE_BIT BIT(0)
27#define PUNIT_SEMAPHORE_ACQUIRE BIT(1)
David Box894acb22015-01-15 01:12:17 -080028
29static unsigned long acquired;
30
31static int get_sem(struct device *dev, u32 *sem)
32{
Andy Shevchenko9b5c9f02015-02-10 19:06:06 +020033 u32 data;
David Box894acb22015-01-15 01:12:17 -080034 int ret;
35
Andy Shevchenko4077a382015-11-11 19:59:29 +020036 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, PUNIT_SEMAPHORE, &data);
David Box894acb22015-01-15 01:12:17 -080037 if (ret) {
38 dev_err(dev, "iosf failed to read punit semaphore\n");
39 return ret;
40 }
41
Andy Shevchenko9b5c9f02015-02-10 19:06:06 +020042 *sem = data & PUNIT_SEMAPHORE_BIT;
David Box894acb22015-01-15 01:12:17 -080043
44 return 0;
45}
46
47static void reset_semaphore(struct device *dev)
48{
49 u32 data;
50
Andy Shevchenko4077a382015-11-11 19:59:29 +020051 if (iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, PUNIT_SEMAPHORE, &data)) {
David Box894acb22015-01-15 01:12:17 -080052 dev_err(dev, "iosf failed to reset punit semaphore during read\n");
53 return;
54 }
55
Andy Shevchenko9b5c9f02015-02-10 19:06:06 +020056 data &= ~PUNIT_SEMAPHORE_BIT;
Andy Shevchenko4077a382015-11-11 19:59:29 +020057 if (iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, PUNIT_SEMAPHORE, data))
David Box894acb22015-01-15 01:12:17 -080058 dev_err(dev, "iosf failed to reset punit semaphore during write\n");
59}
60
Andy Shevchenkoc8e043e2015-02-10 19:06:08 +020061static int baytrail_i2c_acquire(struct dw_i2c_dev *dev)
David Box894acb22015-01-15 01:12:17 -080062{
Andy Shevchenko4077a382015-11-11 19:59:29 +020063 u32 sem = PUNIT_SEMAPHORE_ACQUIRE;
David Box894acb22015-01-15 01:12:17 -080064 int ret;
65 unsigned long start, end;
66
Andy Shevchenkoebf2ef82015-02-10 19:06:10 +020067 might_sleep();
68
David Box894acb22015-01-15 01:12:17 -080069 if (!dev || !dev->dev)
70 return -ENODEV;
71
Andy Shevchenko30be7742015-02-10 19:06:09 +020072 if (!dev->release_lock)
David Box894acb22015-01-15 01:12:17 -080073 return 0;
74
Andy Shevchenko9b5c9f02015-02-10 19:06:06 +020075 /* host driver writes to side band semaphore register */
Andy Shevchenko4077a382015-11-11 19:59:29 +020076 ret = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, PUNIT_SEMAPHORE, sem);
David Box894acb22015-01-15 01:12:17 -080077 if (ret) {
78 dev_err(dev->dev, "iosf punit semaphore request failed\n");
79 return ret;
80 }
81
82 /* host driver waits for bit 0 to be set in semaphore register */
83 start = jiffies;
84 end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT);
Andy Shevchenkoebf2ef82015-02-10 19:06:10 +020085 do {
David Box894acb22015-01-15 01:12:17 -080086 ret = get_sem(dev->dev, &sem);
87 if (!ret && sem) {
88 acquired = jiffies;
89 dev_dbg(dev->dev, "punit semaphore acquired after %ums\n",
90 jiffies_to_msecs(jiffies - start));
91 return 0;
92 }
93
94 usleep_range(1000, 2000);
Andy Shevchenkoebf2ef82015-02-10 19:06:10 +020095 } while (time_before(jiffies, end));
David Box894acb22015-01-15 01:12:17 -080096
97 dev_err(dev->dev, "punit semaphore timed out, resetting\n");
98 reset_semaphore(dev->dev);
99
Andy Shevchenko4077a382015-11-11 19:59:29 +0200100 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, PUNIT_SEMAPHORE, &sem);
Andy Shevchenko259aada2015-02-10 19:06:07 +0200101 if (ret)
David Box894acb22015-01-15 01:12:17 -0800102 dev_err(dev->dev, "iosf failed to read punit semaphore\n");
103 else
104 dev_err(dev->dev, "PUNIT SEM: %d\n", sem);
105
106 WARN_ON(1);
107
108 return -ETIMEDOUT;
109}
David Box894acb22015-01-15 01:12:17 -0800110
Andy Shevchenkoc8e043e2015-02-10 19:06:08 +0200111static void baytrail_i2c_release(struct dw_i2c_dev *dev)
David Box894acb22015-01-15 01:12:17 -0800112{
113 if (!dev || !dev->dev)
114 return;
115
116 if (!dev->acquire_lock)
117 return;
118
119 reset_semaphore(dev->dev);
120 dev_dbg(dev->dev, "punit semaphore held for %ums\n",
121 jiffies_to_msecs(jiffies - acquired));
122}
David Box894acb22015-01-15 01:12:17 -0800123
124int i2c_dw_eval_lock_support(struct dw_i2c_dev *dev)
125{
126 acpi_status status;
127 unsigned long long shared_host = 0;
128 acpi_handle handle;
129
130 if (!dev || !dev->dev)
131 return 0;
132
133 handle = ACPI_HANDLE(dev->dev);
134 if (!handle)
135 return 0;
136
137 status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host);
David Box894acb22015-01-15 01:12:17 -0800138 if (ACPI_FAILURE(status))
139 return 0;
140
141 if (shared_host) {
142 dev_info(dev->dev, "I2C bus managed by PUNIT\n");
143 dev->acquire_lock = baytrail_i2c_acquire;
144 dev->release_lock = baytrail_i2c_release;
145 dev->pm_runtime_disabled = true;
146 }
147
148 if (!iosf_mbi_available())
149 return -EPROBE_DEFER;
150
151 return 0;
152}