1 // SPDX-License-Identifier: GPL-2.0
3 * Intel BayTrail PMIC I2C bus semaphore implementaion
4 * Copyright (c) 2014, Intel Corporation.
6 #include <linux/delay.h>
7 #include <linux/device.h>
8 #include <linux/acpi.h>
10 #include <linux/interrupt.h>
11 #include <linux/pm_qos.h>
13 #include <asm/iosf_mbi.h>
15 #include "i2c-designware-core.h"
17 #define SEMAPHORE_TIMEOUT 500
18 #define PUNIT_SEMAPHORE 0x7
19 #define PUNIT_SEMAPHORE_CHT 0x10e
20 #define PUNIT_SEMAPHORE_BIT BIT(0)
21 #define PUNIT_SEMAPHORE_ACQUIRE BIT(1)
23 static unsigned long acquired
;
25 static u32
get_sem_addr(struct dw_i2c_dev
*dev
)
27 if (dev
->flags
& MODEL_CHERRYTRAIL
)
28 return PUNIT_SEMAPHORE_CHT
;
30 return PUNIT_SEMAPHORE
;
33 static int get_sem(struct dw_i2c_dev
*dev
, u32
*sem
)
35 u32 addr
= get_sem_addr(dev
);
39 ret
= iosf_mbi_read(BT_MBI_UNIT_PMC
, MBI_REG_READ
, addr
, &data
);
41 dev_err(dev
->dev
, "iosf failed to read punit semaphore\n");
45 *sem
= data
& PUNIT_SEMAPHORE_BIT
;
50 static void reset_semaphore(struct dw_i2c_dev
*dev
)
52 if (iosf_mbi_modify(BT_MBI_UNIT_PMC
, MBI_REG_READ
, get_sem_addr(dev
),
53 0, PUNIT_SEMAPHORE_BIT
))
54 dev_err(dev
->dev
, "iosf failed to reset punit semaphore during write\n");
56 pm_qos_update_request(&dev
->pm_qos
, PM_QOS_DEFAULT_VALUE
);
58 iosf_mbi_call_pmic_bus_access_notifier_chain(MBI_PMIC_BUS_ACCESS_END
,
60 iosf_mbi_punit_release();
63 static int baytrail_i2c_acquire(struct dw_i2c_dev
*dev
)
66 u32 sem
= PUNIT_SEMAPHORE_ACQUIRE
;
68 unsigned long start
, end
;
72 if (!dev
|| !dev
->dev
)
75 if (!dev
->release_lock
)
78 iosf_mbi_punit_acquire();
79 iosf_mbi_call_pmic_bus_access_notifier_chain(MBI_PMIC_BUS_ACCESS_BEGIN
,
83 * Disallow the CPU to enter C6 or C7 state, entering these states
84 * requires the punit to talk to the pmic and if this happens while
85 * we're holding the semaphore, the SoC hangs.
87 pm_qos_update_request(&dev
->pm_qos
, 0);
89 addr
= get_sem_addr(dev
);
91 /* host driver writes to side band semaphore register */
92 ret
= iosf_mbi_write(BT_MBI_UNIT_PMC
, MBI_REG_WRITE
, addr
, sem
);
94 dev_err(dev
->dev
, "iosf punit semaphore request failed\n");
98 /* host driver waits for bit 0 to be set in semaphore register */
100 end
= start
+ msecs_to_jiffies(SEMAPHORE_TIMEOUT
);
102 ret
= get_sem(dev
, &sem
);
105 dev_dbg(dev
->dev
, "punit semaphore acquired after %ums\n",
106 jiffies_to_msecs(jiffies
- start
));
110 usleep_range(1000, 2000);
111 } while (time_before(jiffies
, end
));
113 dev_err(dev
->dev
, "punit semaphore timed out, resetting\n");
115 reset_semaphore(dev
);
117 ret
= iosf_mbi_read(BT_MBI_UNIT_PMC
, MBI_REG_READ
, addr
, &sem
);
119 dev_err(dev
->dev
, "iosf failed to read punit semaphore\n");
121 dev_err(dev
->dev
, "PUNIT SEM: %d\n", sem
);
128 static void baytrail_i2c_release(struct dw_i2c_dev
*dev
)
130 if (!dev
|| !dev
->dev
)
133 if (!dev
->acquire_lock
)
136 reset_semaphore(dev
);
137 dev_dbg(dev
->dev
, "punit semaphore held for %ums\n",
138 jiffies_to_msecs(jiffies
- acquired
));
141 int i2c_dw_probe_lock_support(struct dw_i2c_dev
*dev
)
144 unsigned long long shared_host
= 0;
147 if (!dev
|| !dev
->dev
)
150 handle
= ACPI_HANDLE(dev
->dev
);
154 status
= acpi_evaluate_integer(handle
, "_SEM", NULL
, &shared_host
);
155 if (ACPI_FAILURE(status
))
161 if (!iosf_mbi_available())
162 return -EPROBE_DEFER
;
164 dev_info(dev
->dev
, "I2C bus managed by PUNIT\n");
165 dev
->acquire_lock
= baytrail_i2c_acquire
;
166 dev
->release_lock
= baytrail_i2c_release
;
167 dev
->pm_disabled
= true;
169 pm_qos_add_request(&dev
->pm_qos
, PM_QOS_CPU_DMA_LATENCY
,
170 PM_QOS_DEFAULT_VALUE
);
175 void i2c_dw_remove_lock_support(struct dw_i2c_dev
*dev
)
177 if (dev
->acquire_lock
)
178 pm_qos_remove_request(&dev
->pm_qos
);