blob: 650a700f215e572fccc804139a370adfc9bab9f2 [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>
Hans de Goede086cb4a2017-02-10 11:27:56 +010019#include <linux/pm_qos.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
Hans de Goede62aee932017-02-10 11:27:54 +010032static int get_sem(struct dw_i2c_dev *dev, u32 *sem)
David Box894acb22015-01-15 01:12:17 -080033{
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) {
Hans de Goede62aee932017-02-10 11:27:54 +010039 dev_err(dev->dev, "iosf failed to read punit semaphore\n");
David Box894acb22015-01-15 01:12:17 -080040 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
Hans de Goede62aee932017-02-10 11:27:54 +010048static void reset_semaphore(struct dw_i2c_dev *dev)
David Box894acb22015-01-15 01:12:17 -080049{
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)) {
Hans de Goede62aee932017-02-10 11:27:54 +010053 dev_err(dev->dev, "iosf failed to reset punit semaphore during read\n");
David Box894acb22015-01-15 01:12:17 -080054 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))
Hans de Goede62aee932017-02-10 11:27:54 +010059 dev_err(dev->dev, "iosf failed to reset punit semaphore during write\n");
Hans de Goede086cb4a2017-02-10 11:27:56 +010060
61 pm_qos_update_request(&dev->pm_qos, PM_QOS_DEFAULT_VALUE);
David Box894acb22015-01-15 01:12:17 -080062}
63
Andy Shevchenkoc8e043e2015-02-10 19:06:08 +020064static int baytrail_i2c_acquire(struct dw_i2c_dev *dev)
David Box894acb22015-01-15 01:12:17 -080065{
Andy Shevchenko4077a382015-11-11 19:59:29 +020066 u32 sem = PUNIT_SEMAPHORE_ACQUIRE;
David Box894acb22015-01-15 01:12:17 -080067 int ret;
68 unsigned long start, end;
69
Andy Shevchenkoebf2ef82015-02-10 19:06:10 +020070 might_sleep();
71
David Box894acb22015-01-15 01:12:17 -080072 if (!dev || !dev->dev)
73 return -ENODEV;
74
Andy Shevchenko30be7742015-02-10 19:06:09 +020075 if (!dev->release_lock)
David Box894acb22015-01-15 01:12:17 -080076 return 0;
77
Hans de Goede086cb4a2017-02-10 11:27:56 +010078 /*
79 * Disallow the CPU to enter C6 or C7 state, entering these states
80 * requires the punit to talk to the pmic and if this happens while
81 * we're holding the semaphore, the SoC hangs.
82 */
83 pm_qos_update_request(&dev->pm_qos, 0);
84
Andy Shevchenko9b5c9f02015-02-10 19:06:06 +020085 /* host driver writes to side band semaphore register */
Andy Shevchenko4077a382015-11-11 19:59:29 +020086 ret = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, PUNIT_SEMAPHORE, sem);
David Box894acb22015-01-15 01:12:17 -080087 if (ret) {
88 dev_err(dev->dev, "iosf punit semaphore request failed\n");
Hans de Goede086cb4a2017-02-10 11:27:56 +010089 goto out;
David Box894acb22015-01-15 01:12:17 -080090 }
91
92 /* host driver waits for bit 0 to be set in semaphore register */
93 start = jiffies;
94 end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT);
Andy Shevchenkoebf2ef82015-02-10 19:06:10 +020095 do {
Hans de Goede62aee932017-02-10 11:27:54 +010096 ret = get_sem(dev, &sem);
David Box894acb22015-01-15 01:12:17 -080097 if (!ret && sem) {
98 acquired = jiffies;
99 dev_dbg(dev->dev, "punit semaphore acquired after %ums\n",
100 jiffies_to_msecs(jiffies - start));
101 return 0;
102 }
103
104 usleep_range(1000, 2000);
Andy Shevchenkoebf2ef82015-02-10 19:06:10 +0200105 } while (time_before(jiffies, end));
David Box894acb22015-01-15 01:12:17 -0800106
107 dev_err(dev->dev, "punit semaphore timed out, resetting\n");
Hans de Goede086cb4a2017-02-10 11:27:56 +0100108out:
Hans de Goede62aee932017-02-10 11:27:54 +0100109 reset_semaphore(dev);
David Box894acb22015-01-15 01:12:17 -0800110
Andy Shevchenko4077a382015-11-11 19:59:29 +0200111 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, PUNIT_SEMAPHORE, &sem);
Andy Shevchenko259aada2015-02-10 19:06:07 +0200112 if (ret)
David Box894acb22015-01-15 01:12:17 -0800113 dev_err(dev->dev, "iosf failed to read punit semaphore\n");
114 else
115 dev_err(dev->dev, "PUNIT SEM: %d\n", sem);
116
117 WARN_ON(1);
118
119 return -ETIMEDOUT;
120}
David Box894acb22015-01-15 01:12:17 -0800121
Andy Shevchenkoc8e043e2015-02-10 19:06:08 +0200122static void baytrail_i2c_release(struct dw_i2c_dev *dev)
David Box894acb22015-01-15 01:12:17 -0800123{
124 if (!dev || !dev->dev)
125 return;
126
127 if (!dev->acquire_lock)
128 return;
129
Hans de Goede62aee932017-02-10 11:27:54 +0100130 reset_semaphore(dev);
David Box894acb22015-01-15 01:12:17 -0800131 dev_dbg(dev->dev, "punit semaphore held for %ums\n",
132 jiffies_to_msecs(jiffies - acquired));
133}
David Box894acb22015-01-15 01:12:17 -0800134
Hans de Goede086cb4a2017-02-10 11:27:56 +0100135int i2c_dw_probe_lock_support(struct dw_i2c_dev *dev)
David Box894acb22015-01-15 01:12:17 -0800136{
137 acpi_status status;
138 unsigned long long shared_host = 0;
139 acpi_handle handle;
140
141 if (!dev || !dev->dev)
142 return 0;
143
144 handle = ACPI_HANDLE(dev->dev);
145 if (!handle)
146 return 0;
147
148 status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host);
David Box894acb22015-01-15 01:12:17 -0800149 if (ACPI_FAILURE(status))
150 return 0;
151
Hans de Goedee234ed22017-02-10 11:27:55 +0100152 if (!shared_host)
153 return 0;
David Box894acb22015-01-15 01:12:17 -0800154
155 if (!iosf_mbi_available())
156 return -EPROBE_DEFER;
157
Hans de Goedee234ed22017-02-10 11:27:55 +0100158 dev_info(dev->dev, "I2C bus managed by PUNIT\n");
159 dev->acquire_lock = baytrail_i2c_acquire;
160 dev->release_lock = baytrail_i2c_release;
161 dev->pm_runtime_disabled = true;
162
Hans de Goede086cb4a2017-02-10 11:27:56 +0100163 pm_qos_add_request(&dev->pm_qos, PM_QOS_CPU_DMA_LATENCY,
164 PM_QOS_DEFAULT_VALUE);
165
David Box894acb22015-01-15 01:12:17 -0800166 return 0;
167}
Hans de Goede086cb4a2017-02-10 11:27:56 +0100168
169void i2c_dw_remove_lock_support(struct dw_i2c_dev *dev)
170{
171 if (dev->acquire_lock)
172 pm_qos_remove_request(&dev->pm_qos);
173}