blob: b320cc602668317b9b351c9def8935e9f9d6489a [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>
Linus Walleije3726fc2010-08-19 12:36:01 +010033#include <mach/hardware.h>
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020034#include <mach/irqs.h>
35#include <mach/db8500-regs.h>
36#include <mach/id.h>
Mattias Nilsson73180f82011-08-12 10:28:10 +020037#include "dbx500-prcmu-regs.h"
Linus Walleije3726fc2010-08-19 12:36:01 +010038
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020039/* Offset for the firmware version within the TCPM */
40#define PRCMU_FW_VERSION_OFFSET 0xA4
Linus Walleije3726fc2010-08-19 12:36:01 +010041
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020042/* Index of different voltages to be used when accessing AVSData */
43#define PRCM_AVS_BASE 0x2FC
44#define PRCM_AVS_VBB_RET (PRCM_AVS_BASE + 0x0)
45#define PRCM_AVS_VBB_MAX_OPP (PRCM_AVS_BASE + 0x1)
46#define PRCM_AVS_VBB_100_OPP (PRCM_AVS_BASE + 0x2)
47#define PRCM_AVS_VBB_50_OPP (PRCM_AVS_BASE + 0x3)
48#define PRCM_AVS_VARM_MAX_OPP (PRCM_AVS_BASE + 0x4)
49#define PRCM_AVS_VARM_100_OPP (PRCM_AVS_BASE + 0x5)
50#define PRCM_AVS_VARM_50_OPP (PRCM_AVS_BASE + 0x6)
51#define PRCM_AVS_VARM_RET (PRCM_AVS_BASE + 0x7)
52#define PRCM_AVS_VAPE_100_OPP (PRCM_AVS_BASE + 0x8)
53#define PRCM_AVS_VAPE_50_OPP (PRCM_AVS_BASE + 0x9)
54#define PRCM_AVS_VMOD_100_OPP (PRCM_AVS_BASE + 0xA)
55#define PRCM_AVS_VMOD_50_OPP (PRCM_AVS_BASE + 0xB)
56#define PRCM_AVS_VSAFE (PRCM_AVS_BASE + 0xC)
Martin Perssone0befb22010-12-08 15:13:28 +010057
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020058#define PRCM_AVS_VOLTAGE 0
59#define PRCM_AVS_VOLTAGE_MASK 0x3f
60#define PRCM_AVS_ISSLOWSTARTUP 6
61#define PRCM_AVS_ISSLOWSTARTUP_MASK (1 << PRCM_AVS_ISSLOWSTARTUP)
Martin Perssone0befb22010-12-08 15:13:28 +010062#define PRCM_AVS_ISMODEENABLE 7
63#define PRCM_AVS_ISMODEENABLE_MASK (1 << PRCM_AVS_ISMODEENABLE)
64
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020065#define PRCM_BOOT_STATUS 0xFFF
66#define PRCM_ROMCODE_A2P 0xFFE
67#define PRCM_ROMCODE_P2A 0xFFD
68#define PRCM_XP70_CUR_PWR_STATE 0xFFC /* 4 BYTES */
Linus Walleije3726fc2010-08-19 12:36:01 +010069
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020070#define PRCM_SW_RST_REASON 0xFF8 /* 2 bytes */
71
72#define _PRCM_MBOX_HEADER 0xFE8 /* 16 bytes */
73#define PRCM_MBOX_HEADER_REQ_MB0 (_PRCM_MBOX_HEADER + 0x0)
74#define PRCM_MBOX_HEADER_REQ_MB1 (_PRCM_MBOX_HEADER + 0x1)
75#define PRCM_MBOX_HEADER_REQ_MB2 (_PRCM_MBOX_HEADER + 0x2)
76#define PRCM_MBOX_HEADER_REQ_MB3 (_PRCM_MBOX_HEADER + 0x3)
77#define PRCM_MBOX_HEADER_REQ_MB4 (_PRCM_MBOX_HEADER + 0x4)
78#define PRCM_MBOX_HEADER_REQ_MB5 (_PRCM_MBOX_HEADER + 0x5)
79#define PRCM_MBOX_HEADER_ACK_MB0 (_PRCM_MBOX_HEADER + 0x8)
80
81/* Req Mailboxes */
82#define PRCM_REQ_MB0 0xFDC /* 12 bytes */
83#define PRCM_REQ_MB1 0xFD0 /* 12 bytes */
84#define PRCM_REQ_MB2 0xFC0 /* 16 bytes */
85#define PRCM_REQ_MB3 0xE4C /* 372 bytes */
86#define PRCM_REQ_MB4 0xE48 /* 4 bytes */
87#define PRCM_REQ_MB5 0xE44 /* 4 bytes */
88
89/* Ack Mailboxes */
90#define PRCM_ACK_MB0 0xE08 /* 52 bytes */
91#define PRCM_ACK_MB1 0xE04 /* 4 bytes */
92#define PRCM_ACK_MB2 0xE00 /* 4 bytes */
93#define PRCM_ACK_MB3 0xDFC /* 4 bytes */
94#define PRCM_ACK_MB4 0xDF8 /* 4 bytes */
95#define PRCM_ACK_MB5 0xDF4 /* 4 bytes */
96
97/* Mailbox 0 headers */
98#define MB0H_POWER_STATE_TRANS 0
99#define MB0H_CONFIG_WAKEUPS_EXE 1
100#define MB0H_READ_WAKEUP_ACK 3
101#define MB0H_CONFIG_WAKEUPS_SLEEP 4
102
103#define MB0H_WAKEUP_EXE 2
104#define MB0H_WAKEUP_SLEEP 5
105
106/* Mailbox 0 REQs */
107#define PRCM_REQ_MB0_AP_POWER_STATE (PRCM_REQ_MB0 + 0x0)
108#define PRCM_REQ_MB0_AP_PLL_STATE (PRCM_REQ_MB0 + 0x1)
109#define PRCM_REQ_MB0_ULP_CLOCK_STATE (PRCM_REQ_MB0 + 0x2)
110#define PRCM_REQ_MB0_DO_NOT_WFI (PRCM_REQ_MB0 + 0x3)
111#define PRCM_REQ_MB0_WAKEUP_8500 (PRCM_REQ_MB0 + 0x4)
112#define PRCM_REQ_MB0_WAKEUP_4500 (PRCM_REQ_MB0 + 0x8)
113
114/* Mailbox 0 ACKs */
115#define PRCM_ACK_MB0_AP_PWRSTTR_STATUS (PRCM_ACK_MB0 + 0x0)
116#define PRCM_ACK_MB0_READ_POINTER (PRCM_ACK_MB0 + 0x1)
117#define PRCM_ACK_MB0_WAKEUP_0_8500 (PRCM_ACK_MB0 + 0x4)
118#define PRCM_ACK_MB0_WAKEUP_0_4500 (PRCM_ACK_MB0 + 0x8)
119#define PRCM_ACK_MB0_WAKEUP_1_8500 (PRCM_ACK_MB0 + 0x1C)
120#define PRCM_ACK_MB0_WAKEUP_1_4500 (PRCM_ACK_MB0 + 0x20)
121#define PRCM_ACK_MB0_EVENT_4500_NUMBERS 20
122
123/* Mailbox 1 headers */
124#define MB1H_ARM_APE_OPP 0x0
125#define MB1H_RESET_MODEM 0x2
126#define MB1H_REQUEST_APE_OPP_100_VOLT 0x3
127#define MB1H_RELEASE_APE_OPP_100_VOLT 0x4
128#define MB1H_RELEASE_USB_WAKEUP 0x5
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200129#define MB1H_PLL_ON_OFF 0x6
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200130
131/* Mailbox 1 Requests */
132#define PRCM_REQ_MB1_ARM_OPP (PRCM_REQ_MB1 + 0x0)
133#define PRCM_REQ_MB1_APE_OPP (PRCM_REQ_MB1 + 0x1)
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200134#define PRCM_REQ_MB1_PLL_ON_OFF (PRCM_REQ_MB1 + 0x4)
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100135#define PLL_SOC0_OFF 0x1
136#define PLL_SOC0_ON 0x2
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200137#define PLL_SOC1_OFF 0x4
138#define PLL_SOC1_ON 0x8
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200139
140/* Mailbox 1 ACKs */
141#define PRCM_ACK_MB1_CURRENT_ARM_OPP (PRCM_ACK_MB1 + 0x0)
142#define PRCM_ACK_MB1_CURRENT_APE_OPP (PRCM_ACK_MB1 + 0x1)
143#define PRCM_ACK_MB1_APE_VOLTAGE_STATUS (PRCM_ACK_MB1 + 0x2)
144#define PRCM_ACK_MB1_DVFS_STATUS (PRCM_ACK_MB1 + 0x3)
145
146/* Mailbox 2 headers */
147#define MB2H_DPS 0x0
148#define MB2H_AUTO_PWR 0x1
149
150/* Mailbox 2 REQs */
151#define PRCM_REQ_MB2_SVA_MMDSP (PRCM_REQ_MB2 + 0x0)
152#define PRCM_REQ_MB2_SVA_PIPE (PRCM_REQ_MB2 + 0x1)
153#define PRCM_REQ_MB2_SIA_MMDSP (PRCM_REQ_MB2 + 0x2)
154#define PRCM_REQ_MB2_SIA_PIPE (PRCM_REQ_MB2 + 0x3)
155#define PRCM_REQ_MB2_SGA (PRCM_REQ_MB2 + 0x4)
156#define PRCM_REQ_MB2_B2R2_MCDE (PRCM_REQ_MB2 + 0x5)
157#define PRCM_REQ_MB2_ESRAM12 (PRCM_REQ_MB2 + 0x6)
158#define PRCM_REQ_MB2_ESRAM34 (PRCM_REQ_MB2 + 0x7)
159#define PRCM_REQ_MB2_AUTO_PM_SLEEP (PRCM_REQ_MB2 + 0x8)
160#define PRCM_REQ_MB2_AUTO_PM_IDLE (PRCM_REQ_MB2 + 0xC)
161
162/* Mailbox 2 ACKs */
163#define PRCM_ACK_MB2_DPS_STATUS (PRCM_ACK_MB2 + 0x0)
164#define HWACC_PWR_ST_OK 0xFE
165
166/* Mailbox 3 headers */
167#define MB3H_ANC 0x0
168#define MB3H_SIDETONE 0x1
169#define MB3H_SYSCLK 0xE
170
171/* Mailbox 3 Requests */
172#define PRCM_REQ_MB3_ANC_FIR_COEFF (PRCM_REQ_MB3 + 0x0)
173#define PRCM_REQ_MB3_ANC_IIR_COEFF (PRCM_REQ_MB3 + 0x20)
174#define PRCM_REQ_MB3_ANC_SHIFTER (PRCM_REQ_MB3 + 0x60)
175#define PRCM_REQ_MB3_ANC_WARP (PRCM_REQ_MB3 + 0x64)
176#define PRCM_REQ_MB3_SIDETONE_FIR_GAIN (PRCM_REQ_MB3 + 0x68)
177#define PRCM_REQ_MB3_SIDETONE_FIR_COEFF (PRCM_REQ_MB3 + 0x6C)
178#define PRCM_REQ_MB3_SYSCLK_MGT (PRCM_REQ_MB3 + 0x16C)
179
180/* Mailbox 4 headers */
181#define MB4H_DDR_INIT 0x0
182#define MB4H_MEM_ST 0x1
183#define MB4H_HOTDOG 0x12
184#define MB4H_HOTMON 0x13
185#define MB4H_HOT_PERIOD 0x14
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200186#define MB4H_A9WDOG_CONF 0x16
187#define MB4H_A9WDOG_EN 0x17
188#define MB4H_A9WDOG_DIS 0x18
189#define MB4H_A9WDOG_LOAD 0x19
190#define MB4H_A9WDOG_KICK 0x20
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200191
192/* Mailbox 4 Requests */
193#define PRCM_REQ_MB4_DDR_ST_AP_SLEEP_IDLE (PRCM_REQ_MB4 + 0x0)
194#define PRCM_REQ_MB4_DDR_ST_AP_DEEP_IDLE (PRCM_REQ_MB4 + 0x1)
195#define PRCM_REQ_MB4_ESRAM0_ST (PRCM_REQ_MB4 + 0x3)
196#define PRCM_REQ_MB4_HOTDOG_THRESHOLD (PRCM_REQ_MB4 + 0x0)
197#define PRCM_REQ_MB4_HOTMON_LOW (PRCM_REQ_MB4 + 0x0)
198#define PRCM_REQ_MB4_HOTMON_HIGH (PRCM_REQ_MB4 + 0x1)
199#define PRCM_REQ_MB4_HOTMON_CONFIG (PRCM_REQ_MB4 + 0x2)
200#define PRCM_REQ_MB4_HOT_PERIOD (PRCM_REQ_MB4 + 0x0)
201#define HOTMON_CONFIG_LOW BIT(0)
202#define HOTMON_CONFIG_HIGH BIT(1)
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200203#define PRCM_REQ_MB4_A9WDOG_0 (PRCM_REQ_MB4 + 0x0)
204#define PRCM_REQ_MB4_A9WDOG_1 (PRCM_REQ_MB4 + 0x1)
205#define PRCM_REQ_MB4_A9WDOG_2 (PRCM_REQ_MB4 + 0x2)
206#define PRCM_REQ_MB4_A9WDOG_3 (PRCM_REQ_MB4 + 0x3)
207#define A9WDOG_AUTO_OFF_EN BIT(7)
208#define A9WDOG_AUTO_OFF_DIS 0
209#define A9WDOG_ID_MASK 0xf
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200210
211/* Mailbox 5 Requests */
212#define PRCM_REQ_MB5_I2C_SLAVE_OP (PRCM_REQ_MB5 + 0x0)
213#define PRCM_REQ_MB5_I2C_HW_BITS (PRCM_REQ_MB5 + 0x1)
214#define PRCM_REQ_MB5_I2C_REG (PRCM_REQ_MB5 + 0x2)
215#define PRCM_REQ_MB5_I2C_VAL (PRCM_REQ_MB5 + 0x3)
216#define PRCMU_I2C_WRITE(slave) \
217 (((slave) << 1) | (cpu_is_u8500v2() ? BIT(6) : 0))
218#define PRCMU_I2C_READ(slave) \
219 (((slave) << 1) | BIT(0) | (cpu_is_u8500v2() ? BIT(6) : 0))
220#define PRCMU_I2C_STOP_EN BIT(3)
221
222/* Mailbox 5 ACKs */
223#define PRCM_ACK_MB5_I2C_STATUS (PRCM_ACK_MB5 + 0x1)
224#define PRCM_ACK_MB5_I2C_VAL (PRCM_ACK_MB5 + 0x3)
225#define I2C_WR_OK 0x1
226#define I2C_RD_OK 0x2
227
228#define NUM_MB 8
229#define MBOX_BIT BIT
230#define ALL_MBOX_BITS (MBOX_BIT(NUM_MB) - 1)
231
232/*
233 * Wakeups/IRQs
234 */
235
236#define WAKEUP_BIT_RTC BIT(0)
237#define WAKEUP_BIT_RTT0 BIT(1)
238#define WAKEUP_BIT_RTT1 BIT(2)
239#define WAKEUP_BIT_HSI0 BIT(3)
240#define WAKEUP_BIT_HSI1 BIT(4)
241#define WAKEUP_BIT_CA_WAKE BIT(5)
242#define WAKEUP_BIT_USB BIT(6)
243#define WAKEUP_BIT_ABB BIT(7)
244#define WAKEUP_BIT_ABB_FIFO BIT(8)
245#define WAKEUP_BIT_SYSCLK_OK BIT(9)
246#define WAKEUP_BIT_CA_SLEEP BIT(10)
247#define WAKEUP_BIT_AC_WAKE_ACK BIT(11)
248#define WAKEUP_BIT_SIDE_TONE_OK BIT(12)
249#define WAKEUP_BIT_ANC_OK BIT(13)
250#define WAKEUP_BIT_SW_ERROR BIT(14)
251#define WAKEUP_BIT_AC_SLEEP_ACK BIT(15)
252#define WAKEUP_BIT_ARM BIT(17)
253#define WAKEUP_BIT_HOTMON_LOW BIT(18)
254#define WAKEUP_BIT_HOTMON_HIGH BIT(19)
255#define WAKEUP_BIT_MODEM_SW_RESET_REQ BIT(20)
256#define WAKEUP_BIT_GPIO0 BIT(23)
257#define WAKEUP_BIT_GPIO1 BIT(24)
258#define WAKEUP_BIT_GPIO2 BIT(25)
259#define WAKEUP_BIT_GPIO3 BIT(26)
260#define WAKEUP_BIT_GPIO4 BIT(27)
261#define WAKEUP_BIT_GPIO5 BIT(28)
262#define WAKEUP_BIT_GPIO6 BIT(29)
263#define WAKEUP_BIT_GPIO7 BIT(30)
264#define WAKEUP_BIT_GPIO8 BIT(31)
265
Mattias Nilssonb58d12f2012-01-13 16:20:10 +0100266static struct {
267 bool valid;
268 struct prcmu_fw_version version;
269} fw_info;
270
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200271/*
272 * This vector maps irq numbers to the bits in the bit field used in
273 * communication with the PRCMU firmware.
274 *
275 * The reason for having this is to keep the irq numbers contiguous even though
276 * the bits in the bit field are not. (The bits also have a tendency to move
277 * around, to further complicate matters.)
278 */
279#define IRQ_INDEX(_name) ((IRQ_PRCMU_##_name) - IRQ_PRCMU_BASE)
280#define IRQ_ENTRY(_name)[IRQ_INDEX(_name)] = (WAKEUP_BIT_##_name)
281static u32 prcmu_irq_bit[NUM_PRCMU_WAKEUPS] = {
282 IRQ_ENTRY(RTC),
283 IRQ_ENTRY(RTT0),
284 IRQ_ENTRY(RTT1),
285 IRQ_ENTRY(HSI0),
286 IRQ_ENTRY(HSI1),
287 IRQ_ENTRY(CA_WAKE),
288 IRQ_ENTRY(USB),
289 IRQ_ENTRY(ABB),
290 IRQ_ENTRY(ABB_FIFO),
291 IRQ_ENTRY(CA_SLEEP),
292 IRQ_ENTRY(ARM),
293 IRQ_ENTRY(HOTMON_LOW),
294 IRQ_ENTRY(HOTMON_HIGH),
295 IRQ_ENTRY(MODEM_SW_RESET_REQ),
296 IRQ_ENTRY(GPIO0),
297 IRQ_ENTRY(GPIO1),
298 IRQ_ENTRY(GPIO2),
299 IRQ_ENTRY(GPIO3),
300 IRQ_ENTRY(GPIO4),
301 IRQ_ENTRY(GPIO5),
302 IRQ_ENTRY(GPIO6),
303 IRQ_ENTRY(GPIO7),
304 IRQ_ENTRY(GPIO8)
Martin Perssone0befb22010-12-08 15:13:28 +0100305};
306
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200307#define VALID_WAKEUPS (BIT(NUM_PRCMU_WAKEUP_INDICES) - 1)
308#define WAKEUP_ENTRY(_name)[PRCMU_WAKEUP_INDEX_##_name] = (WAKEUP_BIT_##_name)
309static u32 prcmu_wakeup_bit[NUM_PRCMU_WAKEUP_INDICES] = {
310 WAKEUP_ENTRY(RTC),
311 WAKEUP_ENTRY(RTT0),
312 WAKEUP_ENTRY(RTT1),
313 WAKEUP_ENTRY(HSI0),
314 WAKEUP_ENTRY(HSI1),
315 WAKEUP_ENTRY(USB),
316 WAKEUP_ENTRY(ABB),
317 WAKEUP_ENTRY(ABB_FIFO),
318 WAKEUP_ENTRY(ARM)
319};
320
321/*
322 * mb0_transfer - state needed for mailbox 0 communication.
323 * @lock: The transaction lock.
324 * @dbb_events_lock: A lock used to handle concurrent access to (parts of)
325 * the request data.
326 * @mask_work: Work structure used for (un)masking wakeup interrupts.
327 * @req: Request data that need to persist between requests.
328 */
329static struct {
330 spinlock_t lock;
331 spinlock_t dbb_irqs_lock;
332 struct work_struct mask_work;
333 struct mutex ac_wake_lock;
334 struct completion ac_wake_work;
335 struct {
336 u32 dbb_irqs;
337 u32 dbb_wakeups;
338 u32 abb_events;
339 } req;
340} mb0_transfer;
341
342/*
343 * mb1_transfer - state needed for mailbox 1 communication.
344 * @lock: The transaction lock.
345 * @work: The transaction completion structure.
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100346 * @ape_opp: The current APE OPP.
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200347 * @ack: Reply ("acknowledge") data.
348 */
Martin Perssone0befb22010-12-08 15:13:28 +0100349static struct {
350 struct mutex lock;
351 struct completion work;
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100352 u8 ape_opp;
Martin Perssone0befb22010-12-08 15:13:28 +0100353 struct {
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200354 u8 header;
Martin Perssone0befb22010-12-08 15:13:28 +0100355 u8 arm_opp;
356 u8 ape_opp;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200357 u8 ape_voltage_status;
Martin Perssone0befb22010-12-08 15:13:28 +0100358 } ack;
359} mb1_transfer;
360
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200361/*
362 * mb2_transfer - state needed for mailbox 2 communication.
363 * @lock: The transaction lock.
364 * @work: The transaction completion structure.
365 * @auto_pm_lock: The autonomous power management configuration lock.
366 * @auto_pm_enabled: A flag indicating whether autonomous PM is enabled.
367 * @req: Request data that need to persist between requests.
368 * @ack: Reply ("acknowledge") data.
369 */
Linus Walleije3726fc2010-08-19 12:36:01 +0100370static struct {
371 struct mutex lock;
372 struct completion work;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200373 spinlock_t auto_pm_lock;
374 bool auto_pm_enabled;
375 struct {
376 u8 status;
377 } ack;
378} mb2_transfer;
379
380/*
381 * mb3_transfer - state needed for mailbox 3 communication.
382 * @lock: The request lock.
383 * @sysclk_lock: A lock used to handle concurrent sysclk requests.
384 * @sysclk_work: Work structure used for sysclk requests.
385 */
386static struct {
387 spinlock_t lock;
388 struct mutex sysclk_lock;
389 struct completion sysclk_work;
390} mb3_transfer;
391
392/*
393 * mb4_transfer - state needed for mailbox 4 communication.
394 * @lock: The transaction lock.
395 * @work: The transaction completion structure.
396 */
397static struct {
398 struct mutex lock;
399 struct completion work;
400} mb4_transfer;
401
402/*
403 * mb5_transfer - state needed for mailbox 5 communication.
404 * @lock: The transaction lock.
405 * @work: The transaction completion structure.
406 * @ack: Reply ("acknowledge") data.
407 */
408static struct {
409 struct mutex lock;
410 struct completion work;
Linus Walleije3726fc2010-08-19 12:36:01 +0100411 struct {
412 u8 status;
413 u8 value;
414 } ack;
415} mb5_transfer;
416
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200417static atomic_t ac_wake_req_state = ATOMIC_INIT(0);
418
419/* Spinlocks */
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100420static DEFINE_SPINLOCK(prcmu_lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200421static DEFINE_SPINLOCK(clkout_lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200422
423/* Global var to runtime determine TCDM base for v2 or v1 */
424static __iomem void *tcdm_base;
425
426struct clk_mgt {
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100427 void __iomem *reg;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200428 u32 pllsw;
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100429 int branch;
430 bool clk38div;
431};
432
433enum {
434 PLL_RAW,
435 PLL_FIX,
436 PLL_DIV
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200437};
438
439static DEFINE_SPINLOCK(clk_mgt_lock);
440
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100441#define CLK_MGT_ENTRY(_name, _branch, _clk38div)[PRCMU_##_name] = \
442 { (PRCM_##_name##_MGT), 0 , _branch, _clk38div}
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200443struct clk_mgt clk_mgt[PRCMU_NUM_REG_CLOCKS] = {
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100444 CLK_MGT_ENTRY(SGACLK, PLL_DIV, false),
445 CLK_MGT_ENTRY(UARTCLK, PLL_FIX, true),
446 CLK_MGT_ENTRY(MSP02CLK, PLL_FIX, true),
447 CLK_MGT_ENTRY(MSP1CLK, PLL_FIX, true),
448 CLK_MGT_ENTRY(I2CCLK, PLL_FIX, true),
449 CLK_MGT_ENTRY(SDMMCCLK, PLL_DIV, true),
450 CLK_MGT_ENTRY(SLIMCLK, PLL_FIX, true),
451 CLK_MGT_ENTRY(PER1CLK, PLL_DIV, true),
452 CLK_MGT_ENTRY(PER2CLK, PLL_DIV, true),
453 CLK_MGT_ENTRY(PER3CLK, PLL_DIV, true),
454 CLK_MGT_ENTRY(PER5CLK, PLL_DIV, true),
455 CLK_MGT_ENTRY(PER6CLK, PLL_DIV, true),
456 CLK_MGT_ENTRY(PER7CLK, PLL_DIV, true),
457 CLK_MGT_ENTRY(LCDCLK, PLL_FIX, true),
458 CLK_MGT_ENTRY(BMLCLK, PLL_DIV, true),
459 CLK_MGT_ENTRY(HSITXCLK, PLL_DIV, true),
460 CLK_MGT_ENTRY(HSIRXCLK, PLL_DIV, true),
461 CLK_MGT_ENTRY(HDMICLK, PLL_FIX, false),
462 CLK_MGT_ENTRY(APEATCLK, PLL_DIV, true),
463 CLK_MGT_ENTRY(APETRACECLK, PLL_DIV, true),
464 CLK_MGT_ENTRY(MCDECLK, PLL_DIV, true),
465 CLK_MGT_ENTRY(IPI2CCLK, PLL_FIX, true),
466 CLK_MGT_ENTRY(DSIALTCLK, PLL_FIX, false),
467 CLK_MGT_ENTRY(DMACLK, PLL_DIV, true),
468 CLK_MGT_ENTRY(B2R2CLK, PLL_DIV, true),
469 CLK_MGT_ENTRY(TVCLK, PLL_FIX, true),
470 CLK_MGT_ENTRY(SSPCLK, PLL_FIX, true),
471 CLK_MGT_ENTRY(RNGCLK, PLL_FIX, true),
472 CLK_MGT_ENTRY(UICCCLK, PLL_FIX, false),
473};
474
475struct dsiclk {
476 u32 divsel_mask;
477 u32 divsel_shift;
478 u32 divsel;
479};
480
481static struct dsiclk dsiclk[2] = {
482 {
483 .divsel_mask = PRCM_DSI_PLLOUT_SEL_DSI0_PLLOUT_DIVSEL_MASK,
484 .divsel_shift = PRCM_DSI_PLLOUT_SEL_DSI0_PLLOUT_DIVSEL_SHIFT,
485 .divsel = PRCM_DSI_PLLOUT_SEL_PHI,
486 },
487 {
488 .divsel_mask = PRCM_DSI_PLLOUT_SEL_DSI1_PLLOUT_DIVSEL_MASK,
489 .divsel_shift = PRCM_DSI_PLLOUT_SEL_DSI1_PLLOUT_DIVSEL_SHIFT,
490 .divsel = PRCM_DSI_PLLOUT_SEL_PHI,
491 }
492};
493
494struct dsiescclk {
495 u32 en;
496 u32 div_mask;
497 u32 div_shift;
498};
499
500static struct dsiescclk dsiescclk[3] = {
501 {
502 .en = PRCM_DSITVCLK_DIV_DSI0_ESC_CLK_EN,
503 .div_mask = PRCM_DSITVCLK_DIV_DSI0_ESC_CLK_DIV_MASK,
504 .div_shift = PRCM_DSITVCLK_DIV_DSI0_ESC_CLK_DIV_SHIFT,
505 },
506 {
507 .en = PRCM_DSITVCLK_DIV_DSI1_ESC_CLK_EN,
508 .div_mask = PRCM_DSITVCLK_DIV_DSI1_ESC_CLK_DIV_MASK,
509 .div_shift = PRCM_DSITVCLK_DIV_DSI1_ESC_CLK_DIV_SHIFT,
510 },
511 {
512 .en = PRCM_DSITVCLK_DIV_DSI2_ESC_CLK_EN,
513 .div_mask = PRCM_DSITVCLK_DIV_DSI2_ESC_CLK_DIV_MASK,
514 .div_shift = PRCM_DSITVCLK_DIV_DSI2_ESC_CLK_DIV_SHIFT,
515 }
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200516};
517
Mattias Nilsson0837bb72011-08-12 10:28:18 +0200518static struct regulator *hwacc_regulator[NUM_HW_ACC];
519static struct regulator *hwacc_ret_regulator[NUM_HW_ACC];
520
521static bool hwacc_enabled[NUM_HW_ACC];
522static bool hwacc_ret_enabled[NUM_HW_ACC];
523
524static const char *hwacc_regulator_name[NUM_HW_ACC] = {
525 [HW_ACC_SVAMMDSP] = "hwacc-sva-mmdsp",
526 [HW_ACC_SVAPIPE] = "hwacc-sva-pipe",
527 [HW_ACC_SIAMMDSP] = "hwacc-sia-mmdsp",
528 [HW_ACC_SIAPIPE] = "hwacc-sia-pipe",
529 [HW_ACC_SGA] = "hwacc-sga",
530 [HW_ACC_B2R2] = "hwacc-b2r2",
531 [HW_ACC_MCDE] = "hwacc-mcde",
532 [HW_ACC_ESRAM1] = "hwacc-esram1",
533 [HW_ACC_ESRAM2] = "hwacc-esram2",
534 [HW_ACC_ESRAM3] = "hwacc-esram3",
535 [HW_ACC_ESRAM4] = "hwacc-esram4",
536};
537
538static const char *hwacc_ret_regulator_name[NUM_HW_ACC] = {
539 [HW_ACC_SVAMMDSP] = "hwacc-sva-mmdsp-ret",
540 [HW_ACC_SIAMMDSP] = "hwacc-sia-mmdsp-ret",
541 [HW_ACC_ESRAM1] = "hwacc-esram1-ret",
542 [HW_ACC_ESRAM2] = "hwacc-esram2-ret",
543 [HW_ACC_ESRAM3] = "hwacc-esram3-ret",
544 [HW_ACC_ESRAM4] = "hwacc-esram4-ret",
545};
546
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200547/*
548* Used by MCDE to setup all necessary PRCMU registers
549*/
550#define PRCMU_RESET_DSIPLL 0x00004000
551#define PRCMU_UNCLAMP_DSIPLL 0x00400800
552
553#define PRCMU_CLK_PLL_DIV_SHIFT 0
554#define PRCMU_CLK_PLL_SW_SHIFT 5
555#define PRCMU_CLK_38 (1 << 9)
556#define PRCMU_CLK_38_SRC (1 << 10)
557#define PRCMU_CLK_38_DIV (1 << 11)
558
559/* PLLDIV=12, PLLSW=4 (PLLDDR) */
560#define PRCMU_DSI_CLOCK_SETTING 0x0000008C
561
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200562/* DPI 50000000 Hz */
563#define PRCMU_DPI_CLOCK_SETTING ((1 << PRCMU_CLK_PLL_SW_SHIFT) | \
564 (16 << PRCMU_CLK_PLL_DIV_SHIFT))
565#define PRCMU_DSI_LP_CLOCK_SETTING 0x00000E00
566
567/* D=101, N=1, R=4, SELDIV2=0 */
568#define PRCMU_PLLDSI_FREQ_SETTING 0x00040165
569
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200570#define PRCMU_ENABLE_PLLDSI 0x00000001
571#define PRCMU_DISABLE_PLLDSI 0x00000000
572#define PRCMU_RELEASE_RESET_DSS 0x0000400C
573#define PRCMU_DSI_PLLOUT_SEL_SETTING 0x00000202
574/* ESC clk, div0=1, div1=1, div2=3 */
575#define PRCMU_ENABLE_ESCAPE_CLOCK_DIV 0x07030101
576#define PRCMU_DISABLE_ESCAPE_CLOCK_DIV 0x00030101
577#define PRCMU_DSI_RESET_SW 0x00000007
578
579#define PRCMU_PLLDSI_LOCKP_LOCKED 0x3
580
Mattias Nilsson73180f82011-08-12 10:28:10 +0200581int db8500_prcmu_enable_dsipll(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200582{
583 int i;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200584
585 /* Clear DSIPLL_RESETN */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200586 writel(PRCMU_RESET_DSIPLL, PRCM_APE_RESETN_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200587 /* Unclamp DSIPLL in/out */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200588 writel(PRCMU_UNCLAMP_DSIPLL, PRCM_MMIP_LS_CLAMP_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200589
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200590 /* Set DSI PLL FREQ */
Daniel Willerudc72fe852012-01-13 16:20:03 +0100591 writel(PRCMU_PLLDSI_FREQ_SETTING, PRCM_PLLDSI_FREQ);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200592 writel(PRCMU_DSI_PLLOUT_SEL_SETTING, PRCM_DSI_PLLOUT_SEL);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200593 /* Enable Escape clocks */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200594 writel(PRCMU_ENABLE_ESCAPE_CLOCK_DIV, PRCM_DSITVCLK_DIV);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200595
596 /* Start DSI PLL */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200597 writel(PRCMU_ENABLE_PLLDSI, PRCM_PLLDSI_ENABLE);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200598 /* Reset DSI PLL */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200599 writel(PRCMU_DSI_RESET_SW, PRCM_DSI_SW_RESET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200600 for (i = 0; i < 10; i++) {
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200601 if ((readl(PRCM_PLLDSI_LOCKP) & PRCMU_PLLDSI_LOCKP_LOCKED)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200602 == PRCMU_PLLDSI_LOCKP_LOCKED)
603 break;
604 udelay(100);
605 }
606 /* Set DSIPLL_RESETN */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200607 writel(PRCMU_RESET_DSIPLL, PRCM_APE_RESETN_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200608 return 0;
609}
610
Mattias Nilsson73180f82011-08-12 10:28:10 +0200611int db8500_prcmu_disable_dsipll(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200612{
613 /* Disable dsi pll */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200614 writel(PRCMU_DISABLE_PLLDSI, PRCM_PLLDSI_ENABLE);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200615 /* Disable escapeclock */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200616 writel(PRCMU_DISABLE_ESCAPE_CLOCK_DIV, PRCM_DSITVCLK_DIV);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200617 return 0;
618}
619
Mattias Nilsson73180f82011-08-12 10:28:10 +0200620int db8500_prcmu_set_display_clocks(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200621{
622 unsigned long flags;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200623
624 spin_lock_irqsave(&clk_mgt_lock, flags);
625
626 /* Grab the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200627 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200628 cpu_relax();
629
Daniel Willerudc72fe852012-01-13 16:20:03 +0100630 writel(PRCMU_DSI_CLOCK_SETTING, PRCM_HDMICLK_MGT);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200631 writel(PRCMU_DSI_LP_CLOCK_SETTING, PRCM_TVCLK_MGT);
632 writel(PRCMU_DPI_CLOCK_SETTING, PRCM_LCDCLK_MGT);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200633
634 /* Release the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200635 writel(0, PRCM_SEM);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200636
637 spin_unlock_irqrestore(&clk_mgt_lock, flags);
638
639 return 0;
640}
641
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100642u32 db8500_prcmu_read(unsigned int reg)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200643{
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100644 return readl(_PRCMU_BASE + reg);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200645}
646
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100647void db8500_prcmu_write(unsigned int reg, u32 value)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200648{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200649 unsigned long flags;
650
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100651 spin_lock_irqsave(&prcmu_lock, flags);
652 writel(value, (_PRCMU_BASE + reg));
653 spin_unlock_irqrestore(&prcmu_lock, flags);
654}
655
656void db8500_prcmu_write_masked(unsigned int reg, u32 mask, u32 value)
657{
658 u32 val;
659 unsigned long flags;
660
661 spin_lock_irqsave(&prcmu_lock, flags);
662 val = readl(_PRCMU_BASE + reg);
663 val = ((val & ~mask) | (value & mask));
664 writel(val, (_PRCMU_BASE + reg));
665 spin_unlock_irqrestore(&prcmu_lock, flags);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200666}
667
Mattias Nilssonb58d12f2012-01-13 16:20:10 +0100668struct prcmu_fw_version *prcmu_get_fw_version(void)
669{
670 return fw_info.valid ? &fw_info.version : NULL;
671}
672
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200673bool prcmu_has_arm_maxopp(void)
674{
675 return (readb(tcdm_base + PRCM_AVS_VARM_MAX_OPP) &
676 PRCM_AVS_ISMODEENABLE_MASK) == PRCM_AVS_ISMODEENABLE_MASK;
677}
678
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200679/**
680 * prcmu_get_boot_status - PRCMU boot status checking
681 * Returns: the current PRCMU boot status
682 */
683int prcmu_get_boot_status(void)
684{
685 return readb(tcdm_base + PRCM_BOOT_STATUS);
686}
687
688/**
689 * prcmu_set_rc_a2p - This function is used to run few power state sequences
690 * @val: Value to be set, i.e. transition requested
691 * Returns: 0 on success, -EINVAL on invalid argument
692 *
693 * This function is used to run the following power state sequences -
694 * any state to ApReset, ApDeepSleep to ApExecute, ApExecute to ApDeepSleep
695 */
696int prcmu_set_rc_a2p(enum romcode_write val)
697{
698 if (val < RDY_2_DS || val > RDY_2_XP70_RST)
699 return -EINVAL;
700 writeb(val, (tcdm_base + PRCM_ROMCODE_A2P));
701 return 0;
702}
703
704/**
705 * prcmu_get_rc_p2a - This function is used to get power state sequences
706 * Returns: the power transition that has last happened
707 *
708 * This function can return the following transitions-
709 * any state to ApReset, ApDeepSleep to ApExecute, ApExecute to ApDeepSleep
710 */
711enum romcode_read prcmu_get_rc_p2a(void)
712{
713 return readb(tcdm_base + PRCM_ROMCODE_P2A);
714}
715
716/**
717 * prcmu_get_current_mode - Return the current XP70 power mode
718 * Returns: Returns the current AP(ARM) power mode: init,
719 * apBoot, apExecute, apDeepSleep, apSleep, apIdle, apReset
720 */
721enum ap_pwrst prcmu_get_xp70_current_state(void)
722{
723 return readb(tcdm_base + PRCM_XP70_CUR_PWR_STATE);
724}
725
726/**
727 * prcmu_config_clkout - Configure one of the programmable clock outputs.
728 * @clkout: The CLKOUT number (0 or 1).
729 * @source: The clock to be used (one of the PRCMU_CLKSRC_*).
730 * @div: The divider to be applied.
731 *
732 * Configures one of the programmable clock outputs (CLKOUTs).
733 * @div should be in the range [1,63] to request a configuration, or 0 to
734 * inform that the configuration is no longer requested.
735 */
736int prcmu_config_clkout(u8 clkout, u8 source, u8 div)
737{
738 static int requests[2];
739 int r = 0;
740 unsigned long flags;
741 u32 val;
742 u32 bits;
743 u32 mask;
744 u32 div_mask;
745
746 BUG_ON(clkout > 1);
747 BUG_ON(div > 63);
748 BUG_ON((clkout == 0) && (source > PRCMU_CLKSRC_CLK009));
749
750 if (!div && !requests[clkout])
751 return -EINVAL;
752
753 switch (clkout) {
754 case 0:
755 div_mask = PRCM_CLKOCR_CLKODIV0_MASK;
756 mask = (PRCM_CLKOCR_CLKODIV0_MASK | PRCM_CLKOCR_CLKOSEL0_MASK);
757 bits = ((source << PRCM_CLKOCR_CLKOSEL0_SHIFT) |
758 (div << PRCM_CLKOCR_CLKODIV0_SHIFT));
759 break;
760 case 1:
761 div_mask = PRCM_CLKOCR_CLKODIV1_MASK;
762 mask = (PRCM_CLKOCR_CLKODIV1_MASK | PRCM_CLKOCR_CLKOSEL1_MASK |
763 PRCM_CLKOCR_CLK1TYPE);
764 bits = ((source << PRCM_CLKOCR_CLKOSEL1_SHIFT) |
765 (div << PRCM_CLKOCR_CLKODIV1_SHIFT));
766 break;
767 }
768 bits &= mask;
769
770 spin_lock_irqsave(&clkout_lock, flags);
771
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200772 val = readl(PRCM_CLKOCR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200773 if (val & div_mask) {
774 if (div) {
775 if ((val & mask) != bits) {
776 r = -EBUSY;
777 goto unlock_and_return;
778 }
779 } else {
780 if ((val & mask & ~div_mask) != bits) {
781 r = -EINVAL;
782 goto unlock_and_return;
783 }
784 }
785 }
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200786 writel((bits | (val & ~mask)), PRCM_CLKOCR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200787 requests[clkout] += (div ? 1 : -1);
788
789unlock_and_return:
790 spin_unlock_irqrestore(&clkout_lock, flags);
791
792 return r;
793}
794
Mattias Nilsson73180f82011-08-12 10:28:10 +0200795int db8500_prcmu_set_power_state(u8 state, bool keep_ulp_clk, bool keep_ap_pll)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200796{
797 unsigned long flags;
798
799 BUG_ON((state < PRCMU_AP_SLEEP) || (PRCMU_AP_DEEP_IDLE < state));
800
801 spin_lock_irqsave(&mb0_transfer.lock, flags);
802
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200803 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200804 cpu_relax();
805
806 writeb(MB0H_POWER_STATE_TRANS, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
807 writeb(state, (tcdm_base + PRCM_REQ_MB0_AP_POWER_STATE));
808 writeb((keep_ap_pll ? 1 : 0), (tcdm_base + PRCM_REQ_MB0_AP_PLL_STATE));
809 writeb((keep_ulp_clk ? 1 : 0),
810 (tcdm_base + PRCM_REQ_MB0_ULP_CLOCK_STATE));
811 writeb(0, (tcdm_base + PRCM_REQ_MB0_DO_NOT_WFI));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200812 writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200813
814 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
815
816 return 0;
817}
818
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100819u8 db8500_prcmu_get_power_state_result(void)
820{
821 return readb(tcdm_base + PRCM_ACK_MB0_AP_PWRSTTR_STATUS);
822}
823
Daniel Lezcano485540d2012-02-20 12:30:26 +0100824#define PRCMU_A9_MASK_REQ 0x00000328
825#define PRCMU_A9_MASK_REQ_MASK 0x00000001
826#define PRCMU_GIC_DELAY 1
827
828/* This function decouple the gic from the prcmu */
829int db8500_prcmu_gic_decouple(void)
830{
831 u32 val = readl(_PRCMU_BASE + PRCMU_A9_MASK_REQ);
832
833 /* Set bit 0 register value to 1 */
834 writel(val | PRCMU_A9_MASK_REQ_MASK, _PRCMU_BASE + PRCMU_A9_MASK_REQ);
835
836 /* Make sure the register is updated */
837 readl(_PRCMU_BASE + PRCMU_A9_MASK_REQ);
838
839 /* Wait a few cycles for the gic mask completion */
840 udelay(PRCMU_GIC_DELAY);
841
842 return 0;
843}
844
845/* This function recouple the gic with the prcmu */
846int db8500_prcmu_gic_recouple(void)
847{
848 u32 val = readl(_PRCMU_BASE + PRCMU_A9_MASK_REQ);
849
850 /* Set bit 0 register value to 0 */
851 writel(val & ~PRCMU_A9_MASK_REQ_MASK, _PRCMU_BASE + PRCMU_A9_MASK_REQ);
852
853 return 0;
854}
855
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200856/* This function should only be called while mb0_transfer.lock is held. */
857static void config_wakeups(void)
858{
859 const u8 header[2] = {
860 MB0H_CONFIG_WAKEUPS_EXE,
861 MB0H_CONFIG_WAKEUPS_SLEEP
862 };
863 static u32 last_dbb_events;
864 static u32 last_abb_events;
865 u32 dbb_events;
866 u32 abb_events;
867 unsigned int i;
868
869 dbb_events = mb0_transfer.req.dbb_irqs | mb0_transfer.req.dbb_wakeups;
870 dbb_events |= (WAKEUP_BIT_AC_WAKE_ACK | WAKEUP_BIT_AC_SLEEP_ACK);
871
872 abb_events = mb0_transfer.req.abb_events;
873
874 if ((dbb_events == last_dbb_events) && (abb_events == last_abb_events))
875 return;
876
877 for (i = 0; i < 2; i++) {
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200878 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200879 cpu_relax();
880 writel(dbb_events, (tcdm_base + PRCM_REQ_MB0_WAKEUP_8500));
881 writel(abb_events, (tcdm_base + PRCM_REQ_MB0_WAKEUP_4500));
882 writeb(header[i], (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200883 writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200884 }
885 last_dbb_events = dbb_events;
886 last_abb_events = abb_events;
887}
888
Mattias Nilsson73180f82011-08-12 10:28:10 +0200889void db8500_prcmu_enable_wakeups(u32 wakeups)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200890{
891 unsigned long flags;
892 u32 bits;
893 int i;
894
895 BUG_ON(wakeups != (wakeups & VALID_WAKEUPS));
896
897 for (i = 0, bits = 0; i < NUM_PRCMU_WAKEUP_INDICES; i++) {
898 if (wakeups & BIT(i))
899 bits |= prcmu_wakeup_bit[i];
900 }
901
902 spin_lock_irqsave(&mb0_transfer.lock, flags);
903
904 mb0_transfer.req.dbb_wakeups = bits;
905 config_wakeups();
906
907 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
908}
909
Mattias Nilsson73180f82011-08-12 10:28:10 +0200910void db8500_prcmu_config_abb_event_readout(u32 abb_events)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200911{
912 unsigned long flags;
913
914 spin_lock_irqsave(&mb0_transfer.lock, flags);
915
916 mb0_transfer.req.abb_events = abb_events;
917 config_wakeups();
918
919 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
920}
921
Mattias Nilsson73180f82011-08-12 10:28:10 +0200922void db8500_prcmu_get_abb_event_buffer(void __iomem **buf)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200923{
924 if (readb(tcdm_base + PRCM_ACK_MB0_READ_POINTER) & 1)
925 *buf = (tcdm_base + PRCM_ACK_MB0_WAKEUP_1_4500);
926 else
927 *buf = (tcdm_base + PRCM_ACK_MB0_WAKEUP_0_4500);
928}
929
930/**
Mattias Nilsson73180f82011-08-12 10:28:10 +0200931 * db8500_prcmu_set_arm_opp - set the appropriate ARM OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200932 * @opp: The new ARM operating point to which transition is to be made
933 * Returns: 0 on success, non-zero on failure
934 *
935 * This function sets the the operating point of the ARM.
936 */
Mattias Nilsson73180f82011-08-12 10:28:10 +0200937int db8500_prcmu_set_arm_opp(u8 opp)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200938{
939 int r;
940
941 if (opp < ARM_NO_CHANGE || opp > ARM_EXTCLK)
942 return -EINVAL;
943
944 r = 0;
945
946 mutex_lock(&mb1_transfer.lock);
947
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200948 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200949 cpu_relax();
950
951 writeb(MB1H_ARM_APE_OPP, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
952 writeb(opp, (tcdm_base + PRCM_REQ_MB1_ARM_OPP));
953 writeb(APE_NO_CHANGE, (tcdm_base + PRCM_REQ_MB1_APE_OPP));
954
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200955 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200956 wait_for_completion(&mb1_transfer.work);
957
958 if ((mb1_transfer.ack.header != MB1H_ARM_APE_OPP) ||
959 (mb1_transfer.ack.arm_opp != opp))
960 r = -EIO;
961
962 mutex_unlock(&mb1_transfer.lock);
963
964 return r;
965}
966
967/**
Mattias Nilsson73180f82011-08-12 10:28:10 +0200968 * db8500_prcmu_get_arm_opp - get the current ARM OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200969 *
970 * Returns: the current ARM OPP
971 */
Mattias Nilsson73180f82011-08-12 10:28:10 +0200972int db8500_prcmu_get_arm_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200973{
974 return readb(tcdm_base + PRCM_ACK_MB1_CURRENT_ARM_OPP);
975}
976
977/**
Mattias Nilsson05089012012-01-13 16:20:20 +0100978 * db8500_prcmu_get_ddr_opp - get the current DDR OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200979 *
980 * Returns: the current DDR OPP
981 */
Mattias Nilsson05089012012-01-13 16:20:20 +0100982int db8500_prcmu_get_ddr_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200983{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200984 return readb(PRCM_DDR_SUBSYS_APE_MINBW);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200985}
986
987/**
Mattias Nilsson05089012012-01-13 16:20:20 +0100988 * db8500_set_ddr_opp - set the appropriate DDR OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200989 * @opp: The new DDR operating point to which transition is to be made
990 * Returns: 0 on success, non-zero on failure
991 *
992 * This function sets the operating point of the DDR.
993 */
Mattias Nilsson05089012012-01-13 16:20:20 +0100994int db8500_prcmu_set_ddr_opp(u8 opp)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200995{
996 if (opp < DDR_100_OPP || opp > DDR_25_OPP)
997 return -EINVAL;
998 /* Changing the DDR OPP can hang the hardware pre-v21 */
999 if (cpu_is_u8500v20_or_later() && !cpu_is_u8500v20())
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001000 writeb(opp, PRCM_DDR_SUBSYS_APE_MINBW);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001001
1002 return 0;
1003}
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001004
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001005/* Divide the frequency of certain clocks by 2 for APE_50_PARTLY_25_OPP. */
1006static void request_even_slower_clocks(bool enable)
1007{
1008 void __iomem *clock_reg[] = {
1009 PRCM_ACLK_MGT,
1010 PRCM_DMACLK_MGT
1011 };
1012 unsigned long flags;
1013 unsigned int i;
1014
1015 spin_lock_irqsave(&clk_mgt_lock, flags);
1016
1017 /* Grab the HW semaphore. */
1018 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
1019 cpu_relax();
1020
1021 for (i = 0; i < ARRAY_SIZE(clock_reg); i++) {
1022 u32 val;
1023 u32 div;
1024
1025 val = readl(clock_reg[i]);
1026 div = (val & PRCM_CLK_MGT_CLKPLLDIV_MASK);
1027 if (enable) {
1028 if ((div <= 1) || (div > 15)) {
1029 pr_err("prcmu: Bad clock divider %d in %s\n",
1030 div, __func__);
1031 goto unlock_and_return;
1032 }
1033 div <<= 1;
1034 } else {
1035 if (div <= 2)
1036 goto unlock_and_return;
1037 div >>= 1;
1038 }
1039 val = ((val & ~PRCM_CLK_MGT_CLKPLLDIV_MASK) |
1040 (div & PRCM_CLK_MGT_CLKPLLDIV_MASK));
1041 writel(val, clock_reg[i]);
1042 }
1043
1044unlock_and_return:
1045 /* Release the HW semaphore. */
1046 writel(0, PRCM_SEM);
1047
1048 spin_unlock_irqrestore(&clk_mgt_lock, flags);
1049}
1050
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001051/**
Mattias Nilsson05089012012-01-13 16:20:20 +01001052 * db8500_set_ape_opp - set the appropriate APE OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001053 * @opp: The new APE operating point to which transition is to be made
1054 * Returns: 0 on success, non-zero on failure
1055 *
1056 * This function sets the operating point of the APE.
1057 */
Mattias Nilsson05089012012-01-13 16:20:20 +01001058int db8500_prcmu_set_ape_opp(u8 opp)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001059{
1060 int r = 0;
1061
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001062 if (opp == mb1_transfer.ape_opp)
1063 return 0;
1064
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001065 mutex_lock(&mb1_transfer.lock);
1066
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001067 if (mb1_transfer.ape_opp == APE_50_PARTLY_25_OPP)
1068 request_even_slower_clocks(false);
1069
1070 if ((opp != APE_100_OPP) && (mb1_transfer.ape_opp != APE_100_OPP))
1071 goto skip_message;
1072
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001073 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001074 cpu_relax();
1075
1076 writeb(MB1H_ARM_APE_OPP, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1077 writeb(ARM_NO_CHANGE, (tcdm_base + PRCM_REQ_MB1_ARM_OPP));
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001078 writeb(((opp == APE_50_PARTLY_25_OPP) ? APE_50_OPP : opp),
1079 (tcdm_base + PRCM_REQ_MB1_APE_OPP));
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001080
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001081 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001082 wait_for_completion(&mb1_transfer.work);
1083
1084 if ((mb1_transfer.ack.header != MB1H_ARM_APE_OPP) ||
1085 (mb1_transfer.ack.ape_opp != opp))
1086 r = -EIO;
1087
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001088skip_message:
1089 if ((!r && (opp == APE_50_PARTLY_25_OPP)) ||
1090 (r && (mb1_transfer.ape_opp == APE_50_PARTLY_25_OPP)))
1091 request_even_slower_clocks(true);
1092 if (!r)
1093 mb1_transfer.ape_opp = opp;
1094
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001095 mutex_unlock(&mb1_transfer.lock);
1096
1097 return r;
1098}
1099
1100/**
Mattias Nilsson05089012012-01-13 16:20:20 +01001101 * db8500_prcmu_get_ape_opp - get the current APE OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001102 *
1103 * Returns: the current APE OPP
1104 */
Mattias Nilsson05089012012-01-13 16:20:20 +01001105int db8500_prcmu_get_ape_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001106{
1107 return readb(tcdm_base + PRCM_ACK_MB1_CURRENT_APE_OPP);
1108}
1109
1110/**
1111 * prcmu_request_ape_opp_100_voltage - Request APE OPP 100% voltage
1112 * @enable: true to request the higher voltage, false to drop a request.
1113 *
1114 * Calls to this function to enable and disable requests must be balanced.
1115 */
1116int prcmu_request_ape_opp_100_voltage(bool enable)
1117{
1118 int r = 0;
1119 u8 header;
1120 static unsigned int requests;
1121
1122 mutex_lock(&mb1_transfer.lock);
1123
1124 if (enable) {
1125 if (0 != requests++)
1126 goto unlock_and_return;
1127 header = MB1H_REQUEST_APE_OPP_100_VOLT;
1128 } else {
1129 if (requests == 0) {
1130 r = -EIO;
1131 goto unlock_and_return;
1132 } else if (1 != requests--) {
1133 goto unlock_and_return;
1134 }
1135 header = MB1H_RELEASE_APE_OPP_100_VOLT;
1136 }
1137
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001138 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001139 cpu_relax();
1140
1141 writeb(header, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1142
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001143 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001144 wait_for_completion(&mb1_transfer.work);
1145
1146 if ((mb1_transfer.ack.header != header) ||
1147 ((mb1_transfer.ack.ape_voltage_status & BIT(0)) != 0))
1148 r = -EIO;
1149
1150unlock_and_return:
1151 mutex_unlock(&mb1_transfer.lock);
1152
1153 return r;
1154}
1155
1156/**
1157 * prcmu_release_usb_wakeup_state - release the state required by a USB wakeup
1158 *
1159 * This function releases the power state requirements of a USB wakeup.
1160 */
1161int prcmu_release_usb_wakeup_state(void)
1162{
1163 int r = 0;
1164
1165 mutex_lock(&mb1_transfer.lock);
1166
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001167 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001168 cpu_relax();
1169
1170 writeb(MB1H_RELEASE_USB_WAKEUP,
1171 (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1172
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001173 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001174 wait_for_completion(&mb1_transfer.work);
1175
1176 if ((mb1_transfer.ack.header != MB1H_RELEASE_USB_WAKEUP) ||
1177 ((mb1_transfer.ack.ape_voltage_status & BIT(0)) != 0))
1178 r = -EIO;
1179
1180 mutex_unlock(&mb1_transfer.lock);
1181
1182 return r;
1183}
1184
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001185static int request_pll(u8 clock, bool enable)
1186{
1187 int r = 0;
1188
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001189 if (clock == PRCMU_PLLSOC0)
1190 clock = (enable ? PLL_SOC0_ON : PLL_SOC0_OFF);
1191 else if (clock == PRCMU_PLLSOC1)
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001192 clock = (enable ? PLL_SOC1_ON : PLL_SOC1_OFF);
1193 else
1194 return -EINVAL;
1195
1196 mutex_lock(&mb1_transfer.lock);
1197
1198 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
1199 cpu_relax();
1200
1201 writeb(MB1H_PLL_ON_OFF, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1202 writeb(clock, (tcdm_base + PRCM_REQ_MB1_PLL_ON_OFF));
1203
1204 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
1205 wait_for_completion(&mb1_transfer.work);
1206
1207 if (mb1_transfer.ack.header != MB1H_PLL_ON_OFF)
1208 r = -EIO;
1209
1210 mutex_unlock(&mb1_transfer.lock);
1211
1212 return r;
1213}
1214
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001215/**
Bengt Jonsson0b9199e2011-08-12 10:28:25 +02001216 * prcmu_set_hwacc - set the power state of a h/w accelerator
1217 * @hwacc_dev: The hardware accelerator (enum hw_acc_dev).
1218 * @state: The new power state (enum hw_acc_state).
1219 *
1220 * This function sets the power state of a hardware accelerator.
1221 * This function should not be called from interrupt context.
1222 *
1223 * NOTE! Deprecated, to be removed when all users switched over to use the
1224 * regulator framework API.
1225 */
1226int prcmu_set_hwacc(u16 hwacc_dev, u8 state)
1227{
1228 int r = 0;
1229 bool ram_retention = false;
1230 bool enable, enable_ret;
1231
1232 /* check argument */
1233 BUG_ON(hwacc_dev >= NUM_HW_ACC);
1234
1235 /* get state of switches */
1236 enable = hwacc_enabled[hwacc_dev];
1237 enable_ret = hwacc_ret_enabled[hwacc_dev];
1238
1239 /* set flag if retention is possible */
1240 switch (hwacc_dev) {
1241 case HW_ACC_SVAMMDSP:
1242 case HW_ACC_SIAMMDSP:
1243 case HW_ACC_ESRAM1:
1244 case HW_ACC_ESRAM2:
1245 case HW_ACC_ESRAM3:
1246 case HW_ACC_ESRAM4:
1247 ram_retention = true;
1248 break;
1249 }
1250
1251 /* check argument */
1252 BUG_ON(state > HW_ON);
1253 BUG_ON(state == HW_OFF_RAMRET && !ram_retention);
1254
1255 /* modify enable flags */
1256 switch (state) {
1257 case HW_OFF:
1258 enable_ret = false;
1259 enable = false;
1260 break;
1261 case HW_ON:
1262 enable = true;
1263 break;
1264 case HW_OFF_RAMRET:
1265 enable_ret = true;
1266 enable = false;
1267 break;
1268 }
1269
1270 /* get regulator (lazy) */
1271 if (hwacc_regulator[hwacc_dev] == NULL) {
1272 hwacc_regulator[hwacc_dev] = regulator_get(NULL,
1273 hwacc_regulator_name[hwacc_dev]);
1274 if (IS_ERR(hwacc_regulator[hwacc_dev])) {
1275 pr_err("prcmu: failed to get supply %s\n",
1276 hwacc_regulator_name[hwacc_dev]);
1277 r = PTR_ERR(hwacc_regulator[hwacc_dev]);
1278 goto out;
1279 }
1280 }
1281
1282 if (ram_retention) {
1283 if (hwacc_ret_regulator[hwacc_dev] == NULL) {
1284 hwacc_ret_regulator[hwacc_dev] = regulator_get(NULL,
1285 hwacc_ret_regulator_name[hwacc_dev]);
1286 if (IS_ERR(hwacc_ret_regulator[hwacc_dev])) {
1287 pr_err("prcmu: failed to get supply %s\n",
1288 hwacc_ret_regulator_name[hwacc_dev]);
1289 r = PTR_ERR(hwacc_ret_regulator[hwacc_dev]);
1290 goto out;
1291 }
1292 }
1293 }
1294
1295 /* set regulators */
1296 if (ram_retention) {
1297 if (enable_ret && !hwacc_ret_enabled[hwacc_dev]) {
1298 r = regulator_enable(hwacc_ret_regulator[hwacc_dev]);
1299 if (r < 0) {
1300 pr_err("prcmu_set_hwacc: ret enable failed\n");
1301 goto out;
1302 }
1303 hwacc_ret_enabled[hwacc_dev] = true;
1304 }
1305 }
1306
1307 if (enable && !hwacc_enabled[hwacc_dev]) {
1308 r = regulator_enable(hwacc_regulator[hwacc_dev]);
1309 if (r < 0) {
1310 pr_err("prcmu_set_hwacc: enable failed\n");
1311 goto out;
1312 }
1313 hwacc_enabled[hwacc_dev] = true;
1314 }
1315
1316 if (!enable && hwacc_enabled[hwacc_dev]) {
1317 r = regulator_disable(hwacc_regulator[hwacc_dev]);
1318 if (r < 0) {
1319 pr_err("prcmu_set_hwacc: disable failed\n");
1320 goto out;
1321 }
1322 hwacc_enabled[hwacc_dev] = false;
1323 }
1324
1325 if (ram_retention) {
1326 if (!enable_ret && hwacc_ret_enabled[hwacc_dev]) {
1327 r = regulator_disable(hwacc_ret_regulator[hwacc_dev]);
1328 if (r < 0) {
1329 pr_err("prcmu_set_hwacc: ret disable failed\n");
1330 goto out;
1331 }
1332 hwacc_ret_enabled[hwacc_dev] = false;
1333 }
1334 }
1335
1336out:
1337 return r;
1338}
1339EXPORT_SYMBOL(prcmu_set_hwacc);
1340
1341/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02001342 * db8500_prcmu_set_epod - set the state of a EPOD (power domain)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001343 * @epod_id: The EPOD to set
1344 * @epod_state: The new EPOD state
1345 *
1346 * This function sets the state of a EPOD (power domain). It may not be called
1347 * from interrupt context.
1348 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02001349int db8500_prcmu_set_epod(u16 epod_id, u8 epod_state)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001350{
1351 int r = 0;
1352 bool ram_retention = false;
1353 int i;
1354
1355 /* check argument */
1356 BUG_ON(epod_id >= NUM_EPOD_ID);
1357
1358 /* set flag if retention is possible */
1359 switch (epod_id) {
1360 case EPOD_ID_SVAMMDSP:
1361 case EPOD_ID_SIAMMDSP:
1362 case EPOD_ID_ESRAM12:
1363 case EPOD_ID_ESRAM34:
1364 ram_retention = true;
1365 break;
1366 }
1367
1368 /* check argument */
1369 BUG_ON(epod_state > EPOD_STATE_ON);
1370 BUG_ON(epod_state == EPOD_STATE_RAMRET && !ram_retention);
1371
1372 /* get lock */
1373 mutex_lock(&mb2_transfer.lock);
1374
1375 /* wait for mailbox */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001376 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(2))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001377 cpu_relax();
1378
1379 /* fill in mailbox */
1380 for (i = 0; i < NUM_EPOD_ID; i++)
1381 writeb(EPOD_STATE_NO_CHANGE, (tcdm_base + PRCM_REQ_MB2 + i));
1382 writeb(epod_state, (tcdm_base + PRCM_REQ_MB2 + epod_id));
1383
1384 writeb(MB2H_DPS, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB2));
1385
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001386 writel(MBOX_BIT(2), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001387
1388 /*
1389 * The current firmware version does not handle errors correctly,
1390 * and we cannot recover if there is an error.
1391 * This is expected to change when the firmware is updated.
1392 */
1393 if (!wait_for_completion_timeout(&mb2_transfer.work,
1394 msecs_to_jiffies(20000))) {
1395 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
1396 __func__);
1397 r = -EIO;
1398 goto unlock_and_return;
1399 }
1400
1401 if (mb2_transfer.ack.status != HWACC_PWR_ST_OK)
1402 r = -EIO;
1403
1404unlock_and_return:
1405 mutex_unlock(&mb2_transfer.lock);
1406 return r;
1407}
1408
1409/**
1410 * prcmu_configure_auto_pm - Configure autonomous power management.
1411 * @sleep: Configuration for ApSleep.
1412 * @idle: Configuration for ApIdle.
1413 */
1414void prcmu_configure_auto_pm(struct prcmu_auto_pm_config *sleep,
1415 struct prcmu_auto_pm_config *idle)
1416{
1417 u32 sleep_cfg;
1418 u32 idle_cfg;
1419 unsigned long flags;
1420
1421 BUG_ON((sleep == NULL) || (idle == NULL));
1422
1423 sleep_cfg = (sleep->sva_auto_pm_enable & 0xF);
1424 sleep_cfg = ((sleep_cfg << 4) | (sleep->sia_auto_pm_enable & 0xF));
1425 sleep_cfg = ((sleep_cfg << 8) | (sleep->sva_power_on & 0xFF));
1426 sleep_cfg = ((sleep_cfg << 8) | (sleep->sia_power_on & 0xFF));
1427 sleep_cfg = ((sleep_cfg << 4) | (sleep->sva_policy & 0xF));
1428 sleep_cfg = ((sleep_cfg << 4) | (sleep->sia_policy & 0xF));
1429
1430 idle_cfg = (idle->sva_auto_pm_enable & 0xF);
1431 idle_cfg = ((idle_cfg << 4) | (idle->sia_auto_pm_enable & 0xF));
1432 idle_cfg = ((idle_cfg << 8) | (idle->sva_power_on & 0xFF));
1433 idle_cfg = ((idle_cfg << 8) | (idle->sia_power_on & 0xFF));
1434 idle_cfg = ((idle_cfg << 4) | (idle->sva_policy & 0xF));
1435 idle_cfg = ((idle_cfg << 4) | (idle->sia_policy & 0xF));
1436
1437 spin_lock_irqsave(&mb2_transfer.auto_pm_lock, flags);
1438
1439 /*
1440 * The autonomous power management configuration is done through
1441 * fields in mailbox 2, but these fields are only used as shared
1442 * variables - i.e. there is no need to send a message.
1443 */
1444 writel(sleep_cfg, (tcdm_base + PRCM_REQ_MB2_AUTO_PM_SLEEP));
1445 writel(idle_cfg, (tcdm_base + PRCM_REQ_MB2_AUTO_PM_IDLE));
1446
1447 mb2_transfer.auto_pm_enabled =
1448 ((sleep->sva_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1449 (sleep->sia_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1450 (idle->sva_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1451 (idle->sia_auto_pm_enable == PRCMU_AUTO_PM_ON));
1452
1453 spin_unlock_irqrestore(&mb2_transfer.auto_pm_lock, flags);
1454}
1455EXPORT_SYMBOL(prcmu_configure_auto_pm);
1456
1457bool prcmu_is_auto_pm_enabled(void)
1458{
1459 return mb2_transfer.auto_pm_enabled;
1460}
1461
1462static int request_sysclk(bool enable)
1463{
1464 int r;
1465 unsigned long flags;
1466
1467 r = 0;
1468
1469 mutex_lock(&mb3_transfer.sysclk_lock);
1470
1471 spin_lock_irqsave(&mb3_transfer.lock, flags);
1472
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001473 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(3))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001474 cpu_relax();
1475
1476 writeb((enable ? ON : OFF), (tcdm_base + PRCM_REQ_MB3_SYSCLK_MGT));
1477
1478 writeb(MB3H_SYSCLK, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB3));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001479 writel(MBOX_BIT(3), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001480
1481 spin_unlock_irqrestore(&mb3_transfer.lock, flags);
1482
1483 /*
1484 * The firmware only sends an ACK if we want to enable the
1485 * SysClk, and it succeeds.
1486 */
1487 if (enable && !wait_for_completion_timeout(&mb3_transfer.sysclk_work,
1488 msecs_to_jiffies(20000))) {
1489 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
1490 __func__);
1491 r = -EIO;
1492 }
1493
1494 mutex_unlock(&mb3_transfer.sysclk_lock);
1495
1496 return r;
1497}
1498
1499static int request_timclk(bool enable)
1500{
1501 u32 val = (PRCM_TCR_DOZE_MODE | PRCM_TCR_TENSEL_MASK);
1502
1503 if (!enable)
1504 val |= PRCM_TCR_STOP_TIMERS;
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001505 writel(val, PRCM_TCR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001506
1507 return 0;
1508}
1509
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001510static int request_clock(u8 clock, bool enable)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001511{
1512 u32 val;
1513 unsigned long flags;
1514
1515 spin_lock_irqsave(&clk_mgt_lock, flags);
1516
1517 /* Grab the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001518 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001519 cpu_relax();
1520
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001521 val = readl(clk_mgt[clock].reg);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001522 if (enable) {
1523 val |= (PRCM_CLK_MGT_CLKEN | clk_mgt[clock].pllsw);
1524 } else {
1525 clk_mgt[clock].pllsw = (val & PRCM_CLK_MGT_CLKPLLSW_MASK);
1526 val &= ~(PRCM_CLK_MGT_CLKEN | PRCM_CLK_MGT_CLKPLLSW_MASK);
1527 }
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001528 writel(val, clk_mgt[clock].reg);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001529
1530 /* Release the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001531 writel(0, PRCM_SEM);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001532
1533 spin_unlock_irqrestore(&clk_mgt_lock, flags);
1534
1535 return 0;
1536}
1537
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001538static int request_sga_clock(u8 clock, bool enable)
1539{
1540 u32 val;
1541 int ret;
1542
1543 if (enable) {
1544 val = readl(PRCM_CGATING_BYPASS);
1545 writel(val | PRCM_CGATING_BYPASS_ICN2, PRCM_CGATING_BYPASS);
1546 }
1547
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001548 ret = request_clock(clock, enable);
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001549
1550 if (!ret && !enable) {
1551 val = readl(PRCM_CGATING_BYPASS);
1552 writel(val & ~PRCM_CGATING_BYPASS_ICN2, PRCM_CGATING_BYPASS);
1553 }
1554
1555 return ret;
1556}
1557
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001558static inline bool plldsi_locked(void)
1559{
1560 return (readl(PRCM_PLLDSI_LOCKP) &
1561 (PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP10 |
1562 PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP3)) ==
1563 (PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP10 |
1564 PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP3);
1565}
1566
1567static int request_plldsi(bool enable)
1568{
1569 int r = 0;
1570 u32 val;
1571
1572 writel((PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMP |
1573 PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMPI), (enable ?
1574 PRCM_MMIP_LS_CLAMP_CLR : PRCM_MMIP_LS_CLAMP_SET));
1575
1576 val = readl(PRCM_PLLDSI_ENABLE);
1577 if (enable)
1578 val |= PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1579 else
1580 val &= ~PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1581 writel(val, PRCM_PLLDSI_ENABLE);
1582
1583 if (enable) {
1584 unsigned int i;
1585 bool locked = plldsi_locked();
1586
1587 for (i = 10; !locked && (i > 0); --i) {
1588 udelay(100);
1589 locked = plldsi_locked();
1590 }
1591 if (locked) {
1592 writel(PRCM_APE_RESETN_DSIPLL_RESETN,
1593 PRCM_APE_RESETN_SET);
1594 } else {
1595 writel((PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMP |
1596 PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMPI),
1597 PRCM_MMIP_LS_CLAMP_SET);
1598 val &= ~PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1599 writel(val, PRCM_PLLDSI_ENABLE);
1600 r = -EAGAIN;
1601 }
1602 } else {
1603 writel(PRCM_APE_RESETN_DSIPLL_RESETN, PRCM_APE_RESETN_CLR);
1604 }
1605 return r;
1606}
1607
1608static int request_dsiclk(u8 n, bool enable)
1609{
1610 u32 val;
1611
1612 val = readl(PRCM_DSI_PLLOUT_SEL);
1613 val &= ~dsiclk[n].divsel_mask;
1614 val |= ((enable ? dsiclk[n].divsel : PRCM_DSI_PLLOUT_SEL_OFF) <<
1615 dsiclk[n].divsel_shift);
1616 writel(val, PRCM_DSI_PLLOUT_SEL);
1617 return 0;
1618}
1619
1620static int request_dsiescclk(u8 n, bool enable)
1621{
1622 u32 val;
1623
1624 val = readl(PRCM_DSITVCLK_DIV);
1625 enable ? (val |= dsiescclk[n].en) : (val &= ~dsiescclk[n].en);
1626 writel(val, PRCM_DSITVCLK_DIV);
1627 return 0;
1628}
1629
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001630/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02001631 * db8500_prcmu_request_clock() - Request for a clock to be enabled or disabled.
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001632 * @clock: The clock for which the request is made.
1633 * @enable: Whether the clock should be enabled (true) or disabled (false).
1634 *
1635 * This function should only be used by the clock implementation.
1636 * Do not use it from any other place!
1637 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02001638int db8500_prcmu_request_clock(u8 clock, bool enable)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001639{
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001640 if (clock == PRCMU_SGACLK)
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001641 return request_sga_clock(clock, enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001642 else if (clock < PRCMU_NUM_REG_CLOCKS)
1643 return request_clock(clock, enable);
1644 else if (clock == PRCMU_TIMCLK)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001645 return request_timclk(enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001646 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1647 return request_dsiclk((clock - PRCMU_DSI0CLK), enable);
1648 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1649 return request_dsiescclk((clock - PRCMU_DSI0ESCCLK), enable);
1650 else if (clock == PRCMU_PLLDSI)
1651 return request_plldsi(enable);
1652 else if (clock == PRCMU_SYSCLK)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001653 return request_sysclk(enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001654 else if ((clock == PRCMU_PLLSOC0) || (clock == PRCMU_PLLSOC1))
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001655 return request_pll(clock, enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001656 else
1657 return -EINVAL;
1658}
1659
1660static unsigned long pll_rate(void __iomem *reg, unsigned long src_rate,
1661 int branch)
1662{
1663 u64 rate;
1664 u32 val;
1665 u32 d;
1666 u32 div = 1;
1667
1668 val = readl(reg);
1669
1670 rate = src_rate;
1671 rate *= ((val & PRCM_PLL_FREQ_D_MASK) >> PRCM_PLL_FREQ_D_SHIFT);
1672
1673 d = ((val & PRCM_PLL_FREQ_N_MASK) >> PRCM_PLL_FREQ_N_SHIFT);
1674 if (d > 1)
1675 div *= d;
1676
1677 d = ((val & PRCM_PLL_FREQ_R_MASK) >> PRCM_PLL_FREQ_R_SHIFT);
1678 if (d > 1)
1679 div *= d;
1680
1681 if (val & PRCM_PLL_FREQ_SELDIV2)
1682 div *= 2;
1683
1684 if ((branch == PLL_FIX) || ((branch == PLL_DIV) &&
1685 (val & PRCM_PLL_FREQ_DIV2EN) &&
1686 ((reg == PRCM_PLLSOC0_FREQ) ||
1687 (reg == PRCM_PLLDDR_FREQ))))
1688 div *= 2;
1689
1690 (void)do_div(rate, div);
1691
1692 return (unsigned long)rate;
1693}
1694
1695#define ROOT_CLOCK_RATE 38400000
1696
1697static unsigned long clock_rate(u8 clock)
1698{
1699 u32 val;
1700 u32 pllsw;
1701 unsigned long rate = ROOT_CLOCK_RATE;
1702
1703 val = readl(clk_mgt[clock].reg);
1704
1705 if (val & PRCM_CLK_MGT_CLK38) {
1706 if (clk_mgt[clock].clk38div && (val & PRCM_CLK_MGT_CLK38DIV))
1707 rate /= 2;
1708 return rate;
Linus Walleije62ccf32011-10-10 12:14:14 +02001709 }
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001710
1711 val |= clk_mgt[clock].pllsw;
1712 pllsw = (val & PRCM_CLK_MGT_CLKPLLSW_MASK);
1713
1714 if (pllsw == PRCM_CLK_MGT_CLKPLLSW_SOC0)
1715 rate = pll_rate(PRCM_PLLSOC0_FREQ, rate, clk_mgt[clock].branch);
1716 else if (pllsw == PRCM_CLK_MGT_CLKPLLSW_SOC1)
1717 rate = pll_rate(PRCM_PLLSOC1_FREQ, rate, clk_mgt[clock].branch);
1718 else if (pllsw == PRCM_CLK_MGT_CLKPLLSW_DDR)
1719 rate = pll_rate(PRCM_PLLDDR_FREQ, rate, clk_mgt[clock].branch);
1720 else
1721 return 0;
1722
1723 if ((clock == PRCMU_SGACLK) &&
1724 (val & PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN)) {
1725 u64 r = (rate * 10);
1726
1727 (void)do_div(r, 25);
1728 return (unsigned long)r;
1729 }
1730 val &= PRCM_CLK_MGT_CLKPLLDIV_MASK;
1731 if (val)
1732 return rate / val;
1733 else
1734 return 0;
1735}
1736
1737static unsigned long dsiclk_rate(u8 n)
1738{
1739 u32 divsel;
1740 u32 div = 1;
1741
1742 divsel = readl(PRCM_DSI_PLLOUT_SEL);
1743 divsel = ((divsel & dsiclk[n].divsel_mask) >> dsiclk[n].divsel_shift);
1744
1745 if (divsel == PRCM_DSI_PLLOUT_SEL_OFF)
1746 divsel = dsiclk[n].divsel;
1747
1748 switch (divsel) {
1749 case PRCM_DSI_PLLOUT_SEL_PHI_4:
1750 div *= 2;
1751 case PRCM_DSI_PLLOUT_SEL_PHI_2:
1752 div *= 2;
1753 case PRCM_DSI_PLLOUT_SEL_PHI:
1754 return pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1755 PLL_RAW) / div;
1756 default:
1757 return 0;
1758 }
1759}
1760
1761static unsigned long dsiescclk_rate(u8 n)
1762{
1763 u32 div;
1764
1765 div = readl(PRCM_DSITVCLK_DIV);
1766 div = ((div & dsiescclk[n].div_mask) >> (dsiescclk[n].div_shift));
1767 return clock_rate(PRCMU_TVCLK) / max((u32)1, div);
1768}
1769
1770unsigned long prcmu_clock_rate(u8 clock)
1771{
Linus Walleije62ccf32011-10-10 12:14:14 +02001772 if (clock < PRCMU_NUM_REG_CLOCKS)
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001773 return clock_rate(clock);
1774 else if (clock == PRCMU_TIMCLK)
1775 return ROOT_CLOCK_RATE / 16;
1776 else if (clock == PRCMU_SYSCLK)
1777 return ROOT_CLOCK_RATE;
1778 else if (clock == PRCMU_PLLSOC0)
1779 return pll_rate(PRCM_PLLSOC0_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
1780 else if (clock == PRCMU_PLLSOC1)
1781 return pll_rate(PRCM_PLLSOC1_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
1782 else if (clock == PRCMU_PLLDDR)
1783 return pll_rate(PRCM_PLLDDR_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
1784 else if (clock == PRCMU_PLLDSI)
1785 return pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1786 PLL_RAW);
1787 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1788 return dsiclk_rate(clock - PRCMU_DSI0CLK);
1789 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1790 return dsiescclk_rate(clock - PRCMU_DSI0ESCCLK);
1791 else
1792 return 0;
1793}
1794
1795static unsigned long clock_source_rate(u32 clk_mgt_val, int branch)
1796{
1797 if (clk_mgt_val & PRCM_CLK_MGT_CLK38)
1798 return ROOT_CLOCK_RATE;
1799 clk_mgt_val &= PRCM_CLK_MGT_CLKPLLSW_MASK;
1800 if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_SOC0)
1801 return pll_rate(PRCM_PLLSOC0_FREQ, ROOT_CLOCK_RATE, branch);
1802 else if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_SOC1)
1803 return pll_rate(PRCM_PLLSOC1_FREQ, ROOT_CLOCK_RATE, branch);
1804 else if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_DDR)
1805 return pll_rate(PRCM_PLLDDR_FREQ, ROOT_CLOCK_RATE, branch);
1806 else
1807 return 0;
1808}
1809
1810static u32 clock_divider(unsigned long src_rate, unsigned long rate)
1811{
1812 u32 div;
1813
1814 div = (src_rate / rate);
1815 if (div == 0)
1816 return 1;
1817 if (rate < (src_rate / div))
1818 div++;
1819 return div;
1820}
1821
1822static long round_clock_rate(u8 clock, unsigned long rate)
1823{
1824 u32 val;
1825 u32 div;
1826 unsigned long src_rate;
1827 long rounded_rate;
1828
1829 val = readl(clk_mgt[clock].reg);
1830 src_rate = clock_source_rate((val | clk_mgt[clock].pllsw),
1831 clk_mgt[clock].branch);
1832 div = clock_divider(src_rate, rate);
1833 if (val & PRCM_CLK_MGT_CLK38) {
1834 if (clk_mgt[clock].clk38div) {
1835 if (div > 2)
1836 div = 2;
1837 } else {
1838 div = 1;
1839 }
1840 } else if ((clock == PRCMU_SGACLK) && (div == 3)) {
1841 u64 r = (src_rate * 10);
1842
1843 (void)do_div(r, 25);
1844 if (r <= rate)
1845 return (unsigned long)r;
1846 }
1847 rounded_rate = (src_rate / min(div, (u32)31));
1848
1849 return rounded_rate;
1850}
1851
1852#define MIN_PLL_VCO_RATE 600000000ULL
1853#define MAX_PLL_VCO_RATE 1680640000ULL
1854
1855static long round_plldsi_rate(unsigned long rate)
1856{
1857 long rounded_rate = 0;
1858 unsigned long src_rate;
1859 unsigned long rem;
1860 u32 r;
1861
1862 src_rate = clock_rate(PRCMU_HDMICLK);
1863 rem = rate;
1864
1865 for (r = 7; (rem > 0) && (r > 0); r--) {
1866 u64 d;
1867
1868 d = (r * rate);
1869 (void)do_div(d, src_rate);
1870 if (d < 6)
1871 d = 6;
1872 else if (d > 255)
1873 d = 255;
1874 d *= src_rate;
1875 if (((2 * d) < (r * MIN_PLL_VCO_RATE)) ||
1876 ((r * MAX_PLL_VCO_RATE) < (2 * d)))
1877 continue;
1878 (void)do_div(d, r);
1879 if (rate < d) {
1880 if (rounded_rate == 0)
1881 rounded_rate = (long)d;
1882 break;
1883 }
1884 if ((rate - d) < rem) {
1885 rem = (rate - d);
1886 rounded_rate = (long)d;
1887 }
1888 }
1889 return rounded_rate;
1890}
1891
1892static long round_dsiclk_rate(unsigned long rate)
1893{
1894 u32 div;
1895 unsigned long src_rate;
1896 long rounded_rate;
1897
1898 src_rate = pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1899 PLL_RAW);
1900 div = clock_divider(src_rate, rate);
1901 rounded_rate = (src_rate / ((div > 2) ? 4 : div));
1902
1903 return rounded_rate;
1904}
1905
1906static long round_dsiescclk_rate(unsigned long rate)
1907{
1908 u32 div;
1909 unsigned long src_rate;
1910 long rounded_rate;
1911
1912 src_rate = clock_rate(PRCMU_TVCLK);
1913 div = clock_divider(src_rate, rate);
1914 rounded_rate = (src_rate / min(div, (u32)255));
1915
1916 return rounded_rate;
1917}
1918
1919long prcmu_round_clock_rate(u8 clock, unsigned long rate)
1920{
1921 if (clock < PRCMU_NUM_REG_CLOCKS)
1922 return round_clock_rate(clock, rate);
1923 else if (clock == PRCMU_PLLDSI)
1924 return round_plldsi_rate(rate);
1925 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1926 return round_dsiclk_rate(rate);
1927 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1928 return round_dsiescclk_rate(rate);
1929 else
1930 return (long)prcmu_clock_rate(clock);
1931}
1932
1933static void set_clock_rate(u8 clock, unsigned long rate)
1934{
1935 u32 val;
1936 u32 div;
1937 unsigned long src_rate;
1938 unsigned long flags;
1939
1940 spin_lock_irqsave(&clk_mgt_lock, flags);
1941
1942 /* Grab the HW semaphore. */
1943 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
1944 cpu_relax();
1945
1946 val = readl(clk_mgt[clock].reg);
1947 src_rate = clock_source_rate((val | clk_mgt[clock].pllsw),
1948 clk_mgt[clock].branch);
1949 div = clock_divider(src_rate, rate);
1950 if (val & PRCM_CLK_MGT_CLK38) {
1951 if (clk_mgt[clock].clk38div) {
1952 if (div > 1)
1953 val |= PRCM_CLK_MGT_CLK38DIV;
1954 else
1955 val &= ~PRCM_CLK_MGT_CLK38DIV;
1956 }
1957 } else if (clock == PRCMU_SGACLK) {
1958 val &= ~(PRCM_CLK_MGT_CLKPLLDIV_MASK |
1959 PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN);
1960 if (div == 3) {
1961 u64 r = (src_rate * 10);
1962
1963 (void)do_div(r, 25);
1964 if (r <= rate) {
1965 val |= PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN;
1966 div = 0;
1967 }
1968 }
1969 val |= min(div, (u32)31);
1970 } else {
1971 val &= ~PRCM_CLK_MGT_CLKPLLDIV_MASK;
1972 val |= min(div, (u32)31);
1973 }
1974 writel(val, clk_mgt[clock].reg);
1975
1976 /* Release the HW semaphore. */
1977 writel(0, PRCM_SEM);
1978
1979 spin_unlock_irqrestore(&clk_mgt_lock, flags);
1980}
1981
1982static int set_plldsi_rate(unsigned long rate)
1983{
1984 unsigned long src_rate;
1985 unsigned long rem;
1986 u32 pll_freq = 0;
1987 u32 r;
1988
1989 src_rate = clock_rate(PRCMU_HDMICLK);
1990 rem = rate;
1991
1992 for (r = 7; (rem > 0) && (r > 0); r--) {
1993 u64 d;
1994 u64 hwrate;
1995
1996 d = (r * rate);
1997 (void)do_div(d, src_rate);
1998 if (d < 6)
1999 d = 6;
2000 else if (d > 255)
2001 d = 255;
2002 hwrate = (d * src_rate);
2003 if (((2 * hwrate) < (r * MIN_PLL_VCO_RATE)) ||
2004 ((r * MAX_PLL_VCO_RATE) < (2 * hwrate)))
2005 continue;
2006 (void)do_div(hwrate, r);
2007 if (rate < hwrate) {
2008 if (pll_freq == 0)
2009 pll_freq = (((u32)d << PRCM_PLL_FREQ_D_SHIFT) |
2010 (r << PRCM_PLL_FREQ_R_SHIFT));
2011 break;
2012 }
2013 if ((rate - hwrate) < rem) {
2014 rem = (rate - hwrate);
2015 pll_freq = (((u32)d << PRCM_PLL_FREQ_D_SHIFT) |
2016 (r << PRCM_PLL_FREQ_R_SHIFT));
2017 }
2018 }
2019 if (pll_freq == 0)
2020 return -EINVAL;
2021
2022 pll_freq |= (1 << PRCM_PLL_FREQ_N_SHIFT);
2023 writel(pll_freq, PRCM_PLLDSI_FREQ);
2024
2025 return 0;
2026}
2027
2028static void set_dsiclk_rate(u8 n, unsigned long rate)
2029{
2030 u32 val;
2031 u32 div;
2032
2033 div = clock_divider(pll_rate(PRCM_PLLDSI_FREQ,
2034 clock_rate(PRCMU_HDMICLK), PLL_RAW), rate);
2035
2036 dsiclk[n].divsel = (div == 1) ? PRCM_DSI_PLLOUT_SEL_PHI :
2037 (div == 2) ? PRCM_DSI_PLLOUT_SEL_PHI_2 :
2038 /* else */ PRCM_DSI_PLLOUT_SEL_PHI_4;
2039
2040 val = readl(PRCM_DSI_PLLOUT_SEL);
2041 val &= ~dsiclk[n].divsel_mask;
2042 val |= (dsiclk[n].divsel << dsiclk[n].divsel_shift);
2043 writel(val, PRCM_DSI_PLLOUT_SEL);
2044}
2045
2046static void set_dsiescclk_rate(u8 n, unsigned long rate)
2047{
2048 u32 val;
2049 u32 div;
2050
2051 div = clock_divider(clock_rate(PRCMU_TVCLK), rate);
2052 val = readl(PRCM_DSITVCLK_DIV);
2053 val &= ~dsiescclk[n].div_mask;
2054 val |= (min(div, (u32)255) << dsiescclk[n].div_shift);
2055 writel(val, PRCM_DSITVCLK_DIV);
2056}
2057
2058int prcmu_set_clock_rate(u8 clock, unsigned long rate)
2059{
2060 if (clock < PRCMU_NUM_REG_CLOCKS)
2061 set_clock_rate(clock, rate);
2062 else if (clock == PRCMU_PLLDSI)
2063 return set_plldsi_rate(rate);
2064 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
2065 set_dsiclk_rate((clock - PRCMU_DSI0CLK), rate);
2066 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
2067 set_dsiescclk_rate((clock - PRCMU_DSI0ESCCLK), rate);
2068 return 0;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002069}
2070
Mattias Nilsson73180f82011-08-12 10:28:10 +02002071int db8500_prcmu_config_esram0_deep_sleep(u8 state)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002072{
2073 if ((state > ESRAM0_DEEP_SLEEP_STATE_RET) ||
2074 (state < ESRAM0_DEEP_SLEEP_STATE_OFF))
2075 return -EINVAL;
2076
2077 mutex_lock(&mb4_transfer.lock);
2078
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002079 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002080 cpu_relax();
2081
2082 writeb(MB4H_MEM_ST, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2083 writeb(((DDR_PWR_STATE_OFFHIGHLAT << 4) | DDR_PWR_STATE_ON),
2084 (tcdm_base + PRCM_REQ_MB4_DDR_ST_AP_SLEEP_IDLE));
2085 writeb(DDR_PWR_STATE_ON,
2086 (tcdm_base + PRCM_REQ_MB4_DDR_ST_AP_DEEP_IDLE));
2087 writeb(state, (tcdm_base + PRCM_REQ_MB4_ESRAM0_ST));
2088
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002089 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002090 wait_for_completion(&mb4_transfer.work);
2091
2092 mutex_unlock(&mb4_transfer.lock);
2093
2094 return 0;
2095}
2096
Mattias Nilsson05089012012-01-13 16:20:20 +01002097int db8500_prcmu_config_hotdog(u8 threshold)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002098{
2099 mutex_lock(&mb4_transfer.lock);
2100
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002101 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002102 cpu_relax();
2103
2104 writeb(threshold, (tcdm_base + PRCM_REQ_MB4_HOTDOG_THRESHOLD));
2105 writeb(MB4H_HOTDOG, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2106
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002107 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002108 wait_for_completion(&mb4_transfer.work);
2109
2110 mutex_unlock(&mb4_transfer.lock);
2111
2112 return 0;
2113}
2114
Mattias Nilsson05089012012-01-13 16:20:20 +01002115int db8500_prcmu_config_hotmon(u8 low, u8 high)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002116{
2117 mutex_lock(&mb4_transfer.lock);
2118
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002119 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002120 cpu_relax();
2121
2122 writeb(low, (tcdm_base + PRCM_REQ_MB4_HOTMON_LOW));
2123 writeb(high, (tcdm_base + PRCM_REQ_MB4_HOTMON_HIGH));
2124 writeb((HOTMON_CONFIG_LOW | HOTMON_CONFIG_HIGH),
2125 (tcdm_base + PRCM_REQ_MB4_HOTMON_CONFIG));
2126 writeb(MB4H_HOTMON, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2127
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002128 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002129 wait_for_completion(&mb4_transfer.work);
2130
2131 mutex_unlock(&mb4_transfer.lock);
2132
2133 return 0;
2134}
2135
2136static int config_hot_period(u16 val)
2137{
2138 mutex_lock(&mb4_transfer.lock);
2139
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002140 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002141 cpu_relax();
2142
2143 writew(val, (tcdm_base + PRCM_REQ_MB4_HOT_PERIOD));
2144 writeb(MB4H_HOT_PERIOD, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2145
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002146 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002147 wait_for_completion(&mb4_transfer.work);
2148
2149 mutex_unlock(&mb4_transfer.lock);
2150
2151 return 0;
2152}
2153
Mattias Nilsson05089012012-01-13 16:20:20 +01002154int db8500_prcmu_start_temp_sense(u16 cycles32k)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002155{
2156 if (cycles32k == 0xFFFF)
2157 return -EINVAL;
2158
2159 return config_hot_period(cycles32k);
2160}
2161
Mattias Nilsson05089012012-01-13 16:20:20 +01002162int db8500_prcmu_stop_temp_sense(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002163{
2164 return config_hot_period(0xFFFF);
2165}
2166
Jonas Aberg84165b82011-08-12 10:28:33 +02002167static int prcmu_a9wdog(u8 cmd, u8 d0, u8 d1, u8 d2, u8 d3)
2168{
2169
2170 mutex_lock(&mb4_transfer.lock);
2171
2172 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
2173 cpu_relax();
2174
2175 writeb(d0, (tcdm_base + PRCM_REQ_MB4_A9WDOG_0));
2176 writeb(d1, (tcdm_base + PRCM_REQ_MB4_A9WDOG_1));
2177 writeb(d2, (tcdm_base + PRCM_REQ_MB4_A9WDOG_2));
2178 writeb(d3, (tcdm_base + PRCM_REQ_MB4_A9WDOG_3));
2179
2180 writeb(cmd, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2181
2182 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
2183 wait_for_completion(&mb4_transfer.work);
2184
2185 mutex_unlock(&mb4_transfer.lock);
2186
2187 return 0;
2188
2189}
2190
Mattias Nilsson05089012012-01-13 16:20:20 +01002191int db8500_prcmu_config_a9wdog(u8 num, bool sleep_auto_off)
Jonas Aberg84165b82011-08-12 10:28:33 +02002192{
2193 BUG_ON(num == 0 || num > 0xf);
2194 return prcmu_a9wdog(MB4H_A9WDOG_CONF, num, 0, 0,
2195 sleep_auto_off ? A9WDOG_AUTO_OFF_EN :
2196 A9WDOG_AUTO_OFF_DIS);
2197}
2198
Mattias Nilsson05089012012-01-13 16:20:20 +01002199int db8500_prcmu_enable_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002200{
2201 return prcmu_a9wdog(MB4H_A9WDOG_EN, id, 0, 0, 0);
2202}
2203
Mattias Nilsson05089012012-01-13 16:20:20 +01002204int db8500_prcmu_disable_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002205{
2206 return prcmu_a9wdog(MB4H_A9WDOG_DIS, id, 0, 0, 0);
2207}
2208
Mattias Nilsson05089012012-01-13 16:20:20 +01002209int db8500_prcmu_kick_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002210{
2211 return prcmu_a9wdog(MB4H_A9WDOG_KICK, id, 0, 0, 0);
2212}
2213
2214/*
2215 * timeout is 28 bit, in ms.
2216 */
Mattias Nilsson05089012012-01-13 16:20:20 +01002217int db8500_prcmu_load_a9wdog(u8 id, u32 timeout)
Jonas Aberg84165b82011-08-12 10:28:33 +02002218{
Jonas Aberg84165b82011-08-12 10:28:33 +02002219 return prcmu_a9wdog(MB4H_A9WDOG_LOAD,
2220 (id & A9WDOG_ID_MASK) |
2221 /*
2222 * Put the lowest 28 bits of timeout at
2223 * offset 4. Four first bits are used for id.
2224 */
2225 (u8)((timeout << 4) & 0xf0),
2226 (u8)((timeout >> 4) & 0xff),
2227 (u8)((timeout >> 12) & 0xff),
2228 (u8)((timeout >> 20) & 0xff));
2229}
2230
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002231/**
Linus Walleije3726fc2010-08-19 12:36:01 +01002232 * prcmu_abb_read() - Read register value(s) from the ABB.
2233 * @slave: The I2C slave address.
2234 * @reg: The (start) register address.
2235 * @value: The read out value(s).
2236 * @size: The number of registers to read.
2237 *
2238 * Reads register value(s) from the ABB.
2239 * @size has to be 1 for the current firmware version.
2240 */
2241int prcmu_abb_read(u8 slave, u8 reg, u8 *value, u8 size)
2242{
2243 int r;
2244
2245 if (size != 1)
2246 return -EINVAL;
2247
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002248 mutex_lock(&mb5_transfer.lock);
Linus Walleije3726fc2010-08-19 12:36:01 +01002249
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002250 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(5))
Linus Walleije3726fc2010-08-19 12:36:01 +01002251 cpu_relax();
2252
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002253 writeb(PRCMU_I2C_READ(slave), (tcdm_base + PRCM_REQ_MB5_I2C_SLAVE_OP));
2254 writeb(PRCMU_I2C_STOP_EN, (tcdm_base + PRCM_REQ_MB5_I2C_HW_BITS));
2255 writeb(reg, (tcdm_base + PRCM_REQ_MB5_I2C_REG));
2256 writeb(0, (tcdm_base + PRCM_REQ_MB5_I2C_VAL));
Linus Walleije3726fc2010-08-19 12:36:01 +01002257
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002258 writel(MBOX_BIT(5), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002259
Linus Walleije3726fc2010-08-19 12:36:01 +01002260 if (!wait_for_completion_timeout(&mb5_transfer.work,
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002261 msecs_to_jiffies(20000))) {
2262 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
2263 __func__);
Linus Walleije3726fc2010-08-19 12:36:01 +01002264 r = -EIO;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002265 } else {
2266 r = ((mb5_transfer.ack.status == I2C_RD_OK) ? 0 : -EIO);
Linus Walleije3726fc2010-08-19 12:36:01 +01002267 }
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002268
Linus Walleije3726fc2010-08-19 12:36:01 +01002269 if (!r)
2270 *value = mb5_transfer.ack.value;
2271
Linus Walleije3726fc2010-08-19 12:36:01 +01002272 mutex_unlock(&mb5_transfer.lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002273
Linus Walleije3726fc2010-08-19 12:36:01 +01002274 return r;
2275}
Linus Walleije3726fc2010-08-19 12:36:01 +01002276
2277/**
2278 * prcmu_abb_write() - Write register value(s) to the ABB.
2279 * @slave: The I2C slave address.
2280 * @reg: The (start) register address.
2281 * @value: The value(s) to write.
2282 * @size: The number of registers to write.
2283 *
2284 * Reads register value(s) from the ABB.
2285 * @size has to be 1 for the current firmware version.
2286 */
2287int prcmu_abb_write(u8 slave, u8 reg, u8 *value, u8 size)
2288{
2289 int r;
2290
2291 if (size != 1)
2292 return -EINVAL;
2293
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002294 mutex_lock(&mb5_transfer.lock);
Linus Walleije3726fc2010-08-19 12:36:01 +01002295
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002296 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(5))
Linus Walleije3726fc2010-08-19 12:36:01 +01002297 cpu_relax();
2298
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002299 writeb(PRCMU_I2C_WRITE(slave), (tcdm_base + PRCM_REQ_MB5_I2C_SLAVE_OP));
2300 writeb(PRCMU_I2C_STOP_EN, (tcdm_base + PRCM_REQ_MB5_I2C_HW_BITS));
2301 writeb(reg, (tcdm_base + PRCM_REQ_MB5_I2C_REG));
2302 writeb(*value, (tcdm_base + PRCM_REQ_MB5_I2C_VAL));
Linus Walleije3726fc2010-08-19 12:36:01 +01002303
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002304 writel(MBOX_BIT(5), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002305
Linus Walleije3726fc2010-08-19 12:36:01 +01002306 if (!wait_for_completion_timeout(&mb5_transfer.work,
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002307 msecs_to_jiffies(20000))) {
2308 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
2309 __func__);
Linus Walleije3726fc2010-08-19 12:36:01 +01002310 r = -EIO;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002311 } else {
2312 r = ((mb5_transfer.ack.status == I2C_WR_OK) ? 0 : -EIO);
Linus Walleije3726fc2010-08-19 12:36:01 +01002313 }
Linus Walleije3726fc2010-08-19 12:36:01 +01002314
Linus Walleije3726fc2010-08-19 12:36:01 +01002315 mutex_unlock(&mb5_transfer.lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002316
Linus Walleije3726fc2010-08-19 12:36:01 +01002317 return r;
2318}
Linus Walleije3726fc2010-08-19 12:36:01 +01002319
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002320/**
2321 * prcmu_ac_wake_req - should be called whenever ARM wants to wakeup Modem
2322 */
2323void prcmu_ac_wake_req(void)
Martin Perssone0befb22010-12-08 15:13:28 +01002324{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002325 u32 val;
Mattias Nilssond6e30022011-08-12 10:28:43 +02002326 u32 status;
Martin Perssone0befb22010-12-08 15:13:28 +01002327
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002328 mutex_lock(&mb0_transfer.ac_wake_lock);
Martin Perssone0befb22010-12-08 15:13:28 +01002329
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002330 val = readl(PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002331 if (val & PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ)
2332 goto unlock_and_return;
2333
2334 atomic_set(&ac_wake_req_state, 1);
2335
Mattias Nilssond6e30022011-08-12 10:28:43 +02002336retry:
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002337 writel((val | PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ), PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002338
2339 if (!wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
Mattias Nilssond6e30022011-08-12 10:28:43 +02002340 msecs_to_jiffies(5000))) {
Linus Walleij57265bc2011-10-10 13:04:44 +02002341 pr_crit("prcmu: %s timed out (5 s) waiting for a reply.\n",
Mattias Nilssond6e30022011-08-12 10:28:43 +02002342 __func__);
2343 goto unlock_and_return;
2344 }
2345
2346 /*
2347 * The modem can generate an AC_WAKE_ACK, and then still go to sleep.
2348 * As a workaround, we wait, and then check that the modem is indeed
2349 * awake (in terms of the value of the PRCM_MOD_AWAKE_STATUS
2350 * register, which may not be the whole truth).
2351 */
2352 udelay(400);
2353 status = (readl(PRCM_MOD_AWAKE_STATUS) & BITS(0, 2));
2354 if (status != (PRCM_MOD_AWAKE_STATUS_PRCM_MOD_AAPD_AWAKE |
2355 PRCM_MOD_AWAKE_STATUS_PRCM_MOD_COREPD_AWAKE)) {
2356 pr_err("prcmu: %s received ack, but modem not awake (0x%X).\n",
2357 __func__, status);
2358 udelay(1200);
2359 writel(val, PRCM_HOSTACCESS_REQ);
2360 if (wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
2361 msecs_to_jiffies(5000)))
2362 goto retry;
Linus Walleij57265bc2011-10-10 13:04:44 +02002363 pr_crit("prcmu: %s timed out (5 s) waiting for AC_SLEEP_ACK.\n",
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002364 __func__);
2365 }
2366
2367unlock_and_return:
2368 mutex_unlock(&mb0_transfer.ac_wake_lock);
2369}
2370
2371/**
2372 * prcmu_ac_sleep_req - called when ARM no longer needs to talk to modem
2373 */
2374void prcmu_ac_sleep_req()
2375{
2376 u32 val;
2377
2378 mutex_lock(&mb0_transfer.ac_wake_lock);
2379
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002380 val = readl(PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002381 if (!(val & PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ))
2382 goto unlock_and_return;
2383
2384 writel((val & ~PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ),
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002385 PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002386
2387 if (!wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
Mattias Nilssond6e30022011-08-12 10:28:43 +02002388 msecs_to_jiffies(5000))) {
Linus Walleij57265bc2011-10-10 13:04:44 +02002389 pr_crit("prcmu: %s timed out (5 s) waiting for a reply.\n",
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002390 __func__);
2391 }
2392
2393 atomic_set(&ac_wake_req_state, 0);
2394
2395unlock_and_return:
2396 mutex_unlock(&mb0_transfer.ac_wake_lock);
2397}
2398
Mattias Nilsson73180f82011-08-12 10:28:10 +02002399bool db8500_prcmu_is_ac_wake_requested(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002400{
2401 return (atomic_read(&ac_wake_req_state) != 0);
2402}
2403
2404/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02002405 * db8500_prcmu_system_reset - System reset
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002406 *
Mattias Nilsson73180f82011-08-12 10:28:10 +02002407 * Saves the reset reason code and then sets the APE_SOFTRST register which
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002408 * fires interrupt to fw
2409 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02002410void db8500_prcmu_system_reset(u16 reset_code)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002411{
2412 writew(reset_code, (tcdm_base + PRCM_SW_RST_REASON));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002413 writel(1, PRCM_APE_SOFTRST);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002414}
2415
2416/**
Sebastian Rasmussen597045d2011-08-12 10:28:53 +02002417 * db8500_prcmu_get_reset_code - Retrieve SW reset reason code
2418 *
2419 * Retrieves the reset reason code stored by prcmu_system_reset() before
2420 * last restart.
2421 */
2422u16 db8500_prcmu_get_reset_code(void)
2423{
2424 return readw(tcdm_base + PRCM_SW_RST_REASON);
2425}
2426
2427/**
Mattias Nilsson05089012012-01-13 16:20:20 +01002428 * db8500_prcmu_reset_modem - ask the PRCMU to reset modem
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002429 */
Mattias Nilsson05089012012-01-13 16:20:20 +01002430void db8500_prcmu_modem_reset(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002431{
Martin Perssone0befb22010-12-08 15:13:28 +01002432 mutex_lock(&mb1_transfer.lock);
2433
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002434 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Martin Perssone0befb22010-12-08 15:13:28 +01002435 cpu_relax();
2436
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002437 writeb(MB1H_RESET_MODEM, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002438 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Martin Perssone0befb22010-12-08 15:13:28 +01002439 wait_for_completion(&mb1_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002440
2441 /*
2442 * No need to check return from PRCMU as modem should go in reset state
2443 * This state is already managed by upper layer
2444 */
Martin Perssone0befb22010-12-08 15:13:28 +01002445
2446 mutex_unlock(&mb1_transfer.lock);
Martin Perssone0befb22010-12-08 15:13:28 +01002447}
2448
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002449static void ack_dbb_wakeup(void)
Martin Perssone0befb22010-12-08 15:13:28 +01002450{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002451 unsigned long flags;
Martin Perssone0befb22010-12-08 15:13:28 +01002452
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002453 spin_lock_irqsave(&mb0_transfer.lock, flags);
Martin Perssone0befb22010-12-08 15:13:28 +01002454
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002455 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002456 cpu_relax();
Martin Perssone0befb22010-12-08 15:13:28 +01002457
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002458 writeb(MB0H_READ_WAKEUP_ACK, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002459 writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
Martin Perssone0befb22010-12-08 15:13:28 +01002460
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002461 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
Martin Perssone0befb22010-12-08 15:13:28 +01002462}
2463
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002464static inline void print_unknown_header_warning(u8 n, u8 header)
Linus Walleije3726fc2010-08-19 12:36:01 +01002465{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002466 pr_warning("prcmu: Unknown message header (%d) in mailbox %d.\n",
2467 header, n);
Linus Walleije3726fc2010-08-19 12:36:01 +01002468}
2469
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002470static bool read_mailbox_0(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002471{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002472 bool r;
2473 u32 ev;
2474 unsigned int n;
2475 u8 header;
2476
2477 header = readb(tcdm_base + PRCM_MBOX_HEADER_ACK_MB0);
2478 switch (header) {
2479 case MB0H_WAKEUP_EXE:
2480 case MB0H_WAKEUP_SLEEP:
2481 if (readb(tcdm_base + PRCM_ACK_MB0_READ_POINTER) & 1)
2482 ev = readl(tcdm_base + PRCM_ACK_MB0_WAKEUP_1_8500);
2483 else
2484 ev = readl(tcdm_base + PRCM_ACK_MB0_WAKEUP_0_8500);
2485
2486 if (ev & (WAKEUP_BIT_AC_WAKE_ACK | WAKEUP_BIT_AC_SLEEP_ACK))
2487 complete(&mb0_transfer.ac_wake_work);
2488 if (ev & WAKEUP_BIT_SYSCLK_OK)
2489 complete(&mb3_transfer.sysclk_work);
2490
2491 ev &= mb0_transfer.req.dbb_irqs;
2492
2493 for (n = 0; n < NUM_PRCMU_WAKEUPS; n++) {
2494 if (ev & prcmu_irq_bit[n])
2495 generic_handle_irq(IRQ_PRCMU_BASE + n);
2496 }
2497 r = true;
2498 break;
2499 default:
2500 print_unknown_header_warning(0, header);
2501 r = false;
2502 break;
2503 }
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002504 writel(MBOX_BIT(0), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002505 return r;
2506}
2507
2508static bool read_mailbox_1(void)
2509{
2510 mb1_transfer.ack.header = readb(tcdm_base + PRCM_MBOX_HEADER_REQ_MB1);
2511 mb1_transfer.ack.arm_opp = readb(tcdm_base +
2512 PRCM_ACK_MB1_CURRENT_ARM_OPP);
2513 mb1_transfer.ack.ape_opp = readb(tcdm_base +
2514 PRCM_ACK_MB1_CURRENT_APE_OPP);
2515 mb1_transfer.ack.ape_voltage_status = readb(tcdm_base +
2516 PRCM_ACK_MB1_APE_VOLTAGE_STATUS);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002517 writel(MBOX_BIT(1), PRCM_ARM_IT1_CLR);
Martin Perssone0befb22010-12-08 15:13:28 +01002518 complete(&mb1_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002519 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002520}
2521
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002522static bool read_mailbox_2(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002523{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002524 mb2_transfer.ack.status = readb(tcdm_base + PRCM_ACK_MB2_DPS_STATUS);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002525 writel(MBOX_BIT(2), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002526 complete(&mb2_transfer.work);
2527 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002528}
2529
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002530static bool read_mailbox_3(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002531{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002532 writel(MBOX_BIT(3), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002533 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002534}
2535
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002536static bool read_mailbox_4(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002537{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002538 u8 header;
2539 bool do_complete = true;
2540
2541 header = readb(tcdm_base + PRCM_MBOX_HEADER_REQ_MB4);
2542 switch (header) {
2543 case MB4H_MEM_ST:
2544 case MB4H_HOTDOG:
2545 case MB4H_HOTMON:
2546 case MB4H_HOT_PERIOD:
Mattias Nilssona592c2e2011-08-12 10:27:41 +02002547 case MB4H_A9WDOG_CONF:
2548 case MB4H_A9WDOG_EN:
2549 case MB4H_A9WDOG_DIS:
2550 case MB4H_A9WDOG_LOAD:
2551 case MB4H_A9WDOG_KICK:
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002552 break;
2553 default:
2554 print_unknown_header_warning(4, header);
2555 do_complete = false;
2556 break;
2557 }
2558
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002559 writel(MBOX_BIT(4), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002560
2561 if (do_complete)
2562 complete(&mb4_transfer.work);
2563
2564 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002565}
2566
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002567static bool read_mailbox_5(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002568{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002569 mb5_transfer.ack.status = readb(tcdm_base + PRCM_ACK_MB5_I2C_STATUS);
2570 mb5_transfer.ack.value = readb(tcdm_base + PRCM_ACK_MB5_I2C_VAL);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002571 writel(MBOX_BIT(5), PRCM_ARM_IT1_CLR);
Linus Walleije3726fc2010-08-19 12:36:01 +01002572 complete(&mb5_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002573 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002574}
2575
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002576static bool read_mailbox_6(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002577{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002578 writel(MBOX_BIT(6), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002579 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002580}
2581
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002582static bool read_mailbox_7(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002583{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002584 writel(MBOX_BIT(7), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002585 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002586}
2587
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002588static bool (* const read_mailbox[NUM_MB])(void) = {
Linus Walleije3726fc2010-08-19 12:36:01 +01002589 read_mailbox_0,
2590 read_mailbox_1,
2591 read_mailbox_2,
2592 read_mailbox_3,
2593 read_mailbox_4,
2594 read_mailbox_5,
2595 read_mailbox_6,
2596 read_mailbox_7
2597};
2598
2599static irqreturn_t prcmu_irq_handler(int irq, void *data)
2600{
2601 u32 bits;
2602 u8 n;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002603 irqreturn_t r;
Linus Walleije3726fc2010-08-19 12:36:01 +01002604
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002605 bits = (readl(PRCM_ARM_IT1_VAL) & ALL_MBOX_BITS);
Linus Walleije3726fc2010-08-19 12:36:01 +01002606 if (unlikely(!bits))
2607 return IRQ_NONE;
2608
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002609 r = IRQ_HANDLED;
Linus Walleije3726fc2010-08-19 12:36:01 +01002610 for (n = 0; bits; n++) {
2611 if (bits & MBOX_BIT(n)) {
2612 bits -= MBOX_BIT(n);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002613 if (read_mailbox[n]())
2614 r = IRQ_WAKE_THREAD;
Linus Walleije3726fc2010-08-19 12:36:01 +01002615 }
2616 }
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002617 return r;
2618}
2619
2620static irqreturn_t prcmu_irq_thread_fn(int irq, void *data)
2621{
2622 ack_dbb_wakeup();
Linus Walleije3726fc2010-08-19 12:36:01 +01002623 return IRQ_HANDLED;
2624}
2625
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002626static void prcmu_mask_work(struct work_struct *work)
2627{
2628 unsigned long flags;
2629
2630 spin_lock_irqsave(&mb0_transfer.lock, flags);
2631
2632 config_wakeups();
2633
2634 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
2635}
2636
2637static void prcmu_irq_mask(struct irq_data *d)
2638{
2639 unsigned long flags;
2640
2641 spin_lock_irqsave(&mb0_transfer.dbb_irqs_lock, flags);
2642
2643 mb0_transfer.req.dbb_irqs &= ~prcmu_irq_bit[d->irq - IRQ_PRCMU_BASE];
2644
2645 spin_unlock_irqrestore(&mb0_transfer.dbb_irqs_lock, flags);
2646
2647 if (d->irq != IRQ_PRCMU_CA_SLEEP)
2648 schedule_work(&mb0_transfer.mask_work);
2649}
2650
2651static void prcmu_irq_unmask(struct irq_data *d)
2652{
2653 unsigned long flags;
2654
2655 spin_lock_irqsave(&mb0_transfer.dbb_irqs_lock, flags);
2656
2657 mb0_transfer.req.dbb_irqs |= prcmu_irq_bit[d->irq - IRQ_PRCMU_BASE];
2658
2659 spin_unlock_irqrestore(&mb0_transfer.dbb_irqs_lock, flags);
2660
2661 if (d->irq != IRQ_PRCMU_CA_SLEEP)
2662 schedule_work(&mb0_transfer.mask_work);
2663}
2664
2665static void noop(struct irq_data *d)
2666{
2667}
2668
2669static struct irq_chip prcmu_irq_chip = {
2670 .name = "prcmu",
2671 .irq_disable = prcmu_irq_mask,
2672 .irq_ack = noop,
2673 .irq_mask = prcmu_irq_mask,
2674 .irq_unmask = prcmu_irq_unmask,
2675};
2676
Mattias Nilssonb58d12f2012-01-13 16:20:10 +01002677static char *fw_project_name(u8 project)
2678{
2679 switch (project) {
2680 case PRCMU_FW_PROJECT_U8500:
2681 return "U8500";
2682 case PRCMU_FW_PROJECT_U8500_C2:
2683 return "U8500 C2";
2684 case PRCMU_FW_PROJECT_U9500:
2685 return "U9500";
2686 case PRCMU_FW_PROJECT_U9500_C2:
2687 return "U9500 C2";
2688 default:
2689 return "Unknown";
2690 }
2691}
2692
Mattias Nilsson73180f82011-08-12 10:28:10 +02002693void __init db8500_prcmu_early_init(void)
Mattias Wallinfcbd4582010-12-02 16:20:42 +01002694{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002695 unsigned int i;
Linus Walleij3e2762c2012-01-02 14:17:40 +01002696 if (cpu_is_u8500v2()) {
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002697 void *tcpm_base = ioremap_nocache(U8500_PRCMU_TCPM_BASE, SZ_4K);
2698
2699 if (tcpm_base != NULL) {
Linus Walleij3e2762c2012-01-02 14:17:40 +01002700 u32 version;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002701 version = readl(tcpm_base + PRCMU_FW_VERSION_OFFSET);
Mattias Nilssonb58d12f2012-01-13 16:20:10 +01002702 fw_info.version.project = version & 0xFF;
2703 fw_info.version.api_version = (version >> 8) & 0xFF;
2704 fw_info.version.func_version = (version >> 16) & 0xFF;
2705 fw_info.version.errata = (version >> 24) & 0xFF;
2706 fw_info.valid = true;
2707 pr_info("PRCMU firmware: %s, version %d.%d.%d\n",
2708 fw_project_name(fw_info.version.project),
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002709 (version >> 8) & 0xFF, (version >> 16) & 0xFF,
2710 (version >> 24) & 0xFF);
2711 iounmap(tcpm_base);
2712 }
2713
Mattias Wallinfcbd4582010-12-02 16:20:42 +01002714 tcdm_base = __io_address(U8500_PRCMU_TCDM_BASE);
2715 } else {
2716 pr_err("prcmu: Unsupported chip version\n");
2717 BUG();
2718 }
Mattias Wallinfcbd4582010-12-02 16:20:42 +01002719
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002720 spin_lock_init(&mb0_transfer.lock);
2721 spin_lock_init(&mb0_transfer.dbb_irqs_lock);
2722 mutex_init(&mb0_transfer.ac_wake_lock);
2723 init_completion(&mb0_transfer.ac_wake_work);
Martin Perssone0befb22010-12-08 15:13:28 +01002724 mutex_init(&mb1_transfer.lock);
2725 init_completion(&mb1_transfer.work);
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01002726 mb1_transfer.ape_opp = APE_NO_CHANGE;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002727 mutex_init(&mb2_transfer.lock);
2728 init_completion(&mb2_transfer.work);
2729 spin_lock_init(&mb2_transfer.auto_pm_lock);
2730 spin_lock_init(&mb3_transfer.lock);
2731 mutex_init(&mb3_transfer.sysclk_lock);
2732 init_completion(&mb3_transfer.sysclk_work);
2733 mutex_init(&mb4_transfer.lock);
2734 init_completion(&mb4_transfer.work);
Linus Walleije3726fc2010-08-19 12:36:01 +01002735 mutex_init(&mb5_transfer.lock);
2736 init_completion(&mb5_transfer.work);
2737
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002738 INIT_WORK(&mb0_transfer.mask_work, prcmu_mask_work);
Linus Walleije3726fc2010-08-19 12:36:01 +01002739
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002740 /* Initalize irqs. */
2741 for (i = 0; i < NUM_PRCMU_WAKEUPS; i++) {
2742 unsigned int irq;
2743
2744 irq = IRQ_PRCMU_BASE + i;
2745 irq_set_chip_and_handler(irq, &prcmu_irq_chip,
2746 handle_simple_irq);
2747 set_irq_flags(irq, IRQF_VALID);
2748 }
Linus Walleije3726fc2010-08-19 12:36:01 +01002749}
2750
Mattias Nilsson05089012012-01-13 16:20:20 +01002751static void __init init_prcm_registers(void)
Mattias Nilssond65e12d2011-08-12 10:27:50 +02002752{
2753 u32 val;
2754
2755 val = readl(PRCM_A9PL_FORCE_CLKEN);
2756 val &= ~(PRCM_A9PL_FORCE_CLKEN_PRCM_A9PL_FORCE_CLKEN |
2757 PRCM_A9PL_FORCE_CLKEN_PRCM_A9AXI_FORCE_CLKEN);
2758 writel(val, (PRCM_A9PL_FORCE_CLKEN));
2759}
2760
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002761/*
2762 * Power domain switches (ePODs) modeled as regulators for the DB8500 SoC
2763 */
2764static struct regulator_consumer_supply db8500_vape_consumers[] = {
2765 REGULATOR_SUPPLY("v-ape", NULL),
2766 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.0"),
2767 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.1"),
2768 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.2"),
2769 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.3"),
2770 /* "v-mmc" changed to "vcore" in the mainline kernel */
2771 REGULATOR_SUPPLY("vcore", "sdi0"),
2772 REGULATOR_SUPPLY("vcore", "sdi1"),
2773 REGULATOR_SUPPLY("vcore", "sdi2"),
2774 REGULATOR_SUPPLY("vcore", "sdi3"),
2775 REGULATOR_SUPPLY("vcore", "sdi4"),
2776 REGULATOR_SUPPLY("v-dma", "dma40.0"),
2777 REGULATOR_SUPPLY("v-ape", "ab8500-usb.0"),
2778 /* "v-uart" changed to "vcore" in the mainline kernel */
2779 REGULATOR_SUPPLY("vcore", "uart0"),
2780 REGULATOR_SUPPLY("vcore", "uart1"),
2781 REGULATOR_SUPPLY("vcore", "uart2"),
2782 REGULATOR_SUPPLY("v-ape", "nmk-ske-keypad.0"),
Bengt Jonsson992b1332012-01-13 16:20:36 +01002783 REGULATOR_SUPPLY("v-hsi", "ste_hsi.0"),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002784};
2785
2786static struct regulator_consumer_supply db8500_vsmps2_consumers[] = {
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002787 REGULATOR_SUPPLY("musb_1v8", "ab8500-usb.0"),
2788 /* AV8100 regulator */
2789 REGULATOR_SUPPLY("hdmi_1v8", "0-0070"),
2790};
2791
2792static struct regulator_consumer_supply db8500_b2r2_mcde_consumers[] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002793 REGULATOR_SUPPLY("vsupply", "b2r2_bus"),
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002794 REGULATOR_SUPPLY("vsupply", "mcde"),
2795};
2796
2797/* SVA MMDSP regulator switch */
2798static struct regulator_consumer_supply db8500_svammdsp_consumers[] = {
2799 REGULATOR_SUPPLY("sva-mmdsp", "cm_control"),
2800};
2801
2802/* SVA pipe regulator switch */
2803static struct regulator_consumer_supply db8500_svapipe_consumers[] = {
2804 REGULATOR_SUPPLY("sva-pipe", "cm_control"),
2805};
2806
2807/* SIA MMDSP regulator switch */
2808static struct regulator_consumer_supply db8500_siammdsp_consumers[] = {
2809 REGULATOR_SUPPLY("sia-mmdsp", "cm_control"),
2810};
2811
2812/* SIA pipe regulator switch */
2813static struct regulator_consumer_supply db8500_siapipe_consumers[] = {
2814 REGULATOR_SUPPLY("sia-pipe", "cm_control"),
2815};
2816
2817static struct regulator_consumer_supply db8500_sga_consumers[] = {
2818 REGULATOR_SUPPLY("v-mali", NULL),
2819};
2820
2821/* ESRAM1 and 2 regulator switch */
2822static struct regulator_consumer_supply db8500_esram12_consumers[] = {
2823 REGULATOR_SUPPLY("esram12", "cm_control"),
2824};
2825
2826/* ESRAM3 and 4 regulator switch */
2827static struct regulator_consumer_supply db8500_esram34_consumers[] = {
2828 REGULATOR_SUPPLY("v-esram34", "mcde"),
2829 REGULATOR_SUPPLY("esram34", "cm_control"),
Bengt Jonsson992b1332012-01-13 16:20:36 +01002830 REGULATOR_SUPPLY("lcla_esram", "dma40.0"),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002831};
2832
2833static struct regulator_init_data db8500_regulators[DB8500_NUM_REGULATORS] = {
2834 [DB8500_REGULATOR_VAPE] = {
2835 .constraints = {
2836 .name = "db8500-vape",
2837 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2838 },
2839 .consumer_supplies = db8500_vape_consumers,
2840 .num_consumer_supplies = ARRAY_SIZE(db8500_vape_consumers),
2841 },
2842 [DB8500_REGULATOR_VARM] = {
2843 .constraints = {
2844 .name = "db8500-varm",
2845 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2846 },
2847 },
2848 [DB8500_REGULATOR_VMODEM] = {
2849 .constraints = {
2850 .name = "db8500-vmodem",
2851 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2852 },
2853 },
2854 [DB8500_REGULATOR_VPLL] = {
2855 .constraints = {
2856 .name = "db8500-vpll",
2857 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2858 },
2859 },
2860 [DB8500_REGULATOR_VSMPS1] = {
2861 .constraints = {
2862 .name = "db8500-vsmps1",
2863 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2864 },
2865 },
2866 [DB8500_REGULATOR_VSMPS2] = {
2867 .constraints = {
2868 .name = "db8500-vsmps2",
2869 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2870 },
2871 .consumer_supplies = db8500_vsmps2_consumers,
2872 .num_consumer_supplies = ARRAY_SIZE(db8500_vsmps2_consumers),
2873 },
2874 [DB8500_REGULATOR_VSMPS3] = {
2875 .constraints = {
2876 .name = "db8500-vsmps3",
2877 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2878 },
2879 },
2880 [DB8500_REGULATOR_VRF1] = {
2881 .constraints = {
2882 .name = "db8500-vrf1",
2883 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2884 },
2885 },
2886 [DB8500_REGULATOR_SWITCH_SVAMMDSP] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002887 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002888 .constraints = {
2889 .name = "db8500-sva-mmdsp",
2890 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2891 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002892 .consumer_supplies = db8500_svammdsp_consumers,
2893 .num_consumer_supplies = ARRAY_SIZE(db8500_svammdsp_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002894 },
2895 [DB8500_REGULATOR_SWITCH_SVAMMDSPRET] = {
2896 .constraints = {
2897 /* "ret" means "retention" */
2898 .name = "db8500-sva-mmdsp-ret",
2899 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2900 },
2901 },
2902 [DB8500_REGULATOR_SWITCH_SVAPIPE] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002903 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002904 .constraints = {
2905 .name = "db8500-sva-pipe",
2906 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2907 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002908 .consumer_supplies = db8500_svapipe_consumers,
2909 .num_consumer_supplies = ARRAY_SIZE(db8500_svapipe_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002910 },
2911 [DB8500_REGULATOR_SWITCH_SIAMMDSP] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002912 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002913 .constraints = {
2914 .name = "db8500-sia-mmdsp",
2915 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2916 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002917 .consumer_supplies = db8500_siammdsp_consumers,
2918 .num_consumer_supplies = ARRAY_SIZE(db8500_siammdsp_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002919 },
2920 [DB8500_REGULATOR_SWITCH_SIAMMDSPRET] = {
2921 .constraints = {
2922 .name = "db8500-sia-mmdsp-ret",
2923 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2924 },
2925 },
2926 [DB8500_REGULATOR_SWITCH_SIAPIPE] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002927 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002928 .constraints = {
2929 .name = "db8500-sia-pipe",
2930 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2931 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002932 .consumer_supplies = db8500_siapipe_consumers,
2933 .num_consumer_supplies = ARRAY_SIZE(db8500_siapipe_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002934 },
2935 [DB8500_REGULATOR_SWITCH_SGA] = {
2936 .supply_regulator = "db8500-vape",
2937 .constraints = {
2938 .name = "db8500-sga",
2939 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2940 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002941 .consumer_supplies = db8500_sga_consumers,
2942 .num_consumer_supplies = ARRAY_SIZE(db8500_sga_consumers),
2943
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002944 },
2945 [DB8500_REGULATOR_SWITCH_B2R2_MCDE] = {
2946 .supply_regulator = "db8500-vape",
2947 .constraints = {
2948 .name = "db8500-b2r2-mcde",
2949 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2950 },
2951 .consumer_supplies = db8500_b2r2_mcde_consumers,
2952 .num_consumer_supplies = ARRAY_SIZE(db8500_b2r2_mcde_consumers),
2953 },
2954 [DB8500_REGULATOR_SWITCH_ESRAM12] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002955 /*
2956 * esram12 is set in retention and supplied by Vsafe when Vape is off,
2957 * no need to hold Vape
2958 */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002959 .constraints = {
2960 .name = "db8500-esram12",
2961 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2962 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002963 .consumer_supplies = db8500_esram12_consumers,
2964 .num_consumer_supplies = ARRAY_SIZE(db8500_esram12_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002965 },
2966 [DB8500_REGULATOR_SWITCH_ESRAM12RET] = {
2967 .constraints = {
2968 .name = "db8500-esram12-ret",
2969 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2970 },
2971 },
2972 [DB8500_REGULATOR_SWITCH_ESRAM34] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002973 /*
2974 * esram34 is set in retention and supplied by Vsafe when Vape is off,
2975 * no need to hold Vape
2976 */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002977 .constraints = {
2978 .name = "db8500-esram34",
2979 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2980 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002981 .consumer_supplies = db8500_esram34_consumers,
2982 .num_consumer_supplies = ARRAY_SIZE(db8500_esram34_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002983 },
2984 [DB8500_REGULATOR_SWITCH_ESRAM34RET] = {
2985 .constraints = {
2986 .name = "db8500-esram34-ret",
2987 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2988 },
2989 },
2990};
2991
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002992static struct mfd_cell db8500_prcmu_devs[] = {
2993 {
2994 .name = "db8500-prcmu-regulators",
Mattias Wallin1ed78912011-05-27 11:49:43 +02002995 .platform_data = &db8500_regulators,
2996 .pdata_size = sizeof(db8500_regulators),
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002997 },
2998 {
2999 .name = "cpufreq-u8500",
3000 },
3001};
3002
3003/**
3004 * prcmu_fw_init - arch init call for the Linux PRCMU fw init logic
3005 *
3006 */
3007static int __init db8500_prcmu_probe(struct platform_device *pdev)
3008{
3009 int err = 0;
3010
3011 if (ux500_is_svp())
3012 return -ENODEV;
3013
Mattias Nilsson05089012012-01-13 16:20:20 +01003014 init_prcm_registers();
Mattias Nilssond65e12d2011-08-12 10:27:50 +02003015
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003016 /* Clean up the mailbox interrupts after pre-kernel code. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02003017 writel(ALL_MBOX_BITS, PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003018
3019 err = request_threaded_irq(IRQ_DB8500_PRCMU1, prcmu_irq_handler,
3020 prcmu_irq_thread_fn, IRQF_NO_SUSPEND, "prcmu", NULL);
3021 if (err < 0) {
3022 pr_err("prcmu: Failed to allocate IRQ_DB8500_PRCMU1.\n");
3023 err = -EBUSY;
3024 goto no_irq_return;
3025 }
3026
3027 if (cpu_is_u8500v20_or_later())
3028 prcmu_config_esram0_deep_sleep(ESRAM0_DEEP_SLEEP_STATE_RET);
3029
3030 err = mfd_add_devices(&pdev->dev, 0, db8500_prcmu_devs,
3031 ARRAY_SIZE(db8500_prcmu_devs), NULL,
3032 0);
3033
3034 if (err)
3035 pr_err("prcmu: Failed to add subdevices\n");
3036 else
3037 pr_info("DB8500 PRCMU initialized\n");
3038
3039no_irq_return:
3040 return err;
3041}
3042
3043static struct platform_driver db8500_prcmu_driver = {
3044 .driver = {
3045 .name = "db8500-prcmu",
3046 .owner = THIS_MODULE,
3047 },
3048};
3049
3050static int __init db8500_prcmu_init(void)
3051{
3052 return platform_driver_probe(&db8500_prcmu_driver, db8500_prcmu_probe);
3053}
3054
3055arch_initcall(db8500_prcmu_init);
3056
3057MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com>");
3058MODULE_DESCRIPTION("DB8500 PRCM Unit driver");
3059MODULE_LICENSE("GPL v2");