blob: dbda8c9c8a1ce07e0563182d3dbe01d6380b5ac6 [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
Oliver Neukum78c43af2017-03-20 14:37:30 +010025#define SEMAPHORE_TIMEOUT 500
David Box894acb22015-01-15 01:12:17 -080026#define PUNIT_SEMAPHORE 0x7
Hans de Goedefd476fa2017-02-10 11:27:58 +010027#define PUNIT_SEMAPHORE_CHT 0x10e
Andy Shevchenko9b5c9f02015-02-10 19:06:06 +020028#define PUNIT_SEMAPHORE_BIT BIT(0)
29#define PUNIT_SEMAPHORE_ACQUIRE BIT(1)
David Box894acb22015-01-15 01:12:17 -080030
31static unsigned long acquired;
32
Hans de Goedefd476fa2017-02-10 11:27:58 +010033static u32 get_sem_addr(struct dw_i2c_dev *dev)
34{
35 if (dev->flags & MODEL_CHERRYTRAIL)
36 return PUNIT_SEMAPHORE_CHT;
37 else
38 return PUNIT_SEMAPHORE;
39}
40
Hans de Goede62aee932017-02-10 11:27:54 +010041static int get_sem(struct dw_i2c_dev *dev, u32 *sem)
David Box894acb22015-01-15 01:12:17 -080042{
Hans de Goedefd476fa2017-02-10 11:27:58 +010043 u32 addr = get_sem_addr(dev);
Andy Shevchenko9b5c9f02015-02-10 19:06:06 +020044 u32 data;
David Box894acb22015-01-15 01:12:17 -080045 int ret;
46
Hans de Goedefd476fa2017-02-10 11:27:58 +010047 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, addr, &data);
David Box894acb22015-01-15 01:12:17 -080048 if (ret) {
Hans de Goede62aee932017-02-10 11:27:54 +010049 dev_err(dev->dev, "iosf failed to read punit semaphore\n");
David Box894acb22015-01-15 01:12:17 -080050 return ret;
51 }
52
Andy Shevchenko9b5c9f02015-02-10 19:06:06 +020053 *sem = data & PUNIT_SEMAPHORE_BIT;
David Box894acb22015-01-15 01:12:17 -080054
55 return 0;
56}
57
Hans de Goede62aee932017-02-10 11:27:54 +010058static void reset_semaphore(struct dw_i2c_dev *dev)
David Box894acb22015-01-15 01:12:17 -080059{
Hans de Goedefd476fa2017-02-10 11:27:58 +010060 if (iosf_mbi_modify(BT_MBI_UNIT_PMC, MBI_REG_READ, get_sem_addr(dev),
Hans de Goede519e23a2017-02-10 11:27:57 +010061 0, PUNIT_SEMAPHORE_BIT))
Hans de Goede62aee932017-02-10 11:27:54 +010062 dev_err(dev->dev, "iosf failed to reset punit semaphore during write\n");
Hans de Goede086cb4a2017-02-10 11:27:56 +010063
64 pm_qos_update_request(&dev->pm_qos, PM_QOS_DEFAULT_VALUE);
Hans de Goede5b2cacc2017-02-10 11:27:59 +010065
Hans de Goeded93a6ed2017-02-10 11:28:00 +010066 iosf_mbi_call_pmic_bus_access_notifier_chain(MBI_PMIC_BUS_ACCESS_END,
67 NULL);
Hans de Goede5b2cacc2017-02-10 11:27:59 +010068 iosf_mbi_punit_release();
David Box894acb22015-01-15 01:12:17 -080069}
70
Andy Shevchenkoc8e043e2015-02-10 19:06:08 +020071static int baytrail_i2c_acquire(struct dw_i2c_dev *dev)
David Box894acb22015-01-15 01:12:17 -080072{
Colin Ian Kingc7f82ea2017-03-20 11:51:23 +000073 u32 addr;
Andy Shevchenko4077a382015-11-11 19:59:29 +020074 u32 sem = PUNIT_SEMAPHORE_ACQUIRE;
David Box894acb22015-01-15 01:12:17 -080075 int ret;
76 unsigned long start, end;
77
Andy Shevchenkoebf2ef82015-02-10 19:06:10 +020078 might_sleep();
79
David Box894acb22015-01-15 01:12:17 -080080 if (!dev || !dev->dev)
81 return -ENODEV;
82
Andy Shevchenko30be7742015-02-10 19:06:09 +020083 if (!dev->release_lock)
David Box894acb22015-01-15 01:12:17 -080084 return 0;
85
Hans de Goede5b2cacc2017-02-10 11:27:59 +010086 iosf_mbi_punit_acquire();
Hans de Goeded93a6ed2017-02-10 11:28:00 +010087 iosf_mbi_call_pmic_bus_access_notifier_chain(MBI_PMIC_BUS_ACCESS_BEGIN,
88 NULL);
Hans de Goede5b2cacc2017-02-10 11:27:59 +010089
Hans de Goede086cb4a2017-02-10 11:27:56 +010090 /*
91 * Disallow the CPU to enter C6 or C7 state, entering these states
92 * requires the punit to talk to the pmic and if this happens while
93 * we're holding the semaphore, the SoC hangs.
94 */
95 pm_qos_update_request(&dev->pm_qos, 0);
96
Colin Ian Kingc7f82ea2017-03-20 11:51:23 +000097 addr = get_sem_addr(dev);
98
Andy Shevchenko9b5c9f02015-02-10 19:06:06 +020099 /* host driver writes to side band semaphore register */
Hans de Goedefd476fa2017-02-10 11:27:58 +0100100 ret = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, addr, sem);
David Box894acb22015-01-15 01:12:17 -0800101 if (ret) {
102 dev_err(dev->dev, "iosf punit semaphore request failed\n");
Hans de Goede086cb4a2017-02-10 11:27:56 +0100103 goto out;
David Box894acb22015-01-15 01:12:17 -0800104 }
105
106 /* host driver waits for bit 0 to be set in semaphore register */
107 start = jiffies;
108 end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT);
Andy Shevchenkoebf2ef82015-02-10 19:06:10 +0200109 do {
Hans de Goede62aee932017-02-10 11:27:54 +0100110 ret = get_sem(dev, &sem);
David Box894acb22015-01-15 01:12:17 -0800111 if (!ret && sem) {
112 acquired = jiffies;
113 dev_dbg(dev->dev, "punit semaphore acquired after %ums\n",
114 jiffies_to_msecs(jiffies - start));
115 return 0;
116 }
117
118 usleep_range(1000, 2000);
Andy Shevchenkoebf2ef82015-02-10 19:06:10 +0200119 } while (time_before(jiffies, end));
David Box894acb22015-01-15 01:12:17 -0800120
121 dev_err(dev->dev, "punit semaphore timed out, resetting\n");
Hans de Goede086cb4a2017-02-10 11:27:56 +0100122out:
Hans de Goede62aee932017-02-10 11:27:54 +0100123 reset_semaphore(dev);
David Box894acb22015-01-15 01:12:17 -0800124
Hans de Goedefd476fa2017-02-10 11:27:58 +0100125 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, addr, &sem);
Andy Shevchenko259aada2015-02-10 19:06:07 +0200126 if (ret)
David Box894acb22015-01-15 01:12:17 -0800127 dev_err(dev->dev, "iosf failed to read punit semaphore\n");
128 else
129 dev_err(dev->dev, "PUNIT SEM: %d\n", sem);
130
131 WARN_ON(1);
132
133 return -ETIMEDOUT;
134}
David Box894acb22015-01-15 01:12:17 -0800135
Andy Shevchenkoc8e043e2015-02-10 19:06:08 +0200136static void baytrail_i2c_release(struct dw_i2c_dev *dev)
David Box894acb22015-01-15 01:12:17 -0800137{
138 if (!dev || !dev->dev)
139 return;
140
141 if (!dev->acquire_lock)
142 return;
143
Hans de Goede62aee932017-02-10 11:27:54 +0100144 reset_semaphore(dev);
David Box894acb22015-01-15 01:12:17 -0800145 dev_dbg(dev->dev, "punit semaphore held for %ums\n",
146 jiffies_to_msecs(jiffies - acquired));
147}
David Box894acb22015-01-15 01:12:17 -0800148
Hans de Goede086cb4a2017-02-10 11:27:56 +0100149int i2c_dw_probe_lock_support(struct dw_i2c_dev *dev)
David Box894acb22015-01-15 01:12:17 -0800150{
151 acpi_status status;
152 unsigned long long shared_host = 0;
153 acpi_handle handle;
154
155 if (!dev || !dev->dev)
156 return 0;
157
158 handle = ACPI_HANDLE(dev->dev);
159 if (!handle)
160 return 0;
161
162 status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host);
David Box894acb22015-01-15 01:12:17 -0800163 if (ACPI_FAILURE(status))
164 return 0;
165
Hans de Goedee234ed22017-02-10 11:27:55 +0100166 if (!shared_host)
167 return 0;
David Box894acb22015-01-15 01:12:17 -0800168
169 if (!iosf_mbi_available())
170 return -EPROBE_DEFER;
171
Hans de Goedee234ed22017-02-10 11:27:55 +0100172 dev_info(dev->dev, "I2C bus managed by PUNIT\n");
173 dev->acquire_lock = baytrail_i2c_acquire;
174 dev->release_lock = baytrail_i2c_release;
Hans de Goede41c80b82017-03-13 23:25:09 +0100175 dev->pm_disabled = true;
Hans de Goedee234ed22017-02-10 11:27:55 +0100176
Hans de Goede086cb4a2017-02-10 11:27:56 +0100177 pm_qos_add_request(&dev->pm_qos, PM_QOS_CPU_DMA_LATENCY,
178 PM_QOS_DEFAULT_VALUE);
179
David Box894acb22015-01-15 01:12:17 -0800180 return 0;
181}
Hans de Goede086cb4a2017-02-10 11:27:56 +0100182
183void i2c_dw_remove_lock_support(struct dw_i2c_dev *dev)
184{
185 if (dev->acquire_lock)
186 pm_qos_remove_request(&dev->pm_qos);
187}