blob: e38c2bbba94030d27976fec66d637d164fcd1e8c [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 */
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>
Andy Shevchenkoc8e043e2015-02-10 19:06:08 +020020
David Box894acb22015-01-15 01:12:17 -080021#include <asm/iosf_mbi.h>
Andy Shevchenkoc8e043e2015-02-10 19:06:08 +020022
David Box894acb22015-01-15 01:12:17 -080023#include "i2c-designware-core.h"
24
25#define SEMAPHORE_TIMEOUT 100
26#define PUNIT_SEMAPHORE 0x7
Andy Shevchenko9b5c9f02015-02-10 19:06:06 +020027#define PUNIT_SEMAPHORE_BIT BIT(0)
28#define PUNIT_SEMAPHORE_ACQUIRE BIT(1)
David Box894acb22015-01-15 01:12:17 -080029
30static unsigned long acquired;
31
32static int get_sem(struct device *dev, u32 *sem)
33{
Andy Shevchenko9b5c9f02015-02-10 19:06:06 +020034 u32 data;
David Box894acb22015-01-15 01:12:17 -080035 int ret;
36
Andy Shevchenko4077a382015-11-11 19:59:29 +020037 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, PUNIT_SEMAPHORE, &data);
David Box894acb22015-01-15 01:12:17 -080038 if (ret) {
39 dev_err(dev, "iosf failed to read punit semaphore\n");
40 return ret;
41 }
42
Andy Shevchenko9b5c9f02015-02-10 19:06:06 +020043 *sem = data & PUNIT_SEMAPHORE_BIT;
David Box894acb22015-01-15 01:12:17 -080044
45 return 0;
46}
47
48static void reset_semaphore(struct device *dev)
49{
50 u32 data;
51
Andy Shevchenko4077a382015-11-11 19:59:29 +020052 if (iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, PUNIT_SEMAPHORE, &data)) {
David Box894acb22015-01-15 01:12:17 -080053 dev_err(dev, "iosf failed to reset punit semaphore during read\n");
54 return;
55 }
56
Andy Shevchenko9b5c9f02015-02-10 19:06:06 +020057 data &= ~PUNIT_SEMAPHORE_BIT;
Andy Shevchenko4077a382015-11-11 19:59:29 +020058 if (iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, PUNIT_SEMAPHORE, data))
David Box894acb22015-01-15 01:12:17 -080059 dev_err(dev, "iosf failed to reset punit semaphore during write\n");
60}
61
Andy Shevchenkoc8e043e2015-02-10 19:06:08 +020062static int baytrail_i2c_acquire(struct dw_i2c_dev *dev)
David Box894acb22015-01-15 01:12:17 -080063{
Andy Shevchenko4077a382015-11-11 19:59:29 +020064 u32 sem = PUNIT_SEMAPHORE_ACQUIRE;
David Box894acb22015-01-15 01:12:17 -080065 int ret;
66 unsigned long start, end;
67
Andy Shevchenkoebf2ef82015-02-10 19:06:10 +020068 might_sleep();
69
David Box894acb22015-01-15 01:12:17 -080070 if (!dev || !dev->dev)
71 return -ENODEV;
72
Andy Shevchenko30be7742015-02-10 19:06:09 +020073 if (!dev->release_lock)
David Box894acb22015-01-15 01:12:17 -080074 return 0;
75
Andy Shevchenko9b5c9f02015-02-10 19:06:06 +020076 /* host driver writes to side band semaphore register */
Andy Shevchenko4077a382015-11-11 19:59:29 +020077 ret = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, PUNIT_SEMAPHORE, sem);
David Box894acb22015-01-15 01:12:17 -080078 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);
Andy Shevchenkoebf2ef82015-02-10 19:06:10 +020086 do {
David Box894acb22015-01-15 01:12:17 -080087 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);
Andy Shevchenkoebf2ef82015-02-10 19:06:10 +020096 } while (time_before(jiffies, end));
David Box894acb22015-01-15 01:12:17 -080097
98 dev_err(dev->dev, "punit semaphore timed out, resetting\n");
99 reset_semaphore(dev->dev);
100
Andy Shevchenko4077a382015-11-11 19:59:29 +0200101 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, PUNIT_SEMAPHORE, &sem);
Andy Shevchenko259aada2015-02-10 19:06:07 +0200102 if (ret)
David Box894acb22015-01-15 01:12:17 -0800103 dev_err(dev->dev, "iosf failed to read punit semaphore\n");
104 else
105 dev_err(dev->dev, "PUNIT SEM: %d\n", sem);
106
107 WARN_ON(1);
108
109 return -ETIMEDOUT;
110}
David Box894acb22015-01-15 01:12:17 -0800111
Andy Shevchenkoc8e043e2015-02-10 19:06:08 +0200112static void baytrail_i2c_release(struct dw_i2c_dev *dev)
David Box894acb22015-01-15 01:12:17 -0800113{
114 if (!dev || !dev->dev)
115 return;
116
117 if (!dev->acquire_lock)
118 return;
119
120 reset_semaphore(dev->dev);
121 dev_dbg(dev->dev, "punit semaphore held for %ums\n",
122 jiffies_to_msecs(jiffies - acquired));
123}
David Box894acb22015-01-15 01:12:17 -0800124
125int i2c_dw_eval_lock_support(struct dw_i2c_dev *dev)
126{
127 acpi_status status;
128 unsigned long long shared_host = 0;
129 acpi_handle handle;
130
131 if (!dev || !dev->dev)
132 return 0;
133
134 handle = ACPI_HANDLE(dev->dev);
135 if (!handle)
136 return 0;
137
138 status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host);
David Box894acb22015-01-15 01:12:17 -0800139 if (ACPI_FAILURE(status))
140 return 0;
141
142 if (shared_host) {
143 dev_info(dev->dev, "I2C bus managed by PUNIT\n");
144 dev->acquire_lock = baytrail_i2c_acquire;
145 dev->release_lock = baytrail_i2c_release;
146 dev->pm_runtime_disabled = true;
147 }
148
149 if (!iosf_mbi_available())
150 return -EPROBE_DEFER;
151
152 return 0;
153}
David Box894acb22015-01-15 01:12:17 -0800154
155MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
156MODULE_DESCRIPTION("Baytrail I2C Semaphore driver");
157MODULE_LICENSE("GPL v2");