blob: 8346a0e399493a88fe6b163c3c140e1129444662 [file] [log] [blame]
Linus Walleije3726fc2010-08-19 12:36:01 +01001/*
Martin Perssone0befb22010-12-08 15:13:28 +01002 * Copyright (C) STMicroelectronics 2009
3 * Copyright (C) ST-Ericsson SA 2010
Linus Walleije3726fc2010-08-19 12:36:01 +01004 *
5 * License Terms: GNU General Public License v2
Martin Perssone0befb22010-12-08 15:13:28 +01006 * Author: Kumar Sanghvi <kumar.sanghvi@stericsson.com>
7 * Author: Sundar Iyer <sundar.iyer@stericsson.com>
Linus Walleije3726fc2010-08-19 12:36:01 +01008 * Author: Mattias Nilsson <mattias.i.nilsson@stericsson.com>
9 *
Martin Perssone0befb22010-12-08 15:13:28 +010010 * U8500 PRCM Unit interface driver
11 *
Linus Walleije3726fc2010-08-19 12:36:01 +010012 */
Linus Walleije3726fc2010-08-19 12:36:01 +010013#include <linux/module.h>
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020014#include <linux/kernel.h>
15#include <linux/delay.h>
Linus Walleije3726fc2010-08-19 12:36:01 +010016#include <linux/errno.h>
17#include <linux/err.h>
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020018#include <linux/spinlock.h>
Linus Walleije3726fc2010-08-19 12:36:01 +010019#include <linux/io.h>
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020020#include <linux/slab.h>
Linus Walleije3726fc2010-08-19 12:36:01 +010021#include <linux/mutex.h>
22#include <linux/completion.h>
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020023#include <linux/irq.h>
Linus Walleije3726fc2010-08-19 12:36:01 +010024#include <linux/jiffies.h>
25#include <linux/bitops.h>
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020026#include <linux/fs.h>
27#include <linux/platform_device.h>
28#include <linux/uaccess.h>
29#include <linux/mfd/core.h>
Mattias Nilsson73180f82011-08-12 10:28:10 +020030#include <linux/mfd/dbx500-prcmu.h>
Bengt Jonsson1032fbf2011-04-01 14:43:33 +020031#include <linux/regulator/db8500-prcmu.h>
32#include <linux/regulator/machine.h>
Daniel Lezcanocc9a0f62012-02-28 22:46:06 +010033#include <asm/hardware/gic.h>
Linus Walleije3726fc2010-08-19 12:36:01 +010034#include <mach/hardware.h>
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020035#include <mach/irqs.h>
36#include <mach/db8500-regs.h>
37#include <mach/id.h>
Mattias Nilsson73180f82011-08-12 10:28:10 +020038#include "dbx500-prcmu-regs.h"
Linus Walleije3726fc2010-08-19 12:36:01 +010039
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020040/* Offset for the firmware version within the TCPM */
41#define PRCMU_FW_VERSION_OFFSET 0xA4
Linus Walleije3726fc2010-08-19 12:36:01 +010042
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020043/* Index of different voltages to be used when accessing AVSData */
44#define PRCM_AVS_BASE 0x2FC
45#define PRCM_AVS_VBB_RET (PRCM_AVS_BASE + 0x0)
46#define PRCM_AVS_VBB_MAX_OPP (PRCM_AVS_BASE + 0x1)
47#define PRCM_AVS_VBB_100_OPP (PRCM_AVS_BASE + 0x2)
48#define PRCM_AVS_VBB_50_OPP (PRCM_AVS_BASE + 0x3)
49#define PRCM_AVS_VARM_MAX_OPP (PRCM_AVS_BASE + 0x4)
50#define PRCM_AVS_VARM_100_OPP (PRCM_AVS_BASE + 0x5)
51#define PRCM_AVS_VARM_50_OPP (PRCM_AVS_BASE + 0x6)
52#define PRCM_AVS_VARM_RET (PRCM_AVS_BASE + 0x7)
53#define PRCM_AVS_VAPE_100_OPP (PRCM_AVS_BASE + 0x8)
54#define PRCM_AVS_VAPE_50_OPP (PRCM_AVS_BASE + 0x9)
55#define PRCM_AVS_VMOD_100_OPP (PRCM_AVS_BASE + 0xA)
56#define PRCM_AVS_VMOD_50_OPP (PRCM_AVS_BASE + 0xB)
57#define PRCM_AVS_VSAFE (PRCM_AVS_BASE + 0xC)
Martin Perssone0befb22010-12-08 15:13:28 +010058
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020059#define PRCM_AVS_VOLTAGE 0
60#define PRCM_AVS_VOLTAGE_MASK 0x3f
61#define PRCM_AVS_ISSLOWSTARTUP 6
62#define PRCM_AVS_ISSLOWSTARTUP_MASK (1 << PRCM_AVS_ISSLOWSTARTUP)
Martin Perssone0befb22010-12-08 15:13:28 +010063#define PRCM_AVS_ISMODEENABLE 7
64#define PRCM_AVS_ISMODEENABLE_MASK (1 << PRCM_AVS_ISMODEENABLE)
65
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020066#define PRCM_BOOT_STATUS 0xFFF
67#define PRCM_ROMCODE_A2P 0xFFE
68#define PRCM_ROMCODE_P2A 0xFFD
69#define PRCM_XP70_CUR_PWR_STATE 0xFFC /* 4 BYTES */
Linus Walleije3726fc2010-08-19 12:36:01 +010070
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020071#define PRCM_SW_RST_REASON 0xFF8 /* 2 bytes */
72
73#define _PRCM_MBOX_HEADER 0xFE8 /* 16 bytes */
74#define PRCM_MBOX_HEADER_REQ_MB0 (_PRCM_MBOX_HEADER + 0x0)
75#define PRCM_MBOX_HEADER_REQ_MB1 (_PRCM_MBOX_HEADER + 0x1)
76#define PRCM_MBOX_HEADER_REQ_MB2 (_PRCM_MBOX_HEADER + 0x2)
77#define PRCM_MBOX_HEADER_REQ_MB3 (_PRCM_MBOX_HEADER + 0x3)
78#define PRCM_MBOX_HEADER_REQ_MB4 (_PRCM_MBOX_HEADER + 0x4)
79#define PRCM_MBOX_HEADER_REQ_MB5 (_PRCM_MBOX_HEADER + 0x5)
80#define PRCM_MBOX_HEADER_ACK_MB0 (_PRCM_MBOX_HEADER + 0x8)
81
82/* Req Mailboxes */
83#define PRCM_REQ_MB0 0xFDC /* 12 bytes */
84#define PRCM_REQ_MB1 0xFD0 /* 12 bytes */
85#define PRCM_REQ_MB2 0xFC0 /* 16 bytes */
86#define PRCM_REQ_MB3 0xE4C /* 372 bytes */
87#define PRCM_REQ_MB4 0xE48 /* 4 bytes */
88#define PRCM_REQ_MB5 0xE44 /* 4 bytes */
89
90/* Ack Mailboxes */
91#define PRCM_ACK_MB0 0xE08 /* 52 bytes */
92#define PRCM_ACK_MB1 0xE04 /* 4 bytes */
93#define PRCM_ACK_MB2 0xE00 /* 4 bytes */
94#define PRCM_ACK_MB3 0xDFC /* 4 bytes */
95#define PRCM_ACK_MB4 0xDF8 /* 4 bytes */
96#define PRCM_ACK_MB5 0xDF4 /* 4 bytes */
97
98/* Mailbox 0 headers */
99#define MB0H_POWER_STATE_TRANS 0
100#define MB0H_CONFIG_WAKEUPS_EXE 1
101#define MB0H_READ_WAKEUP_ACK 3
102#define MB0H_CONFIG_WAKEUPS_SLEEP 4
103
104#define MB0H_WAKEUP_EXE 2
105#define MB0H_WAKEUP_SLEEP 5
106
107/* Mailbox 0 REQs */
108#define PRCM_REQ_MB0_AP_POWER_STATE (PRCM_REQ_MB0 + 0x0)
109#define PRCM_REQ_MB0_AP_PLL_STATE (PRCM_REQ_MB0 + 0x1)
110#define PRCM_REQ_MB0_ULP_CLOCK_STATE (PRCM_REQ_MB0 + 0x2)
111#define PRCM_REQ_MB0_DO_NOT_WFI (PRCM_REQ_MB0 + 0x3)
112#define PRCM_REQ_MB0_WAKEUP_8500 (PRCM_REQ_MB0 + 0x4)
113#define PRCM_REQ_MB0_WAKEUP_4500 (PRCM_REQ_MB0 + 0x8)
114
115/* Mailbox 0 ACKs */
116#define PRCM_ACK_MB0_AP_PWRSTTR_STATUS (PRCM_ACK_MB0 + 0x0)
117#define PRCM_ACK_MB0_READ_POINTER (PRCM_ACK_MB0 + 0x1)
118#define PRCM_ACK_MB0_WAKEUP_0_8500 (PRCM_ACK_MB0 + 0x4)
119#define PRCM_ACK_MB0_WAKEUP_0_4500 (PRCM_ACK_MB0 + 0x8)
120#define PRCM_ACK_MB0_WAKEUP_1_8500 (PRCM_ACK_MB0 + 0x1C)
121#define PRCM_ACK_MB0_WAKEUP_1_4500 (PRCM_ACK_MB0 + 0x20)
122#define PRCM_ACK_MB0_EVENT_4500_NUMBERS 20
123
124/* Mailbox 1 headers */
125#define MB1H_ARM_APE_OPP 0x0
126#define MB1H_RESET_MODEM 0x2
127#define MB1H_REQUEST_APE_OPP_100_VOLT 0x3
128#define MB1H_RELEASE_APE_OPP_100_VOLT 0x4
129#define MB1H_RELEASE_USB_WAKEUP 0x5
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200130#define MB1H_PLL_ON_OFF 0x6
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200131
132/* Mailbox 1 Requests */
133#define PRCM_REQ_MB1_ARM_OPP (PRCM_REQ_MB1 + 0x0)
134#define PRCM_REQ_MB1_APE_OPP (PRCM_REQ_MB1 + 0x1)
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200135#define PRCM_REQ_MB1_PLL_ON_OFF (PRCM_REQ_MB1 + 0x4)
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100136#define PLL_SOC0_OFF 0x1
137#define PLL_SOC0_ON 0x2
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200138#define PLL_SOC1_OFF 0x4
139#define PLL_SOC1_ON 0x8
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200140
141/* Mailbox 1 ACKs */
142#define PRCM_ACK_MB1_CURRENT_ARM_OPP (PRCM_ACK_MB1 + 0x0)
143#define PRCM_ACK_MB1_CURRENT_APE_OPP (PRCM_ACK_MB1 + 0x1)
144#define PRCM_ACK_MB1_APE_VOLTAGE_STATUS (PRCM_ACK_MB1 + 0x2)
145#define PRCM_ACK_MB1_DVFS_STATUS (PRCM_ACK_MB1 + 0x3)
146
147/* Mailbox 2 headers */
148#define MB2H_DPS 0x0
149#define MB2H_AUTO_PWR 0x1
150
151/* Mailbox 2 REQs */
152#define PRCM_REQ_MB2_SVA_MMDSP (PRCM_REQ_MB2 + 0x0)
153#define PRCM_REQ_MB2_SVA_PIPE (PRCM_REQ_MB2 + 0x1)
154#define PRCM_REQ_MB2_SIA_MMDSP (PRCM_REQ_MB2 + 0x2)
155#define PRCM_REQ_MB2_SIA_PIPE (PRCM_REQ_MB2 + 0x3)
156#define PRCM_REQ_MB2_SGA (PRCM_REQ_MB2 + 0x4)
157#define PRCM_REQ_MB2_B2R2_MCDE (PRCM_REQ_MB2 + 0x5)
158#define PRCM_REQ_MB2_ESRAM12 (PRCM_REQ_MB2 + 0x6)
159#define PRCM_REQ_MB2_ESRAM34 (PRCM_REQ_MB2 + 0x7)
160#define PRCM_REQ_MB2_AUTO_PM_SLEEP (PRCM_REQ_MB2 + 0x8)
161#define PRCM_REQ_MB2_AUTO_PM_IDLE (PRCM_REQ_MB2 + 0xC)
162
163/* Mailbox 2 ACKs */
164#define PRCM_ACK_MB2_DPS_STATUS (PRCM_ACK_MB2 + 0x0)
165#define HWACC_PWR_ST_OK 0xFE
166
167/* Mailbox 3 headers */
168#define MB3H_ANC 0x0
169#define MB3H_SIDETONE 0x1
170#define MB3H_SYSCLK 0xE
171
172/* Mailbox 3 Requests */
173#define PRCM_REQ_MB3_ANC_FIR_COEFF (PRCM_REQ_MB3 + 0x0)
174#define PRCM_REQ_MB3_ANC_IIR_COEFF (PRCM_REQ_MB3 + 0x20)
175#define PRCM_REQ_MB3_ANC_SHIFTER (PRCM_REQ_MB3 + 0x60)
176#define PRCM_REQ_MB3_ANC_WARP (PRCM_REQ_MB3 + 0x64)
177#define PRCM_REQ_MB3_SIDETONE_FIR_GAIN (PRCM_REQ_MB3 + 0x68)
178#define PRCM_REQ_MB3_SIDETONE_FIR_COEFF (PRCM_REQ_MB3 + 0x6C)
179#define PRCM_REQ_MB3_SYSCLK_MGT (PRCM_REQ_MB3 + 0x16C)
180
181/* Mailbox 4 headers */
182#define MB4H_DDR_INIT 0x0
183#define MB4H_MEM_ST 0x1
184#define MB4H_HOTDOG 0x12
185#define MB4H_HOTMON 0x13
186#define MB4H_HOT_PERIOD 0x14
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200187#define MB4H_A9WDOG_CONF 0x16
188#define MB4H_A9WDOG_EN 0x17
189#define MB4H_A9WDOG_DIS 0x18
190#define MB4H_A9WDOG_LOAD 0x19
191#define MB4H_A9WDOG_KICK 0x20
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200192
193/* Mailbox 4 Requests */
194#define PRCM_REQ_MB4_DDR_ST_AP_SLEEP_IDLE (PRCM_REQ_MB4 + 0x0)
195#define PRCM_REQ_MB4_DDR_ST_AP_DEEP_IDLE (PRCM_REQ_MB4 + 0x1)
196#define PRCM_REQ_MB4_ESRAM0_ST (PRCM_REQ_MB4 + 0x3)
197#define PRCM_REQ_MB4_HOTDOG_THRESHOLD (PRCM_REQ_MB4 + 0x0)
198#define PRCM_REQ_MB4_HOTMON_LOW (PRCM_REQ_MB4 + 0x0)
199#define PRCM_REQ_MB4_HOTMON_HIGH (PRCM_REQ_MB4 + 0x1)
200#define PRCM_REQ_MB4_HOTMON_CONFIG (PRCM_REQ_MB4 + 0x2)
201#define PRCM_REQ_MB4_HOT_PERIOD (PRCM_REQ_MB4 + 0x0)
202#define HOTMON_CONFIG_LOW BIT(0)
203#define HOTMON_CONFIG_HIGH BIT(1)
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200204#define PRCM_REQ_MB4_A9WDOG_0 (PRCM_REQ_MB4 + 0x0)
205#define PRCM_REQ_MB4_A9WDOG_1 (PRCM_REQ_MB4 + 0x1)
206#define PRCM_REQ_MB4_A9WDOG_2 (PRCM_REQ_MB4 + 0x2)
207#define PRCM_REQ_MB4_A9WDOG_3 (PRCM_REQ_MB4 + 0x3)
208#define A9WDOG_AUTO_OFF_EN BIT(7)
209#define A9WDOG_AUTO_OFF_DIS 0
210#define A9WDOG_ID_MASK 0xf
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200211
212/* Mailbox 5 Requests */
213#define PRCM_REQ_MB5_I2C_SLAVE_OP (PRCM_REQ_MB5 + 0x0)
214#define PRCM_REQ_MB5_I2C_HW_BITS (PRCM_REQ_MB5 + 0x1)
215#define PRCM_REQ_MB5_I2C_REG (PRCM_REQ_MB5 + 0x2)
216#define PRCM_REQ_MB5_I2C_VAL (PRCM_REQ_MB5 + 0x3)
217#define PRCMU_I2C_WRITE(slave) \
218 (((slave) << 1) | (cpu_is_u8500v2() ? BIT(6) : 0))
219#define PRCMU_I2C_READ(slave) \
220 (((slave) << 1) | BIT(0) | (cpu_is_u8500v2() ? BIT(6) : 0))
221#define PRCMU_I2C_STOP_EN BIT(3)
222
223/* Mailbox 5 ACKs */
224#define PRCM_ACK_MB5_I2C_STATUS (PRCM_ACK_MB5 + 0x1)
225#define PRCM_ACK_MB5_I2C_VAL (PRCM_ACK_MB5 + 0x3)
226#define I2C_WR_OK 0x1
227#define I2C_RD_OK 0x2
228
229#define NUM_MB 8
230#define MBOX_BIT BIT
231#define ALL_MBOX_BITS (MBOX_BIT(NUM_MB) - 1)
232
233/*
234 * Wakeups/IRQs
235 */
236
237#define WAKEUP_BIT_RTC BIT(0)
238#define WAKEUP_BIT_RTT0 BIT(1)
239#define WAKEUP_BIT_RTT1 BIT(2)
240#define WAKEUP_BIT_HSI0 BIT(3)
241#define WAKEUP_BIT_HSI1 BIT(4)
242#define WAKEUP_BIT_CA_WAKE BIT(5)
243#define WAKEUP_BIT_USB BIT(6)
244#define WAKEUP_BIT_ABB BIT(7)
245#define WAKEUP_BIT_ABB_FIFO BIT(8)
246#define WAKEUP_BIT_SYSCLK_OK BIT(9)
247#define WAKEUP_BIT_CA_SLEEP BIT(10)
248#define WAKEUP_BIT_AC_WAKE_ACK BIT(11)
249#define WAKEUP_BIT_SIDE_TONE_OK BIT(12)
250#define WAKEUP_BIT_ANC_OK BIT(13)
251#define WAKEUP_BIT_SW_ERROR BIT(14)
252#define WAKEUP_BIT_AC_SLEEP_ACK BIT(15)
253#define WAKEUP_BIT_ARM BIT(17)
254#define WAKEUP_BIT_HOTMON_LOW BIT(18)
255#define WAKEUP_BIT_HOTMON_HIGH BIT(19)
256#define WAKEUP_BIT_MODEM_SW_RESET_REQ BIT(20)
257#define WAKEUP_BIT_GPIO0 BIT(23)
258#define WAKEUP_BIT_GPIO1 BIT(24)
259#define WAKEUP_BIT_GPIO2 BIT(25)
260#define WAKEUP_BIT_GPIO3 BIT(26)
261#define WAKEUP_BIT_GPIO4 BIT(27)
262#define WAKEUP_BIT_GPIO5 BIT(28)
263#define WAKEUP_BIT_GPIO6 BIT(29)
264#define WAKEUP_BIT_GPIO7 BIT(30)
265#define WAKEUP_BIT_GPIO8 BIT(31)
266
Mattias Nilssonb58d12f2012-01-13 16:20:10 +0100267static struct {
268 bool valid;
269 struct prcmu_fw_version version;
270} fw_info;
271
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200272/*
273 * This vector maps irq numbers to the bits in the bit field used in
274 * communication with the PRCMU firmware.
275 *
276 * The reason for having this is to keep the irq numbers contiguous even though
277 * the bits in the bit field are not. (The bits also have a tendency to move
278 * around, to further complicate matters.)
279 */
280#define IRQ_INDEX(_name) ((IRQ_PRCMU_##_name) - IRQ_PRCMU_BASE)
281#define IRQ_ENTRY(_name)[IRQ_INDEX(_name)] = (WAKEUP_BIT_##_name)
282static u32 prcmu_irq_bit[NUM_PRCMU_WAKEUPS] = {
283 IRQ_ENTRY(RTC),
284 IRQ_ENTRY(RTT0),
285 IRQ_ENTRY(RTT1),
286 IRQ_ENTRY(HSI0),
287 IRQ_ENTRY(HSI1),
288 IRQ_ENTRY(CA_WAKE),
289 IRQ_ENTRY(USB),
290 IRQ_ENTRY(ABB),
291 IRQ_ENTRY(ABB_FIFO),
292 IRQ_ENTRY(CA_SLEEP),
293 IRQ_ENTRY(ARM),
294 IRQ_ENTRY(HOTMON_LOW),
295 IRQ_ENTRY(HOTMON_HIGH),
296 IRQ_ENTRY(MODEM_SW_RESET_REQ),
297 IRQ_ENTRY(GPIO0),
298 IRQ_ENTRY(GPIO1),
299 IRQ_ENTRY(GPIO2),
300 IRQ_ENTRY(GPIO3),
301 IRQ_ENTRY(GPIO4),
302 IRQ_ENTRY(GPIO5),
303 IRQ_ENTRY(GPIO6),
304 IRQ_ENTRY(GPIO7),
305 IRQ_ENTRY(GPIO8)
Martin Perssone0befb22010-12-08 15:13:28 +0100306};
307
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200308#define VALID_WAKEUPS (BIT(NUM_PRCMU_WAKEUP_INDICES) - 1)
309#define WAKEUP_ENTRY(_name)[PRCMU_WAKEUP_INDEX_##_name] = (WAKEUP_BIT_##_name)
310static u32 prcmu_wakeup_bit[NUM_PRCMU_WAKEUP_INDICES] = {
311 WAKEUP_ENTRY(RTC),
312 WAKEUP_ENTRY(RTT0),
313 WAKEUP_ENTRY(RTT1),
314 WAKEUP_ENTRY(HSI0),
315 WAKEUP_ENTRY(HSI1),
316 WAKEUP_ENTRY(USB),
317 WAKEUP_ENTRY(ABB),
318 WAKEUP_ENTRY(ABB_FIFO),
319 WAKEUP_ENTRY(ARM)
320};
321
322/*
323 * mb0_transfer - state needed for mailbox 0 communication.
324 * @lock: The transaction lock.
325 * @dbb_events_lock: A lock used to handle concurrent access to (parts of)
326 * the request data.
327 * @mask_work: Work structure used for (un)masking wakeup interrupts.
328 * @req: Request data that need to persist between requests.
329 */
330static struct {
331 spinlock_t lock;
332 spinlock_t dbb_irqs_lock;
333 struct work_struct mask_work;
334 struct mutex ac_wake_lock;
335 struct completion ac_wake_work;
336 struct {
337 u32 dbb_irqs;
338 u32 dbb_wakeups;
339 u32 abb_events;
340 } req;
341} mb0_transfer;
342
343/*
344 * mb1_transfer - state needed for mailbox 1 communication.
345 * @lock: The transaction lock.
346 * @work: The transaction completion structure.
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100347 * @ape_opp: The current APE OPP.
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200348 * @ack: Reply ("acknowledge") data.
349 */
Martin Perssone0befb22010-12-08 15:13:28 +0100350static struct {
351 struct mutex lock;
352 struct completion work;
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100353 u8 ape_opp;
Martin Perssone0befb22010-12-08 15:13:28 +0100354 struct {
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200355 u8 header;
Martin Perssone0befb22010-12-08 15:13:28 +0100356 u8 arm_opp;
357 u8 ape_opp;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200358 u8 ape_voltage_status;
Martin Perssone0befb22010-12-08 15:13:28 +0100359 } ack;
360} mb1_transfer;
361
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200362/*
363 * mb2_transfer - state needed for mailbox 2 communication.
364 * @lock: The transaction lock.
365 * @work: The transaction completion structure.
366 * @auto_pm_lock: The autonomous power management configuration lock.
367 * @auto_pm_enabled: A flag indicating whether autonomous PM is enabled.
368 * @req: Request data that need to persist between requests.
369 * @ack: Reply ("acknowledge") data.
370 */
Linus Walleije3726fc2010-08-19 12:36:01 +0100371static struct {
372 struct mutex lock;
373 struct completion work;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200374 spinlock_t auto_pm_lock;
375 bool auto_pm_enabled;
376 struct {
377 u8 status;
378 } ack;
379} mb2_transfer;
380
381/*
382 * mb3_transfer - state needed for mailbox 3 communication.
383 * @lock: The request lock.
384 * @sysclk_lock: A lock used to handle concurrent sysclk requests.
385 * @sysclk_work: Work structure used for sysclk requests.
386 */
387static struct {
388 spinlock_t lock;
389 struct mutex sysclk_lock;
390 struct completion sysclk_work;
391} mb3_transfer;
392
393/*
394 * mb4_transfer - state needed for mailbox 4 communication.
395 * @lock: The transaction lock.
396 * @work: The transaction completion structure.
397 */
398static struct {
399 struct mutex lock;
400 struct completion work;
401} mb4_transfer;
402
403/*
404 * mb5_transfer - state needed for mailbox 5 communication.
405 * @lock: The transaction lock.
406 * @work: The transaction completion structure.
407 * @ack: Reply ("acknowledge") data.
408 */
409static struct {
410 struct mutex lock;
411 struct completion work;
Linus Walleije3726fc2010-08-19 12:36:01 +0100412 struct {
413 u8 status;
414 u8 value;
415 } ack;
416} mb5_transfer;
417
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200418static atomic_t ac_wake_req_state = ATOMIC_INIT(0);
419
420/* Spinlocks */
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100421static DEFINE_SPINLOCK(prcmu_lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200422static DEFINE_SPINLOCK(clkout_lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200423
424/* Global var to runtime determine TCDM base for v2 or v1 */
425static __iomem void *tcdm_base;
426
427struct clk_mgt {
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100428 void __iomem *reg;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200429 u32 pllsw;
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100430 int branch;
431 bool clk38div;
432};
433
434enum {
435 PLL_RAW,
436 PLL_FIX,
437 PLL_DIV
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200438};
439
440static DEFINE_SPINLOCK(clk_mgt_lock);
441
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100442#define CLK_MGT_ENTRY(_name, _branch, _clk38div)[PRCMU_##_name] = \
443 { (PRCM_##_name##_MGT), 0 , _branch, _clk38div}
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200444struct clk_mgt clk_mgt[PRCMU_NUM_REG_CLOCKS] = {
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100445 CLK_MGT_ENTRY(SGACLK, PLL_DIV, false),
446 CLK_MGT_ENTRY(UARTCLK, PLL_FIX, true),
447 CLK_MGT_ENTRY(MSP02CLK, PLL_FIX, true),
448 CLK_MGT_ENTRY(MSP1CLK, PLL_FIX, true),
449 CLK_MGT_ENTRY(I2CCLK, PLL_FIX, true),
450 CLK_MGT_ENTRY(SDMMCCLK, PLL_DIV, true),
451 CLK_MGT_ENTRY(SLIMCLK, PLL_FIX, true),
452 CLK_MGT_ENTRY(PER1CLK, PLL_DIV, true),
453 CLK_MGT_ENTRY(PER2CLK, PLL_DIV, true),
454 CLK_MGT_ENTRY(PER3CLK, PLL_DIV, true),
455 CLK_MGT_ENTRY(PER5CLK, PLL_DIV, true),
456 CLK_MGT_ENTRY(PER6CLK, PLL_DIV, true),
457 CLK_MGT_ENTRY(PER7CLK, PLL_DIV, true),
458 CLK_MGT_ENTRY(LCDCLK, PLL_FIX, true),
459 CLK_MGT_ENTRY(BMLCLK, PLL_DIV, true),
460 CLK_MGT_ENTRY(HSITXCLK, PLL_DIV, true),
461 CLK_MGT_ENTRY(HSIRXCLK, PLL_DIV, true),
462 CLK_MGT_ENTRY(HDMICLK, PLL_FIX, false),
463 CLK_MGT_ENTRY(APEATCLK, PLL_DIV, true),
464 CLK_MGT_ENTRY(APETRACECLK, PLL_DIV, true),
465 CLK_MGT_ENTRY(MCDECLK, PLL_DIV, true),
466 CLK_MGT_ENTRY(IPI2CCLK, PLL_FIX, true),
467 CLK_MGT_ENTRY(DSIALTCLK, PLL_FIX, false),
468 CLK_MGT_ENTRY(DMACLK, PLL_DIV, true),
469 CLK_MGT_ENTRY(B2R2CLK, PLL_DIV, true),
470 CLK_MGT_ENTRY(TVCLK, PLL_FIX, true),
471 CLK_MGT_ENTRY(SSPCLK, PLL_FIX, true),
472 CLK_MGT_ENTRY(RNGCLK, PLL_FIX, true),
473 CLK_MGT_ENTRY(UICCCLK, PLL_FIX, false),
474};
475
476struct dsiclk {
477 u32 divsel_mask;
478 u32 divsel_shift;
479 u32 divsel;
480};
481
482static struct dsiclk dsiclk[2] = {
483 {
484 .divsel_mask = PRCM_DSI_PLLOUT_SEL_DSI0_PLLOUT_DIVSEL_MASK,
485 .divsel_shift = PRCM_DSI_PLLOUT_SEL_DSI0_PLLOUT_DIVSEL_SHIFT,
486 .divsel = PRCM_DSI_PLLOUT_SEL_PHI,
487 },
488 {
489 .divsel_mask = PRCM_DSI_PLLOUT_SEL_DSI1_PLLOUT_DIVSEL_MASK,
490 .divsel_shift = PRCM_DSI_PLLOUT_SEL_DSI1_PLLOUT_DIVSEL_SHIFT,
491 .divsel = PRCM_DSI_PLLOUT_SEL_PHI,
492 }
493};
494
495struct dsiescclk {
496 u32 en;
497 u32 div_mask;
498 u32 div_shift;
499};
500
501static struct dsiescclk dsiescclk[3] = {
502 {
503 .en = PRCM_DSITVCLK_DIV_DSI0_ESC_CLK_EN,
504 .div_mask = PRCM_DSITVCLK_DIV_DSI0_ESC_CLK_DIV_MASK,
505 .div_shift = PRCM_DSITVCLK_DIV_DSI0_ESC_CLK_DIV_SHIFT,
506 },
507 {
508 .en = PRCM_DSITVCLK_DIV_DSI1_ESC_CLK_EN,
509 .div_mask = PRCM_DSITVCLK_DIV_DSI1_ESC_CLK_DIV_MASK,
510 .div_shift = PRCM_DSITVCLK_DIV_DSI1_ESC_CLK_DIV_SHIFT,
511 },
512 {
513 .en = PRCM_DSITVCLK_DIV_DSI2_ESC_CLK_EN,
514 .div_mask = PRCM_DSITVCLK_DIV_DSI2_ESC_CLK_DIV_MASK,
515 .div_shift = PRCM_DSITVCLK_DIV_DSI2_ESC_CLK_DIV_SHIFT,
516 }
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200517};
518
Mattias Nilsson0837bb72011-08-12 10:28:18 +0200519static struct regulator *hwacc_regulator[NUM_HW_ACC];
520static struct regulator *hwacc_ret_regulator[NUM_HW_ACC];
521
522static bool hwacc_enabled[NUM_HW_ACC];
523static bool hwacc_ret_enabled[NUM_HW_ACC];
524
525static const char *hwacc_regulator_name[NUM_HW_ACC] = {
526 [HW_ACC_SVAMMDSP] = "hwacc-sva-mmdsp",
527 [HW_ACC_SVAPIPE] = "hwacc-sva-pipe",
528 [HW_ACC_SIAMMDSP] = "hwacc-sia-mmdsp",
529 [HW_ACC_SIAPIPE] = "hwacc-sia-pipe",
530 [HW_ACC_SGA] = "hwacc-sga",
531 [HW_ACC_B2R2] = "hwacc-b2r2",
532 [HW_ACC_MCDE] = "hwacc-mcde",
533 [HW_ACC_ESRAM1] = "hwacc-esram1",
534 [HW_ACC_ESRAM2] = "hwacc-esram2",
535 [HW_ACC_ESRAM3] = "hwacc-esram3",
536 [HW_ACC_ESRAM4] = "hwacc-esram4",
537};
538
539static const char *hwacc_ret_regulator_name[NUM_HW_ACC] = {
540 [HW_ACC_SVAMMDSP] = "hwacc-sva-mmdsp-ret",
541 [HW_ACC_SIAMMDSP] = "hwacc-sia-mmdsp-ret",
542 [HW_ACC_ESRAM1] = "hwacc-esram1-ret",
543 [HW_ACC_ESRAM2] = "hwacc-esram2-ret",
544 [HW_ACC_ESRAM3] = "hwacc-esram3-ret",
545 [HW_ACC_ESRAM4] = "hwacc-esram4-ret",
546};
547
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200548/*
549* Used by MCDE to setup all necessary PRCMU registers
550*/
551#define PRCMU_RESET_DSIPLL 0x00004000
552#define PRCMU_UNCLAMP_DSIPLL 0x00400800
553
554#define PRCMU_CLK_PLL_DIV_SHIFT 0
555#define PRCMU_CLK_PLL_SW_SHIFT 5
556#define PRCMU_CLK_38 (1 << 9)
557#define PRCMU_CLK_38_SRC (1 << 10)
558#define PRCMU_CLK_38_DIV (1 << 11)
559
560/* PLLDIV=12, PLLSW=4 (PLLDDR) */
561#define PRCMU_DSI_CLOCK_SETTING 0x0000008C
562
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200563/* DPI 50000000 Hz */
564#define PRCMU_DPI_CLOCK_SETTING ((1 << PRCMU_CLK_PLL_SW_SHIFT) | \
565 (16 << PRCMU_CLK_PLL_DIV_SHIFT))
566#define PRCMU_DSI_LP_CLOCK_SETTING 0x00000E00
567
568/* D=101, N=1, R=4, SELDIV2=0 */
569#define PRCMU_PLLDSI_FREQ_SETTING 0x00040165
570
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200571#define PRCMU_ENABLE_PLLDSI 0x00000001
572#define PRCMU_DISABLE_PLLDSI 0x00000000
573#define PRCMU_RELEASE_RESET_DSS 0x0000400C
574#define PRCMU_DSI_PLLOUT_SEL_SETTING 0x00000202
575/* ESC clk, div0=1, div1=1, div2=3 */
576#define PRCMU_ENABLE_ESCAPE_CLOCK_DIV 0x07030101
577#define PRCMU_DISABLE_ESCAPE_CLOCK_DIV 0x00030101
578#define PRCMU_DSI_RESET_SW 0x00000007
579
580#define PRCMU_PLLDSI_LOCKP_LOCKED 0x3
581
Mattias Nilsson73180f82011-08-12 10:28:10 +0200582int db8500_prcmu_enable_dsipll(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200583{
584 int i;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200585
586 /* Clear DSIPLL_RESETN */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200587 writel(PRCMU_RESET_DSIPLL, PRCM_APE_RESETN_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200588 /* Unclamp DSIPLL in/out */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200589 writel(PRCMU_UNCLAMP_DSIPLL, PRCM_MMIP_LS_CLAMP_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200590
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200591 /* Set DSI PLL FREQ */
Daniel Willerudc72fe852012-01-13 16:20:03 +0100592 writel(PRCMU_PLLDSI_FREQ_SETTING, PRCM_PLLDSI_FREQ);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200593 writel(PRCMU_DSI_PLLOUT_SEL_SETTING, PRCM_DSI_PLLOUT_SEL);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200594 /* Enable Escape clocks */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200595 writel(PRCMU_ENABLE_ESCAPE_CLOCK_DIV, PRCM_DSITVCLK_DIV);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200596
597 /* Start DSI PLL */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200598 writel(PRCMU_ENABLE_PLLDSI, PRCM_PLLDSI_ENABLE);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200599 /* Reset DSI PLL */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200600 writel(PRCMU_DSI_RESET_SW, PRCM_DSI_SW_RESET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200601 for (i = 0; i < 10; i++) {
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200602 if ((readl(PRCM_PLLDSI_LOCKP) & PRCMU_PLLDSI_LOCKP_LOCKED)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200603 == PRCMU_PLLDSI_LOCKP_LOCKED)
604 break;
605 udelay(100);
606 }
607 /* Set DSIPLL_RESETN */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200608 writel(PRCMU_RESET_DSIPLL, PRCM_APE_RESETN_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200609 return 0;
610}
611
Mattias Nilsson73180f82011-08-12 10:28:10 +0200612int db8500_prcmu_disable_dsipll(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200613{
614 /* Disable dsi pll */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200615 writel(PRCMU_DISABLE_PLLDSI, PRCM_PLLDSI_ENABLE);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200616 /* Disable escapeclock */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200617 writel(PRCMU_DISABLE_ESCAPE_CLOCK_DIV, PRCM_DSITVCLK_DIV);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200618 return 0;
619}
620
Mattias Nilsson73180f82011-08-12 10:28:10 +0200621int db8500_prcmu_set_display_clocks(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200622{
623 unsigned long flags;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200624
625 spin_lock_irqsave(&clk_mgt_lock, flags);
626
627 /* Grab the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200628 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200629 cpu_relax();
630
Daniel Willerudc72fe852012-01-13 16:20:03 +0100631 writel(PRCMU_DSI_CLOCK_SETTING, PRCM_HDMICLK_MGT);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200632 writel(PRCMU_DSI_LP_CLOCK_SETTING, PRCM_TVCLK_MGT);
633 writel(PRCMU_DPI_CLOCK_SETTING, PRCM_LCDCLK_MGT);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200634
635 /* Release the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200636 writel(0, PRCM_SEM);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200637
638 spin_unlock_irqrestore(&clk_mgt_lock, flags);
639
640 return 0;
641}
642
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100643u32 db8500_prcmu_read(unsigned int reg)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200644{
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100645 return readl(_PRCMU_BASE + reg);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200646}
647
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100648void db8500_prcmu_write(unsigned int reg, u32 value)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200649{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200650 unsigned long flags;
651
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100652 spin_lock_irqsave(&prcmu_lock, flags);
653 writel(value, (_PRCMU_BASE + reg));
654 spin_unlock_irqrestore(&prcmu_lock, flags);
655}
656
657void db8500_prcmu_write_masked(unsigned int reg, u32 mask, u32 value)
658{
659 u32 val;
660 unsigned long flags;
661
662 spin_lock_irqsave(&prcmu_lock, flags);
663 val = readl(_PRCMU_BASE + reg);
664 val = ((val & ~mask) | (value & mask));
665 writel(val, (_PRCMU_BASE + reg));
666 spin_unlock_irqrestore(&prcmu_lock, flags);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200667}
668
Mattias Nilssonb58d12f2012-01-13 16:20:10 +0100669struct prcmu_fw_version *prcmu_get_fw_version(void)
670{
671 return fw_info.valid ? &fw_info.version : NULL;
672}
673
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200674bool prcmu_has_arm_maxopp(void)
675{
676 return (readb(tcdm_base + PRCM_AVS_VARM_MAX_OPP) &
677 PRCM_AVS_ISMODEENABLE_MASK) == PRCM_AVS_ISMODEENABLE_MASK;
678}
679
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200680/**
681 * prcmu_get_boot_status - PRCMU boot status checking
682 * Returns: the current PRCMU boot status
683 */
684int prcmu_get_boot_status(void)
685{
686 return readb(tcdm_base + PRCM_BOOT_STATUS);
687}
688
689/**
690 * prcmu_set_rc_a2p - This function is used to run few power state sequences
691 * @val: Value to be set, i.e. transition requested
692 * Returns: 0 on success, -EINVAL on invalid argument
693 *
694 * This function is used to run the following power state sequences -
695 * any state to ApReset, ApDeepSleep to ApExecute, ApExecute to ApDeepSleep
696 */
697int prcmu_set_rc_a2p(enum romcode_write val)
698{
699 if (val < RDY_2_DS || val > RDY_2_XP70_RST)
700 return -EINVAL;
701 writeb(val, (tcdm_base + PRCM_ROMCODE_A2P));
702 return 0;
703}
704
705/**
706 * prcmu_get_rc_p2a - This function is used to get power state sequences
707 * Returns: the power transition that has last happened
708 *
709 * This function can return the following transitions-
710 * any state to ApReset, ApDeepSleep to ApExecute, ApExecute to ApDeepSleep
711 */
712enum romcode_read prcmu_get_rc_p2a(void)
713{
714 return readb(tcdm_base + PRCM_ROMCODE_P2A);
715}
716
717/**
718 * prcmu_get_current_mode - Return the current XP70 power mode
719 * Returns: Returns the current AP(ARM) power mode: init,
720 * apBoot, apExecute, apDeepSleep, apSleep, apIdle, apReset
721 */
722enum ap_pwrst prcmu_get_xp70_current_state(void)
723{
724 return readb(tcdm_base + PRCM_XP70_CUR_PWR_STATE);
725}
726
727/**
728 * prcmu_config_clkout - Configure one of the programmable clock outputs.
729 * @clkout: The CLKOUT number (0 or 1).
730 * @source: The clock to be used (one of the PRCMU_CLKSRC_*).
731 * @div: The divider to be applied.
732 *
733 * Configures one of the programmable clock outputs (CLKOUTs).
734 * @div should be in the range [1,63] to request a configuration, or 0 to
735 * inform that the configuration is no longer requested.
736 */
737int prcmu_config_clkout(u8 clkout, u8 source, u8 div)
738{
739 static int requests[2];
740 int r = 0;
741 unsigned long flags;
742 u32 val;
743 u32 bits;
744 u32 mask;
745 u32 div_mask;
746
747 BUG_ON(clkout > 1);
748 BUG_ON(div > 63);
749 BUG_ON((clkout == 0) && (source > PRCMU_CLKSRC_CLK009));
750
751 if (!div && !requests[clkout])
752 return -EINVAL;
753
754 switch (clkout) {
755 case 0:
756 div_mask = PRCM_CLKOCR_CLKODIV0_MASK;
757 mask = (PRCM_CLKOCR_CLKODIV0_MASK | PRCM_CLKOCR_CLKOSEL0_MASK);
758 bits = ((source << PRCM_CLKOCR_CLKOSEL0_SHIFT) |
759 (div << PRCM_CLKOCR_CLKODIV0_SHIFT));
760 break;
761 case 1:
762 div_mask = PRCM_CLKOCR_CLKODIV1_MASK;
763 mask = (PRCM_CLKOCR_CLKODIV1_MASK | PRCM_CLKOCR_CLKOSEL1_MASK |
764 PRCM_CLKOCR_CLK1TYPE);
765 bits = ((source << PRCM_CLKOCR_CLKOSEL1_SHIFT) |
766 (div << PRCM_CLKOCR_CLKODIV1_SHIFT));
767 break;
768 }
769 bits &= mask;
770
771 spin_lock_irqsave(&clkout_lock, flags);
772
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200773 val = readl(PRCM_CLKOCR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200774 if (val & div_mask) {
775 if (div) {
776 if ((val & mask) != bits) {
777 r = -EBUSY;
778 goto unlock_and_return;
779 }
780 } else {
781 if ((val & mask & ~div_mask) != bits) {
782 r = -EINVAL;
783 goto unlock_and_return;
784 }
785 }
786 }
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200787 writel((bits | (val & ~mask)), PRCM_CLKOCR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200788 requests[clkout] += (div ? 1 : -1);
789
790unlock_and_return:
791 spin_unlock_irqrestore(&clkout_lock, flags);
792
793 return r;
794}
795
Mattias Nilsson73180f82011-08-12 10:28:10 +0200796int db8500_prcmu_set_power_state(u8 state, bool keep_ulp_clk, bool keep_ap_pll)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200797{
798 unsigned long flags;
799
800 BUG_ON((state < PRCMU_AP_SLEEP) || (PRCMU_AP_DEEP_IDLE < state));
801
802 spin_lock_irqsave(&mb0_transfer.lock, flags);
803
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200804 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200805 cpu_relax();
806
807 writeb(MB0H_POWER_STATE_TRANS, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
808 writeb(state, (tcdm_base + PRCM_REQ_MB0_AP_POWER_STATE));
809 writeb((keep_ap_pll ? 1 : 0), (tcdm_base + PRCM_REQ_MB0_AP_PLL_STATE));
810 writeb((keep_ulp_clk ? 1 : 0),
811 (tcdm_base + PRCM_REQ_MB0_ULP_CLOCK_STATE));
812 writeb(0, (tcdm_base + PRCM_REQ_MB0_DO_NOT_WFI));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200813 writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200814
815 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
816
817 return 0;
818}
819
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100820u8 db8500_prcmu_get_power_state_result(void)
821{
822 return readb(tcdm_base + PRCM_ACK_MB0_AP_PWRSTTR_STATUS);
823}
824
Daniel Lezcano485540d2012-02-20 12:30:26 +0100825/* This function decouple the gic from the prcmu */
826int db8500_prcmu_gic_decouple(void)
827{
Daniel Lezcano801448e2012-02-28 22:46:05 +0100828 u32 val = readl(PRCM_A9_MASK_REQ);
Daniel Lezcano485540d2012-02-20 12:30:26 +0100829
830 /* Set bit 0 register value to 1 */
Daniel Lezcano801448e2012-02-28 22:46:05 +0100831 writel(val | PRCM_A9_MASK_REQ_PRCM_A9_MASK_REQ,
832 PRCM_A9_MASK_REQ);
Daniel Lezcano485540d2012-02-20 12:30:26 +0100833
834 /* Make sure the register is updated */
Daniel Lezcano801448e2012-02-28 22:46:05 +0100835 readl(PRCM_A9_MASK_REQ);
Daniel Lezcano485540d2012-02-20 12:30:26 +0100836
837 /* Wait a few cycles for the gic mask completion */
Daniel Lezcano801448e2012-02-28 22:46:05 +0100838 udelay(1);
Daniel Lezcano485540d2012-02-20 12:30:26 +0100839
840 return 0;
841}
842
843/* This function recouple the gic with the prcmu */
844int db8500_prcmu_gic_recouple(void)
845{
Daniel Lezcano801448e2012-02-28 22:46:05 +0100846 u32 val = readl(PRCM_A9_MASK_REQ);
Daniel Lezcano485540d2012-02-20 12:30:26 +0100847
848 /* Set bit 0 register value to 0 */
Daniel Lezcano801448e2012-02-28 22:46:05 +0100849 writel(val & ~PRCM_A9_MASK_REQ_PRCM_A9_MASK_REQ, PRCM_A9_MASK_REQ);
Daniel Lezcano485540d2012-02-20 12:30:26 +0100850
851 return 0;
852}
853
Daniel Lezcanocc9a0f62012-02-28 22:46:06 +0100854#define PRCMU_GIC_NUMBER_REGS 5
855
856/*
857 * This function checks if there are pending irq on the gic. It only
858 * makes sense if the gic has been decoupled before with the
859 * db8500_prcmu_gic_decouple function. Disabling an interrupt only
860 * disables the forwarding of the interrupt to any CPU interface. It
861 * does not prevent the interrupt from changing state, for example
862 * becoming pending, or active and pending if it is already
863 * active. Hence, we have to check the interrupt is pending *and* is
864 * active.
865 */
866bool db8500_prcmu_gic_pending_irq(void)
867{
868 u32 pr; /* Pending register */
869 u32 er; /* Enable register */
870 void __iomem *dist_base = __io_address(U8500_GIC_DIST_BASE);
871 int i;
872
873 /* 5 registers. STI & PPI not skipped */
874 for (i = 0; i < PRCMU_GIC_NUMBER_REGS; i++) {
875
876 pr = readl_relaxed(dist_base + GIC_DIST_PENDING_SET + i * 4);
877 er = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
878
879 if (pr & er)
880 return true; /* There is a pending interrupt */
881 }
882
883 return false;
884}
885
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200886/* This function should only be called while mb0_transfer.lock is held. */
887static void config_wakeups(void)
888{
889 const u8 header[2] = {
890 MB0H_CONFIG_WAKEUPS_EXE,
891 MB0H_CONFIG_WAKEUPS_SLEEP
892 };
893 static u32 last_dbb_events;
894 static u32 last_abb_events;
895 u32 dbb_events;
896 u32 abb_events;
897 unsigned int i;
898
899 dbb_events = mb0_transfer.req.dbb_irqs | mb0_transfer.req.dbb_wakeups;
900 dbb_events |= (WAKEUP_BIT_AC_WAKE_ACK | WAKEUP_BIT_AC_SLEEP_ACK);
901
902 abb_events = mb0_transfer.req.abb_events;
903
904 if ((dbb_events == last_dbb_events) && (abb_events == last_abb_events))
905 return;
906
907 for (i = 0; i < 2; i++) {
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200908 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200909 cpu_relax();
910 writel(dbb_events, (tcdm_base + PRCM_REQ_MB0_WAKEUP_8500));
911 writel(abb_events, (tcdm_base + PRCM_REQ_MB0_WAKEUP_4500));
912 writeb(header[i], (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200913 writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200914 }
915 last_dbb_events = dbb_events;
916 last_abb_events = abb_events;
917}
918
Mattias Nilsson73180f82011-08-12 10:28:10 +0200919void db8500_prcmu_enable_wakeups(u32 wakeups)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200920{
921 unsigned long flags;
922 u32 bits;
923 int i;
924
925 BUG_ON(wakeups != (wakeups & VALID_WAKEUPS));
926
927 for (i = 0, bits = 0; i < NUM_PRCMU_WAKEUP_INDICES; i++) {
928 if (wakeups & BIT(i))
929 bits |= prcmu_wakeup_bit[i];
930 }
931
932 spin_lock_irqsave(&mb0_transfer.lock, flags);
933
934 mb0_transfer.req.dbb_wakeups = bits;
935 config_wakeups();
936
937 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
938}
939
Mattias Nilsson73180f82011-08-12 10:28:10 +0200940void db8500_prcmu_config_abb_event_readout(u32 abb_events)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200941{
942 unsigned long flags;
943
944 spin_lock_irqsave(&mb0_transfer.lock, flags);
945
946 mb0_transfer.req.abb_events = abb_events;
947 config_wakeups();
948
949 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
950}
951
Mattias Nilsson73180f82011-08-12 10:28:10 +0200952void db8500_prcmu_get_abb_event_buffer(void __iomem **buf)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200953{
954 if (readb(tcdm_base + PRCM_ACK_MB0_READ_POINTER) & 1)
955 *buf = (tcdm_base + PRCM_ACK_MB0_WAKEUP_1_4500);
956 else
957 *buf = (tcdm_base + PRCM_ACK_MB0_WAKEUP_0_4500);
958}
959
960/**
Mattias Nilsson73180f82011-08-12 10:28:10 +0200961 * db8500_prcmu_set_arm_opp - set the appropriate ARM OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200962 * @opp: The new ARM operating point to which transition is to be made
963 * Returns: 0 on success, non-zero on failure
964 *
965 * This function sets the the operating point of the ARM.
966 */
Mattias Nilsson73180f82011-08-12 10:28:10 +0200967int db8500_prcmu_set_arm_opp(u8 opp)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200968{
969 int r;
970
971 if (opp < ARM_NO_CHANGE || opp > ARM_EXTCLK)
972 return -EINVAL;
973
974 r = 0;
975
976 mutex_lock(&mb1_transfer.lock);
977
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200978 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200979 cpu_relax();
980
981 writeb(MB1H_ARM_APE_OPP, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
982 writeb(opp, (tcdm_base + PRCM_REQ_MB1_ARM_OPP));
983 writeb(APE_NO_CHANGE, (tcdm_base + PRCM_REQ_MB1_APE_OPP));
984
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200985 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200986 wait_for_completion(&mb1_transfer.work);
987
988 if ((mb1_transfer.ack.header != MB1H_ARM_APE_OPP) ||
989 (mb1_transfer.ack.arm_opp != opp))
990 r = -EIO;
991
992 mutex_unlock(&mb1_transfer.lock);
993
994 return r;
995}
996
997/**
Mattias Nilsson73180f82011-08-12 10:28:10 +0200998 * db8500_prcmu_get_arm_opp - get the current ARM OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200999 *
1000 * Returns: the current ARM OPP
1001 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02001002int db8500_prcmu_get_arm_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001003{
1004 return readb(tcdm_base + PRCM_ACK_MB1_CURRENT_ARM_OPP);
1005}
1006
1007/**
Mattias Nilsson05089012012-01-13 16:20:20 +01001008 * db8500_prcmu_get_ddr_opp - get the current DDR OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001009 *
1010 * Returns: the current DDR OPP
1011 */
Mattias Nilsson05089012012-01-13 16:20:20 +01001012int db8500_prcmu_get_ddr_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001013{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001014 return readb(PRCM_DDR_SUBSYS_APE_MINBW);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001015}
1016
1017/**
Mattias Nilsson05089012012-01-13 16:20:20 +01001018 * db8500_set_ddr_opp - set the appropriate DDR OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001019 * @opp: The new DDR operating point to which transition is to be made
1020 * Returns: 0 on success, non-zero on failure
1021 *
1022 * This function sets the operating point of the DDR.
1023 */
Mattias Nilsson05089012012-01-13 16:20:20 +01001024int db8500_prcmu_set_ddr_opp(u8 opp)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001025{
1026 if (opp < DDR_100_OPP || opp > DDR_25_OPP)
1027 return -EINVAL;
1028 /* Changing the DDR OPP can hang the hardware pre-v21 */
1029 if (cpu_is_u8500v20_or_later() && !cpu_is_u8500v20())
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001030 writeb(opp, PRCM_DDR_SUBSYS_APE_MINBW);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001031
1032 return 0;
1033}
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001034
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001035/* Divide the frequency of certain clocks by 2 for APE_50_PARTLY_25_OPP. */
1036static void request_even_slower_clocks(bool enable)
1037{
1038 void __iomem *clock_reg[] = {
1039 PRCM_ACLK_MGT,
1040 PRCM_DMACLK_MGT
1041 };
1042 unsigned long flags;
1043 unsigned int i;
1044
1045 spin_lock_irqsave(&clk_mgt_lock, flags);
1046
1047 /* Grab the HW semaphore. */
1048 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
1049 cpu_relax();
1050
1051 for (i = 0; i < ARRAY_SIZE(clock_reg); i++) {
1052 u32 val;
1053 u32 div;
1054
1055 val = readl(clock_reg[i]);
1056 div = (val & PRCM_CLK_MGT_CLKPLLDIV_MASK);
1057 if (enable) {
1058 if ((div <= 1) || (div > 15)) {
1059 pr_err("prcmu: Bad clock divider %d in %s\n",
1060 div, __func__);
1061 goto unlock_and_return;
1062 }
1063 div <<= 1;
1064 } else {
1065 if (div <= 2)
1066 goto unlock_and_return;
1067 div >>= 1;
1068 }
1069 val = ((val & ~PRCM_CLK_MGT_CLKPLLDIV_MASK) |
1070 (div & PRCM_CLK_MGT_CLKPLLDIV_MASK));
1071 writel(val, clock_reg[i]);
1072 }
1073
1074unlock_and_return:
1075 /* Release the HW semaphore. */
1076 writel(0, PRCM_SEM);
1077
1078 spin_unlock_irqrestore(&clk_mgt_lock, flags);
1079}
1080
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001081/**
Mattias Nilsson05089012012-01-13 16:20:20 +01001082 * db8500_set_ape_opp - set the appropriate APE OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001083 * @opp: The new APE operating point to which transition is to be made
1084 * Returns: 0 on success, non-zero on failure
1085 *
1086 * This function sets the operating point of the APE.
1087 */
Mattias Nilsson05089012012-01-13 16:20:20 +01001088int db8500_prcmu_set_ape_opp(u8 opp)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001089{
1090 int r = 0;
1091
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001092 if (opp == mb1_transfer.ape_opp)
1093 return 0;
1094
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001095 mutex_lock(&mb1_transfer.lock);
1096
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001097 if (mb1_transfer.ape_opp == APE_50_PARTLY_25_OPP)
1098 request_even_slower_clocks(false);
1099
1100 if ((opp != APE_100_OPP) && (mb1_transfer.ape_opp != APE_100_OPP))
1101 goto skip_message;
1102
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001103 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001104 cpu_relax();
1105
1106 writeb(MB1H_ARM_APE_OPP, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1107 writeb(ARM_NO_CHANGE, (tcdm_base + PRCM_REQ_MB1_ARM_OPP));
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001108 writeb(((opp == APE_50_PARTLY_25_OPP) ? APE_50_OPP : opp),
1109 (tcdm_base + PRCM_REQ_MB1_APE_OPP));
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001110
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001111 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001112 wait_for_completion(&mb1_transfer.work);
1113
1114 if ((mb1_transfer.ack.header != MB1H_ARM_APE_OPP) ||
1115 (mb1_transfer.ack.ape_opp != opp))
1116 r = -EIO;
1117
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001118skip_message:
1119 if ((!r && (opp == APE_50_PARTLY_25_OPP)) ||
1120 (r && (mb1_transfer.ape_opp == APE_50_PARTLY_25_OPP)))
1121 request_even_slower_clocks(true);
1122 if (!r)
1123 mb1_transfer.ape_opp = opp;
1124
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001125 mutex_unlock(&mb1_transfer.lock);
1126
1127 return r;
1128}
1129
1130/**
Mattias Nilsson05089012012-01-13 16:20:20 +01001131 * db8500_prcmu_get_ape_opp - get the current APE OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001132 *
1133 * Returns: the current APE OPP
1134 */
Mattias Nilsson05089012012-01-13 16:20:20 +01001135int db8500_prcmu_get_ape_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001136{
1137 return readb(tcdm_base + PRCM_ACK_MB1_CURRENT_APE_OPP);
1138}
1139
1140/**
1141 * prcmu_request_ape_opp_100_voltage - Request APE OPP 100% voltage
1142 * @enable: true to request the higher voltage, false to drop a request.
1143 *
1144 * Calls to this function to enable and disable requests must be balanced.
1145 */
1146int prcmu_request_ape_opp_100_voltage(bool enable)
1147{
1148 int r = 0;
1149 u8 header;
1150 static unsigned int requests;
1151
1152 mutex_lock(&mb1_transfer.lock);
1153
1154 if (enable) {
1155 if (0 != requests++)
1156 goto unlock_and_return;
1157 header = MB1H_REQUEST_APE_OPP_100_VOLT;
1158 } else {
1159 if (requests == 0) {
1160 r = -EIO;
1161 goto unlock_and_return;
1162 } else if (1 != requests--) {
1163 goto unlock_and_return;
1164 }
1165 header = MB1H_RELEASE_APE_OPP_100_VOLT;
1166 }
1167
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001168 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001169 cpu_relax();
1170
1171 writeb(header, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1172
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001173 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001174 wait_for_completion(&mb1_transfer.work);
1175
1176 if ((mb1_transfer.ack.header != header) ||
1177 ((mb1_transfer.ack.ape_voltage_status & BIT(0)) != 0))
1178 r = -EIO;
1179
1180unlock_and_return:
1181 mutex_unlock(&mb1_transfer.lock);
1182
1183 return r;
1184}
1185
1186/**
1187 * prcmu_release_usb_wakeup_state - release the state required by a USB wakeup
1188 *
1189 * This function releases the power state requirements of a USB wakeup.
1190 */
1191int prcmu_release_usb_wakeup_state(void)
1192{
1193 int r = 0;
1194
1195 mutex_lock(&mb1_transfer.lock);
1196
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001197 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001198 cpu_relax();
1199
1200 writeb(MB1H_RELEASE_USB_WAKEUP,
1201 (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1202
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001203 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001204 wait_for_completion(&mb1_transfer.work);
1205
1206 if ((mb1_transfer.ack.header != MB1H_RELEASE_USB_WAKEUP) ||
1207 ((mb1_transfer.ack.ape_voltage_status & BIT(0)) != 0))
1208 r = -EIO;
1209
1210 mutex_unlock(&mb1_transfer.lock);
1211
1212 return r;
1213}
1214
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001215static int request_pll(u8 clock, bool enable)
1216{
1217 int r = 0;
1218
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001219 if (clock == PRCMU_PLLSOC0)
1220 clock = (enable ? PLL_SOC0_ON : PLL_SOC0_OFF);
1221 else if (clock == PRCMU_PLLSOC1)
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001222 clock = (enable ? PLL_SOC1_ON : PLL_SOC1_OFF);
1223 else
1224 return -EINVAL;
1225
1226 mutex_lock(&mb1_transfer.lock);
1227
1228 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
1229 cpu_relax();
1230
1231 writeb(MB1H_PLL_ON_OFF, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1232 writeb(clock, (tcdm_base + PRCM_REQ_MB1_PLL_ON_OFF));
1233
1234 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
1235 wait_for_completion(&mb1_transfer.work);
1236
1237 if (mb1_transfer.ack.header != MB1H_PLL_ON_OFF)
1238 r = -EIO;
1239
1240 mutex_unlock(&mb1_transfer.lock);
1241
1242 return r;
1243}
1244
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001245/**
Bengt Jonsson0b9199e2011-08-12 10:28:25 +02001246 * prcmu_set_hwacc - set the power state of a h/w accelerator
1247 * @hwacc_dev: The hardware accelerator (enum hw_acc_dev).
1248 * @state: The new power state (enum hw_acc_state).
1249 *
1250 * This function sets the power state of a hardware accelerator.
1251 * This function should not be called from interrupt context.
1252 *
1253 * NOTE! Deprecated, to be removed when all users switched over to use the
1254 * regulator framework API.
1255 */
1256int prcmu_set_hwacc(u16 hwacc_dev, u8 state)
1257{
1258 int r = 0;
1259 bool ram_retention = false;
1260 bool enable, enable_ret;
1261
1262 /* check argument */
1263 BUG_ON(hwacc_dev >= NUM_HW_ACC);
1264
1265 /* get state of switches */
1266 enable = hwacc_enabled[hwacc_dev];
1267 enable_ret = hwacc_ret_enabled[hwacc_dev];
1268
1269 /* set flag if retention is possible */
1270 switch (hwacc_dev) {
1271 case HW_ACC_SVAMMDSP:
1272 case HW_ACC_SIAMMDSP:
1273 case HW_ACC_ESRAM1:
1274 case HW_ACC_ESRAM2:
1275 case HW_ACC_ESRAM3:
1276 case HW_ACC_ESRAM4:
1277 ram_retention = true;
1278 break;
1279 }
1280
1281 /* check argument */
1282 BUG_ON(state > HW_ON);
1283 BUG_ON(state == HW_OFF_RAMRET && !ram_retention);
1284
1285 /* modify enable flags */
1286 switch (state) {
1287 case HW_OFF:
1288 enable_ret = false;
1289 enable = false;
1290 break;
1291 case HW_ON:
1292 enable = true;
1293 break;
1294 case HW_OFF_RAMRET:
1295 enable_ret = true;
1296 enable = false;
1297 break;
1298 }
1299
1300 /* get regulator (lazy) */
1301 if (hwacc_regulator[hwacc_dev] == NULL) {
1302 hwacc_regulator[hwacc_dev] = regulator_get(NULL,
1303 hwacc_regulator_name[hwacc_dev]);
1304 if (IS_ERR(hwacc_regulator[hwacc_dev])) {
1305 pr_err("prcmu: failed to get supply %s\n",
1306 hwacc_regulator_name[hwacc_dev]);
1307 r = PTR_ERR(hwacc_regulator[hwacc_dev]);
1308 goto out;
1309 }
1310 }
1311
1312 if (ram_retention) {
1313 if (hwacc_ret_regulator[hwacc_dev] == NULL) {
1314 hwacc_ret_regulator[hwacc_dev] = regulator_get(NULL,
1315 hwacc_ret_regulator_name[hwacc_dev]);
1316 if (IS_ERR(hwacc_ret_regulator[hwacc_dev])) {
1317 pr_err("prcmu: failed to get supply %s\n",
1318 hwacc_ret_regulator_name[hwacc_dev]);
1319 r = PTR_ERR(hwacc_ret_regulator[hwacc_dev]);
1320 goto out;
1321 }
1322 }
1323 }
1324
1325 /* set regulators */
1326 if (ram_retention) {
1327 if (enable_ret && !hwacc_ret_enabled[hwacc_dev]) {
1328 r = regulator_enable(hwacc_ret_regulator[hwacc_dev]);
1329 if (r < 0) {
1330 pr_err("prcmu_set_hwacc: ret enable failed\n");
1331 goto out;
1332 }
1333 hwacc_ret_enabled[hwacc_dev] = true;
1334 }
1335 }
1336
1337 if (enable && !hwacc_enabled[hwacc_dev]) {
1338 r = regulator_enable(hwacc_regulator[hwacc_dev]);
1339 if (r < 0) {
1340 pr_err("prcmu_set_hwacc: enable failed\n");
1341 goto out;
1342 }
1343 hwacc_enabled[hwacc_dev] = true;
1344 }
1345
1346 if (!enable && hwacc_enabled[hwacc_dev]) {
1347 r = regulator_disable(hwacc_regulator[hwacc_dev]);
1348 if (r < 0) {
1349 pr_err("prcmu_set_hwacc: disable failed\n");
1350 goto out;
1351 }
1352 hwacc_enabled[hwacc_dev] = false;
1353 }
1354
1355 if (ram_retention) {
1356 if (!enable_ret && hwacc_ret_enabled[hwacc_dev]) {
1357 r = regulator_disable(hwacc_ret_regulator[hwacc_dev]);
1358 if (r < 0) {
1359 pr_err("prcmu_set_hwacc: ret disable failed\n");
1360 goto out;
1361 }
1362 hwacc_ret_enabled[hwacc_dev] = false;
1363 }
1364 }
1365
1366out:
1367 return r;
1368}
1369EXPORT_SYMBOL(prcmu_set_hwacc);
1370
1371/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02001372 * db8500_prcmu_set_epod - set the state of a EPOD (power domain)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001373 * @epod_id: The EPOD to set
1374 * @epod_state: The new EPOD state
1375 *
1376 * This function sets the state of a EPOD (power domain). It may not be called
1377 * from interrupt context.
1378 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02001379int db8500_prcmu_set_epod(u16 epod_id, u8 epod_state)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001380{
1381 int r = 0;
1382 bool ram_retention = false;
1383 int i;
1384
1385 /* check argument */
1386 BUG_ON(epod_id >= NUM_EPOD_ID);
1387
1388 /* set flag if retention is possible */
1389 switch (epod_id) {
1390 case EPOD_ID_SVAMMDSP:
1391 case EPOD_ID_SIAMMDSP:
1392 case EPOD_ID_ESRAM12:
1393 case EPOD_ID_ESRAM34:
1394 ram_retention = true;
1395 break;
1396 }
1397
1398 /* check argument */
1399 BUG_ON(epod_state > EPOD_STATE_ON);
1400 BUG_ON(epod_state == EPOD_STATE_RAMRET && !ram_retention);
1401
1402 /* get lock */
1403 mutex_lock(&mb2_transfer.lock);
1404
1405 /* wait for mailbox */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001406 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(2))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001407 cpu_relax();
1408
1409 /* fill in mailbox */
1410 for (i = 0; i < NUM_EPOD_ID; i++)
1411 writeb(EPOD_STATE_NO_CHANGE, (tcdm_base + PRCM_REQ_MB2 + i));
1412 writeb(epod_state, (tcdm_base + PRCM_REQ_MB2 + epod_id));
1413
1414 writeb(MB2H_DPS, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB2));
1415
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001416 writel(MBOX_BIT(2), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001417
1418 /*
1419 * The current firmware version does not handle errors correctly,
1420 * and we cannot recover if there is an error.
1421 * This is expected to change when the firmware is updated.
1422 */
1423 if (!wait_for_completion_timeout(&mb2_transfer.work,
1424 msecs_to_jiffies(20000))) {
1425 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
1426 __func__);
1427 r = -EIO;
1428 goto unlock_and_return;
1429 }
1430
1431 if (mb2_transfer.ack.status != HWACC_PWR_ST_OK)
1432 r = -EIO;
1433
1434unlock_and_return:
1435 mutex_unlock(&mb2_transfer.lock);
1436 return r;
1437}
1438
1439/**
1440 * prcmu_configure_auto_pm - Configure autonomous power management.
1441 * @sleep: Configuration for ApSleep.
1442 * @idle: Configuration for ApIdle.
1443 */
1444void prcmu_configure_auto_pm(struct prcmu_auto_pm_config *sleep,
1445 struct prcmu_auto_pm_config *idle)
1446{
1447 u32 sleep_cfg;
1448 u32 idle_cfg;
1449 unsigned long flags;
1450
1451 BUG_ON((sleep == NULL) || (idle == NULL));
1452
1453 sleep_cfg = (sleep->sva_auto_pm_enable & 0xF);
1454 sleep_cfg = ((sleep_cfg << 4) | (sleep->sia_auto_pm_enable & 0xF));
1455 sleep_cfg = ((sleep_cfg << 8) | (sleep->sva_power_on & 0xFF));
1456 sleep_cfg = ((sleep_cfg << 8) | (sleep->sia_power_on & 0xFF));
1457 sleep_cfg = ((sleep_cfg << 4) | (sleep->sva_policy & 0xF));
1458 sleep_cfg = ((sleep_cfg << 4) | (sleep->sia_policy & 0xF));
1459
1460 idle_cfg = (idle->sva_auto_pm_enable & 0xF);
1461 idle_cfg = ((idle_cfg << 4) | (idle->sia_auto_pm_enable & 0xF));
1462 idle_cfg = ((idle_cfg << 8) | (idle->sva_power_on & 0xFF));
1463 idle_cfg = ((idle_cfg << 8) | (idle->sia_power_on & 0xFF));
1464 idle_cfg = ((idle_cfg << 4) | (idle->sva_policy & 0xF));
1465 idle_cfg = ((idle_cfg << 4) | (idle->sia_policy & 0xF));
1466
1467 spin_lock_irqsave(&mb2_transfer.auto_pm_lock, flags);
1468
1469 /*
1470 * The autonomous power management configuration is done through
1471 * fields in mailbox 2, but these fields are only used as shared
1472 * variables - i.e. there is no need to send a message.
1473 */
1474 writel(sleep_cfg, (tcdm_base + PRCM_REQ_MB2_AUTO_PM_SLEEP));
1475 writel(idle_cfg, (tcdm_base + PRCM_REQ_MB2_AUTO_PM_IDLE));
1476
1477 mb2_transfer.auto_pm_enabled =
1478 ((sleep->sva_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1479 (sleep->sia_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1480 (idle->sva_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1481 (idle->sia_auto_pm_enable == PRCMU_AUTO_PM_ON));
1482
1483 spin_unlock_irqrestore(&mb2_transfer.auto_pm_lock, flags);
1484}
1485EXPORT_SYMBOL(prcmu_configure_auto_pm);
1486
1487bool prcmu_is_auto_pm_enabled(void)
1488{
1489 return mb2_transfer.auto_pm_enabled;
1490}
1491
1492static int request_sysclk(bool enable)
1493{
1494 int r;
1495 unsigned long flags;
1496
1497 r = 0;
1498
1499 mutex_lock(&mb3_transfer.sysclk_lock);
1500
1501 spin_lock_irqsave(&mb3_transfer.lock, flags);
1502
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001503 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(3))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001504 cpu_relax();
1505
1506 writeb((enable ? ON : OFF), (tcdm_base + PRCM_REQ_MB3_SYSCLK_MGT));
1507
1508 writeb(MB3H_SYSCLK, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB3));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001509 writel(MBOX_BIT(3), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001510
1511 spin_unlock_irqrestore(&mb3_transfer.lock, flags);
1512
1513 /*
1514 * The firmware only sends an ACK if we want to enable the
1515 * SysClk, and it succeeds.
1516 */
1517 if (enable && !wait_for_completion_timeout(&mb3_transfer.sysclk_work,
1518 msecs_to_jiffies(20000))) {
1519 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
1520 __func__);
1521 r = -EIO;
1522 }
1523
1524 mutex_unlock(&mb3_transfer.sysclk_lock);
1525
1526 return r;
1527}
1528
1529static int request_timclk(bool enable)
1530{
1531 u32 val = (PRCM_TCR_DOZE_MODE | PRCM_TCR_TENSEL_MASK);
1532
1533 if (!enable)
1534 val |= PRCM_TCR_STOP_TIMERS;
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001535 writel(val, PRCM_TCR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001536
1537 return 0;
1538}
1539
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001540static int request_clock(u8 clock, bool enable)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001541{
1542 u32 val;
1543 unsigned long flags;
1544
1545 spin_lock_irqsave(&clk_mgt_lock, flags);
1546
1547 /* Grab the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001548 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001549 cpu_relax();
1550
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001551 val = readl(clk_mgt[clock].reg);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001552 if (enable) {
1553 val |= (PRCM_CLK_MGT_CLKEN | clk_mgt[clock].pllsw);
1554 } else {
1555 clk_mgt[clock].pllsw = (val & PRCM_CLK_MGT_CLKPLLSW_MASK);
1556 val &= ~(PRCM_CLK_MGT_CLKEN | PRCM_CLK_MGT_CLKPLLSW_MASK);
1557 }
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001558 writel(val, clk_mgt[clock].reg);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001559
1560 /* Release the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001561 writel(0, PRCM_SEM);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001562
1563 spin_unlock_irqrestore(&clk_mgt_lock, flags);
1564
1565 return 0;
1566}
1567
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001568static int request_sga_clock(u8 clock, bool enable)
1569{
1570 u32 val;
1571 int ret;
1572
1573 if (enable) {
1574 val = readl(PRCM_CGATING_BYPASS);
1575 writel(val | PRCM_CGATING_BYPASS_ICN2, PRCM_CGATING_BYPASS);
1576 }
1577
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001578 ret = request_clock(clock, enable);
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001579
1580 if (!ret && !enable) {
1581 val = readl(PRCM_CGATING_BYPASS);
1582 writel(val & ~PRCM_CGATING_BYPASS_ICN2, PRCM_CGATING_BYPASS);
1583 }
1584
1585 return ret;
1586}
1587
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001588static inline bool plldsi_locked(void)
1589{
1590 return (readl(PRCM_PLLDSI_LOCKP) &
1591 (PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP10 |
1592 PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP3)) ==
1593 (PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP10 |
1594 PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP3);
1595}
1596
1597static int request_plldsi(bool enable)
1598{
1599 int r = 0;
1600 u32 val;
1601
1602 writel((PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMP |
1603 PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMPI), (enable ?
1604 PRCM_MMIP_LS_CLAMP_CLR : PRCM_MMIP_LS_CLAMP_SET));
1605
1606 val = readl(PRCM_PLLDSI_ENABLE);
1607 if (enable)
1608 val |= PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1609 else
1610 val &= ~PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1611 writel(val, PRCM_PLLDSI_ENABLE);
1612
1613 if (enable) {
1614 unsigned int i;
1615 bool locked = plldsi_locked();
1616
1617 for (i = 10; !locked && (i > 0); --i) {
1618 udelay(100);
1619 locked = plldsi_locked();
1620 }
1621 if (locked) {
1622 writel(PRCM_APE_RESETN_DSIPLL_RESETN,
1623 PRCM_APE_RESETN_SET);
1624 } else {
1625 writel((PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMP |
1626 PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMPI),
1627 PRCM_MMIP_LS_CLAMP_SET);
1628 val &= ~PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1629 writel(val, PRCM_PLLDSI_ENABLE);
1630 r = -EAGAIN;
1631 }
1632 } else {
1633 writel(PRCM_APE_RESETN_DSIPLL_RESETN, PRCM_APE_RESETN_CLR);
1634 }
1635 return r;
1636}
1637
1638static int request_dsiclk(u8 n, bool enable)
1639{
1640 u32 val;
1641
1642 val = readl(PRCM_DSI_PLLOUT_SEL);
1643 val &= ~dsiclk[n].divsel_mask;
1644 val |= ((enable ? dsiclk[n].divsel : PRCM_DSI_PLLOUT_SEL_OFF) <<
1645 dsiclk[n].divsel_shift);
1646 writel(val, PRCM_DSI_PLLOUT_SEL);
1647 return 0;
1648}
1649
1650static int request_dsiescclk(u8 n, bool enable)
1651{
1652 u32 val;
1653
1654 val = readl(PRCM_DSITVCLK_DIV);
1655 enable ? (val |= dsiescclk[n].en) : (val &= ~dsiescclk[n].en);
1656 writel(val, PRCM_DSITVCLK_DIV);
1657 return 0;
1658}
1659
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001660/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02001661 * db8500_prcmu_request_clock() - Request for a clock to be enabled or disabled.
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001662 * @clock: The clock for which the request is made.
1663 * @enable: Whether the clock should be enabled (true) or disabled (false).
1664 *
1665 * This function should only be used by the clock implementation.
1666 * Do not use it from any other place!
1667 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02001668int db8500_prcmu_request_clock(u8 clock, bool enable)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001669{
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001670 if (clock == PRCMU_SGACLK)
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001671 return request_sga_clock(clock, enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001672 else if (clock < PRCMU_NUM_REG_CLOCKS)
1673 return request_clock(clock, enable);
1674 else if (clock == PRCMU_TIMCLK)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001675 return request_timclk(enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001676 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1677 return request_dsiclk((clock - PRCMU_DSI0CLK), enable);
1678 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1679 return request_dsiescclk((clock - PRCMU_DSI0ESCCLK), enable);
1680 else if (clock == PRCMU_PLLDSI)
1681 return request_plldsi(enable);
1682 else if (clock == PRCMU_SYSCLK)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001683 return request_sysclk(enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001684 else if ((clock == PRCMU_PLLSOC0) || (clock == PRCMU_PLLSOC1))
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001685 return request_pll(clock, enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001686 else
1687 return -EINVAL;
1688}
1689
1690static unsigned long pll_rate(void __iomem *reg, unsigned long src_rate,
1691 int branch)
1692{
1693 u64 rate;
1694 u32 val;
1695 u32 d;
1696 u32 div = 1;
1697
1698 val = readl(reg);
1699
1700 rate = src_rate;
1701 rate *= ((val & PRCM_PLL_FREQ_D_MASK) >> PRCM_PLL_FREQ_D_SHIFT);
1702
1703 d = ((val & PRCM_PLL_FREQ_N_MASK) >> PRCM_PLL_FREQ_N_SHIFT);
1704 if (d > 1)
1705 div *= d;
1706
1707 d = ((val & PRCM_PLL_FREQ_R_MASK) >> PRCM_PLL_FREQ_R_SHIFT);
1708 if (d > 1)
1709 div *= d;
1710
1711 if (val & PRCM_PLL_FREQ_SELDIV2)
1712 div *= 2;
1713
1714 if ((branch == PLL_FIX) || ((branch == PLL_DIV) &&
1715 (val & PRCM_PLL_FREQ_DIV2EN) &&
1716 ((reg == PRCM_PLLSOC0_FREQ) ||
1717 (reg == PRCM_PLLDDR_FREQ))))
1718 div *= 2;
1719
1720 (void)do_div(rate, div);
1721
1722 return (unsigned long)rate;
1723}
1724
1725#define ROOT_CLOCK_RATE 38400000
1726
1727static unsigned long clock_rate(u8 clock)
1728{
1729 u32 val;
1730 u32 pllsw;
1731 unsigned long rate = ROOT_CLOCK_RATE;
1732
1733 val = readl(clk_mgt[clock].reg);
1734
1735 if (val & PRCM_CLK_MGT_CLK38) {
1736 if (clk_mgt[clock].clk38div && (val & PRCM_CLK_MGT_CLK38DIV))
1737 rate /= 2;
1738 return rate;
Linus Walleije62ccf32011-10-10 12:14:14 +02001739 }
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001740
1741 val |= clk_mgt[clock].pllsw;
1742 pllsw = (val & PRCM_CLK_MGT_CLKPLLSW_MASK);
1743
1744 if (pllsw == PRCM_CLK_MGT_CLKPLLSW_SOC0)
1745 rate = pll_rate(PRCM_PLLSOC0_FREQ, rate, clk_mgt[clock].branch);
1746 else if (pllsw == PRCM_CLK_MGT_CLKPLLSW_SOC1)
1747 rate = pll_rate(PRCM_PLLSOC1_FREQ, rate, clk_mgt[clock].branch);
1748 else if (pllsw == PRCM_CLK_MGT_CLKPLLSW_DDR)
1749 rate = pll_rate(PRCM_PLLDDR_FREQ, rate, clk_mgt[clock].branch);
1750 else
1751 return 0;
1752
1753 if ((clock == PRCMU_SGACLK) &&
1754 (val & PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN)) {
1755 u64 r = (rate * 10);
1756
1757 (void)do_div(r, 25);
1758 return (unsigned long)r;
1759 }
1760 val &= PRCM_CLK_MGT_CLKPLLDIV_MASK;
1761 if (val)
1762 return rate / val;
1763 else
1764 return 0;
1765}
1766
1767static unsigned long dsiclk_rate(u8 n)
1768{
1769 u32 divsel;
1770 u32 div = 1;
1771
1772 divsel = readl(PRCM_DSI_PLLOUT_SEL);
1773 divsel = ((divsel & dsiclk[n].divsel_mask) >> dsiclk[n].divsel_shift);
1774
1775 if (divsel == PRCM_DSI_PLLOUT_SEL_OFF)
1776 divsel = dsiclk[n].divsel;
1777
1778 switch (divsel) {
1779 case PRCM_DSI_PLLOUT_SEL_PHI_4:
1780 div *= 2;
1781 case PRCM_DSI_PLLOUT_SEL_PHI_2:
1782 div *= 2;
1783 case PRCM_DSI_PLLOUT_SEL_PHI:
1784 return pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1785 PLL_RAW) / div;
1786 default:
1787 return 0;
1788 }
1789}
1790
1791static unsigned long dsiescclk_rate(u8 n)
1792{
1793 u32 div;
1794
1795 div = readl(PRCM_DSITVCLK_DIV);
1796 div = ((div & dsiescclk[n].div_mask) >> (dsiescclk[n].div_shift));
1797 return clock_rate(PRCMU_TVCLK) / max((u32)1, div);
1798}
1799
1800unsigned long prcmu_clock_rate(u8 clock)
1801{
Linus Walleije62ccf32011-10-10 12:14:14 +02001802 if (clock < PRCMU_NUM_REG_CLOCKS)
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001803 return clock_rate(clock);
1804 else if (clock == PRCMU_TIMCLK)
1805 return ROOT_CLOCK_RATE / 16;
1806 else if (clock == PRCMU_SYSCLK)
1807 return ROOT_CLOCK_RATE;
1808 else if (clock == PRCMU_PLLSOC0)
1809 return pll_rate(PRCM_PLLSOC0_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
1810 else if (clock == PRCMU_PLLSOC1)
1811 return pll_rate(PRCM_PLLSOC1_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
1812 else if (clock == PRCMU_PLLDDR)
1813 return pll_rate(PRCM_PLLDDR_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
1814 else if (clock == PRCMU_PLLDSI)
1815 return pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1816 PLL_RAW);
1817 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1818 return dsiclk_rate(clock - PRCMU_DSI0CLK);
1819 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1820 return dsiescclk_rate(clock - PRCMU_DSI0ESCCLK);
1821 else
1822 return 0;
1823}
1824
1825static unsigned long clock_source_rate(u32 clk_mgt_val, int branch)
1826{
1827 if (clk_mgt_val & PRCM_CLK_MGT_CLK38)
1828 return ROOT_CLOCK_RATE;
1829 clk_mgt_val &= PRCM_CLK_MGT_CLKPLLSW_MASK;
1830 if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_SOC0)
1831 return pll_rate(PRCM_PLLSOC0_FREQ, ROOT_CLOCK_RATE, branch);
1832 else if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_SOC1)
1833 return pll_rate(PRCM_PLLSOC1_FREQ, ROOT_CLOCK_RATE, branch);
1834 else if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_DDR)
1835 return pll_rate(PRCM_PLLDDR_FREQ, ROOT_CLOCK_RATE, branch);
1836 else
1837 return 0;
1838}
1839
1840static u32 clock_divider(unsigned long src_rate, unsigned long rate)
1841{
1842 u32 div;
1843
1844 div = (src_rate / rate);
1845 if (div == 0)
1846 return 1;
1847 if (rate < (src_rate / div))
1848 div++;
1849 return div;
1850}
1851
1852static long round_clock_rate(u8 clock, unsigned long rate)
1853{
1854 u32 val;
1855 u32 div;
1856 unsigned long src_rate;
1857 long rounded_rate;
1858
1859 val = readl(clk_mgt[clock].reg);
1860 src_rate = clock_source_rate((val | clk_mgt[clock].pllsw),
1861 clk_mgt[clock].branch);
1862 div = clock_divider(src_rate, rate);
1863 if (val & PRCM_CLK_MGT_CLK38) {
1864 if (clk_mgt[clock].clk38div) {
1865 if (div > 2)
1866 div = 2;
1867 } else {
1868 div = 1;
1869 }
1870 } else if ((clock == PRCMU_SGACLK) && (div == 3)) {
1871 u64 r = (src_rate * 10);
1872
1873 (void)do_div(r, 25);
1874 if (r <= rate)
1875 return (unsigned long)r;
1876 }
1877 rounded_rate = (src_rate / min(div, (u32)31));
1878
1879 return rounded_rate;
1880}
1881
1882#define MIN_PLL_VCO_RATE 600000000ULL
1883#define MAX_PLL_VCO_RATE 1680640000ULL
1884
1885static long round_plldsi_rate(unsigned long rate)
1886{
1887 long rounded_rate = 0;
1888 unsigned long src_rate;
1889 unsigned long rem;
1890 u32 r;
1891
1892 src_rate = clock_rate(PRCMU_HDMICLK);
1893 rem = rate;
1894
1895 for (r = 7; (rem > 0) && (r > 0); r--) {
1896 u64 d;
1897
1898 d = (r * rate);
1899 (void)do_div(d, src_rate);
1900 if (d < 6)
1901 d = 6;
1902 else if (d > 255)
1903 d = 255;
1904 d *= src_rate;
1905 if (((2 * d) < (r * MIN_PLL_VCO_RATE)) ||
1906 ((r * MAX_PLL_VCO_RATE) < (2 * d)))
1907 continue;
1908 (void)do_div(d, r);
1909 if (rate < d) {
1910 if (rounded_rate == 0)
1911 rounded_rate = (long)d;
1912 break;
1913 }
1914 if ((rate - d) < rem) {
1915 rem = (rate - d);
1916 rounded_rate = (long)d;
1917 }
1918 }
1919 return rounded_rate;
1920}
1921
1922static long round_dsiclk_rate(unsigned long rate)
1923{
1924 u32 div;
1925 unsigned long src_rate;
1926 long rounded_rate;
1927
1928 src_rate = pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1929 PLL_RAW);
1930 div = clock_divider(src_rate, rate);
1931 rounded_rate = (src_rate / ((div > 2) ? 4 : div));
1932
1933 return rounded_rate;
1934}
1935
1936static long round_dsiescclk_rate(unsigned long rate)
1937{
1938 u32 div;
1939 unsigned long src_rate;
1940 long rounded_rate;
1941
1942 src_rate = clock_rate(PRCMU_TVCLK);
1943 div = clock_divider(src_rate, rate);
1944 rounded_rate = (src_rate / min(div, (u32)255));
1945
1946 return rounded_rate;
1947}
1948
1949long prcmu_round_clock_rate(u8 clock, unsigned long rate)
1950{
1951 if (clock < PRCMU_NUM_REG_CLOCKS)
1952 return round_clock_rate(clock, rate);
1953 else if (clock == PRCMU_PLLDSI)
1954 return round_plldsi_rate(rate);
1955 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1956 return round_dsiclk_rate(rate);
1957 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1958 return round_dsiescclk_rate(rate);
1959 else
1960 return (long)prcmu_clock_rate(clock);
1961}
1962
1963static void set_clock_rate(u8 clock, unsigned long rate)
1964{
1965 u32 val;
1966 u32 div;
1967 unsigned long src_rate;
1968 unsigned long flags;
1969
1970 spin_lock_irqsave(&clk_mgt_lock, flags);
1971
1972 /* Grab the HW semaphore. */
1973 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
1974 cpu_relax();
1975
1976 val = readl(clk_mgt[clock].reg);
1977 src_rate = clock_source_rate((val | clk_mgt[clock].pllsw),
1978 clk_mgt[clock].branch);
1979 div = clock_divider(src_rate, rate);
1980 if (val & PRCM_CLK_MGT_CLK38) {
1981 if (clk_mgt[clock].clk38div) {
1982 if (div > 1)
1983 val |= PRCM_CLK_MGT_CLK38DIV;
1984 else
1985 val &= ~PRCM_CLK_MGT_CLK38DIV;
1986 }
1987 } else if (clock == PRCMU_SGACLK) {
1988 val &= ~(PRCM_CLK_MGT_CLKPLLDIV_MASK |
1989 PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN);
1990 if (div == 3) {
1991 u64 r = (src_rate * 10);
1992
1993 (void)do_div(r, 25);
1994 if (r <= rate) {
1995 val |= PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN;
1996 div = 0;
1997 }
1998 }
1999 val |= min(div, (u32)31);
2000 } else {
2001 val &= ~PRCM_CLK_MGT_CLKPLLDIV_MASK;
2002 val |= min(div, (u32)31);
2003 }
2004 writel(val, clk_mgt[clock].reg);
2005
2006 /* Release the HW semaphore. */
2007 writel(0, PRCM_SEM);
2008
2009 spin_unlock_irqrestore(&clk_mgt_lock, flags);
2010}
2011
2012static int set_plldsi_rate(unsigned long rate)
2013{
2014 unsigned long src_rate;
2015 unsigned long rem;
2016 u32 pll_freq = 0;
2017 u32 r;
2018
2019 src_rate = clock_rate(PRCMU_HDMICLK);
2020 rem = rate;
2021
2022 for (r = 7; (rem > 0) && (r > 0); r--) {
2023 u64 d;
2024 u64 hwrate;
2025
2026 d = (r * rate);
2027 (void)do_div(d, src_rate);
2028 if (d < 6)
2029 d = 6;
2030 else if (d > 255)
2031 d = 255;
2032 hwrate = (d * src_rate);
2033 if (((2 * hwrate) < (r * MIN_PLL_VCO_RATE)) ||
2034 ((r * MAX_PLL_VCO_RATE) < (2 * hwrate)))
2035 continue;
2036 (void)do_div(hwrate, r);
2037 if (rate < hwrate) {
2038 if (pll_freq == 0)
2039 pll_freq = (((u32)d << PRCM_PLL_FREQ_D_SHIFT) |
2040 (r << PRCM_PLL_FREQ_R_SHIFT));
2041 break;
2042 }
2043 if ((rate - hwrate) < rem) {
2044 rem = (rate - hwrate);
2045 pll_freq = (((u32)d << PRCM_PLL_FREQ_D_SHIFT) |
2046 (r << PRCM_PLL_FREQ_R_SHIFT));
2047 }
2048 }
2049 if (pll_freq == 0)
2050 return -EINVAL;
2051
2052 pll_freq |= (1 << PRCM_PLL_FREQ_N_SHIFT);
2053 writel(pll_freq, PRCM_PLLDSI_FREQ);
2054
2055 return 0;
2056}
2057
2058static void set_dsiclk_rate(u8 n, unsigned long rate)
2059{
2060 u32 val;
2061 u32 div;
2062
2063 div = clock_divider(pll_rate(PRCM_PLLDSI_FREQ,
2064 clock_rate(PRCMU_HDMICLK), PLL_RAW), rate);
2065
2066 dsiclk[n].divsel = (div == 1) ? PRCM_DSI_PLLOUT_SEL_PHI :
2067 (div == 2) ? PRCM_DSI_PLLOUT_SEL_PHI_2 :
2068 /* else */ PRCM_DSI_PLLOUT_SEL_PHI_4;
2069
2070 val = readl(PRCM_DSI_PLLOUT_SEL);
2071 val &= ~dsiclk[n].divsel_mask;
2072 val |= (dsiclk[n].divsel << dsiclk[n].divsel_shift);
2073 writel(val, PRCM_DSI_PLLOUT_SEL);
2074}
2075
2076static void set_dsiescclk_rate(u8 n, unsigned long rate)
2077{
2078 u32 val;
2079 u32 div;
2080
2081 div = clock_divider(clock_rate(PRCMU_TVCLK), rate);
2082 val = readl(PRCM_DSITVCLK_DIV);
2083 val &= ~dsiescclk[n].div_mask;
2084 val |= (min(div, (u32)255) << dsiescclk[n].div_shift);
2085 writel(val, PRCM_DSITVCLK_DIV);
2086}
2087
2088int prcmu_set_clock_rate(u8 clock, unsigned long rate)
2089{
2090 if (clock < PRCMU_NUM_REG_CLOCKS)
2091 set_clock_rate(clock, rate);
2092 else if (clock == PRCMU_PLLDSI)
2093 return set_plldsi_rate(rate);
2094 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
2095 set_dsiclk_rate((clock - PRCMU_DSI0CLK), rate);
2096 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
2097 set_dsiescclk_rate((clock - PRCMU_DSI0ESCCLK), rate);
2098 return 0;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002099}
2100
Mattias Nilsson73180f82011-08-12 10:28:10 +02002101int db8500_prcmu_config_esram0_deep_sleep(u8 state)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002102{
2103 if ((state > ESRAM0_DEEP_SLEEP_STATE_RET) ||
2104 (state < ESRAM0_DEEP_SLEEP_STATE_OFF))
2105 return -EINVAL;
2106
2107 mutex_lock(&mb4_transfer.lock);
2108
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002109 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002110 cpu_relax();
2111
2112 writeb(MB4H_MEM_ST, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2113 writeb(((DDR_PWR_STATE_OFFHIGHLAT << 4) | DDR_PWR_STATE_ON),
2114 (tcdm_base + PRCM_REQ_MB4_DDR_ST_AP_SLEEP_IDLE));
2115 writeb(DDR_PWR_STATE_ON,
2116 (tcdm_base + PRCM_REQ_MB4_DDR_ST_AP_DEEP_IDLE));
2117 writeb(state, (tcdm_base + PRCM_REQ_MB4_ESRAM0_ST));
2118
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002119 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002120 wait_for_completion(&mb4_transfer.work);
2121
2122 mutex_unlock(&mb4_transfer.lock);
2123
2124 return 0;
2125}
2126
Mattias Nilsson05089012012-01-13 16:20:20 +01002127int db8500_prcmu_config_hotdog(u8 threshold)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002128{
2129 mutex_lock(&mb4_transfer.lock);
2130
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002131 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002132 cpu_relax();
2133
2134 writeb(threshold, (tcdm_base + PRCM_REQ_MB4_HOTDOG_THRESHOLD));
2135 writeb(MB4H_HOTDOG, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2136
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002137 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002138 wait_for_completion(&mb4_transfer.work);
2139
2140 mutex_unlock(&mb4_transfer.lock);
2141
2142 return 0;
2143}
2144
Mattias Nilsson05089012012-01-13 16:20:20 +01002145int db8500_prcmu_config_hotmon(u8 low, u8 high)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002146{
2147 mutex_lock(&mb4_transfer.lock);
2148
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002149 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002150 cpu_relax();
2151
2152 writeb(low, (tcdm_base + PRCM_REQ_MB4_HOTMON_LOW));
2153 writeb(high, (tcdm_base + PRCM_REQ_MB4_HOTMON_HIGH));
2154 writeb((HOTMON_CONFIG_LOW | HOTMON_CONFIG_HIGH),
2155 (tcdm_base + PRCM_REQ_MB4_HOTMON_CONFIG));
2156 writeb(MB4H_HOTMON, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2157
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002158 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002159 wait_for_completion(&mb4_transfer.work);
2160
2161 mutex_unlock(&mb4_transfer.lock);
2162
2163 return 0;
2164}
2165
2166static int config_hot_period(u16 val)
2167{
2168 mutex_lock(&mb4_transfer.lock);
2169
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002170 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002171 cpu_relax();
2172
2173 writew(val, (tcdm_base + PRCM_REQ_MB4_HOT_PERIOD));
2174 writeb(MB4H_HOT_PERIOD, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2175
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002176 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002177 wait_for_completion(&mb4_transfer.work);
2178
2179 mutex_unlock(&mb4_transfer.lock);
2180
2181 return 0;
2182}
2183
Mattias Nilsson05089012012-01-13 16:20:20 +01002184int db8500_prcmu_start_temp_sense(u16 cycles32k)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002185{
2186 if (cycles32k == 0xFFFF)
2187 return -EINVAL;
2188
2189 return config_hot_period(cycles32k);
2190}
2191
Mattias Nilsson05089012012-01-13 16:20:20 +01002192int db8500_prcmu_stop_temp_sense(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002193{
2194 return config_hot_period(0xFFFF);
2195}
2196
Jonas Aberg84165b82011-08-12 10:28:33 +02002197static int prcmu_a9wdog(u8 cmd, u8 d0, u8 d1, u8 d2, u8 d3)
2198{
2199
2200 mutex_lock(&mb4_transfer.lock);
2201
2202 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
2203 cpu_relax();
2204
2205 writeb(d0, (tcdm_base + PRCM_REQ_MB4_A9WDOG_0));
2206 writeb(d1, (tcdm_base + PRCM_REQ_MB4_A9WDOG_1));
2207 writeb(d2, (tcdm_base + PRCM_REQ_MB4_A9WDOG_2));
2208 writeb(d3, (tcdm_base + PRCM_REQ_MB4_A9WDOG_3));
2209
2210 writeb(cmd, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2211
2212 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
2213 wait_for_completion(&mb4_transfer.work);
2214
2215 mutex_unlock(&mb4_transfer.lock);
2216
2217 return 0;
2218
2219}
2220
Mattias Nilsson05089012012-01-13 16:20:20 +01002221int db8500_prcmu_config_a9wdog(u8 num, bool sleep_auto_off)
Jonas Aberg84165b82011-08-12 10:28:33 +02002222{
2223 BUG_ON(num == 0 || num > 0xf);
2224 return prcmu_a9wdog(MB4H_A9WDOG_CONF, num, 0, 0,
2225 sleep_auto_off ? A9WDOG_AUTO_OFF_EN :
2226 A9WDOG_AUTO_OFF_DIS);
2227}
2228
Mattias Nilsson05089012012-01-13 16:20:20 +01002229int db8500_prcmu_enable_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002230{
2231 return prcmu_a9wdog(MB4H_A9WDOG_EN, id, 0, 0, 0);
2232}
2233
Mattias Nilsson05089012012-01-13 16:20:20 +01002234int db8500_prcmu_disable_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002235{
2236 return prcmu_a9wdog(MB4H_A9WDOG_DIS, id, 0, 0, 0);
2237}
2238
Mattias Nilsson05089012012-01-13 16:20:20 +01002239int db8500_prcmu_kick_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002240{
2241 return prcmu_a9wdog(MB4H_A9WDOG_KICK, id, 0, 0, 0);
2242}
2243
2244/*
2245 * timeout is 28 bit, in ms.
2246 */
Mattias Nilsson05089012012-01-13 16:20:20 +01002247int db8500_prcmu_load_a9wdog(u8 id, u32 timeout)
Jonas Aberg84165b82011-08-12 10:28:33 +02002248{
Jonas Aberg84165b82011-08-12 10:28:33 +02002249 return prcmu_a9wdog(MB4H_A9WDOG_LOAD,
2250 (id & A9WDOG_ID_MASK) |
2251 /*
2252 * Put the lowest 28 bits of timeout at
2253 * offset 4. Four first bits are used for id.
2254 */
2255 (u8)((timeout << 4) & 0xf0),
2256 (u8)((timeout >> 4) & 0xff),
2257 (u8)((timeout >> 12) & 0xff),
2258 (u8)((timeout >> 20) & 0xff));
2259}
2260
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002261/**
Linus Walleije3726fc2010-08-19 12:36:01 +01002262 * prcmu_abb_read() - Read register value(s) from the ABB.
2263 * @slave: The I2C slave address.
2264 * @reg: The (start) register address.
2265 * @value: The read out value(s).
2266 * @size: The number of registers to read.
2267 *
2268 * Reads register value(s) from the ABB.
2269 * @size has to be 1 for the current firmware version.
2270 */
2271int prcmu_abb_read(u8 slave, u8 reg, u8 *value, u8 size)
2272{
2273 int r;
2274
2275 if (size != 1)
2276 return -EINVAL;
2277
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002278 mutex_lock(&mb5_transfer.lock);
Linus Walleije3726fc2010-08-19 12:36:01 +01002279
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002280 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(5))
Linus Walleije3726fc2010-08-19 12:36:01 +01002281 cpu_relax();
2282
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002283 writeb(PRCMU_I2C_READ(slave), (tcdm_base + PRCM_REQ_MB5_I2C_SLAVE_OP));
2284 writeb(PRCMU_I2C_STOP_EN, (tcdm_base + PRCM_REQ_MB5_I2C_HW_BITS));
2285 writeb(reg, (tcdm_base + PRCM_REQ_MB5_I2C_REG));
2286 writeb(0, (tcdm_base + PRCM_REQ_MB5_I2C_VAL));
Linus Walleije3726fc2010-08-19 12:36:01 +01002287
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002288 writel(MBOX_BIT(5), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002289
Linus Walleije3726fc2010-08-19 12:36:01 +01002290 if (!wait_for_completion_timeout(&mb5_transfer.work,
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002291 msecs_to_jiffies(20000))) {
2292 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
2293 __func__);
Linus Walleije3726fc2010-08-19 12:36:01 +01002294 r = -EIO;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002295 } else {
2296 r = ((mb5_transfer.ack.status == I2C_RD_OK) ? 0 : -EIO);
Linus Walleije3726fc2010-08-19 12:36:01 +01002297 }
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002298
Linus Walleije3726fc2010-08-19 12:36:01 +01002299 if (!r)
2300 *value = mb5_transfer.ack.value;
2301
Linus Walleije3726fc2010-08-19 12:36:01 +01002302 mutex_unlock(&mb5_transfer.lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002303
Linus Walleije3726fc2010-08-19 12:36:01 +01002304 return r;
2305}
Linus Walleije3726fc2010-08-19 12:36:01 +01002306
2307/**
2308 * prcmu_abb_write() - Write register value(s) to the ABB.
2309 * @slave: The I2C slave address.
2310 * @reg: The (start) register address.
2311 * @value: The value(s) to write.
2312 * @size: The number of registers to write.
2313 *
2314 * Reads register value(s) from the ABB.
2315 * @size has to be 1 for the current firmware version.
2316 */
2317int prcmu_abb_write(u8 slave, u8 reg, u8 *value, u8 size)
2318{
2319 int r;
2320
2321 if (size != 1)
2322 return -EINVAL;
2323
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002324 mutex_lock(&mb5_transfer.lock);
Linus Walleije3726fc2010-08-19 12:36:01 +01002325
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002326 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(5))
Linus Walleije3726fc2010-08-19 12:36:01 +01002327 cpu_relax();
2328
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002329 writeb(PRCMU_I2C_WRITE(slave), (tcdm_base + PRCM_REQ_MB5_I2C_SLAVE_OP));
2330 writeb(PRCMU_I2C_STOP_EN, (tcdm_base + PRCM_REQ_MB5_I2C_HW_BITS));
2331 writeb(reg, (tcdm_base + PRCM_REQ_MB5_I2C_REG));
2332 writeb(*value, (tcdm_base + PRCM_REQ_MB5_I2C_VAL));
Linus Walleije3726fc2010-08-19 12:36:01 +01002333
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002334 writel(MBOX_BIT(5), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002335
Linus Walleije3726fc2010-08-19 12:36:01 +01002336 if (!wait_for_completion_timeout(&mb5_transfer.work,
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002337 msecs_to_jiffies(20000))) {
2338 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
2339 __func__);
Linus Walleije3726fc2010-08-19 12:36:01 +01002340 r = -EIO;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002341 } else {
2342 r = ((mb5_transfer.ack.status == I2C_WR_OK) ? 0 : -EIO);
Linus Walleije3726fc2010-08-19 12:36:01 +01002343 }
Linus Walleije3726fc2010-08-19 12:36:01 +01002344
Linus Walleije3726fc2010-08-19 12:36:01 +01002345 mutex_unlock(&mb5_transfer.lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002346
Linus Walleije3726fc2010-08-19 12:36:01 +01002347 return r;
2348}
Linus Walleije3726fc2010-08-19 12:36:01 +01002349
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002350/**
2351 * prcmu_ac_wake_req - should be called whenever ARM wants to wakeup Modem
2352 */
2353void prcmu_ac_wake_req(void)
Martin Perssone0befb22010-12-08 15:13:28 +01002354{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002355 u32 val;
Mattias Nilssond6e30022011-08-12 10:28:43 +02002356 u32 status;
Martin Perssone0befb22010-12-08 15:13:28 +01002357
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002358 mutex_lock(&mb0_transfer.ac_wake_lock);
Martin Perssone0befb22010-12-08 15:13:28 +01002359
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002360 val = readl(PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002361 if (val & PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ)
2362 goto unlock_and_return;
2363
2364 atomic_set(&ac_wake_req_state, 1);
2365
Mattias Nilssond6e30022011-08-12 10:28:43 +02002366retry:
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002367 writel((val | PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ), PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002368
2369 if (!wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
Mattias Nilssond6e30022011-08-12 10:28:43 +02002370 msecs_to_jiffies(5000))) {
Linus Walleij57265bc2011-10-10 13:04:44 +02002371 pr_crit("prcmu: %s timed out (5 s) waiting for a reply.\n",
Mattias Nilssond6e30022011-08-12 10:28:43 +02002372 __func__);
2373 goto unlock_and_return;
2374 }
2375
2376 /*
2377 * The modem can generate an AC_WAKE_ACK, and then still go to sleep.
2378 * As a workaround, we wait, and then check that the modem is indeed
2379 * awake (in terms of the value of the PRCM_MOD_AWAKE_STATUS
2380 * register, which may not be the whole truth).
2381 */
2382 udelay(400);
2383 status = (readl(PRCM_MOD_AWAKE_STATUS) & BITS(0, 2));
2384 if (status != (PRCM_MOD_AWAKE_STATUS_PRCM_MOD_AAPD_AWAKE |
2385 PRCM_MOD_AWAKE_STATUS_PRCM_MOD_COREPD_AWAKE)) {
2386 pr_err("prcmu: %s received ack, but modem not awake (0x%X).\n",
2387 __func__, status);
2388 udelay(1200);
2389 writel(val, PRCM_HOSTACCESS_REQ);
2390 if (wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
2391 msecs_to_jiffies(5000)))
2392 goto retry;
Linus Walleij57265bc2011-10-10 13:04:44 +02002393 pr_crit("prcmu: %s timed out (5 s) waiting for AC_SLEEP_ACK.\n",
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002394 __func__);
2395 }
2396
2397unlock_and_return:
2398 mutex_unlock(&mb0_transfer.ac_wake_lock);
2399}
2400
2401/**
2402 * prcmu_ac_sleep_req - called when ARM no longer needs to talk to modem
2403 */
2404void prcmu_ac_sleep_req()
2405{
2406 u32 val;
2407
2408 mutex_lock(&mb0_transfer.ac_wake_lock);
2409
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002410 val = readl(PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002411 if (!(val & PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ))
2412 goto unlock_and_return;
2413
2414 writel((val & ~PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ),
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002415 PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002416
2417 if (!wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
Mattias Nilssond6e30022011-08-12 10:28:43 +02002418 msecs_to_jiffies(5000))) {
Linus Walleij57265bc2011-10-10 13:04:44 +02002419 pr_crit("prcmu: %s timed out (5 s) waiting for a reply.\n",
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002420 __func__);
2421 }
2422
2423 atomic_set(&ac_wake_req_state, 0);
2424
2425unlock_and_return:
2426 mutex_unlock(&mb0_transfer.ac_wake_lock);
2427}
2428
Mattias Nilsson73180f82011-08-12 10:28:10 +02002429bool db8500_prcmu_is_ac_wake_requested(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002430{
2431 return (atomic_read(&ac_wake_req_state) != 0);
2432}
2433
2434/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02002435 * db8500_prcmu_system_reset - System reset
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002436 *
Mattias Nilsson73180f82011-08-12 10:28:10 +02002437 * Saves the reset reason code and then sets the APE_SOFTRST register which
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002438 * fires interrupt to fw
2439 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02002440void db8500_prcmu_system_reset(u16 reset_code)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002441{
2442 writew(reset_code, (tcdm_base + PRCM_SW_RST_REASON));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002443 writel(1, PRCM_APE_SOFTRST);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002444}
2445
2446/**
Sebastian Rasmussen597045d2011-08-12 10:28:53 +02002447 * db8500_prcmu_get_reset_code - Retrieve SW reset reason code
2448 *
2449 * Retrieves the reset reason code stored by prcmu_system_reset() before
2450 * last restart.
2451 */
2452u16 db8500_prcmu_get_reset_code(void)
2453{
2454 return readw(tcdm_base + PRCM_SW_RST_REASON);
2455}
2456
2457/**
Mattias Nilsson05089012012-01-13 16:20:20 +01002458 * db8500_prcmu_reset_modem - ask the PRCMU to reset modem
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002459 */
Mattias Nilsson05089012012-01-13 16:20:20 +01002460void db8500_prcmu_modem_reset(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002461{
Martin Perssone0befb22010-12-08 15:13:28 +01002462 mutex_lock(&mb1_transfer.lock);
2463
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002464 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Martin Perssone0befb22010-12-08 15:13:28 +01002465 cpu_relax();
2466
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002467 writeb(MB1H_RESET_MODEM, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002468 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Martin Perssone0befb22010-12-08 15:13:28 +01002469 wait_for_completion(&mb1_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002470
2471 /*
2472 * No need to check return from PRCMU as modem should go in reset state
2473 * This state is already managed by upper layer
2474 */
Martin Perssone0befb22010-12-08 15:13:28 +01002475
2476 mutex_unlock(&mb1_transfer.lock);
Martin Perssone0befb22010-12-08 15:13:28 +01002477}
2478
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002479static void ack_dbb_wakeup(void)
Martin Perssone0befb22010-12-08 15:13:28 +01002480{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002481 unsigned long flags;
Martin Perssone0befb22010-12-08 15:13:28 +01002482
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002483 spin_lock_irqsave(&mb0_transfer.lock, flags);
Martin Perssone0befb22010-12-08 15:13:28 +01002484
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002485 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002486 cpu_relax();
Martin Perssone0befb22010-12-08 15:13:28 +01002487
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002488 writeb(MB0H_READ_WAKEUP_ACK, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002489 writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
Martin Perssone0befb22010-12-08 15:13:28 +01002490
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002491 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
Martin Perssone0befb22010-12-08 15:13:28 +01002492}
2493
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002494static inline void print_unknown_header_warning(u8 n, u8 header)
Linus Walleije3726fc2010-08-19 12:36:01 +01002495{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002496 pr_warning("prcmu: Unknown message header (%d) in mailbox %d.\n",
2497 header, n);
Linus Walleije3726fc2010-08-19 12:36:01 +01002498}
2499
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002500static bool read_mailbox_0(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002501{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002502 bool r;
2503 u32 ev;
2504 unsigned int n;
2505 u8 header;
2506
2507 header = readb(tcdm_base + PRCM_MBOX_HEADER_ACK_MB0);
2508 switch (header) {
2509 case MB0H_WAKEUP_EXE:
2510 case MB0H_WAKEUP_SLEEP:
2511 if (readb(tcdm_base + PRCM_ACK_MB0_READ_POINTER) & 1)
2512 ev = readl(tcdm_base + PRCM_ACK_MB0_WAKEUP_1_8500);
2513 else
2514 ev = readl(tcdm_base + PRCM_ACK_MB0_WAKEUP_0_8500);
2515
2516 if (ev & (WAKEUP_BIT_AC_WAKE_ACK | WAKEUP_BIT_AC_SLEEP_ACK))
2517 complete(&mb0_transfer.ac_wake_work);
2518 if (ev & WAKEUP_BIT_SYSCLK_OK)
2519 complete(&mb3_transfer.sysclk_work);
2520
2521 ev &= mb0_transfer.req.dbb_irqs;
2522
2523 for (n = 0; n < NUM_PRCMU_WAKEUPS; n++) {
2524 if (ev & prcmu_irq_bit[n])
2525 generic_handle_irq(IRQ_PRCMU_BASE + n);
2526 }
2527 r = true;
2528 break;
2529 default:
2530 print_unknown_header_warning(0, header);
2531 r = false;
2532 break;
2533 }
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002534 writel(MBOX_BIT(0), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002535 return r;
2536}
2537
2538static bool read_mailbox_1(void)
2539{
2540 mb1_transfer.ack.header = readb(tcdm_base + PRCM_MBOX_HEADER_REQ_MB1);
2541 mb1_transfer.ack.arm_opp = readb(tcdm_base +
2542 PRCM_ACK_MB1_CURRENT_ARM_OPP);
2543 mb1_transfer.ack.ape_opp = readb(tcdm_base +
2544 PRCM_ACK_MB1_CURRENT_APE_OPP);
2545 mb1_transfer.ack.ape_voltage_status = readb(tcdm_base +
2546 PRCM_ACK_MB1_APE_VOLTAGE_STATUS);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002547 writel(MBOX_BIT(1), PRCM_ARM_IT1_CLR);
Martin Perssone0befb22010-12-08 15:13:28 +01002548 complete(&mb1_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002549 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002550}
2551
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002552static bool read_mailbox_2(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002553{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002554 mb2_transfer.ack.status = readb(tcdm_base + PRCM_ACK_MB2_DPS_STATUS);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002555 writel(MBOX_BIT(2), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002556 complete(&mb2_transfer.work);
2557 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002558}
2559
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002560static bool read_mailbox_3(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002561{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002562 writel(MBOX_BIT(3), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002563 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002564}
2565
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002566static bool read_mailbox_4(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002567{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002568 u8 header;
2569 bool do_complete = true;
2570
2571 header = readb(tcdm_base + PRCM_MBOX_HEADER_REQ_MB4);
2572 switch (header) {
2573 case MB4H_MEM_ST:
2574 case MB4H_HOTDOG:
2575 case MB4H_HOTMON:
2576 case MB4H_HOT_PERIOD:
Mattias Nilssona592c2e2011-08-12 10:27:41 +02002577 case MB4H_A9WDOG_CONF:
2578 case MB4H_A9WDOG_EN:
2579 case MB4H_A9WDOG_DIS:
2580 case MB4H_A9WDOG_LOAD:
2581 case MB4H_A9WDOG_KICK:
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002582 break;
2583 default:
2584 print_unknown_header_warning(4, header);
2585 do_complete = false;
2586 break;
2587 }
2588
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002589 writel(MBOX_BIT(4), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002590
2591 if (do_complete)
2592 complete(&mb4_transfer.work);
2593
2594 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002595}
2596
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002597static bool read_mailbox_5(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002598{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002599 mb5_transfer.ack.status = readb(tcdm_base + PRCM_ACK_MB5_I2C_STATUS);
2600 mb5_transfer.ack.value = readb(tcdm_base + PRCM_ACK_MB5_I2C_VAL);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002601 writel(MBOX_BIT(5), PRCM_ARM_IT1_CLR);
Linus Walleije3726fc2010-08-19 12:36:01 +01002602 complete(&mb5_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002603 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002604}
2605
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002606static bool read_mailbox_6(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002607{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002608 writel(MBOX_BIT(6), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002609 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002610}
2611
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002612static bool read_mailbox_7(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002613{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002614 writel(MBOX_BIT(7), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002615 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002616}
2617
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002618static bool (* const read_mailbox[NUM_MB])(void) = {
Linus Walleije3726fc2010-08-19 12:36:01 +01002619 read_mailbox_0,
2620 read_mailbox_1,
2621 read_mailbox_2,
2622 read_mailbox_3,
2623 read_mailbox_4,
2624 read_mailbox_5,
2625 read_mailbox_6,
2626 read_mailbox_7
2627};
2628
2629static irqreturn_t prcmu_irq_handler(int irq, void *data)
2630{
2631 u32 bits;
2632 u8 n;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002633 irqreturn_t r;
Linus Walleije3726fc2010-08-19 12:36:01 +01002634
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002635 bits = (readl(PRCM_ARM_IT1_VAL) & ALL_MBOX_BITS);
Linus Walleije3726fc2010-08-19 12:36:01 +01002636 if (unlikely(!bits))
2637 return IRQ_NONE;
2638
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002639 r = IRQ_HANDLED;
Linus Walleije3726fc2010-08-19 12:36:01 +01002640 for (n = 0; bits; n++) {
2641 if (bits & MBOX_BIT(n)) {
2642 bits -= MBOX_BIT(n);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002643 if (read_mailbox[n]())
2644 r = IRQ_WAKE_THREAD;
Linus Walleije3726fc2010-08-19 12:36:01 +01002645 }
2646 }
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002647 return r;
2648}
2649
2650static irqreturn_t prcmu_irq_thread_fn(int irq, void *data)
2651{
2652 ack_dbb_wakeup();
Linus Walleije3726fc2010-08-19 12:36:01 +01002653 return IRQ_HANDLED;
2654}
2655
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002656static void prcmu_mask_work(struct work_struct *work)
2657{
2658 unsigned long flags;
2659
2660 spin_lock_irqsave(&mb0_transfer.lock, flags);
2661
2662 config_wakeups();
2663
2664 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
2665}
2666
2667static void prcmu_irq_mask(struct irq_data *d)
2668{
2669 unsigned long flags;
2670
2671 spin_lock_irqsave(&mb0_transfer.dbb_irqs_lock, flags);
2672
2673 mb0_transfer.req.dbb_irqs &= ~prcmu_irq_bit[d->irq - IRQ_PRCMU_BASE];
2674
2675 spin_unlock_irqrestore(&mb0_transfer.dbb_irqs_lock, flags);
2676
2677 if (d->irq != IRQ_PRCMU_CA_SLEEP)
2678 schedule_work(&mb0_transfer.mask_work);
2679}
2680
2681static void prcmu_irq_unmask(struct irq_data *d)
2682{
2683 unsigned long flags;
2684
2685 spin_lock_irqsave(&mb0_transfer.dbb_irqs_lock, flags);
2686
2687 mb0_transfer.req.dbb_irqs |= prcmu_irq_bit[d->irq - IRQ_PRCMU_BASE];
2688
2689 spin_unlock_irqrestore(&mb0_transfer.dbb_irqs_lock, flags);
2690
2691 if (d->irq != IRQ_PRCMU_CA_SLEEP)
2692 schedule_work(&mb0_transfer.mask_work);
2693}
2694
2695static void noop(struct irq_data *d)
2696{
2697}
2698
2699static struct irq_chip prcmu_irq_chip = {
2700 .name = "prcmu",
2701 .irq_disable = prcmu_irq_mask,
2702 .irq_ack = noop,
2703 .irq_mask = prcmu_irq_mask,
2704 .irq_unmask = prcmu_irq_unmask,
2705};
2706
Mattias Nilssonb58d12f2012-01-13 16:20:10 +01002707static char *fw_project_name(u8 project)
2708{
2709 switch (project) {
2710 case PRCMU_FW_PROJECT_U8500:
2711 return "U8500";
2712 case PRCMU_FW_PROJECT_U8500_C2:
2713 return "U8500 C2";
2714 case PRCMU_FW_PROJECT_U9500:
2715 return "U9500";
2716 case PRCMU_FW_PROJECT_U9500_C2:
2717 return "U9500 C2";
2718 default:
2719 return "Unknown";
2720 }
2721}
2722
Mattias Nilsson73180f82011-08-12 10:28:10 +02002723void __init db8500_prcmu_early_init(void)
Mattias Wallinfcbd4582010-12-02 16:20:42 +01002724{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002725 unsigned int i;
Linus Walleij3e2762c2012-01-02 14:17:40 +01002726 if (cpu_is_u8500v2()) {
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002727 void *tcpm_base = ioremap_nocache(U8500_PRCMU_TCPM_BASE, SZ_4K);
2728
2729 if (tcpm_base != NULL) {
Linus Walleij3e2762c2012-01-02 14:17:40 +01002730 u32 version;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002731 version = readl(tcpm_base + PRCMU_FW_VERSION_OFFSET);
Mattias Nilssonb58d12f2012-01-13 16:20:10 +01002732 fw_info.version.project = version & 0xFF;
2733 fw_info.version.api_version = (version >> 8) & 0xFF;
2734 fw_info.version.func_version = (version >> 16) & 0xFF;
2735 fw_info.version.errata = (version >> 24) & 0xFF;
2736 fw_info.valid = true;
2737 pr_info("PRCMU firmware: %s, version %d.%d.%d\n",
2738 fw_project_name(fw_info.version.project),
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002739 (version >> 8) & 0xFF, (version >> 16) & 0xFF,
2740 (version >> 24) & 0xFF);
2741 iounmap(tcpm_base);
2742 }
2743
Mattias Wallinfcbd4582010-12-02 16:20:42 +01002744 tcdm_base = __io_address(U8500_PRCMU_TCDM_BASE);
2745 } else {
2746 pr_err("prcmu: Unsupported chip version\n");
2747 BUG();
2748 }
Mattias Wallinfcbd4582010-12-02 16:20:42 +01002749
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002750 spin_lock_init(&mb0_transfer.lock);
2751 spin_lock_init(&mb0_transfer.dbb_irqs_lock);
2752 mutex_init(&mb0_transfer.ac_wake_lock);
2753 init_completion(&mb0_transfer.ac_wake_work);
Martin Perssone0befb22010-12-08 15:13:28 +01002754 mutex_init(&mb1_transfer.lock);
2755 init_completion(&mb1_transfer.work);
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01002756 mb1_transfer.ape_opp = APE_NO_CHANGE;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002757 mutex_init(&mb2_transfer.lock);
2758 init_completion(&mb2_transfer.work);
2759 spin_lock_init(&mb2_transfer.auto_pm_lock);
2760 spin_lock_init(&mb3_transfer.lock);
2761 mutex_init(&mb3_transfer.sysclk_lock);
2762 init_completion(&mb3_transfer.sysclk_work);
2763 mutex_init(&mb4_transfer.lock);
2764 init_completion(&mb4_transfer.work);
Linus Walleije3726fc2010-08-19 12:36:01 +01002765 mutex_init(&mb5_transfer.lock);
2766 init_completion(&mb5_transfer.work);
2767
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002768 INIT_WORK(&mb0_transfer.mask_work, prcmu_mask_work);
Linus Walleije3726fc2010-08-19 12:36:01 +01002769
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002770 /* Initalize irqs. */
2771 for (i = 0; i < NUM_PRCMU_WAKEUPS; i++) {
2772 unsigned int irq;
2773
2774 irq = IRQ_PRCMU_BASE + i;
2775 irq_set_chip_and_handler(irq, &prcmu_irq_chip,
2776 handle_simple_irq);
2777 set_irq_flags(irq, IRQF_VALID);
2778 }
Linus Walleije3726fc2010-08-19 12:36:01 +01002779}
2780
Mattias Nilsson05089012012-01-13 16:20:20 +01002781static void __init init_prcm_registers(void)
Mattias Nilssond65e12d2011-08-12 10:27:50 +02002782{
2783 u32 val;
2784
2785 val = readl(PRCM_A9PL_FORCE_CLKEN);
2786 val &= ~(PRCM_A9PL_FORCE_CLKEN_PRCM_A9PL_FORCE_CLKEN |
2787 PRCM_A9PL_FORCE_CLKEN_PRCM_A9AXI_FORCE_CLKEN);
2788 writel(val, (PRCM_A9PL_FORCE_CLKEN));
2789}
2790
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002791/*
2792 * Power domain switches (ePODs) modeled as regulators for the DB8500 SoC
2793 */
2794static struct regulator_consumer_supply db8500_vape_consumers[] = {
2795 REGULATOR_SUPPLY("v-ape", NULL),
2796 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.0"),
2797 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.1"),
2798 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.2"),
2799 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.3"),
2800 /* "v-mmc" changed to "vcore" in the mainline kernel */
2801 REGULATOR_SUPPLY("vcore", "sdi0"),
2802 REGULATOR_SUPPLY("vcore", "sdi1"),
2803 REGULATOR_SUPPLY("vcore", "sdi2"),
2804 REGULATOR_SUPPLY("vcore", "sdi3"),
2805 REGULATOR_SUPPLY("vcore", "sdi4"),
2806 REGULATOR_SUPPLY("v-dma", "dma40.0"),
2807 REGULATOR_SUPPLY("v-ape", "ab8500-usb.0"),
2808 /* "v-uart" changed to "vcore" in the mainline kernel */
2809 REGULATOR_SUPPLY("vcore", "uart0"),
2810 REGULATOR_SUPPLY("vcore", "uart1"),
2811 REGULATOR_SUPPLY("vcore", "uart2"),
2812 REGULATOR_SUPPLY("v-ape", "nmk-ske-keypad.0"),
Bengt Jonsson992b1332012-01-13 16:20:36 +01002813 REGULATOR_SUPPLY("v-hsi", "ste_hsi.0"),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002814};
2815
2816static struct regulator_consumer_supply db8500_vsmps2_consumers[] = {
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002817 REGULATOR_SUPPLY("musb_1v8", "ab8500-usb.0"),
2818 /* AV8100 regulator */
2819 REGULATOR_SUPPLY("hdmi_1v8", "0-0070"),
2820};
2821
2822static struct regulator_consumer_supply db8500_b2r2_mcde_consumers[] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002823 REGULATOR_SUPPLY("vsupply", "b2r2_bus"),
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002824 REGULATOR_SUPPLY("vsupply", "mcde"),
2825};
2826
2827/* SVA MMDSP regulator switch */
2828static struct regulator_consumer_supply db8500_svammdsp_consumers[] = {
2829 REGULATOR_SUPPLY("sva-mmdsp", "cm_control"),
2830};
2831
2832/* SVA pipe regulator switch */
2833static struct regulator_consumer_supply db8500_svapipe_consumers[] = {
2834 REGULATOR_SUPPLY("sva-pipe", "cm_control"),
2835};
2836
2837/* SIA MMDSP regulator switch */
2838static struct regulator_consumer_supply db8500_siammdsp_consumers[] = {
2839 REGULATOR_SUPPLY("sia-mmdsp", "cm_control"),
2840};
2841
2842/* SIA pipe regulator switch */
2843static struct regulator_consumer_supply db8500_siapipe_consumers[] = {
2844 REGULATOR_SUPPLY("sia-pipe", "cm_control"),
2845};
2846
2847static struct regulator_consumer_supply db8500_sga_consumers[] = {
2848 REGULATOR_SUPPLY("v-mali", NULL),
2849};
2850
2851/* ESRAM1 and 2 regulator switch */
2852static struct regulator_consumer_supply db8500_esram12_consumers[] = {
2853 REGULATOR_SUPPLY("esram12", "cm_control"),
2854};
2855
2856/* ESRAM3 and 4 regulator switch */
2857static struct regulator_consumer_supply db8500_esram34_consumers[] = {
2858 REGULATOR_SUPPLY("v-esram34", "mcde"),
2859 REGULATOR_SUPPLY("esram34", "cm_control"),
Bengt Jonsson992b1332012-01-13 16:20:36 +01002860 REGULATOR_SUPPLY("lcla_esram", "dma40.0"),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002861};
2862
2863static struct regulator_init_data db8500_regulators[DB8500_NUM_REGULATORS] = {
2864 [DB8500_REGULATOR_VAPE] = {
2865 .constraints = {
2866 .name = "db8500-vape",
2867 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2868 },
2869 .consumer_supplies = db8500_vape_consumers,
2870 .num_consumer_supplies = ARRAY_SIZE(db8500_vape_consumers),
2871 },
2872 [DB8500_REGULATOR_VARM] = {
2873 .constraints = {
2874 .name = "db8500-varm",
2875 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2876 },
2877 },
2878 [DB8500_REGULATOR_VMODEM] = {
2879 .constraints = {
2880 .name = "db8500-vmodem",
2881 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2882 },
2883 },
2884 [DB8500_REGULATOR_VPLL] = {
2885 .constraints = {
2886 .name = "db8500-vpll",
2887 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2888 },
2889 },
2890 [DB8500_REGULATOR_VSMPS1] = {
2891 .constraints = {
2892 .name = "db8500-vsmps1",
2893 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2894 },
2895 },
2896 [DB8500_REGULATOR_VSMPS2] = {
2897 .constraints = {
2898 .name = "db8500-vsmps2",
2899 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2900 },
2901 .consumer_supplies = db8500_vsmps2_consumers,
2902 .num_consumer_supplies = ARRAY_SIZE(db8500_vsmps2_consumers),
2903 },
2904 [DB8500_REGULATOR_VSMPS3] = {
2905 .constraints = {
2906 .name = "db8500-vsmps3",
2907 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2908 },
2909 },
2910 [DB8500_REGULATOR_VRF1] = {
2911 .constraints = {
2912 .name = "db8500-vrf1",
2913 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2914 },
2915 },
2916 [DB8500_REGULATOR_SWITCH_SVAMMDSP] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002917 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002918 .constraints = {
2919 .name = "db8500-sva-mmdsp",
2920 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2921 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002922 .consumer_supplies = db8500_svammdsp_consumers,
2923 .num_consumer_supplies = ARRAY_SIZE(db8500_svammdsp_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002924 },
2925 [DB8500_REGULATOR_SWITCH_SVAMMDSPRET] = {
2926 .constraints = {
2927 /* "ret" means "retention" */
2928 .name = "db8500-sva-mmdsp-ret",
2929 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2930 },
2931 },
2932 [DB8500_REGULATOR_SWITCH_SVAPIPE] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002933 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002934 .constraints = {
2935 .name = "db8500-sva-pipe",
2936 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2937 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002938 .consumer_supplies = db8500_svapipe_consumers,
2939 .num_consumer_supplies = ARRAY_SIZE(db8500_svapipe_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002940 },
2941 [DB8500_REGULATOR_SWITCH_SIAMMDSP] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002942 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002943 .constraints = {
2944 .name = "db8500-sia-mmdsp",
2945 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2946 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002947 .consumer_supplies = db8500_siammdsp_consumers,
2948 .num_consumer_supplies = ARRAY_SIZE(db8500_siammdsp_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002949 },
2950 [DB8500_REGULATOR_SWITCH_SIAMMDSPRET] = {
2951 .constraints = {
2952 .name = "db8500-sia-mmdsp-ret",
2953 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2954 },
2955 },
2956 [DB8500_REGULATOR_SWITCH_SIAPIPE] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002957 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002958 .constraints = {
2959 .name = "db8500-sia-pipe",
2960 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2961 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002962 .consumer_supplies = db8500_siapipe_consumers,
2963 .num_consumer_supplies = ARRAY_SIZE(db8500_siapipe_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002964 },
2965 [DB8500_REGULATOR_SWITCH_SGA] = {
2966 .supply_regulator = "db8500-vape",
2967 .constraints = {
2968 .name = "db8500-sga",
2969 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2970 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002971 .consumer_supplies = db8500_sga_consumers,
2972 .num_consumer_supplies = ARRAY_SIZE(db8500_sga_consumers),
2973
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002974 },
2975 [DB8500_REGULATOR_SWITCH_B2R2_MCDE] = {
2976 .supply_regulator = "db8500-vape",
2977 .constraints = {
2978 .name = "db8500-b2r2-mcde",
2979 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2980 },
2981 .consumer_supplies = db8500_b2r2_mcde_consumers,
2982 .num_consumer_supplies = ARRAY_SIZE(db8500_b2r2_mcde_consumers),
2983 },
2984 [DB8500_REGULATOR_SWITCH_ESRAM12] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002985 /*
2986 * esram12 is set in retention and supplied by Vsafe when Vape is off,
2987 * no need to hold Vape
2988 */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002989 .constraints = {
2990 .name = "db8500-esram12",
2991 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2992 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002993 .consumer_supplies = db8500_esram12_consumers,
2994 .num_consumer_supplies = ARRAY_SIZE(db8500_esram12_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002995 },
2996 [DB8500_REGULATOR_SWITCH_ESRAM12RET] = {
2997 .constraints = {
2998 .name = "db8500-esram12-ret",
2999 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
3000 },
3001 },
3002 [DB8500_REGULATOR_SWITCH_ESRAM34] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01003003 /*
3004 * esram34 is set in retention and supplied by Vsafe when Vape is off,
3005 * no need to hold Vape
3006 */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02003007 .constraints = {
3008 .name = "db8500-esram34",
3009 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
3010 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02003011 .consumer_supplies = db8500_esram34_consumers,
3012 .num_consumer_supplies = ARRAY_SIZE(db8500_esram34_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02003013 },
3014 [DB8500_REGULATOR_SWITCH_ESRAM34RET] = {
3015 .constraints = {
3016 .name = "db8500-esram34-ret",
3017 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
3018 },
3019 },
3020};
3021
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003022static struct mfd_cell db8500_prcmu_devs[] = {
3023 {
3024 .name = "db8500-prcmu-regulators",
Mattias Wallin1ed78912011-05-27 11:49:43 +02003025 .platform_data = &db8500_regulators,
3026 .pdata_size = sizeof(db8500_regulators),
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003027 },
3028 {
3029 .name = "cpufreq-u8500",
3030 },
3031};
3032
3033/**
3034 * prcmu_fw_init - arch init call for the Linux PRCMU fw init logic
3035 *
3036 */
3037static int __init db8500_prcmu_probe(struct platform_device *pdev)
3038{
3039 int err = 0;
3040
3041 if (ux500_is_svp())
3042 return -ENODEV;
3043
Mattias Nilsson05089012012-01-13 16:20:20 +01003044 init_prcm_registers();
Mattias Nilssond65e12d2011-08-12 10:27:50 +02003045
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003046 /* Clean up the mailbox interrupts after pre-kernel code. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02003047 writel(ALL_MBOX_BITS, PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003048
3049 err = request_threaded_irq(IRQ_DB8500_PRCMU1, prcmu_irq_handler,
3050 prcmu_irq_thread_fn, IRQF_NO_SUSPEND, "prcmu", NULL);
3051 if (err < 0) {
3052 pr_err("prcmu: Failed to allocate IRQ_DB8500_PRCMU1.\n");
3053 err = -EBUSY;
3054 goto no_irq_return;
3055 }
3056
3057 if (cpu_is_u8500v20_or_later())
3058 prcmu_config_esram0_deep_sleep(ESRAM0_DEEP_SLEEP_STATE_RET);
3059
3060 err = mfd_add_devices(&pdev->dev, 0, db8500_prcmu_devs,
3061 ARRAY_SIZE(db8500_prcmu_devs), NULL,
3062 0);
3063
3064 if (err)
3065 pr_err("prcmu: Failed to add subdevices\n");
3066 else
3067 pr_info("DB8500 PRCMU initialized\n");
3068
3069no_irq_return:
3070 return err;
3071}
3072
3073static struct platform_driver db8500_prcmu_driver = {
3074 .driver = {
3075 .name = "db8500-prcmu",
3076 .owner = THIS_MODULE,
3077 },
3078};
3079
3080static int __init db8500_prcmu_init(void)
3081{
3082 return platform_driver_probe(&db8500_prcmu_driver, db8500_prcmu_probe);
3083}
3084
3085arch_initcall(db8500_prcmu_init);
3086
3087MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com>");
3088MODULE_DESCRIPTION("DB8500 PRCM Unit driver");
3089MODULE_LICENSE("GPL v2");