blob: 4e27db841fabfa69a6e097622289a0941d21ee7b [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
Daniel Lezcano9f60d332012-02-28 22:46:07 +0100886/*
Daniel Lezcano9ab492e2012-02-28 22:46:08 +0100887 * This function checks if there are pending interrupt on the
888 * prcmu which has been delegated to monitor the irqs with the
889 * db8500_prcmu_copy_gic_settings function.
890 */
891bool db8500_prcmu_pending_irq(void)
892{
893 u32 it, im;
894 int i;
895
896 for (i = 0; i < PRCMU_GIC_NUMBER_REGS - 1; i++) {
897 it = readl(PRCM_ARMITVAL31TO0 + i * 4);
898 im = readl(PRCM_ARMITMSK31TO0 + i * 4);
899 if (it & im)
900 return true; /* There is a pending interrupt */
901 }
902
903 return false;
904}
905
906/*
Daniel Lezcano9f60d332012-02-28 22:46:07 +0100907 * This function copies the gic SPI settings to the prcmu in order to
908 * monitor them and abort/finish the retention/off sequence or state.
909 */
910int db8500_prcmu_copy_gic_settings(void)
911{
912 u32 er; /* Enable register */
913 void __iomem *dist_base = __io_address(U8500_GIC_DIST_BASE);
914 int i;
915
916 /* We skip the STI and PPI */
917 for (i = 0; i < PRCMU_GIC_NUMBER_REGS - 1; i++) {
918 er = readl_relaxed(dist_base +
919 GIC_DIST_ENABLE_SET + (i + 1) * 4);
920 writel(er, PRCM_ARMITMSK31TO0 + i * 4);
921 }
922
923 return 0;
924}
925
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200926/* This function should only be called while mb0_transfer.lock is held. */
927static void config_wakeups(void)
928{
929 const u8 header[2] = {
930 MB0H_CONFIG_WAKEUPS_EXE,
931 MB0H_CONFIG_WAKEUPS_SLEEP
932 };
933 static u32 last_dbb_events;
934 static u32 last_abb_events;
935 u32 dbb_events;
936 u32 abb_events;
937 unsigned int i;
938
939 dbb_events = mb0_transfer.req.dbb_irqs | mb0_transfer.req.dbb_wakeups;
940 dbb_events |= (WAKEUP_BIT_AC_WAKE_ACK | WAKEUP_BIT_AC_SLEEP_ACK);
941
942 abb_events = mb0_transfer.req.abb_events;
943
944 if ((dbb_events == last_dbb_events) && (abb_events == last_abb_events))
945 return;
946
947 for (i = 0; i < 2; i++) {
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200948 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200949 cpu_relax();
950 writel(dbb_events, (tcdm_base + PRCM_REQ_MB0_WAKEUP_8500));
951 writel(abb_events, (tcdm_base + PRCM_REQ_MB0_WAKEUP_4500));
952 writeb(header[i], (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200953 writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200954 }
955 last_dbb_events = dbb_events;
956 last_abb_events = abb_events;
957}
958
Mattias Nilsson73180f82011-08-12 10:28:10 +0200959void db8500_prcmu_enable_wakeups(u32 wakeups)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200960{
961 unsigned long flags;
962 u32 bits;
963 int i;
964
965 BUG_ON(wakeups != (wakeups & VALID_WAKEUPS));
966
967 for (i = 0, bits = 0; i < NUM_PRCMU_WAKEUP_INDICES; i++) {
968 if (wakeups & BIT(i))
969 bits |= prcmu_wakeup_bit[i];
970 }
971
972 spin_lock_irqsave(&mb0_transfer.lock, flags);
973
974 mb0_transfer.req.dbb_wakeups = bits;
975 config_wakeups();
976
977 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
978}
979
Mattias Nilsson73180f82011-08-12 10:28:10 +0200980void db8500_prcmu_config_abb_event_readout(u32 abb_events)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200981{
982 unsigned long flags;
983
984 spin_lock_irqsave(&mb0_transfer.lock, flags);
985
986 mb0_transfer.req.abb_events = abb_events;
987 config_wakeups();
988
989 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
990}
991
Mattias Nilsson73180f82011-08-12 10:28:10 +0200992void db8500_prcmu_get_abb_event_buffer(void __iomem **buf)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200993{
994 if (readb(tcdm_base + PRCM_ACK_MB0_READ_POINTER) & 1)
995 *buf = (tcdm_base + PRCM_ACK_MB0_WAKEUP_1_4500);
996 else
997 *buf = (tcdm_base + PRCM_ACK_MB0_WAKEUP_0_4500);
998}
999
1000/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02001001 * db8500_prcmu_set_arm_opp - set the appropriate ARM OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001002 * @opp: The new ARM operating point to which transition is to be made
1003 * Returns: 0 on success, non-zero on failure
1004 *
1005 * This function sets the the operating point of the ARM.
1006 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02001007int db8500_prcmu_set_arm_opp(u8 opp)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001008{
1009 int r;
1010
1011 if (opp < ARM_NO_CHANGE || opp > ARM_EXTCLK)
1012 return -EINVAL;
1013
1014 r = 0;
1015
1016 mutex_lock(&mb1_transfer.lock);
1017
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001018 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001019 cpu_relax();
1020
1021 writeb(MB1H_ARM_APE_OPP, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1022 writeb(opp, (tcdm_base + PRCM_REQ_MB1_ARM_OPP));
1023 writeb(APE_NO_CHANGE, (tcdm_base + PRCM_REQ_MB1_APE_OPP));
1024
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001025 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001026 wait_for_completion(&mb1_transfer.work);
1027
1028 if ((mb1_transfer.ack.header != MB1H_ARM_APE_OPP) ||
1029 (mb1_transfer.ack.arm_opp != opp))
1030 r = -EIO;
1031
1032 mutex_unlock(&mb1_transfer.lock);
1033
1034 return r;
1035}
1036
1037/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02001038 * db8500_prcmu_get_arm_opp - get the current ARM OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001039 *
1040 * Returns: the current ARM OPP
1041 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02001042int db8500_prcmu_get_arm_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001043{
1044 return readb(tcdm_base + PRCM_ACK_MB1_CURRENT_ARM_OPP);
1045}
1046
1047/**
Mattias Nilsson05089012012-01-13 16:20:20 +01001048 * db8500_prcmu_get_ddr_opp - get the current DDR OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001049 *
1050 * Returns: the current DDR OPP
1051 */
Mattias Nilsson05089012012-01-13 16:20:20 +01001052int db8500_prcmu_get_ddr_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001053{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001054 return readb(PRCM_DDR_SUBSYS_APE_MINBW);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001055}
1056
1057/**
Mattias Nilsson05089012012-01-13 16:20:20 +01001058 * db8500_set_ddr_opp - set the appropriate DDR OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001059 * @opp: The new DDR operating point to which transition is to be made
1060 * Returns: 0 on success, non-zero on failure
1061 *
1062 * This function sets the operating point of the DDR.
1063 */
Mattias Nilsson05089012012-01-13 16:20:20 +01001064int db8500_prcmu_set_ddr_opp(u8 opp)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001065{
1066 if (opp < DDR_100_OPP || opp > DDR_25_OPP)
1067 return -EINVAL;
1068 /* Changing the DDR OPP can hang the hardware pre-v21 */
1069 if (cpu_is_u8500v20_or_later() && !cpu_is_u8500v20())
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001070 writeb(opp, PRCM_DDR_SUBSYS_APE_MINBW);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001071
1072 return 0;
1073}
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001074
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001075/* Divide the frequency of certain clocks by 2 for APE_50_PARTLY_25_OPP. */
1076static void request_even_slower_clocks(bool enable)
1077{
1078 void __iomem *clock_reg[] = {
1079 PRCM_ACLK_MGT,
1080 PRCM_DMACLK_MGT
1081 };
1082 unsigned long flags;
1083 unsigned int i;
1084
1085 spin_lock_irqsave(&clk_mgt_lock, flags);
1086
1087 /* Grab the HW semaphore. */
1088 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
1089 cpu_relax();
1090
1091 for (i = 0; i < ARRAY_SIZE(clock_reg); i++) {
1092 u32 val;
1093 u32 div;
1094
1095 val = readl(clock_reg[i]);
1096 div = (val & PRCM_CLK_MGT_CLKPLLDIV_MASK);
1097 if (enable) {
1098 if ((div <= 1) || (div > 15)) {
1099 pr_err("prcmu: Bad clock divider %d in %s\n",
1100 div, __func__);
1101 goto unlock_and_return;
1102 }
1103 div <<= 1;
1104 } else {
1105 if (div <= 2)
1106 goto unlock_and_return;
1107 div >>= 1;
1108 }
1109 val = ((val & ~PRCM_CLK_MGT_CLKPLLDIV_MASK) |
1110 (div & PRCM_CLK_MGT_CLKPLLDIV_MASK));
1111 writel(val, clock_reg[i]);
1112 }
1113
1114unlock_and_return:
1115 /* Release the HW semaphore. */
1116 writel(0, PRCM_SEM);
1117
1118 spin_unlock_irqrestore(&clk_mgt_lock, flags);
1119}
1120
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001121/**
Mattias Nilsson05089012012-01-13 16:20:20 +01001122 * db8500_set_ape_opp - set the appropriate APE OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001123 * @opp: The new APE operating point to which transition is to be made
1124 * Returns: 0 on success, non-zero on failure
1125 *
1126 * This function sets the operating point of the APE.
1127 */
Mattias Nilsson05089012012-01-13 16:20:20 +01001128int db8500_prcmu_set_ape_opp(u8 opp)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001129{
1130 int r = 0;
1131
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001132 if (opp == mb1_transfer.ape_opp)
1133 return 0;
1134
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001135 mutex_lock(&mb1_transfer.lock);
1136
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001137 if (mb1_transfer.ape_opp == APE_50_PARTLY_25_OPP)
1138 request_even_slower_clocks(false);
1139
1140 if ((opp != APE_100_OPP) && (mb1_transfer.ape_opp != APE_100_OPP))
1141 goto skip_message;
1142
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001143 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001144 cpu_relax();
1145
1146 writeb(MB1H_ARM_APE_OPP, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1147 writeb(ARM_NO_CHANGE, (tcdm_base + PRCM_REQ_MB1_ARM_OPP));
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001148 writeb(((opp == APE_50_PARTLY_25_OPP) ? APE_50_OPP : opp),
1149 (tcdm_base + PRCM_REQ_MB1_APE_OPP));
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001150
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001151 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001152 wait_for_completion(&mb1_transfer.work);
1153
1154 if ((mb1_transfer.ack.header != MB1H_ARM_APE_OPP) ||
1155 (mb1_transfer.ack.ape_opp != opp))
1156 r = -EIO;
1157
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001158skip_message:
1159 if ((!r && (opp == APE_50_PARTLY_25_OPP)) ||
1160 (r && (mb1_transfer.ape_opp == APE_50_PARTLY_25_OPP)))
1161 request_even_slower_clocks(true);
1162 if (!r)
1163 mb1_transfer.ape_opp = opp;
1164
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001165 mutex_unlock(&mb1_transfer.lock);
1166
1167 return r;
1168}
1169
1170/**
Mattias Nilsson05089012012-01-13 16:20:20 +01001171 * db8500_prcmu_get_ape_opp - get the current APE OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001172 *
1173 * Returns: the current APE OPP
1174 */
Mattias Nilsson05089012012-01-13 16:20:20 +01001175int db8500_prcmu_get_ape_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001176{
1177 return readb(tcdm_base + PRCM_ACK_MB1_CURRENT_APE_OPP);
1178}
1179
1180/**
1181 * prcmu_request_ape_opp_100_voltage - Request APE OPP 100% voltage
1182 * @enable: true to request the higher voltage, false to drop a request.
1183 *
1184 * Calls to this function to enable and disable requests must be balanced.
1185 */
1186int prcmu_request_ape_opp_100_voltage(bool enable)
1187{
1188 int r = 0;
1189 u8 header;
1190 static unsigned int requests;
1191
1192 mutex_lock(&mb1_transfer.lock);
1193
1194 if (enable) {
1195 if (0 != requests++)
1196 goto unlock_and_return;
1197 header = MB1H_REQUEST_APE_OPP_100_VOLT;
1198 } else {
1199 if (requests == 0) {
1200 r = -EIO;
1201 goto unlock_and_return;
1202 } else if (1 != requests--) {
1203 goto unlock_and_return;
1204 }
1205 header = MB1H_RELEASE_APE_OPP_100_VOLT;
1206 }
1207
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001208 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001209 cpu_relax();
1210
1211 writeb(header, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1212
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001213 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001214 wait_for_completion(&mb1_transfer.work);
1215
1216 if ((mb1_transfer.ack.header != header) ||
1217 ((mb1_transfer.ack.ape_voltage_status & BIT(0)) != 0))
1218 r = -EIO;
1219
1220unlock_and_return:
1221 mutex_unlock(&mb1_transfer.lock);
1222
1223 return r;
1224}
1225
1226/**
1227 * prcmu_release_usb_wakeup_state - release the state required by a USB wakeup
1228 *
1229 * This function releases the power state requirements of a USB wakeup.
1230 */
1231int prcmu_release_usb_wakeup_state(void)
1232{
1233 int r = 0;
1234
1235 mutex_lock(&mb1_transfer.lock);
1236
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001237 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001238 cpu_relax();
1239
1240 writeb(MB1H_RELEASE_USB_WAKEUP,
1241 (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1242
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001243 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001244 wait_for_completion(&mb1_transfer.work);
1245
1246 if ((mb1_transfer.ack.header != MB1H_RELEASE_USB_WAKEUP) ||
1247 ((mb1_transfer.ack.ape_voltage_status & BIT(0)) != 0))
1248 r = -EIO;
1249
1250 mutex_unlock(&mb1_transfer.lock);
1251
1252 return r;
1253}
1254
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001255static int request_pll(u8 clock, bool enable)
1256{
1257 int r = 0;
1258
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001259 if (clock == PRCMU_PLLSOC0)
1260 clock = (enable ? PLL_SOC0_ON : PLL_SOC0_OFF);
1261 else if (clock == PRCMU_PLLSOC1)
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001262 clock = (enable ? PLL_SOC1_ON : PLL_SOC1_OFF);
1263 else
1264 return -EINVAL;
1265
1266 mutex_lock(&mb1_transfer.lock);
1267
1268 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
1269 cpu_relax();
1270
1271 writeb(MB1H_PLL_ON_OFF, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1272 writeb(clock, (tcdm_base + PRCM_REQ_MB1_PLL_ON_OFF));
1273
1274 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
1275 wait_for_completion(&mb1_transfer.work);
1276
1277 if (mb1_transfer.ack.header != MB1H_PLL_ON_OFF)
1278 r = -EIO;
1279
1280 mutex_unlock(&mb1_transfer.lock);
1281
1282 return r;
1283}
1284
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001285/**
Bengt Jonsson0b9199e2011-08-12 10:28:25 +02001286 * prcmu_set_hwacc - set the power state of a h/w accelerator
1287 * @hwacc_dev: The hardware accelerator (enum hw_acc_dev).
1288 * @state: The new power state (enum hw_acc_state).
1289 *
1290 * This function sets the power state of a hardware accelerator.
1291 * This function should not be called from interrupt context.
1292 *
1293 * NOTE! Deprecated, to be removed when all users switched over to use the
1294 * regulator framework API.
1295 */
1296int prcmu_set_hwacc(u16 hwacc_dev, u8 state)
1297{
1298 int r = 0;
1299 bool ram_retention = false;
1300 bool enable, enable_ret;
1301
1302 /* check argument */
1303 BUG_ON(hwacc_dev >= NUM_HW_ACC);
1304
1305 /* get state of switches */
1306 enable = hwacc_enabled[hwacc_dev];
1307 enable_ret = hwacc_ret_enabled[hwacc_dev];
1308
1309 /* set flag if retention is possible */
1310 switch (hwacc_dev) {
1311 case HW_ACC_SVAMMDSP:
1312 case HW_ACC_SIAMMDSP:
1313 case HW_ACC_ESRAM1:
1314 case HW_ACC_ESRAM2:
1315 case HW_ACC_ESRAM3:
1316 case HW_ACC_ESRAM4:
1317 ram_retention = true;
1318 break;
1319 }
1320
1321 /* check argument */
1322 BUG_ON(state > HW_ON);
1323 BUG_ON(state == HW_OFF_RAMRET && !ram_retention);
1324
1325 /* modify enable flags */
1326 switch (state) {
1327 case HW_OFF:
1328 enable_ret = false;
1329 enable = false;
1330 break;
1331 case HW_ON:
1332 enable = true;
1333 break;
1334 case HW_OFF_RAMRET:
1335 enable_ret = true;
1336 enable = false;
1337 break;
1338 }
1339
1340 /* get regulator (lazy) */
1341 if (hwacc_regulator[hwacc_dev] == NULL) {
1342 hwacc_regulator[hwacc_dev] = regulator_get(NULL,
1343 hwacc_regulator_name[hwacc_dev]);
1344 if (IS_ERR(hwacc_regulator[hwacc_dev])) {
1345 pr_err("prcmu: failed to get supply %s\n",
1346 hwacc_regulator_name[hwacc_dev]);
1347 r = PTR_ERR(hwacc_regulator[hwacc_dev]);
1348 goto out;
1349 }
1350 }
1351
1352 if (ram_retention) {
1353 if (hwacc_ret_regulator[hwacc_dev] == NULL) {
1354 hwacc_ret_regulator[hwacc_dev] = regulator_get(NULL,
1355 hwacc_ret_regulator_name[hwacc_dev]);
1356 if (IS_ERR(hwacc_ret_regulator[hwacc_dev])) {
1357 pr_err("prcmu: failed to get supply %s\n",
1358 hwacc_ret_regulator_name[hwacc_dev]);
1359 r = PTR_ERR(hwacc_ret_regulator[hwacc_dev]);
1360 goto out;
1361 }
1362 }
1363 }
1364
1365 /* set regulators */
1366 if (ram_retention) {
1367 if (enable_ret && !hwacc_ret_enabled[hwacc_dev]) {
1368 r = regulator_enable(hwacc_ret_regulator[hwacc_dev]);
1369 if (r < 0) {
1370 pr_err("prcmu_set_hwacc: ret enable failed\n");
1371 goto out;
1372 }
1373 hwacc_ret_enabled[hwacc_dev] = true;
1374 }
1375 }
1376
1377 if (enable && !hwacc_enabled[hwacc_dev]) {
1378 r = regulator_enable(hwacc_regulator[hwacc_dev]);
1379 if (r < 0) {
1380 pr_err("prcmu_set_hwacc: enable failed\n");
1381 goto out;
1382 }
1383 hwacc_enabled[hwacc_dev] = true;
1384 }
1385
1386 if (!enable && hwacc_enabled[hwacc_dev]) {
1387 r = regulator_disable(hwacc_regulator[hwacc_dev]);
1388 if (r < 0) {
1389 pr_err("prcmu_set_hwacc: disable failed\n");
1390 goto out;
1391 }
1392 hwacc_enabled[hwacc_dev] = false;
1393 }
1394
1395 if (ram_retention) {
1396 if (!enable_ret && hwacc_ret_enabled[hwacc_dev]) {
1397 r = regulator_disable(hwacc_ret_regulator[hwacc_dev]);
1398 if (r < 0) {
1399 pr_err("prcmu_set_hwacc: ret disable failed\n");
1400 goto out;
1401 }
1402 hwacc_ret_enabled[hwacc_dev] = false;
1403 }
1404 }
1405
1406out:
1407 return r;
1408}
1409EXPORT_SYMBOL(prcmu_set_hwacc);
1410
1411/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02001412 * db8500_prcmu_set_epod - set the state of a EPOD (power domain)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001413 * @epod_id: The EPOD to set
1414 * @epod_state: The new EPOD state
1415 *
1416 * This function sets the state of a EPOD (power domain). It may not be called
1417 * from interrupt context.
1418 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02001419int db8500_prcmu_set_epod(u16 epod_id, u8 epod_state)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001420{
1421 int r = 0;
1422 bool ram_retention = false;
1423 int i;
1424
1425 /* check argument */
1426 BUG_ON(epod_id >= NUM_EPOD_ID);
1427
1428 /* set flag if retention is possible */
1429 switch (epod_id) {
1430 case EPOD_ID_SVAMMDSP:
1431 case EPOD_ID_SIAMMDSP:
1432 case EPOD_ID_ESRAM12:
1433 case EPOD_ID_ESRAM34:
1434 ram_retention = true;
1435 break;
1436 }
1437
1438 /* check argument */
1439 BUG_ON(epod_state > EPOD_STATE_ON);
1440 BUG_ON(epod_state == EPOD_STATE_RAMRET && !ram_retention);
1441
1442 /* get lock */
1443 mutex_lock(&mb2_transfer.lock);
1444
1445 /* wait for mailbox */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001446 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(2))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001447 cpu_relax();
1448
1449 /* fill in mailbox */
1450 for (i = 0; i < NUM_EPOD_ID; i++)
1451 writeb(EPOD_STATE_NO_CHANGE, (tcdm_base + PRCM_REQ_MB2 + i));
1452 writeb(epod_state, (tcdm_base + PRCM_REQ_MB2 + epod_id));
1453
1454 writeb(MB2H_DPS, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB2));
1455
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001456 writel(MBOX_BIT(2), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001457
1458 /*
1459 * The current firmware version does not handle errors correctly,
1460 * and we cannot recover if there is an error.
1461 * This is expected to change when the firmware is updated.
1462 */
1463 if (!wait_for_completion_timeout(&mb2_transfer.work,
1464 msecs_to_jiffies(20000))) {
1465 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
1466 __func__);
1467 r = -EIO;
1468 goto unlock_and_return;
1469 }
1470
1471 if (mb2_transfer.ack.status != HWACC_PWR_ST_OK)
1472 r = -EIO;
1473
1474unlock_and_return:
1475 mutex_unlock(&mb2_transfer.lock);
1476 return r;
1477}
1478
1479/**
1480 * prcmu_configure_auto_pm - Configure autonomous power management.
1481 * @sleep: Configuration for ApSleep.
1482 * @idle: Configuration for ApIdle.
1483 */
1484void prcmu_configure_auto_pm(struct prcmu_auto_pm_config *sleep,
1485 struct prcmu_auto_pm_config *idle)
1486{
1487 u32 sleep_cfg;
1488 u32 idle_cfg;
1489 unsigned long flags;
1490
1491 BUG_ON((sleep == NULL) || (idle == NULL));
1492
1493 sleep_cfg = (sleep->sva_auto_pm_enable & 0xF);
1494 sleep_cfg = ((sleep_cfg << 4) | (sleep->sia_auto_pm_enable & 0xF));
1495 sleep_cfg = ((sleep_cfg << 8) | (sleep->sva_power_on & 0xFF));
1496 sleep_cfg = ((sleep_cfg << 8) | (sleep->sia_power_on & 0xFF));
1497 sleep_cfg = ((sleep_cfg << 4) | (sleep->sva_policy & 0xF));
1498 sleep_cfg = ((sleep_cfg << 4) | (sleep->sia_policy & 0xF));
1499
1500 idle_cfg = (idle->sva_auto_pm_enable & 0xF);
1501 idle_cfg = ((idle_cfg << 4) | (idle->sia_auto_pm_enable & 0xF));
1502 idle_cfg = ((idle_cfg << 8) | (idle->sva_power_on & 0xFF));
1503 idle_cfg = ((idle_cfg << 8) | (idle->sia_power_on & 0xFF));
1504 idle_cfg = ((idle_cfg << 4) | (idle->sva_policy & 0xF));
1505 idle_cfg = ((idle_cfg << 4) | (idle->sia_policy & 0xF));
1506
1507 spin_lock_irqsave(&mb2_transfer.auto_pm_lock, flags);
1508
1509 /*
1510 * The autonomous power management configuration is done through
1511 * fields in mailbox 2, but these fields are only used as shared
1512 * variables - i.e. there is no need to send a message.
1513 */
1514 writel(sleep_cfg, (tcdm_base + PRCM_REQ_MB2_AUTO_PM_SLEEP));
1515 writel(idle_cfg, (tcdm_base + PRCM_REQ_MB2_AUTO_PM_IDLE));
1516
1517 mb2_transfer.auto_pm_enabled =
1518 ((sleep->sva_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1519 (sleep->sia_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1520 (idle->sva_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1521 (idle->sia_auto_pm_enable == PRCMU_AUTO_PM_ON));
1522
1523 spin_unlock_irqrestore(&mb2_transfer.auto_pm_lock, flags);
1524}
1525EXPORT_SYMBOL(prcmu_configure_auto_pm);
1526
1527bool prcmu_is_auto_pm_enabled(void)
1528{
1529 return mb2_transfer.auto_pm_enabled;
1530}
1531
1532static int request_sysclk(bool enable)
1533{
1534 int r;
1535 unsigned long flags;
1536
1537 r = 0;
1538
1539 mutex_lock(&mb3_transfer.sysclk_lock);
1540
1541 spin_lock_irqsave(&mb3_transfer.lock, flags);
1542
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001543 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(3))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001544 cpu_relax();
1545
1546 writeb((enable ? ON : OFF), (tcdm_base + PRCM_REQ_MB3_SYSCLK_MGT));
1547
1548 writeb(MB3H_SYSCLK, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB3));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001549 writel(MBOX_BIT(3), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001550
1551 spin_unlock_irqrestore(&mb3_transfer.lock, flags);
1552
1553 /*
1554 * The firmware only sends an ACK if we want to enable the
1555 * SysClk, and it succeeds.
1556 */
1557 if (enable && !wait_for_completion_timeout(&mb3_transfer.sysclk_work,
1558 msecs_to_jiffies(20000))) {
1559 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
1560 __func__);
1561 r = -EIO;
1562 }
1563
1564 mutex_unlock(&mb3_transfer.sysclk_lock);
1565
1566 return r;
1567}
1568
1569static int request_timclk(bool enable)
1570{
1571 u32 val = (PRCM_TCR_DOZE_MODE | PRCM_TCR_TENSEL_MASK);
1572
1573 if (!enable)
1574 val |= PRCM_TCR_STOP_TIMERS;
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001575 writel(val, PRCM_TCR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001576
1577 return 0;
1578}
1579
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001580static int request_clock(u8 clock, bool enable)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001581{
1582 u32 val;
1583 unsigned long flags;
1584
1585 spin_lock_irqsave(&clk_mgt_lock, flags);
1586
1587 /* Grab the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001588 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001589 cpu_relax();
1590
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001591 val = readl(clk_mgt[clock].reg);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001592 if (enable) {
1593 val |= (PRCM_CLK_MGT_CLKEN | clk_mgt[clock].pllsw);
1594 } else {
1595 clk_mgt[clock].pllsw = (val & PRCM_CLK_MGT_CLKPLLSW_MASK);
1596 val &= ~(PRCM_CLK_MGT_CLKEN | PRCM_CLK_MGT_CLKPLLSW_MASK);
1597 }
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001598 writel(val, clk_mgt[clock].reg);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001599
1600 /* Release the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001601 writel(0, PRCM_SEM);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001602
1603 spin_unlock_irqrestore(&clk_mgt_lock, flags);
1604
1605 return 0;
1606}
1607
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001608static int request_sga_clock(u8 clock, bool enable)
1609{
1610 u32 val;
1611 int ret;
1612
1613 if (enable) {
1614 val = readl(PRCM_CGATING_BYPASS);
1615 writel(val | PRCM_CGATING_BYPASS_ICN2, PRCM_CGATING_BYPASS);
1616 }
1617
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001618 ret = request_clock(clock, enable);
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001619
1620 if (!ret && !enable) {
1621 val = readl(PRCM_CGATING_BYPASS);
1622 writel(val & ~PRCM_CGATING_BYPASS_ICN2, PRCM_CGATING_BYPASS);
1623 }
1624
1625 return ret;
1626}
1627
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001628static inline bool plldsi_locked(void)
1629{
1630 return (readl(PRCM_PLLDSI_LOCKP) &
1631 (PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP10 |
1632 PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP3)) ==
1633 (PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP10 |
1634 PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP3);
1635}
1636
1637static int request_plldsi(bool enable)
1638{
1639 int r = 0;
1640 u32 val;
1641
1642 writel((PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMP |
1643 PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMPI), (enable ?
1644 PRCM_MMIP_LS_CLAMP_CLR : PRCM_MMIP_LS_CLAMP_SET));
1645
1646 val = readl(PRCM_PLLDSI_ENABLE);
1647 if (enable)
1648 val |= PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1649 else
1650 val &= ~PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1651 writel(val, PRCM_PLLDSI_ENABLE);
1652
1653 if (enable) {
1654 unsigned int i;
1655 bool locked = plldsi_locked();
1656
1657 for (i = 10; !locked && (i > 0); --i) {
1658 udelay(100);
1659 locked = plldsi_locked();
1660 }
1661 if (locked) {
1662 writel(PRCM_APE_RESETN_DSIPLL_RESETN,
1663 PRCM_APE_RESETN_SET);
1664 } else {
1665 writel((PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMP |
1666 PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMPI),
1667 PRCM_MMIP_LS_CLAMP_SET);
1668 val &= ~PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1669 writel(val, PRCM_PLLDSI_ENABLE);
1670 r = -EAGAIN;
1671 }
1672 } else {
1673 writel(PRCM_APE_RESETN_DSIPLL_RESETN, PRCM_APE_RESETN_CLR);
1674 }
1675 return r;
1676}
1677
1678static int request_dsiclk(u8 n, bool enable)
1679{
1680 u32 val;
1681
1682 val = readl(PRCM_DSI_PLLOUT_SEL);
1683 val &= ~dsiclk[n].divsel_mask;
1684 val |= ((enable ? dsiclk[n].divsel : PRCM_DSI_PLLOUT_SEL_OFF) <<
1685 dsiclk[n].divsel_shift);
1686 writel(val, PRCM_DSI_PLLOUT_SEL);
1687 return 0;
1688}
1689
1690static int request_dsiescclk(u8 n, bool enable)
1691{
1692 u32 val;
1693
1694 val = readl(PRCM_DSITVCLK_DIV);
1695 enable ? (val |= dsiescclk[n].en) : (val &= ~dsiescclk[n].en);
1696 writel(val, PRCM_DSITVCLK_DIV);
1697 return 0;
1698}
1699
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001700/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02001701 * db8500_prcmu_request_clock() - Request for a clock to be enabled or disabled.
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001702 * @clock: The clock for which the request is made.
1703 * @enable: Whether the clock should be enabled (true) or disabled (false).
1704 *
1705 * This function should only be used by the clock implementation.
1706 * Do not use it from any other place!
1707 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02001708int db8500_prcmu_request_clock(u8 clock, bool enable)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001709{
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001710 if (clock == PRCMU_SGACLK)
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001711 return request_sga_clock(clock, enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001712 else if (clock < PRCMU_NUM_REG_CLOCKS)
1713 return request_clock(clock, enable);
1714 else if (clock == PRCMU_TIMCLK)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001715 return request_timclk(enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001716 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1717 return request_dsiclk((clock - PRCMU_DSI0CLK), enable);
1718 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1719 return request_dsiescclk((clock - PRCMU_DSI0ESCCLK), enable);
1720 else if (clock == PRCMU_PLLDSI)
1721 return request_plldsi(enable);
1722 else if (clock == PRCMU_SYSCLK)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001723 return request_sysclk(enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001724 else if ((clock == PRCMU_PLLSOC0) || (clock == PRCMU_PLLSOC1))
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001725 return request_pll(clock, enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001726 else
1727 return -EINVAL;
1728}
1729
1730static unsigned long pll_rate(void __iomem *reg, unsigned long src_rate,
1731 int branch)
1732{
1733 u64 rate;
1734 u32 val;
1735 u32 d;
1736 u32 div = 1;
1737
1738 val = readl(reg);
1739
1740 rate = src_rate;
1741 rate *= ((val & PRCM_PLL_FREQ_D_MASK) >> PRCM_PLL_FREQ_D_SHIFT);
1742
1743 d = ((val & PRCM_PLL_FREQ_N_MASK) >> PRCM_PLL_FREQ_N_SHIFT);
1744 if (d > 1)
1745 div *= d;
1746
1747 d = ((val & PRCM_PLL_FREQ_R_MASK) >> PRCM_PLL_FREQ_R_SHIFT);
1748 if (d > 1)
1749 div *= d;
1750
1751 if (val & PRCM_PLL_FREQ_SELDIV2)
1752 div *= 2;
1753
1754 if ((branch == PLL_FIX) || ((branch == PLL_DIV) &&
1755 (val & PRCM_PLL_FREQ_DIV2EN) &&
1756 ((reg == PRCM_PLLSOC0_FREQ) ||
1757 (reg == PRCM_PLLDDR_FREQ))))
1758 div *= 2;
1759
1760 (void)do_div(rate, div);
1761
1762 return (unsigned long)rate;
1763}
1764
1765#define ROOT_CLOCK_RATE 38400000
1766
1767static unsigned long clock_rate(u8 clock)
1768{
1769 u32 val;
1770 u32 pllsw;
1771 unsigned long rate = ROOT_CLOCK_RATE;
1772
1773 val = readl(clk_mgt[clock].reg);
1774
1775 if (val & PRCM_CLK_MGT_CLK38) {
1776 if (clk_mgt[clock].clk38div && (val & PRCM_CLK_MGT_CLK38DIV))
1777 rate /= 2;
1778 return rate;
Linus Walleije62ccf32011-10-10 12:14:14 +02001779 }
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001780
1781 val |= clk_mgt[clock].pllsw;
1782 pllsw = (val & PRCM_CLK_MGT_CLKPLLSW_MASK);
1783
1784 if (pllsw == PRCM_CLK_MGT_CLKPLLSW_SOC0)
1785 rate = pll_rate(PRCM_PLLSOC0_FREQ, rate, clk_mgt[clock].branch);
1786 else if (pllsw == PRCM_CLK_MGT_CLKPLLSW_SOC1)
1787 rate = pll_rate(PRCM_PLLSOC1_FREQ, rate, clk_mgt[clock].branch);
1788 else if (pllsw == PRCM_CLK_MGT_CLKPLLSW_DDR)
1789 rate = pll_rate(PRCM_PLLDDR_FREQ, rate, clk_mgt[clock].branch);
1790 else
1791 return 0;
1792
1793 if ((clock == PRCMU_SGACLK) &&
1794 (val & PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN)) {
1795 u64 r = (rate * 10);
1796
1797 (void)do_div(r, 25);
1798 return (unsigned long)r;
1799 }
1800 val &= PRCM_CLK_MGT_CLKPLLDIV_MASK;
1801 if (val)
1802 return rate / val;
1803 else
1804 return 0;
1805}
1806
1807static unsigned long dsiclk_rate(u8 n)
1808{
1809 u32 divsel;
1810 u32 div = 1;
1811
1812 divsel = readl(PRCM_DSI_PLLOUT_SEL);
1813 divsel = ((divsel & dsiclk[n].divsel_mask) >> dsiclk[n].divsel_shift);
1814
1815 if (divsel == PRCM_DSI_PLLOUT_SEL_OFF)
1816 divsel = dsiclk[n].divsel;
1817
1818 switch (divsel) {
1819 case PRCM_DSI_PLLOUT_SEL_PHI_4:
1820 div *= 2;
1821 case PRCM_DSI_PLLOUT_SEL_PHI_2:
1822 div *= 2;
1823 case PRCM_DSI_PLLOUT_SEL_PHI:
1824 return pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1825 PLL_RAW) / div;
1826 default:
1827 return 0;
1828 }
1829}
1830
1831static unsigned long dsiescclk_rate(u8 n)
1832{
1833 u32 div;
1834
1835 div = readl(PRCM_DSITVCLK_DIV);
1836 div = ((div & dsiescclk[n].div_mask) >> (dsiescclk[n].div_shift));
1837 return clock_rate(PRCMU_TVCLK) / max((u32)1, div);
1838}
1839
1840unsigned long prcmu_clock_rate(u8 clock)
1841{
Linus Walleije62ccf32011-10-10 12:14:14 +02001842 if (clock < PRCMU_NUM_REG_CLOCKS)
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001843 return clock_rate(clock);
1844 else if (clock == PRCMU_TIMCLK)
1845 return ROOT_CLOCK_RATE / 16;
1846 else if (clock == PRCMU_SYSCLK)
1847 return ROOT_CLOCK_RATE;
1848 else if (clock == PRCMU_PLLSOC0)
1849 return pll_rate(PRCM_PLLSOC0_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
1850 else if (clock == PRCMU_PLLSOC1)
1851 return pll_rate(PRCM_PLLSOC1_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
1852 else if (clock == PRCMU_PLLDDR)
1853 return pll_rate(PRCM_PLLDDR_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
1854 else if (clock == PRCMU_PLLDSI)
1855 return pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1856 PLL_RAW);
1857 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1858 return dsiclk_rate(clock - PRCMU_DSI0CLK);
1859 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1860 return dsiescclk_rate(clock - PRCMU_DSI0ESCCLK);
1861 else
1862 return 0;
1863}
1864
1865static unsigned long clock_source_rate(u32 clk_mgt_val, int branch)
1866{
1867 if (clk_mgt_val & PRCM_CLK_MGT_CLK38)
1868 return ROOT_CLOCK_RATE;
1869 clk_mgt_val &= PRCM_CLK_MGT_CLKPLLSW_MASK;
1870 if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_SOC0)
1871 return pll_rate(PRCM_PLLSOC0_FREQ, ROOT_CLOCK_RATE, branch);
1872 else if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_SOC1)
1873 return pll_rate(PRCM_PLLSOC1_FREQ, ROOT_CLOCK_RATE, branch);
1874 else if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_DDR)
1875 return pll_rate(PRCM_PLLDDR_FREQ, ROOT_CLOCK_RATE, branch);
1876 else
1877 return 0;
1878}
1879
1880static u32 clock_divider(unsigned long src_rate, unsigned long rate)
1881{
1882 u32 div;
1883
1884 div = (src_rate / rate);
1885 if (div == 0)
1886 return 1;
1887 if (rate < (src_rate / div))
1888 div++;
1889 return div;
1890}
1891
1892static long round_clock_rate(u8 clock, unsigned long rate)
1893{
1894 u32 val;
1895 u32 div;
1896 unsigned long src_rate;
1897 long rounded_rate;
1898
1899 val = readl(clk_mgt[clock].reg);
1900 src_rate = clock_source_rate((val | clk_mgt[clock].pllsw),
1901 clk_mgt[clock].branch);
1902 div = clock_divider(src_rate, rate);
1903 if (val & PRCM_CLK_MGT_CLK38) {
1904 if (clk_mgt[clock].clk38div) {
1905 if (div > 2)
1906 div = 2;
1907 } else {
1908 div = 1;
1909 }
1910 } else if ((clock == PRCMU_SGACLK) && (div == 3)) {
1911 u64 r = (src_rate * 10);
1912
1913 (void)do_div(r, 25);
1914 if (r <= rate)
1915 return (unsigned long)r;
1916 }
1917 rounded_rate = (src_rate / min(div, (u32)31));
1918
1919 return rounded_rate;
1920}
1921
1922#define MIN_PLL_VCO_RATE 600000000ULL
1923#define MAX_PLL_VCO_RATE 1680640000ULL
1924
1925static long round_plldsi_rate(unsigned long rate)
1926{
1927 long rounded_rate = 0;
1928 unsigned long src_rate;
1929 unsigned long rem;
1930 u32 r;
1931
1932 src_rate = clock_rate(PRCMU_HDMICLK);
1933 rem = rate;
1934
1935 for (r = 7; (rem > 0) && (r > 0); r--) {
1936 u64 d;
1937
1938 d = (r * rate);
1939 (void)do_div(d, src_rate);
1940 if (d < 6)
1941 d = 6;
1942 else if (d > 255)
1943 d = 255;
1944 d *= src_rate;
1945 if (((2 * d) < (r * MIN_PLL_VCO_RATE)) ||
1946 ((r * MAX_PLL_VCO_RATE) < (2 * d)))
1947 continue;
1948 (void)do_div(d, r);
1949 if (rate < d) {
1950 if (rounded_rate == 0)
1951 rounded_rate = (long)d;
1952 break;
1953 }
1954 if ((rate - d) < rem) {
1955 rem = (rate - d);
1956 rounded_rate = (long)d;
1957 }
1958 }
1959 return rounded_rate;
1960}
1961
1962static long round_dsiclk_rate(unsigned long rate)
1963{
1964 u32 div;
1965 unsigned long src_rate;
1966 long rounded_rate;
1967
1968 src_rate = pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1969 PLL_RAW);
1970 div = clock_divider(src_rate, rate);
1971 rounded_rate = (src_rate / ((div > 2) ? 4 : div));
1972
1973 return rounded_rate;
1974}
1975
1976static long round_dsiescclk_rate(unsigned long rate)
1977{
1978 u32 div;
1979 unsigned long src_rate;
1980 long rounded_rate;
1981
1982 src_rate = clock_rate(PRCMU_TVCLK);
1983 div = clock_divider(src_rate, rate);
1984 rounded_rate = (src_rate / min(div, (u32)255));
1985
1986 return rounded_rate;
1987}
1988
1989long prcmu_round_clock_rate(u8 clock, unsigned long rate)
1990{
1991 if (clock < PRCMU_NUM_REG_CLOCKS)
1992 return round_clock_rate(clock, rate);
1993 else if (clock == PRCMU_PLLDSI)
1994 return round_plldsi_rate(rate);
1995 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1996 return round_dsiclk_rate(rate);
1997 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1998 return round_dsiescclk_rate(rate);
1999 else
2000 return (long)prcmu_clock_rate(clock);
2001}
2002
2003static void set_clock_rate(u8 clock, unsigned long rate)
2004{
2005 u32 val;
2006 u32 div;
2007 unsigned long src_rate;
2008 unsigned long flags;
2009
2010 spin_lock_irqsave(&clk_mgt_lock, flags);
2011
2012 /* Grab the HW semaphore. */
2013 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
2014 cpu_relax();
2015
2016 val = readl(clk_mgt[clock].reg);
2017 src_rate = clock_source_rate((val | clk_mgt[clock].pllsw),
2018 clk_mgt[clock].branch);
2019 div = clock_divider(src_rate, rate);
2020 if (val & PRCM_CLK_MGT_CLK38) {
2021 if (clk_mgt[clock].clk38div) {
2022 if (div > 1)
2023 val |= PRCM_CLK_MGT_CLK38DIV;
2024 else
2025 val &= ~PRCM_CLK_MGT_CLK38DIV;
2026 }
2027 } else if (clock == PRCMU_SGACLK) {
2028 val &= ~(PRCM_CLK_MGT_CLKPLLDIV_MASK |
2029 PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN);
2030 if (div == 3) {
2031 u64 r = (src_rate * 10);
2032
2033 (void)do_div(r, 25);
2034 if (r <= rate) {
2035 val |= PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN;
2036 div = 0;
2037 }
2038 }
2039 val |= min(div, (u32)31);
2040 } else {
2041 val &= ~PRCM_CLK_MGT_CLKPLLDIV_MASK;
2042 val |= min(div, (u32)31);
2043 }
2044 writel(val, clk_mgt[clock].reg);
2045
2046 /* Release the HW semaphore. */
2047 writel(0, PRCM_SEM);
2048
2049 spin_unlock_irqrestore(&clk_mgt_lock, flags);
2050}
2051
2052static int set_plldsi_rate(unsigned long rate)
2053{
2054 unsigned long src_rate;
2055 unsigned long rem;
2056 u32 pll_freq = 0;
2057 u32 r;
2058
2059 src_rate = clock_rate(PRCMU_HDMICLK);
2060 rem = rate;
2061
2062 for (r = 7; (rem > 0) && (r > 0); r--) {
2063 u64 d;
2064 u64 hwrate;
2065
2066 d = (r * rate);
2067 (void)do_div(d, src_rate);
2068 if (d < 6)
2069 d = 6;
2070 else if (d > 255)
2071 d = 255;
2072 hwrate = (d * src_rate);
2073 if (((2 * hwrate) < (r * MIN_PLL_VCO_RATE)) ||
2074 ((r * MAX_PLL_VCO_RATE) < (2 * hwrate)))
2075 continue;
2076 (void)do_div(hwrate, r);
2077 if (rate < hwrate) {
2078 if (pll_freq == 0)
2079 pll_freq = (((u32)d << PRCM_PLL_FREQ_D_SHIFT) |
2080 (r << PRCM_PLL_FREQ_R_SHIFT));
2081 break;
2082 }
2083 if ((rate - hwrate) < rem) {
2084 rem = (rate - hwrate);
2085 pll_freq = (((u32)d << PRCM_PLL_FREQ_D_SHIFT) |
2086 (r << PRCM_PLL_FREQ_R_SHIFT));
2087 }
2088 }
2089 if (pll_freq == 0)
2090 return -EINVAL;
2091
2092 pll_freq |= (1 << PRCM_PLL_FREQ_N_SHIFT);
2093 writel(pll_freq, PRCM_PLLDSI_FREQ);
2094
2095 return 0;
2096}
2097
2098static void set_dsiclk_rate(u8 n, unsigned long rate)
2099{
2100 u32 val;
2101 u32 div;
2102
2103 div = clock_divider(pll_rate(PRCM_PLLDSI_FREQ,
2104 clock_rate(PRCMU_HDMICLK), PLL_RAW), rate);
2105
2106 dsiclk[n].divsel = (div == 1) ? PRCM_DSI_PLLOUT_SEL_PHI :
2107 (div == 2) ? PRCM_DSI_PLLOUT_SEL_PHI_2 :
2108 /* else */ PRCM_DSI_PLLOUT_SEL_PHI_4;
2109
2110 val = readl(PRCM_DSI_PLLOUT_SEL);
2111 val &= ~dsiclk[n].divsel_mask;
2112 val |= (dsiclk[n].divsel << dsiclk[n].divsel_shift);
2113 writel(val, PRCM_DSI_PLLOUT_SEL);
2114}
2115
2116static void set_dsiescclk_rate(u8 n, unsigned long rate)
2117{
2118 u32 val;
2119 u32 div;
2120
2121 div = clock_divider(clock_rate(PRCMU_TVCLK), rate);
2122 val = readl(PRCM_DSITVCLK_DIV);
2123 val &= ~dsiescclk[n].div_mask;
2124 val |= (min(div, (u32)255) << dsiescclk[n].div_shift);
2125 writel(val, PRCM_DSITVCLK_DIV);
2126}
2127
2128int prcmu_set_clock_rate(u8 clock, unsigned long rate)
2129{
2130 if (clock < PRCMU_NUM_REG_CLOCKS)
2131 set_clock_rate(clock, rate);
2132 else if (clock == PRCMU_PLLDSI)
2133 return set_plldsi_rate(rate);
2134 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
2135 set_dsiclk_rate((clock - PRCMU_DSI0CLK), rate);
2136 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
2137 set_dsiescclk_rate((clock - PRCMU_DSI0ESCCLK), rate);
2138 return 0;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002139}
2140
Mattias Nilsson73180f82011-08-12 10:28:10 +02002141int db8500_prcmu_config_esram0_deep_sleep(u8 state)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002142{
2143 if ((state > ESRAM0_DEEP_SLEEP_STATE_RET) ||
2144 (state < ESRAM0_DEEP_SLEEP_STATE_OFF))
2145 return -EINVAL;
2146
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(MB4H_MEM_ST, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2153 writeb(((DDR_PWR_STATE_OFFHIGHLAT << 4) | DDR_PWR_STATE_ON),
2154 (tcdm_base + PRCM_REQ_MB4_DDR_ST_AP_SLEEP_IDLE));
2155 writeb(DDR_PWR_STATE_ON,
2156 (tcdm_base + PRCM_REQ_MB4_DDR_ST_AP_DEEP_IDLE));
2157 writeb(state, (tcdm_base + PRCM_REQ_MB4_ESRAM0_ST));
2158
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002159 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002160 wait_for_completion(&mb4_transfer.work);
2161
2162 mutex_unlock(&mb4_transfer.lock);
2163
2164 return 0;
2165}
2166
Mattias Nilsson05089012012-01-13 16:20:20 +01002167int db8500_prcmu_config_hotdog(u8 threshold)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002168{
2169 mutex_lock(&mb4_transfer.lock);
2170
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002171 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002172 cpu_relax();
2173
2174 writeb(threshold, (tcdm_base + PRCM_REQ_MB4_HOTDOG_THRESHOLD));
2175 writeb(MB4H_HOTDOG, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2176
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002177 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002178 wait_for_completion(&mb4_transfer.work);
2179
2180 mutex_unlock(&mb4_transfer.lock);
2181
2182 return 0;
2183}
2184
Mattias Nilsson05089012012-01-13 16:20:20 +01002185int db8500_prcmu_config_hotmon(u8 low, u8 high)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002186{
2187 mutex_lock(&mb4_transfer.lock);
2188
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002189 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002190 cpu_relax();
2191
2192 writeb(low, (tcdm_base + PRCM_REQ_MB4_HOTMON_LOW));
2193 writeb(high, (tcdm_base + PRCM_REQ_MB4_HOTMON_HIGH));
2194 writeb((HOTMON_CONFIG_LOW | HOTMON_CONFIG_HIGH),
2195 (tcdm_base + PRCM_REQ_MB4_HOTMON_CONFIG));
2196 writeb(MB4H_HOTMON, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2197
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002198 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002199 wait_for_completion(&mb4_transfer.work);
2200
2201 mutex_unlock(&mb4_transfer.lock);
2202
2203 return 0;
2204}
2205
2206static int config_hot_period(u16 val)
2207{
2208 mutex_lock(&mb4_transfer.lock);
2209
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002210 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002211 cpu_relax();
2212
2213 writew(val, (tcdm_base + PRCM_REQ_MB4_HOT_PERIOD));
2214 writeb(MB4H_HOT_PERIOD, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2215
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002216 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002217 wait_for_completion(&mb4_transfer.work);
2218
2219 mutex_unlock(&mb4_transfer.lock);
2220
2221 return 0;
2222}
2223
Mattias Nilsson05089012012-01-13 16:20:20 +01002224int db8500_prcmu_start_temp_sense(u16 cycles32k)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002225{
2226 if (cycles32k == 0xFFFF)
2227 return -EINVAL;
2228
2229 return config_hot_period(cycles32k);
2230}
2231
Mattias Nilsson05089012012-01-13 16:20:20 +01002232int db8500_prcmu_stop_temp_sense(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002233{
2234 return config_hot_period(0xFFFF);
2235}
2236
Jonas Aberg84165b82011-08-12 10:28:33 +02002237static int prcmu_a9wdog(u8 cmd, u8 d0, u8 d1, u8 d2, u8 d3)
2238{
2239
2240 mutex_lock(&mb4_transfer.lock);
2241
2242 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
2243 cpu_relax();
2244
2245 writeb(d0, (tcdm_base + PRCM_REQ_MB4_A9WDOG_0));
2246 writeb(d1, (tcdm_base + PRCM_REQ_MB4_A9WDOG_1));
2247 writeb(d2, (tcdm_base + PRCM_REQ_MB4_A9WDOG_2));
2248 writeb(d3, (tcdm_base + PRCM_REQ_MB4_A9WDOG_3));
2249
2250 writeb(cmd, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2251
2252 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
2253 wait_for_completion(&mb4_transfer.work);
2254
2255 mutex_unlock(&mb4_transfer.lock);
2256
2257 return 0;
2258
2259}
2260
Mattias Nilsson05089012012-01-13 16:20:20 +01002261int db8500_prcmu_config_a9wdog(u8 num, bool sleep_auto_off)
Jonas Aberg84165b82011-08-12 10:28:33 +02002262{
2263 BUG_ON(num == 0 || num > 0xf);
2264 return prcmu_a9wdog(MB4H_A9WDOG_CONF, num, 0, 0,
2265 sleep_auto_off ? A9WDOG_AUTO_OFF_EN :
2266 A9WDOG_AUTO_OFF_DIS);
2267}
2268
Mattias Nilsson05089012012-01-13 16:20:20 +01002269int db8500_prcmu_enable_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002270{
2271 return prcmu_a9wdog(MB4H_A9WDOG_EN, id, 0, 0, 0);
2272}
2273
Mattias Nilsson05089012012-01-13 16:20:20 +01002274int db8500_prcmu_disable_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002275{
2276 return prcmu_a9wdog(MB4H_A9WDOG_DIS, id, 0, 0, 0);
2277}
2278
Mattias Nilsson05089012012-01-13 16:20:20 +01002279int db8500_prcmu_kick_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002280{
2281 return prcmu_a9wdog(MB4H_A9WDOG_KICK, id, 0, 0, 0);
2282}
2283
2284/*
2285 * timeout is 28 bit, in ms.
2286 */
Mattias Nilsson05089012012-01-13 16:20:20 +01002287int db8500_prcmu_load_a9wdog(u8 id, u32 timeout)
Jonas Aberg84165b82011-08-12 10:28:33 +02002288{
Jonas Aberg84165b82011-08-12 10:28:33 +02002289 return prcmu_a9wdog(MB4H_A9WDOG_LOAD,
2290 (id & A9WDOG_ID_MASK) |
2291 /*
2292 * Put the lowest 28 bits of timeout at
2293 * offset 4. Four first bits are used for id.
2294 */
2295 (u8)((timeout << 4) & 0xf0),
2296 (u8)((timeout >> 4) & 0xff),
2297 (u8)((timeout >> 12) & 0xff),
2298 (u8)((timeout >> 20) & 0xff));
2299}
2300
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002301/**
Linus Walleije3726fc2010-08-19 12:36:01 +01002302 * prcmu_abb_read() - Read register value(s) from the ABB.
2303 * @slave: The I2C slave address.
2304 * @reg: The (start) register address.
2305 * @value: The read out value(s).
2306 * @size: The number of registers to read.
2307 *
2308 * Reads register value(s) from the ABB.
2309 * @size has to be 1 for the current firmware version.
2310 */
2311int prcmu_abb_read(u8 slave, u8 reg, u8 *value, u8 size)
2312{
2313 int r;
2314
2315 if (size != 1)
2316 return -EINVAL;
2317
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002318 mutex_lock(&mb5_transfer.lock);
Linus Walleije3726fc2010-08-19 12:36:01 +01002319
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002320 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(5))
Linus Walleije3726fc2010-08-19 12:36:01 +01002321 cpu_relax();
2322
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002323 writeb(PRCMU_I2C_READ(slave), (tcdm_base + PRCM_REQ_MB5_I2C_SLAVE_OP));
2324 writeb(PRCMU_I2C_STOP_EN, (tcdm_base + PRCM_REQ_MB5_I2C_HW_BITS));
2325 writeb(reg, (tcdm_base + PRCM_REQ_MB5_I2C_REG));
2326 writeb(0, (tcdm_base + PRCM_REQ_MB5_I2C_VAL));
Linus Walleije3726fc2010-08-19 12:36:01 +01002327
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002328 writel(MBOX_BIT(5), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002329
Linus Walleije3726fc2010-08-19 12:36:01 +01002330 if (!wait_for_completion_timeout(&mb5_transfer.work,
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002331 msecs_to_jiffies(20000))) {
2332 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
2333 __func__);
Linus Walleije3726fc2010-08-19 12:36:01 +01002334 r = -EIO;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002335 } else {
2336 r = ((mb5_transfer.ack.status == I2C_RD_OK) ? 0 : -EIO);
Linus Walleije3726fc2010-08-19 12:36:01 +01002337 }
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002338
Linus Walleije3726fc2010-08-19 12:36:01 +01002339 if (!r)
2340 *value = mb5_transfer.ack.value;
2341
Linus Walleije3726fc2010-08-19 12:36:01 +01002342 mutex_unlock(&mb5_transfer.lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002343
Linus Walleije3726fc2010-08-19 12:36:01 +01002344 return r;
2345}
Linus Walleije3726fc2010-08-19 12:36:01 +01002346
2347/**
2348 * prcmu_abb_write() - Write register value(s) to the ABB.
2349 * @slave: The I2C slave address.
2350 * @reg: The (start) register address.
2351 * @value: The value(s) to write.
2352 * @size: The number of registers to write.
2353 *
2354 * Reads register value(s) from the ABB.
2355 * @size has to be 1 for the current firmware version.
2356 */
2357int prcmu_abb_write(u8 slave, u8 reg, u8 *value, u8 size)
2358{
2359 int r;
2360
2361 if (size != 1)
2362 return -EINVAL;
2363
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002364 mutex_lock(&mb5_transfer.lock);
Linus Walleije3726fc2010-08-19 12:36:01 +01002365
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002366 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(5))
Linus Walleije3726fc2010-08-19 12:36:01 +01002367 cpu_relax();
2368
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002369 writeb(PRCMU_I2C_WRITE(slave), (tcdm_base + PRCM_REQ_MB5_I2C_SLAVE_OP));
2370 writeb(PRCMU_I2C_STOP_EN, (tcdm_base + PRCM_REQ_MB5_I2C_HW_BITS));
2371 writeb(reg, (tcdm_base + PRCM_REQ_MB5_I2C_REG));
2372 writeb(*value, (tcdm_base + PRCM_REQ_MB5_I2C_VAL));
Linus Walleije3726fc2010-08-19 12:36:01 +01002373
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002374 writel(MBOX_BIT(5), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002375
Linus Walleije3726fc2010-08-19 12:36:01 +01002376 if (!wait_for_completion_timeout(&mb5_transfer.work,
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002377 msecs_to_jiffies(20000))) {
2378 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
2379 __func__);
Linus Walleije3726fc2010-08-19 12:36:01 +01002380 r = -EIO;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002381 } else {
2382 r = ((mb5_transfer.ack.status == I2C_WR_OK) ? 0 : -EIO);
Linus Walleije3726fc2010-08-19 12:36:01 +01002383 }
Linus Walleije3726fc2010-08-19 12:36:01 +01002384
Linus Walleije3726fc2010-08-19 12:36:01 +01002385 mutex_unlock(&mb5_transfer.lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002386
Linus Walleije3726fc2010-08-19 12:36:01 +01002387 return r;
2388}
Linus Walleije3726fc2010-08-19 12:36:01 +01002389
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002390/**
2391 * prcmu_ac_wake_req - should be called whenever ARM wants to wakeup Modem
2392 */
2393void prcmu_ac_wake_req(void)
Martin Perssone0befb22010-12-08 15:13:28 +01002394{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002395 u32 val;
Mattias Nilssond6e30022011-08-12 10:28:43 +02002396 u32 status;
Martin Perssone0befb22010-12-08 15:13:28 +01002397
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002398 mutex_lock(&mb0_transfer.ac_wake_lock);
Martin Perssone0befb22010-12-08 15:13:28 +01002399
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002400 val = readl(PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002401 if (val & PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ)
2402 goto unlock_and_return;
2403
2404 atomic_set(&ac_wake_req_state, 1);
2405
Mattias Nilssond6e30022011-08-12 10:28:43 +02002406retry:
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002407 writel((val | PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ), PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002408
2409 if (!wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
Mattias Nilssond6e30022011-08-12 10:28:43 +02002410 msecs_to_jiffies(5000))) {
Linus Walleij57265bc2011-10-10 13:04:44 +02002411 pr_crit("prcmu: %s timed out (5 s) waiting for a reply.\n",
Mattias Nilssond6e30022011-08-12 10:28:43 +02002412 __func__);
2413 goto unlock_and_return;
2414 }
2415
2416 /*
2417 * The modem can generate an AC_WAKE_ACK, and then still go to sleep.
2418 * As a workaround, we wait, and then check that the modem is indeed
2419 * awake (in terms of the value of the PRCM_MOD_AWAKE_STATUS
2420 * register, which may not be the whole truth).
2421 */
2422 udelay(400);
2423 status = (readl(PRCM_MOD_AWAKE_STATUS) & BITS(0, 2));
2424 if (status != (PRCM_MOD_AWAKE_STATUS_PRCM_MOD_AAPD_AWAKE |
2425 PRCM_MOD_AWAKE_STATUS_PRCM_MOD_COREPD_AWAKE)) {
2426 pr_err("prcmu: %s received ack, but modem not awake (0x%X).\n",
2427 __func__, status);
2428 udelay(1200);
2429 writel(val, PRCM_HOSTACCESS_REQ);
2430 if (wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
2431 msecs_to_jiffies(5000)))
2432 goto retry;
Linus Walleij57265bc2011-10-10 13:04:44 +02002433 pr_crit("prcmu: %s timed out (5 s) waiting for AC_SLEEP_ACK.\n",
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002434 __func__);
2435 }
2436
2437unlock_and_return:
2438 mutex_unlock(&mb0_transfer.ac_wake_lock);
2439}
2440
2441/**
2442 * prcmu_ac_sleep_req - called when ARM no longer needs to talk to modem
2443 */
2444void prcmu_ac_sleep_req()
2445{
2446 u32 val;
2447
2448 mutex_lock(&mb0_transfer.ac_wake_lock);
2449
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002450 val = readl(PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002451 if (!(val & PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ))
2452 goto unlock_and_return;
2453
2454 writel((val & ~PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ),
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002455 PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002456
2457 if (!wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
Mattias Nilssond6e30022011-08-12 10:28:43 +02002458 msecs_to_jiffies(5000))) {
Linus Walleij57265bc2011-10-10 13:04:44 +02002459 pr_crit("prcmu: %s timed out (5 s) waiting for a reply.\n",
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002460 __func__);
2461 }
2462
2463 atomic_set(&ac_wake_req_state, 0);
2464
2465unlock_and_return:
2466 mutex_unlock(&mb0_transfer.ac_wake_lock);
2467}
2468
Mattias Nilsson73180f82011-08-12 10:28:10 +02002469bool db8500_prcmu_is_ac_wake_requested(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002470{
2471 return (atomic_read(&ac_wake_req_state) != 0);
2472}
2473
2474/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02002475 * db8500_prcmu_system_reset - System reset
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002476 *
Mattias Nilsson73180f82011-08-12 10:28:10 +02002477 * Saves the reset reason code and then sets the APE_SOFTRST register which
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002478 * fires interrupt to fw
2479 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02002480void db8500_prcmu_system_reset(u16 reset_code)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002481{
2482 writew(reset_code, (tcdm_base + PRCM_SW_RST_REASON));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002483 writel(1, PRCM_APE_SOFTRST);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002484}
2485
2486/**
Sebastian Rasmussen597045d2011-08-12 10:28:53 +02002487 * db8500_prcmu_get_reset_code - Retrieve SW reset reason code
2488 *
2489 * Retrieves the reset reason code stored by prcmu_system_reset() before
2490 * last restart.
2491 */
2492u16 db8500_prcmu_get_reset_code(void)
2493{
2494 return readw(tcdm_base + PRCM_SW_RST_REASON);
2495}
2496
2497/**
Mattias Nilsson05089012012-01-13 16:20:20 +01002498 * db8500_prcmu_reset_modem - ask the PRCMU to reset modem
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002499 */
Mattias Nilsson05089012012-01-13 16:20:20 +01002500void db8500_prcmu_modem_reset(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002501{
Martin Perssone0befb22010-12-08 15:13:28 +01002502 mutex_lock(&mb1_transfer.lock);
2503
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002504 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Martin Perssone0befb22010-12-08 15:13:28 +01002505 cpu_relax();
2506
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002507 writeb(MB1H_RESET_MODEM, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002508 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Martin Perssone0befb22010-12-08 15:13:28 +01002509 wait_for_completion(&mb1_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002510
2511 /*
2512 * No need to check return from PRCMU as modem should go in reset state
2513 * This state is already managed by upper layer
2514 */
Martin Perssone0befb22010-12-08 15:13:28 +01002515
2516 mutex_unlock(&mb1_transfer.lock);
Martin Perssone0befb22010-12-08 15:13:28 +01002517}
2518
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002519static void ack_dbb_wakeup(void)
Martin Perssone0befb22010-12-08 15:13:28 +01002520{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002521 unsigned long flags;
Martin Perssone0befb22010-12-08 15:13:28 +01002522
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002523 spin_lock_irqsave(&mb0_transfer.lock, flags);
Martin Perssone0befb22010-12-08 15:13:28 +01002524
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002525 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002526 cpu_relax();
Martin Perssone0befb22010-12-08 15:13:28 +01002527
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002528 writeb(MB0H_READ_WAKEUP_ACK, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002529 writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
Martin Perssone0befb22010-12-08 15:13:28 +01002530
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002531 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
Martin Perssone0befb22010-12-08 15:13:28 +01002532}
2533
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002534static inline void print_unknown_header_warning(u8 n, u8 header)
Linus Walleije3726fc2010-08-19 12:36:01 +01002535{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002536 pr_warning("prcmu: Unknown message header (%d) in mailbox %d.\n",
2537 header, n);
Linus Walleije3726fc2010-08-19 12:36:01 +01002538}
2539
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002540static bool read_mailbox_0(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002541{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002542 bool r;
2543 u32 ev;
2544 unsigned int n;
2545 u8 header;
2546
2547 header = readb(tcdm_base + PRCM_MBOX_HEADER_ACK_MB0);
2548 switch (header) {
2549 case MB0H_WAKEUP_EXE:
2550 case MB0H_WAKEUP_SLEEP:
2551 if (readb(tcdm_base + PRCM_ACK_MB0_READ_POINTER) & 1)
2552 ev = readl(tcdm_base + PRCM_ACK_MB0_WAKEUP_1_8500);
2553 else
2554 ev = readl(tcdm_base + PRCM_ACK_MB0_WAKEUP_0_8500);
2555
2556 if (ev & (WAKEUP_BIT_AC_WAKE_ACK | WAKEUP_BIT_AC_SLEEP_ACK))
2557 complete(&mb0_transfer.ac_wake_work);
2558 if (ev & WAKEUP_BIT_SYSCLK_OK)
2559 complete(&mb3_transfer.sysclk_work);
2560
2561 ev &= mb0_transfer.req.dbb_irqs;
2562
2563 for (n = 0; n < NUM_PRCMU_WAKEUPS; n++) {
2564 if (ev & prcmu_irq_bit[n])
2565 generic_handle_irq(IRQ_PRCMU_BASE + n);
2566 }
2567 r = true;
2568 break;
2569 default:
2570 print_unknown_header_warning(0, header);
2571 r = false;
2572 break;
2573 }
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002574 writel(MBOX_BIT(0), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002575 return r;
2576}
2577
2578static bool read_mailbox_1(void)
2579{
2580 mb1_transfer.ack.header = readb(tcdm_base + PRCM_MBOX_HEADER_REQ_MB1);
2581 mb1_transfer.ack.arm_opp = readb(tcdm_base +
2582 PRCM_ACK_MB1_CURRENT_ARM_OPP);
2583 mb1_transfer.ack.ape_opp = readb(tcdm_base +
2584 PRCM_ACK_MB1_CURRENT_APE_OPP);
2585 mb1_transfer.ack.ape_voltage_status = readb(tcdm_base +
2586 PRCM_ACK_MB1_APE_VOLTAGE_STATUS);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002587 writel(MBOX_BIT(1), PRCM_ARM_IT1_CLR);
Martin Perssone0befb22010-12-08 15:13:28 +01002588 complete(&mb1_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002589 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002590}
2591
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002592static bool read_mailbox_2(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002593{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002594 mb2_transfer.ack.status = readb(tcdm_base + PRCM_ACK_MB2_DPS_STATUS);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002595 writel(MBOX_BIT(2), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002596 complete(&mb2_transfer.work);
2597 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002598}
2599
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002600static bool read_mailbox_3(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002601{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002602 writel(MBOX_BIT(3), PRCM_ARM_IT1_CLR);
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_4(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002607{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002608 u8 header;
2609 bool do_complete = true;
2610
2611 header = readb(tcdm_base + PRCM_MBOX_HEADER_REQ_MB4);
2612 switch (header) {
2613 case MB4H_MEM_ST:
2614 case MB4H_HOTDOG:
2615 case MB4H_HOTMON:
2616 case MB4H_HOT_PERIOD:
Mattias Nilssona592c2e2011-08-12 10:27:41 +02002617 case MB4H_A9WDOG_CONF:
2618 case MB4H_A9WDOG_EN:
2619 case MB4H_A9WDOG_DIS:
2620 case MB4H_A9WDOG_LOAD:
2621 case MB4H_A9WDOG_KICK:
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002622 break;
2623 default:
2624 print_unknown_header_warning(4, header);
2625 do_complete = false;
2626 break;
2627 }
2628
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002629 writel(MBOX_BIT(4), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002630
2631 if (do_complete)
2632 complete(&mb4_transfer.work);
2633
2634 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002635}
2636
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002637static bool read_mailbox_5(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002638{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002639 mb5_transfer.ack.status = readb(tcdm_base + PRCM_ACK_MB5_I2C_STATUS);
2640 mb5_transfer.ack.value = readb(tcdm_base + PRCM_ACK_MB5_I2C_VAL);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002641 writel(MBOX_BIT(5), PRCM_ARM_IT1_CLR);
Linus Walleije3726fc2010-08-19 12:36:01 +01002642 complete(&mb5_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002643 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002644}
2645
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002646static bool read_mailbox_6(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002647{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002648 writel(MBOX_BIT(6), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002649 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002650}
2651
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002652static bool read_mailbox_7(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002653{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002654 writel(MBOX_BIT(7), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002655 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002656}
2657
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002658static bool (* const read_mailbox[NUM_MB])(void) = {
Linus Walleije3726fc2010-08-19 12:36:01 +01002659 read_mailbox_0,
2660 read_mailbox_1,
2661 read_mailbox_2,
2662 read_mailbox_3,
2663 read_mailbox_4,
2664 read_mailbox_5,
2665 read_mailbox_6,
2666 read_mailbox_7
2667};
2668
2669static irqreturn_t prcmu_irq_handler(int irq, void *data)
2670{
2671 u32 bits;
2672 u8 n;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002673 irqreturn_t r;
Linus Walleije3726fc2010-08-19 12:36:01 +01002674
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002675 bits = (readl(PRCM_ARM_IT1_VAL) & ALL_MBOX_BITS);
Linus Walleije3726fc2010-08-19 12:36:01 +01002676 if (unlikely(!bits))
2677 return IRQ_NONE;
2678
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002679 r = IRQ_HANDLED;
Linus Walleije3726fc2010-08-19 12:36:01 +01002680 for (n = 0; bits; n++) {
2681 if (bits & MBOX_BIT(n)) {
2682 bits -= MBOX_BIT(n);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002683 if (read_mailbox[n]())
2684 r = IRQ_WAKE_THREAD;
Linus Walleije3726fc2010-08-19 12:36:01 +01002685 }
2686 }
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002687 return r;
2688}
2689
2690static irqreturn_t prcmu_irq_thread_fn(int irq, void *data)
2691{
2692 ack_dbb_wakeup();
Linus Walleije3726fc2010-08-19 12:36:01 +01002693 return IRQ_HANDLED;
2694}
2695
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002696static void prcmu_mask_work(struct work_struct *work)
2697{
2698 unsigned long flags;
2699
2700 spin_lock_irqsave(&mb0_transfer.lock, flags);
2701
2702 config_wakeups();
2703
2704 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
2705}
2706
2707static void prcmu_irq_mask(struct irq_data *d)
2708{
2709 unsigned long flags;
2710
2711 spin_lock_irqsave(&mb0_transfer.dbb_irqs_lock, flags);
2712
2713 mb0_transfer.req.dbb_irqs &= ~prcmu_irq_bit[d->irq - IRQ_PRCMU_BASE];
2714
2715 spin_unlock_irqrestore(&mb0_transfer.dbb_irqs_lock, flags);
2716
2717 if (d->irq != IRQ_PRCMU_CA_SLEEP)
2718 schedule_work(&mb0_transfer.mask_work);
2719}
2720
2721static void prcmu_irq_unmask(struct irq_data *d)
2722{
2723 unsigned long flags;
2724
2725 spin_lock_irqsave(&mb0_transfer.dbb_irqs_lock, flags);
2726
2727 mb0_transfer.req.dbb_irqs |= prcmu_irq_bit[d->irq - IRQ_PRCMU_BASE];
2728
2729 spin_unlock_irqrestore(&mb0_transfer.dbb_irqs_lock, flags);
2730
2731 if (d->irq != IRQ_PRCMU_CA_SLEEP)
2732 schedule_work(&mb0_transfer.mask_work);
2733}
2734
2735static void noop(struct irq_data *d)
2736{
2737}
2738
2739static struct irq_chip prcmu_irq_chip = {
2740 .name = "prcmu",
2741 .irq_disable = prcmu_irq_mask,
2742 .irq_ack = noop,
2743 .irq_mask = prcmu_irq_mask,
2744 .irq_unmask = prcmu_irq_unmask,
2745};
2746
Mattias Nilssonb58d12f2012-01-13 16:20:10 +01002747static char *fw_project_name(u8 project)
2748{
2749 switch (project) {
2750 case PRCMU_FW_PROJECT_U8500:
2751 return "U8500";
2752 case PRCMU_FW_PROJECT_U8500_C2:
2753 return "U8500 C2";
2754 case PRCMU_FW_PROJECT_U9500:
2755 return "U9500";
2756 case PRCMU_FW_PROJECT_U9500_C2:
2757 return "U9500 C2";
2758 default:
2759 return "Unknown";
2760 }
2761}
2762
Mattias Nilsson73180f82011-08-12 10:28:10 +02002763void __init db8500_prcmu_early_init(void)
Mattias Wallinfcbd4582010-12-02 16:20:42 +01002764{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002765 unsigned int i;
Linus Walleij3e2762c2012-01-02 14:17:40 +01002766 if (cpu_is_u8500v2()) {
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002767 void *tcpm_base = ioremap_nocache(U8500_PRCMU_TCPM_BASE, SZ_4K);
2768
2769 if (tcpm_base != NULL) {
Linus Walleij3e2762c2012-01-02 14:17:40 +01002770 u32 version;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002771 version = readl(tcpm_base + PRCMU_FW_VERSION_OFFSET);
Mattias Nilssonb58d12f2012-01-13 16:20:10 +01002772 fw_info.version.project = version & 0xFF;
2773 fw_info.version.api_version = (version >> 8) & 0xFF;
2774 fw_info.version.func_version = (version >> 16) & 0xFF;
2775 fw_info.version.errata = (version >> 24) & 0xFF;
2776 fw_info.valid = true;
2777 pr_info("PRCMU firmware: %s, version %d.%d.%d\n",
2778 fw_project_name(fw_info.version.project),
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002779 (version >> 8) & 0xFF, (version >> 16) & 0xFF,
2780 (version >> 24) & 0xFF);
2781 iounmap(tcpm_base);
2782 }
2783
Mattias Wallinfcbd4582010-12-02 16:20:42 +01002784 tcdm_base = __io_address(U8500_PRCMU_TCDM_BASE);
2785 } else {
2786 pr_err("prcmu: Unsupported chip version\n");
2787 BUG();
2788 }
Mattias Wallinfcbd4582010-12-02 16:20:42 +01002789
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002790 spin_lock_init(&mb0_transfer.lock);
2791 spin_lock_init(&mb0_transfer.dbb_irqs_lock);
2792 mutex_init(&mb0_transfer.ac_wake_lock);
2793 init_completion(&mb0_transfer.ac_wake_work);
Martin Perssone0befb22010-12-08 15:13:28 +01002794 mutex_init(&mb1_transfer.lock);
2795 init_completion(&mb1_transfer.work);
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01002796 mb1_transfer.ape_opp = APE_NO_CHANGE;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002797 mutex_init(&mb2_transfer.lock);
2798 init_completion(&mb2_transfer.work);
2799 spin_lock_init(&mb2_transfer.auto_pm_lock);
2800 spin_lock_init(&mb3_transfer.lock);
2801 mutex_init(&mb3_transfer.sysclk_lock);
2802 init_completion(&mb3_transfer.sysclk_work);
2803 mutex_init(&mb4_transfer.lock);
2804 init_completion(&mb4_transfer.work);
Linus Walleije3726fc2010-08-19 12:36:01 +01002805 mutex_init(&mb5_transfer.lock);
2806 init_completion(&mb5_transfer.work);
2807
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002808 INIT_WORK(&mb0_transfer.mask_work, prcmu_mask_work);
Linus Walleije3726fc2010-08-19 12:36:01 +01002809
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002810 /* Initalize irqs. */
2811 for (i = 0; i < NUM_PRCMU_WAKEUPS; i++) {
2812 unsigned int irq;
2813
2814 irq = IRQ_PRCMU_BASE + i;
2815 irq_set_chip_and_handler(irq, &prcmu_irq_chip,
2816 handle_simple_irq);
2817 set_irq_flags(irq, IRQF_VALID);
2818 }
Linus Walleije3726fc2010-08-19 12:36:01 +01002819}
2820
Mattias Nilsson05089012012-01-13 16:20:20 +01002821static void __init init_prcm_registers(void)
Mattias Nilssond65e12d2011-08-12 10:27:50 +02002822{
2823 u32 val;
2824
2825 val = readl(PRCM_A9PL_FORCE_CLKEN);
2826 val &= ~(PRCM_A9PL_FORCE_CLKEN_PRCM_A9PL_FORCE_CLKEN |
2827 PRCM_A9PL_FORCE_CLKEN_PRCM_A9AXI_FORCE_CLKEN);
2828 writel(val, (PRCM_A9PL_FORCE_CLKEN));
2829}
2830
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002831/*
2832 * Power domain switches (ePODs) modeled as regulators for the DB8500 SoC
2833 */
2834static struct regulator_consumer_supply db8500_vape_consumers[] = {
2835 REGULATOR_SUPPLY("v-ape", NULL),
2836 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.0"),
2837 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.1"),
2838 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.2"),
2839 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.3"),
2840 /* "v-mmc" changed to "vcore" in the mainline kernel */
2841 REGULATOR_SUPPLY("vcore", "sdi0"),
2842 REGULATOR_SUPPLY("vcore", "sdi1"),
2843 REGULATOR_SUPPLY("vcore", "sdi2"),
2844 REGULATOR_SUPPLY("vcore", "sdi3"),
2845 REGULATOR_SUPPLY("vcore", "sdi4"),
2846 REGULATOR_SUPPLY("v-dma", "dma40.0"),
2847 REGULATOR_SUPPLY("v-ape", "ab8500-usb.0"),
2848 /* "v-uart" changed to "vcore" in the mainline kernel */
2849 REGULATOR_SUPPLY("vcore", "uart0"),
2850 REGULATOR_SUPPLY("vcore", "uart1"),
2851 REGULATOR_SUPPLY("vcore", "uart2"),
2852 REGULATOR_SUPPLY("v-ape", "nmk-ske-keypad.0"),
Bengt Jonsson992b1332012-01-13 16:20:36 +01002853 REGULATOR_SUPPLY("v-hsi", "ste_hsi.0"),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002854};
2855
2856static struct regulator_consumer_supply db8500_vsmps2_consumers[] = {
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002857 REGULATOR_SUPPLY("musb_1v8", "ab8500-usb.0"),
2858 /* AV8100 regulator */
2859 REGULATOR_SUPPLY("hdmi_1v8", "0-0070"),
2860};
2861
2862static struct regulator_consumer_supply db8500_b2r2_mcde_consumers[] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002863 REGULATOR_SUPPLY("vsupply", "b2r2_bus"),
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002864 REGULATOR_SUPPLY("vsupply", "mcde"),
2865};
2866
2867/* SVA MMDSP regulator switch */
2868static struct regulator_consumer_supply db8500_svammdsp_consumers[] = {
2869 REGULATOR_SUPPLY("sva-mmdsp", "cm_control"),
2870};
2871
2872/* SVA pipe regulator switch */
2873static struct regulator_consumer_supply db8500_svapipe_consumers[] = {
2874 REGULATOR_SUPPLY("sva-pipe", "cm_control"),
2875};
2876
2877/* SIA MMDSP regulator switch */
2878static struct regulator_consumer_supply db8500_siammdsp_consumers[] = {
2879 REGULATOR_SUPPLY("sia-mmdsp", "cm_control"),
2880};
2881
2882/* SIA pipe regulator switch */
2883static struct regulator_consumer_supply db8500_siapipe_consumers[] = {
2884 REGULATOR_SUPPLY("sia-pipe", "cm_control"),
2885};
2886
2887static struct regulator_consumer_supply db8500_sga_consumers[] = {
2888 REGULATOR_SUPPLY("v-mali", NULL),
2889};
2890
2891/* ESRAM1 and 2 regulator switch */
2892static struct regulator_consumer_supply db8500_esram12_consumers[] = {
2893 REGULATOR_SUPPLY("esram12", "cm_control"),
2894};
2895
2896/* ESRAM3 and 4 regulator switch */
2897static struct regulator_consumer_supply db8500_esram34_consumers[] = {
2898 REGULATOR_SUPPLY("v-esram34", "mcde"),
2899 REGULATOR_SUPPLY("esram34", "cm_control"),
Bengt Jonsson992b1332012-01-13 16:20:36 +01002900 REGULATOR_SUPPLY("lcla_esram", "dma40.0"),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002901};
2902
2903static struct regulator_init_data db8500_regulators[DB8500_NUM_REGULATORS] = {
2904 [DB8500_REGULATOR_VAPE] = {
2905 .constraints = {
2906 .name = "db8500-vape",
2907 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2908 },
2909 .consumer_supplies = db8500_vape_consumers,
2910 .num_consumer_supplies = ARRAY_SIZE(db8500_vape_consumers),
2911 },
2912 [DB8500_REGULATOR_VARM] = {
2913 .constraints = {
2914 .name = "db8500-varm",
2915 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2916 },
2917 },
2918 [DB8500_REGULATOR_VMODEM] = {
2919 .constraints = {
2920 .name = "db8500-vmodem",
2921 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2922 },
2923 },
2924 [DB8500_REGULATOR_VPLL] = {
2925 .constraints = {
2926 .name = "db8500-vpll",
2927 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2928 },
2929 },
2930 [DB8500_REGULATOR_VSMPS1] = {
2931 .constraints = {
2932 .name = "db8500-vsmps1",
2933 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2934 },
2935 },
2936 [DB8500_REGULATOR_VSMPS2] = {
2937 .constraints = {
2938 .name = "db8500-vsmps2",
2939 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2940 },
2941 .consumer_supplies = db8500_vsmps2_consumers,
2942 .num_consumer_supplies = ARRAY_SIZE(db8500_vsmps2_consumers),
2943 },
2944 [DB8500_REGULATOR_VSMPS3] = {
2945 .constraints = {
2946 .name = "db8500-vsmps3",
2947 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2948 },
2949 },
2950 [DB8500_REGULATOR_VRF1] = {
2951 .constraints = {
2952 .name = "db8500-vrf1",
2953 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2954 },
2955 },
2956 [DB8500_REGULATOR_SWITCH_SVAMMDSP] = {
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-sva-mmdsp",
2960 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2961 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002962 .consumer_supplies = db8500_svammdsp_consumers,
2963 .num_consumer_supplies = ARRAY_SIZE(db8500_svammdsp_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002964 },
2965 [DB8500_REGULATOR_SWITCH_SVAMMDSPRET] = {
2966 .constraints = {
2967 /* "ret" means "retention" */
2968 .name = "db8500-sva-mmdsp-ret",
2969 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2970 },
2971 },
2972 [DB8500_REGULATOR_SWITCH_SVAPIPE] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002973 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002974 .constraints = {
2975 .name = "db8500-sva-pipe",
2976 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2977 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002978 .consumer_supplies = db8500_svapipe_consumers,
2979 .num_consumer_supplies = ARRAY_SIZE(db8500_svapipe_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002980 },
2981 [DB8500_REGULATOR_SWITCH_SIAMMDSP] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002982 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002983 .constraints = {
2984 .name = "db8500-sia-mmdsp",
2985 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2986 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002987 .consumer_supplies = db8500_siammdsp_consumers,
2988 .num_consumer_supplies = ARRAY_SIZE(db8500_siammdsp_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002989 },
2990 [DB8500_REGULATOR_SWITCH_SIAMMDSPRET] = {
2991 .constraints = {
2992 .name = "db8500-sia-mmdsp-ret",
2993 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2994 },
2995 },
2996 [DB8500_REGULATOR_SWITCH_SIAPIPE] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002997 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002998 .constraints = {
2999 .name = "db8500-sia-pipe",
3000 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
3001 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02003002 .consumer_supplies = db8500_siapipe_consumers,
3003 .num_consumer_supplies = ARRAY_SIZE(db8500_siapipe_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02003004 },
3005 [DB8500_REGULATOR_SWITCH_SGA] = {
3006 .supply_regulator = "db8500-vape",
3007 .constraints = {
3008 .name = "db8500-sga",
3009 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
3010 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02003011 .consumer_supplies = db8500_sga_consumers,
3012 .num_consumer_supplies = ARRAY_SIZE(db8500_sga_consumers),
3013
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02003014 },
3015 [DB8500_REGULATOR_SWITCH_B2R2_MCDE] = {
3016 .supply_regulator = "db8500-vape",
3017 .constraints = {
3018 .name = "db8500-b2r2-mcde",
3019 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
3020 },
3021 .consumer_supplies = db8500_b2r2_mcde_consumers,
3022 .num_consumer_supplies = ARRAY_SIZE(db8500_b2r2_mcde_consumers),
3023 },
3024 [DB8500_REGULATOR_SWITCH_ESRAM12] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01003025 /*
3026 * esram12 is set in retention and supplied by Vsafe when Vape is off,
3027 * no need to hold Vape
3028 */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02003029 .constraints = {
3030 .name = "db8500-esram12",
3031 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
3032 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02003033 .consumer_supplies = db8500_esram12_consumers,
3034 .num_consumer_supplies = ARRAY_SIZE(db8500_esram12_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02003035 },
3036 [DB8500_REGULATOR_SWITCH_ESRAM12RET] = {
3037 .constraints = {
3038 .name = "db8500-esram12-ret",
3039 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
3040 },
3041 },
3042 [DB8500_REGULATOR_SWITCH_ESRAM34] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01003043 /*
3044 * esram34 is set in retention and supplied by Vsafe when Vape is off,
3045 * no need to hold Vape
3046 */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02003047 .constraints = {
3048 .name = "db8500-esram34",
3049 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
3050 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02003051 .consumer_supplies = db8500_esram34_consumers,
3052 .num_consumer_supplies = ARRAY_SIZE(db8500_esram34_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02003053 },
3054 [DB8500_REGULATOR_SWITCH_ESRAM34RET] = {
3055 .constraints = {
3056 .name = "db8500-esram34-ret",
3057 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
3058 },
3059 },
3060};
3061
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003062static struct mfd_cell db8500_prcmu_devs[] = {
3063 {
3064 .name = "db8500-prcmu-regulators",
Mattias Wallin1ed78912011-05-27 11:49:43 +02003065 .platform_data = &db8500_regulators,
3066 .pdata_size = sizeof(db8500_regulators),
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003067 },
3068 {
3069 .name = "cpufreq-u8500",
3070 },
3071};
3072
3073/**
3074 * prcmu_fw_init - arch init call for the Linux PRCMU fw init logic
3075 *
3076 */
3077static int __init db8500_prcmu_probe(struct platform_device *pdev)
3078{
3079 int err = 0;
3080
3081 if (ux500_is_svp())
3082 return -ENODEV;
3083
Mattias Nilsson05089012012-01-13 16:20:20 +01003084 init_prcm_registers();
Mattias Nilssond65e12d2011-08-12 10:27:50 +02003085
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003086 /* Clean up the mailbox interrupts after pre-kernel code. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02003087 writel(ALL_MBOX_BITS, PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003088
3089 err = request_threaded_irq(IRQ_DB8500_PRCMU1, prcmu_irq_handler,
3090 prcmu_irq_thread_fn, IRQF_NO_SUSPEND, "prcmu", NULL);
3091 if (err < 0) {
3092 pr_err("prcmu: Failed to allocate IRQ_DB8500_PRCMU1.\n");
3093 err = -EBUSY;
3094 goto no_irq_return;
3095 }
3096
3097 if (cpu_is_u8500v20_or_later())
3098 prcmu_config_esram0_deep_sleep(ESRAM0_DEEP_SLEEP_STATE_RET);
3099
3100 err = mfd_add_devices(&pdev->dev, 0, db8500_prcmu_devs,
3101 ARRAY_SIZE(db8500_prcmu_devs), NULL,
3102 0);
3103
3104 if (err)
3105 pr_err("prcmu: Failed to add subdevices\n");
3106 else
3107 pr_info("DB8500 PRCMU initialized\n");
3108
3109no_irq_return:
3110 return err;
3111}
3112
3113static struct platform_driver db8500_prcmu_driver = {
3114 .driver = {
3115 .name = "db8500-prcmu",
3116 .owner = THIS_MODULE,
3117 },
3118};
3119
3120static int __init db8500_prcmu_init(void)
3121{
3122 return platform_driver_probe(&db8500_prcmu_driver, db8500_prcmu_probe);
3123}
3124
3125arch_initcall(db8500_prcmu_init);
3126
3127MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com>");
3128MODULE_DESCRIPTION("DB8500 PRCM Unit driver");
3129MODULE_LICENSE("GPL v2");