blob: 128b5f4a4fb0b3393c42234c3fa592c263c9a392 [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
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200824/* This function should only be called while mb0_transfer.lock is held. */
825static void config_wakeups(void)
826{
827 const u8 header[2] = {
828 MB0H_CONFIG_WAKEUPS_EXE,
829 MB0H_CONFIG_WAKEUPS_SLEEP
830 };
831 static u32 last_dbb_events;
832 static u32 last_abb_events;
833 u32 dbb_events;
834 u32 abb_events;
835 unsigned int i;
836
837 dbb_events = mb0_transfer.req.dbb_irqs | mb0_transfer.req.dbb_wakeups;
838 dbb_events |= (WAKEUP_BIT_AC_WAKE_ACK | WAKEUP_BIT_AC_SLEEP_ACK);
839
840 abb_events = mb0_transfer.req.abb_events;
841
842 if ((dbb_events == last_dbb_events) && (abb_events == last_abb_events))
843 return;
844
845 for (i = 0; i < 2; i++) {
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200846 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200847 cpu_relax();
848 writel(dbb_events, (tcdm_base + PRCM_REQ_MB0_WAKEUP_8500));
849 writel(abb_events, (tcdm_base + PRCM_REQ_MB0_WAKEUP_4500));
850 writeb(header[i], (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200851 writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200852 }
853 last_dbb_events = dbb_events;
854 last_abb_events = abb_events;
855}
856
Mattias Nilsson73180f82011-08-12 10:28:10 +0200857void db8500_prcmu_enable_wakeups(u32 wakeups)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200858{
859 unsigned long flags;
860 u32 bits;
861 int i;
862
863 BUG_ON(wakeups != (wakeups & VALID_WAKEUPS));
864
865 for (i = 0, bits = 0; i < NUM_PRCMU_WAKEUP_INDICES; i++) {
866 if (wakeups & BIT(i))
867 bits |= prcmu_wakeup_bit[i];
868 }
869
870 spin_lock_irqsave(&mb0_transfer.lock, flags);
871
872 mb0_transfer.req.dbb_wakeups = bits;
873 config_wakeups();
874
875 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
876}
877
Mattias Nilsson73180f82011-08-12 10:28:10 +0200878void db8500_prcmu_config_abb_event_readout(u32 abb_events)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200879{
880 unsigned long flags;
881
882 spin_lock_irqsave(&mb0_transfer.lock, flags);
883
884 mb0_transfer.req.abb_events = abb_events;
885 config_wakeups();
886
887 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
888}
889
Mattias Nilsson73180f82011-08-12 10:28:10 +0200890void db8500_prcmu_get_abb_event_buffer(void __iomem **buf)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200891{
892 if (readb(tcdm_base + PRCM_ACK_MB0_READ_POINTER) & 1)
893 *buf = (tcdm_base + PRCM_ACK_MB0_WAKEUP_1_4500);
894 else
895 *buf = (tcdm_base + PRCM_ACK_MB0_WAKEUP_0_4500);
896}
897
898/**
Mattias Nilsson73180f82011-08-12 10:28:10 +0200899 * db8500_prcmu_set_arm_opp - set the appropriate ARM OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200900 * @opp: The new ARM operating point to which transition is to be made
901 * Returns: 0 on success, non-zero on failure
902 *
903 * This function sets the the operating point of the ARM.
904 */
Mattias Nilsson73180f82011-08-12 10:28:10 +0200905int db8500_prcmu_set_arm_opp(u8 opp)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200906{
907 int r;
908
909 if (opp < ARM_NO_CHANGE || opp > ARM_EXTCLK)
910 return -EINVAL;
911
912 r = 0;
913
914 mutex_lock(&mb1_transfer.lock);
915
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200916 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200917 cpu_relax();
918
919 writeb(MB1H_ARM_APE_OPP, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
920 writeb(opp, (tcdm_base + PRCM_REQ_MB1_ARM_OPP));
921 writeb(APE_NO_CHANGE, (tcdm_base + PRCM_REQ_MB1_APE_OPP));
922
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200923 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200924 wait_for_completion(&mb1_transfer.work);
925
926 if ((mb1_transfer.ack.header != MB1H_ARM_APE_OPP) ||
927 (mb1_transfer.ack.arm_opp != opp))
928 r = -EIO;
929
930 mutex_unlock(&mb1_transfer.lock);
931
932 return r;
933}
934
935/**
Mattias Nilsson73180f82011-08-12 10:28:10 +0200936 * db8500_prcmu_get_arm_opp - get the current ARM OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200937 *
938 * Returns: the current ARM OPP
939 */
Mattias Nilsson73180f82011-08-12 10:28:10 +0200940int db8500_prcmu_get_arm_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200941{
942 return readb(tcdm_base + PRCM_ACK_MB1_CURRENT_ARM_OPP);
943}
944
945/**
Mattias Nilsson05089012012-01-13 16:20:20 +0100946 * db8500_prcmu_get_ddr_opp - get the current DDR OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200947 *
948 * Returns: the current DDR OPP
949 */
Mattias Nilsson05089012012-01-13 16:20:20 +0100950int db8500_prcmu_get_ddr_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200951{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200952 return readb(PRCM_DDR_SUBSYS_APE_MINBW);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200953}
954
955/**
Mattias Nilsson05089012012-01-13 16:20:20 +0100956 * db8500_set_ddr_opp - set the appropriate DDR OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200957 * @opp: The new DDR operating point to which transition is to be made
958 * Returns: 0 on success, non-zero on failure
959 *
960 * This function sets the operating point of the DDR.
961 */
Mattias Nilsson05089012012-01-13 16:20:20 +0100962int db8500_prcmu_set_ddr_opp(u8 opp)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200963{
964 if (opp < DDR_100_OPP || opp > DDR_25_OPP)
965 return -EINVAL;
966 /* Changing the DDR OPP can hang the hardware pre-v21 */
967 if (cpu_is_u8500v20_or_later() && !cpu_is_u8500v20())
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200968 writeb(opp, PRCM_DDR_SUBSYS_APE_MINBW);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200969
970 return 0;
971}
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100972
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100973/* Divide the frequency of certain clocks by 2 for APE_50_PARTLY_25_OPP. */
974static void request_even_slower_clocks(bool enable)
975{
976 void __iomem *clock_reg[] = {
977 PRCM_ACLK_MGT,
978 PRCM_DMACLK_MGT
979 };
980 unsigned long flags;
981 unsigned int i;
982
983 spin_lock_irqsave(&clk_mgt_lock, flags);
984
985 /* Grab the HW semaphore. */
986 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
987 cpu_relax();
988
989 for (i = 0; i < ARRAY_SIZE(clock_reg); i++) {
990 u32 val;
991 u32 div;
992
993 val = readl(clock_reg[i]);
994 div = (val & PRCM_CLK_MGT_CLKPLLDIV_MASK);
995 if (enable) {
996 if ((div <= 1) || (div > 15)) {
997 pr_err("prcmu: Bad clock divider %d in %s\n",
998 div, __func__);
999 goto unlock_and_return;
1000 }
1001 div <<= 1;
1002 } else {
1003 if (div <= 2)
1004 goto unlock_and_return;
1005 div >>= 1;
1006 }
1007 val = ((val & ~PRCM_CLK_MGT_CLKPLLDIV_MASK) |
1008 (div & PRCM_CLK_MGT_CLKPLLDIV_MASK));
1009 writel(val, clock_reg[i]);
1010 }
1011
1012unlock_and_return:
1013 /* Release the HW semaphore. */
1014 writel(0, PRCM_SEM);
1015
1016 spin_unlock_irqrestore(&clk_mgt_lock, flags);
1017}
1018
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001019/**
Mattias Nilsson05089012012-01-13 16:20:20 +01001020 * db8500_set_ape_opp - set the appropriate APE OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001021 * @opp: The new APE operating point to which transition is to be made
1022 * Returns: 0 on success, non-zero on failure
1023 *
1024 * This function sets the operating point of the APE.
1025 */
Mattias Nilsson05089012012-01-13 16:20:20 +01001026int db8500_prcmu_set_ape_opp(u8 opp)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001027{
1028 int r = 0;
1029
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001030 if (opp == mb1_transfer.ape_opp)
1031 return 0;
1032
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001033 mutex_lock(&mb1_transfer.lock);
1034
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001035 if (mb1_transfer.ape_opp == APE_50_PARTLY_25_OPP)
1036 request_even_slower_clocks(false);
1037
1038 if ((opp != APE_100_OPP) && (mb1_transfer.ape_opp != APE_100_OPP))
1039 goto skip_message;
1040
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001041 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001042 cpu_relax();
1043
1044 writeb(MB1H_ARM_APE_OPP, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1045 writeb(ARM_NO_CHANGE, (tcdm_base + PRCM_REQ_MB1_ARM_OPP));
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001046 writeb(((opp == APE_50_PARTLY_25_OPP) ? APE_50_OPP : opp),
1047 (tcdm_base + PRCM_REQ_MB1_APE_OPP));
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001048
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001049 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001050 wait_for_completion(&mb1_transfer.work);
1051
1052 if ((mb1_transfer.ack.header != MB1H_ARM_APE_OPP) ||
1053 (mb1_transfer.ack.ape_opp != opp))
1054 r = -EIO;
1055
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001056skip_message:
1057 if ((!r && (opp == APE_50_PARTLY_25_OPP)) ||
1058 (r && (mb1_transfer.ape_opp == APE_50_PARTLY_25_OPP)))
1059 request_even_slower_clocks(true);
1060 if (!r)
1061 mb1_transfer.ape_opp = opp;
1062
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001063 mutex_unlock(&mb1_transfer.lock);
1064
1065 return r;
1066}
1067
1068/**
Mattias Nilsson05089012012-01-13 16:20:20 +01001069 * db8500_prcmu_get_ape_opp - get the current APE OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001070 *
1071 * Returns: the current APE OPP
1072 */
Mattias Nilsson05089012012-01-13 16:20:20 +01001073int db8500_prcmu_get_ape_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001074{
1075 return readb(tcdm_base + PRCM_ACK_MB1_CURRENT_APE_OPP);
1076}
1077
1078/**
1079 * prcmu_request_ape_opp_100_voltage - Request APE OPP 100% voltage
1080 * @enable: true to request the higher voltage, false to drop a request.
1081 *
1082 * Calls to this function to enable and disable requests must be balanced.
1083 */
1084int prcmu_request_ape_opp_100_voltage(bool enable)
1085{
1086 int r = 0;
1087 u8 header;
1088 static unsigned int requests;
1089
1090 mutex_lock(&mb1_transfer.lock);
1091
1092 if (enable) {
1093 if (0 != requests++)
1094 goto unlock_and_return;
1095 header = MB1H_REQUEST_APE_OPP_100_VOLT;
1096 } else {
1097 if (requests == 0) {
1098 r = -EIO;
1099 goto unlock_and_return;
1100 } else if (1 != requests--) {
1101 goto unlock_and_return;
1102 }
1103 header = MB1H_RELEASE_APE_OPP_100_VOLT;
1104 }
1105
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001106 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001107 cpu_relax();
1108
1109 writeb(header, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1110
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001111 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001112 wait_for_completion(&mb1_transfer.work);
1113
1114 if ((mb1_transfer.ack.header != header) ||
1115 ((mb1_transfer.ack.ape_voltage_status & BIT(0)) != 0))
1116 r = -EIO;
1117
1118unlock_and_return:
1119 mutex_unlock(&mb1_transfer.lock);
1120
1121 return r;
1122}
1123
1124/**
1125 * prcmu_release_usb_wakeup_state - release the state required by a USB wakeup
1126 *
1127 * This function releases the power state requirements of a USB wakeup.
1128 */
1129int prcmu_release_usb_wakeup_state(void)
1130{
1131 int r = 0;
1132
1133 mutex_lock(&mb1_transfer.lock);
1134
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001135 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001136 cpu_relax();
1137
1138 writeb(MB1H_RELEASE_USB_WAKEUP,
1139 (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1140
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001141 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001142 wait_for_completion(&mb1_transfer.work);
1143
1144 if ((mb1_transfer.ack.header != MB1H_RELEASE_USB_WAKEUP) ||
1145 ((mb1_transfer.ack.ape_voltage_status & BIT(0)) != 0))
1146 r = -EIO;
1147
1148 mutex_unlock(&mb1_transfer.lock);
1149
1150 return r;
1151}
1152
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001153static int request_pll(u8 clock, bool enable)
1154{
1155 int r = 0;
1156
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001157 if (clock == PRCMU_PLLSOC0)
1158 clock = (enable ? PLL_SOC0_ON : PLL_SOC0_OFF);
1159 else if (clock == PRCMU_PLLSOC1)
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001160 clock = (enable ? PLL_SOC1_ON : PLL_SOC1_OFF);
1161 else
1162 return -EINVAL;
1163
1164 mutex_lock(&mb1_transfer.lock);
1165
1166 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
1167 cpu_relax();
1168
1169 writeb(MB1H_PLL_ON_OFF, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1170 writeb(clock, (tcdm_base + PRCM_REQ_MB1_PLL_ON_OFF));
1171
1172 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
1173 wait_for_completion(&mb1_transfer.work);
1174
1175 if (mb1_transfer.ack.header != MB1H_PLL_ON_OFF)
1176 r = -EIO;
1177
1178 mutex_unlock(&mb1_transfer.lock);
1179
1180 return r;
1181}
1182
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001183/**
Bengt Jonsson0b9199e2011-08-12 10:28:25 +02001184 * prcmu_set_hwacc - set the power state of a h/w accelerator
1185 * @hwacc_dev: The hardware accelerator (enum hw_acc_dev).
1186 * @state: The new power state (enum hw_acc_state).
1187 *
1188 * This function sets the power state of a hardware accelerator.
1189 * This function should not be called from interrupt context.
1190 *
1191 * NOTE! Deprecated, to be removed when all users switched over to use the
1192 * regulator framework API.
1193 */
1194int prcmu_set_hwacc(u16 hwacc_dev, u8 state)
1195{
1196 int r = 0;
1197 bool ram_retention = false;
1198 bool enable, enable_ret;
1199
1200 /* check argument */
1201 BUG_ON(hwacc_dev >= NUM_HW_ACC);
1202
1203 /* get state of switches */
1204 enable = hwacc_enabled[hwacc_dev];
1205 enable_ret = hwacc_ret_enabled[hwacc_dev];
1206
1207 /* set flag if retention is possible */
1208 switch (hwacc_dev) {
1209 case HW_ACC_SVAMMDSP:
1210 case HW_ACC_SIAMMDSP:
1211 case HW_ACC_ESRAM1:
1212 case HW_ACC_ESRAM2:
1213 case HW_ACC_ESRAM3:
1214 case HW_ACC_ESRAM4:
1215 ram_retention = true;
1216 break;
1217 }
1218
1219 /* check argument */
1220 BUG_ON(state > HW_ON);
1221 BUG_ON(state == HW_OFF_RAMRET && !ram_retention);
1222
1223 /* modify enable flags */
1224 switch (state) {
1225 case HW_OFF:
1226 enable_ret = false;
1227 enable = false;
1228 break;
1229 case HW_ON:
1230 enable = true;
1231 break;
1232 case HW_OFF_RAMRET:
1233 enable_ret = true;
1234 enable = false;
1235 break;
1236 }
1237
1238 /* get regulator (lazy) */
1239 if (hwacc_regulator[hwacc_dev] == NULL) {
1240 hwacc_regulator[hwacc_dev] = regulator_get(NULL,
1241 hwacc_regulator_name[hwacc_dev]);
1242 if (IS_ERR(hwacc_regulator[hwacc_dev])) {
1243 pr_err("prcmu: failed to get supply %s\n",
1244 hwacc_regulator_name[hwacc_dev]);
1245 r = PTR_ERR(hwacc_regulator[hwacc_dev]);
1246 goto out;
1247 }
1248 }
1249
1250 if (ram_retention) {
1251 if (hwacc_ret_regulator[hwacc_dev] == NULL) {
1252 hwacc_ret_regulator[hwacc_dev] = regulator_get(NULL,
1253 hwacc_ret_regulator_name[hwacc_dev]);
1254 if (IS_ERR(hwacc_ret_regulator[hwacc_dev])) {
1255 pr_err("prcmu: failed to get supply %s\n",
1256 hwacc_ret_regulator_name[hwacc_dev]);
1257 r = PTR_ERR(hwacc_ret_regulator[hwacc_dev]);
1258 goto out;
1259 }
1260 }
1261 }
1262
1263 /* set regulators */
1264 if (ram_retention) {
1265 if (enable_ret && !hwacc_ret_enabled[hwacc_dev]) {
1266 r = regulator_enable(hwacc_ret_regulator[hwacc_dev]);
1267 if (r < 0) {
1268 pr_err("prcmu_set_hwacc: ret enable failed\n");
1269 goto out;
1270 }
1271 hwacc_ret_enabled[hwacc_dev] = true;
1272 }
1273 }
1274
1275 if (enable && !hwacc_enabled[hwacc_dev]) {
1276 r = regulator_enable(hwacc_regulator[hwacc_dev]);
1277 if (r < 0) {
1278 pr_err("prcmu_set_hwacc: enable failed\n");
1279 goto out;
1280 }
1281 hwacc_enabled[hwacc_dev] = true;
1282 }
1283
1284 if (!enable && hwacc_enabled[hwacc_dev]) {
1285 r = regulator_disable(hwacc_regulator[hwacc_dev]);
1286 if (r < 0) {
1287 pr_err("prcmu_set_hwacc: disable failed\n");
1288 goto out;
1289 }
1290 hwacc_enabled[hwacc_dev] = false;
1291 }
1292
1293 if (ram_retention) {
1294 if (!enable_ret && hwacc_ret_enabled[hwacc_dev]) {
1295 r = regulator_disable(hwacc_ret_regulator[hwacc_dev]);
1296 if (r < 0) {
1297 pr_err("prcmu_set_hwacc: ret disable failed\n");
1298 goto out;
1299 }
1300 hwacc_ret_enabled[hwacc_dev] = false;
1301 }
1302 }
1303
1304out:
1305 return r;
1306}
1307EXPORT_SYMBOL(prcmu_set_hwacc);
1308
1309/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02001310 * db8500_prcmu_set_epod - set the state of a EPOD (power domain)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001311 * @epod_id: The EPOD to set
1312 * @epod_state: The new EPOD state
1313 *
1314 * This function sets the state of a EPOD (power domain). It may not be called
1315 * from interrupt context.
1316 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02001317int db8500_prcmu_set_epod(u16 epod_id, u8 epod_state)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001318{
1319 int r = 0;
1320 bool ram_retention = false;
1321 int i;
1322
1323 /* check argument */
1324 BUG_ON(epod_id >= NUM_EPOD_ID);
1325
1326 /* set flag if retention is possible */
1327 switch (epod_id) {
1328 case EPOD_ID_SVAMMDSP:
1329 case EPOD_ID_SIAMMDSP:
1330 case EPOD_ID_ESRAM12:
1331 case EPOD_ID_ESRAM34:
1332 ram_retention = true;
1333 break;
1334 }
1335
1336 /* check argument */
1337 BUG_ON(epod_state > EPOD_STATE_ON);
1338 BUG_ON(epod_state == EPOD_STATE_RAMRET && !ram_retention);
1339
1340 /* get lock */
1341 mutex_lock(&mb2_transfer.lock);
1342
1343 /* wait for mailbox */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001344 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(2))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001345 cpu_relax();
1346
1347 /* fill in mailbox */
1348 for (i = 0; i < NUM_EPOD_ID; i++)
1349 writeb(EPOD_STATE_NO_CHANGE, (tcdm_base + PRCM_REQ_MB2 + i));
1350 writeb(epod_state, (tcdm_base + PRCM_REQ_MB2 + epod_id));
1351
1352 writeb(MB2H_DPS, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB2));
1353
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001354 writel(MBOX_BIT(2), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001355
1356 /*
1357 * The current firmware version does not handle errors correctly,
1358 * and we cannot recover if there is an error.
1359 * This is expected to change when the firmware is updated.
1360 */
1361 if (!wait_for_completion_timeout(&mb2_transfer.work,
1362 msecs_to_jiffies(20000))) {
1363 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
1364 __func__);
1365 r = -EIO;
1366 goto unlock_and_return;
1367 }
1368
1369 if (mb2_transfer.ack.status != HWACC_PWR_ST_OK)
1370 r = -EIO;
1371
1372unlock_and_return:
1373 mutex_unlock(&mb2_transfer.lock);
1374 return r;
1375}
1376
1377/**
1378 * prcmu_configure_auto_pm - Configure autonomous power management.
1379 * @sleep: Configuration for ApSleep.
1380 * @idle: Configuration for ApIdle.
1381 */
1382void prcmu_configure_auto_pm(struct prcmu_auto_pm_config *sleep,
1383 struct prcmu_auto_pm_config *idle)
1384{
1385 u32 sleep_cfg;
1386 u32 idle_cfg;
1387 unsigned long flags;
1388
1389 BUG_ON((sleep == NULL) || (idle == NULL));
1390
1391 sleep_cfg = (sleep->sva_auto_pm_enable & 0xF);
1392 sleep_cfg = ((sleep_cfg << 4) | (sleep->sia_auto_pm_enable & 0xF));
1393 sleep_cfg = ((sleep_cfg << 8) | (sleep->sva_power_on & 0xFF));
1394 sleep_cfg = ((sleep_cfg << 8) | (sleep->sia_power_on & 0xFF));
1395 sleep_cfg = ((sleep_cfg << 4) | (sleep->sva_policy & 0xF));
1396 sleep_cfg = ((sleep_cfg << 4) | (sleep->sia_policy & 0xF));
1397
1398 idle_cfg = (idle->sva_auto_pm_enable & 0xF);
1399 idle_cfg = ((idle_cfg << 4) | (idle->sia_auto_pm_enable & 0xF));
1400 idle_cfg = ((idle_cfg << 8) | (idle->sva_power_on & 0xFF));
1401 idle_cfg = ((idle_cfg << 8) | (idle->sia_power_on & 0xFF));
1402 idle_cfg = ((idle_cfg << 4) | (idle->sva_policy & 0xF));
1403 idle_cfg = ((idle_cfg << 4) | (idle->sia_policy & 0xF));
1404
1405 spin_lock_irqsave(&mb2_transfer.auto_pm_lock, flags);
1406
1407 /*
1408 * The autonomous power management configuration is done through
1409 * fields in mailbox 2, but these fields are only used as shared
1410 * variables - i.e. there is no need to send a message.
1411 */
1412 writel(sleep_cfg, (tcdm_base + PRCM_REQ_MB2_AUTO_PM_SLEEP));
1413 writel(idle_cfg, (tcdm_base + PRCM_REQ_MB2_AUTO_PM_IDLE));
1414
1415 mb2_transfer.auto_pm_enabled =
1416 ((sleep->sva_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1417 (sleep->sia_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1418 (idle->sva_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1419 (idle->sia_auto_pm_enable == PRCMU_AUTO_PM_ON));
1420
1421 spin_unlock_irqrestore(&mb2_transfer.auto_pm_lock, flags);
1422}
1423EXPORT_SYMBOL(prcmu_configure_auto_pm);
1424
1425bool prcmu_is_auto_pm_enabled(void)
1426{
1427 return mb2_transfer.auto_pm_enabled;
1428}
1429
1430static int request_sysclk(bool enable)
1431{
1432 int r;
1433 unsigned long flags;
1434
1435 r = 0;
1436
1437 mutex_lock(&mb3_transfer.sysclk_lock);
1438
1439 spin_lock_irqsave(&mb3_transfer.lock, flags);
1440
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001441 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(3))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001442 cpu_relax();
1443
1444 writeb((enable ? ON : OFF), (tcdm_base + PRCM_REQ_MB3_SYSCLK_MGT));
1445
1446 writeb(MB3H_SYSCLK, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB3));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001447 writel(MBOX_BIT(3), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001448
1449 spin_unlock_irqrestore(&mb3_transfer.lock, flags);
1450
1451 /*
1452 * The firmware only sends an ACK if we want to enable the
1453 * SysClk, and it succeeds.
1454 */
1455 if (enable && !wait_for_completion_timeout(&mb3_transfer.sysclk_work,
1456 msecs_to_jiffies(20000))) {
1457 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
1458 __func__);
1459 r = -EIO;
1460 }
1461
1462 mutex_unlock(&mb3_transfer.sysclk_lock);
1463
1464 return r;
1465}
1466
1467static int request_timclk(bool enable)
1468{
1469 u32 val = (PRCM_TCR_DOZE_MODE | PRCM_TCR_TENSEL_MASK);
1470
1471 if (!enable)
1472 val |= PRCM_TCR_STOP_TIMERS;
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001473 writel(val, PRCM_TCR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001474
1475 return 0;
1476}
1477
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001478static int request_clock(u8 clock, bool enable)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001479{
1480 u32 val;
1481 unsigned long flags;
1482
1483 spin_lock_irqsave(&clk_mgt_lock, flags);
1484
1485 /* Grab the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001486 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001487 cpu_relax();
1488
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001489 val = readl(clk_mgt[clock].reg);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001490 if (enable) {
1491 val |= (PRCM_CLK_MGT_CLKEN | clk_mgt[clock].pllsw);
1492 } else {
1493 clk_mgt[clock].pllsw = (val & PRCM_CLK_MGT_CLKPLLSW_MASK);
1494 val &= ~(PRCM_CLK_MGT_CLKEN | PRCM_CLK_MGT_CLKPLLSW_MASK);
1495 }
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001496 writel(val, clk_mgt[clock].reg);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001497
1498 /* Release the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001499 writel(0, PRCM_SEM);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001500
1501 spin_unlock_irqrestore(&clk_mgt_lock, flags);
1502
1503 return 0;
1504}
1505
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001506static int request_sga_clock(u8 clock, bool enable)
1507{
1508 u32 val;
1509 int ret;
1510
1511 if (enable) {
1512 val = readl(PRCM_CGATING_BYPASS);
1513 writel(val | PRCM_CGATING_BYPASS_ICN2, PRCM_CGATING_BYPASS);
1514 }
1515
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001516 ret = request_clock(clock, enable);
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001517
1518 if (!ret && !enable) {
1519 val = readl(PRCM_CGATING_BYPASS);
1520 writel(val & ~PRCM_CGATING_BYPASS_ICN2, PRCM_CGATING_BYPASS);
1521 }
1522
1523 return ret;
1524}
1525
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001526static inline bool plldsi_locked(void)
1527{
1528 return (readl(PRCM_PLLDSI_LOCKP) &
1529 (PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP10 |
1530 PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP3)) ==
1531 (PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP10 |
1532 PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP3);
1533}
1534
1535static int request_plldsi(bool enable)
1536{
1537 int r = 0;
1538 u32 val;
1539
1540 writel((PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMP |
1541 PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMPI), (enable ?
1542 PRCM_MMIP_LS_CLAMP_CLR : PRCM_MMIP_LS_CLAMP_SET));
1543
1544 val = readl(PRCM_PLLDSI_ENABLE);
1545 if (enable)
1546 val |= PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1547 else
1548 val &= ~PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1549 writel(val, PRCM_PLLDSI_ENABLE);
1550
1551 if (enable) {
1552 unsigned int i;
1553 bool locked = plldsi_locked();
1554
1555 for (i = 10; !locked && (i > 0); --i) {
1556 udelay(100);
1557 locked = plldsi_locked();
1558 }
1559 if (locked) {
1560 writel(PRCM_APE_RESETN_DSIPLL_RESETN,
1561 PRCM_APE_RESETN_SET);
1562 } else {
1563 writel((PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMP |
1564 PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMPI),
1565 PRCM_MMIP_LS_CLAMP_SET);
1566 val &= ~PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1567 writel(val, PRCM_PLLDSI_ENABLE);
1568 r = -EAGAIN;
1569 }
1570 } else {
1571 writel(PRCM_APE_RESETN_DSIPLL_RESETN, PRCM_APE_RESETN_CLR);
1572 }
1573 return r;
1574}
1575
1576static int request_dsiclk(u8 n, bool enable)
1577{
1578 u32 val;
1579
1580 val = readl(PRCM_DSI_PLLOUT_SEL);
1581 val &= ~dsiclk[n].divsel_mask;
1582 val |= ((enable ? dsiclk[n].divsel : PRCM_DSI_PLLOUT_SEL_OFF) <<
1583 dsiclk[n].divsel_shift);
1584 writel(val, PRCM_DSI_PLLOUT_SEL);
1585 return 0;
1586}
1587
1588static int request_dsiescclk(u8 n, bool enable)
1589{
1590 u32 val;
1591
1592 val = readl(PRCM_DSITVCLK_DIV);
1593 enable ? (val |= dsiescclk[n].en) : (val &= ~dsiescclk[n].en);
1594 writel(val, PRCM_DSITVCLK_DIV);
1595 return 0;
1596}
1597
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001598/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02001599 * db8500_prcmu_request_clock() - Request for a clock to be enabled or disabled.
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001600 * @clock: The clock for which the request is made.
1601 * @enable: Whether the clock should be enabled (true) or disabled (false).
1602 *
1603 * This function should only be used by the clock implementation.
1604 * Do not use it from any other place!
1605 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02001606int db8500_prcmu_request_clock(u8 clock, bool enable)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001607{
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001608 if (clock == PRCMU_SGACLK)
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001609 return request_sga_clock(clock, enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001610 else if (clock < PRCMU_NUM_REG_CLOCKS)
1611 return request_clock(clock, enable);
1612 else if (clock == PRCMU_TIMCLK)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001613 return request_timclk(enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001614 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1615 return request_dsiclk((clock - PRCMU_DSI0CLK), enable);
1616 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1617 return request_dsiescclk((clock - PRCMU_DSI0ESCCLK), enable);
1618 else if (clock == PRCMU_PLLDSI)
1619 return request_plldsi(enable);
1620 else if (clock == PRCMU_SYSCLK)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001621 return request_sysclk(enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001622 else if ((clock == PRCMU_PLLSOC0) || (clock == PRCMU_PLLSOC1))
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001623 return request_pll(clock, enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001624 else
1625 return -EINVAL;
1626}
1627
1628static unsigned long pll_rate(void __iomem *reg, unsigned long src_rate,
1629 int branch)
1630{
1631 u64 rate;
1632 u32 val;
1633 u32 d;
1634 u32 div = 1;
1635
1636 val = readl(reg);
1637
1638 rate = src_rate;
1639 rate *= ((val & PRCM_PLL_FREQ_D_MASK) >> PRCM_PLL_FREQ_D_SHIFT);
1640
1641 d = ((val & PRCM_PLL_FREQ_N_MASK) >> PRCM_PLL_FREQ_N_SHIFT);
1642 if (d > 1)
1643 div *= d;
1644
1645 d = ((val & PRCM_PLL_FREQ_R_MASK) >> PRCM_PLL_FREQ_R_SHIFT);
1646 if (d > 1)
1647 div *= d;
1648
1649 if (val & PRCM_PLL_FREQ_SELDIV2)
1650 div *= 2;
1651
1652 if ((branch == PLL_FIX) || ((branch == PLL_DIV) &&
1653 (val & PRCM_PLL_FREQ_DIV2EN) &&
1654 ((reg == PRCM_PLLSOC0_FREQ) ||
1655 (reg == PRCM_PLLDDR_FREQ))))
1656 div *= 2;
1657
1658 (void)do_div(rate, div);
1659
1660 return (unsigned long)rate;
1661}
1662
1663#define ROOT_CLOCK_RATE 38400000
1664
1665static unsigned long clock_rate(u8 clock)
1666{
1667 u32 val;
1668 u32 pllsw;
1669 unsigned long rate = ROOT_CLOCK_RATE;
1670
1671 val = readl(clk_mgt[clock].reg);
1672
1673 if (val & PRCM_CLK_MGT_CLK38) {
1674 if (clk_mgt[clock].clk38div && (val & PRCM_CLK_MGT_CLK38DIV))
1675 rate /= 2;
1676 return rate;
Linus Walleije62ccf32011-10-10 12:14:14 +02001677 }
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001678
1679 val |= clk_mgt[clock].pllsw;
1680 pllsw = (val & PRCM_CLK_MGT_CLKPLLSW_MASK);
1681
1682 if (pllsw == PRCM_CLK_MGT_CLKPLLSW_SOC0)
1683 rate = pll_rate(PRCM_PLLSOC0_FREQ, rate, clk_mgt[clock].branch);
1684 else if (pllsw == PRCM_CLK_MGT_CLKPLLSW_SOC1)
1685 rate = pll_rate(PRCM_PLLSOC1_FREQ, rate, clk_mgt[clock].branch);
1686 else if (pllsw == PRCM_CLK_MGT_CLKPLLSW_DDR)
1687 rate = pll_rate(PRCM_PLLDDR_FREQ, rate, clk_mgt[clock].branch);
1688 else
1689 return 0;
1690
1691 if ((clock == PRCMU_SGACLK) &&
1692 (val & PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN)) {
1693 u64 r = (rate * 10);
1694
1695 (void)do_div(r, 25);
1696 return (unsigned long)r;
1697 }
1698 val &= PRCM_CLK_MGT_CLKPLLDIV_MASK;
1699 if (val)
1700 return rate / val;
1701 else
1702 return 0;
1703}
1704
1705static unsigned long dsiclk_rate(u8 n)
1706{
1707 u32 divsel;
1708 u32 div = 1;
1709
1710 divsel = readl(PRCM_DSI_PLLOUT_SEL);
1711 divsel = ((divsel & dsiclk[n].divsel_mask) >> dsiclk[n].divsel_shift);
1712
1713 if (divsel == PRCM_DSI_PLLOUT_SEL_OFF)
1714 divsel = dsiclk[n].divsel;
1715
1716 switch (divsel) {
1717 case PRCM_DSI_PLLOUT_SEL_PHI_4:
1718 div *= 2;
1719 case PRCM_DSI_PLLOUT_SEL_PHI_2:
1720 div *= 2;
1721 case PRCM_DSI_PLLOUT_SEL_PHI:
1722 return pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1723 PLL_RAW) / div;
1724 default:
1725 return 0;
1726 }
1727}
1728
1729static unsigned long dsiescclk_rate(u8 n)
1730{
1731 u32 div;
1732
1733 div = readl(PRCM_DSITVCLK_DIV);
1734 div = ((div & dsiescclk[n].div_mask) >> (dsiescclk[n].div_shift));
1735 return clock_rate(PRCMU_TVCLK) / max((u32)1, div);
1736}
1737
1738unsigned long prcmu_clock_rate(u8 clock)
1739{
Linus Walleije62ccf32011-10-10 12:14:14 +02001740 if (clock < PRCMU_NUM_REG_CLOCKS)
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001741 return clock_rate(clock);
1742 else if (clock == PRCMU_TIMCLK)
1743 return ROOT_CLOCK_RATE / 16;
1744 else if (clock == PRCMU_SYSCLK)
1745 return ROOT_CLOCK_RATE;
1746 else if (clock == PRCMU_PLLSOC0)
1747 return pll_rate(PRCM_PLLSOC0_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
1748 else if (clock == PRCMU_PLLSOC1)
1749 return pll_rate(PRCM_PLLSOC1_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
1750 else if (clock == PRCMU_PLLDDR)
1751 return pll_rate(PRCM_PLLDDR_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
1752 else if (clock == PRCMU_PLLDSI)
1753 return pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1754 PLL_RAW);
1755 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1756 return dsiclk_rate(clock - PRCMU_DSI0CLK);
1757 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1758 return dsiescclk_rate(clock - PRCMU_DSI0ESCCLK);
1759 else
1760 return 0;
1761}
1762
1763static unsigned long clock_source_rate(u32 clk_mgt_val, int branch)
1764{
1765 if (clk_mgt_val & PRCM_CLK_MGT_CLK38)
1766 return ROOT_CLOCK_RATE;
1767 clk_mgt_val &= PRCM_CLK_MGT_CLKPLLSW_MASK;
1768 if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_SOC0)
1769 return pll_rate(PRCM_PLLSOC0_FREQ, ROOT_CLOCK_RATE, branch);
1770 else if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_SOC1)
1771 return pll_rate(PRCM_PLLSOC1_FREQ, ROOT_CLOCK_RATE, branch);
1772 else if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_DDR)
1773 return pll_rate(PRCM_PLLDDR_FREQ, ROOT_CLOCK_RATE, branch);
1774 else
1775 return 0;
1776}
1777
1778static u32 clock_divider(unsigned long src_rate, unsigned long rate)
1779{
1780 u32 div;
1781
1782 div = (src_rate / rate);
1783 if (div == 0)
1784 return 1;
1785 if (rate < (src_rate / div))
1786 div++;
1787 return div;
1788}
1789
1790static long round_clock_rate(u8 clock, unsigned long rate)
1791{
1792 u32 val;
1793 u32 div;
1794 unsigned long src_rate;
1795 long rounded_rate;
1796
1797 val = readl(clk_mgt[clock].reg);
1798 src_rate = clock_source_rate((val | clk_mgt[clock].pllsw),
1799 clk_mgt[clock].branch);
1800 div = clock_divider(src_rate, rate);
1801 if (val & PRCM_CLK_MGT_CLK38) {
1802 if (clk_mgt[clock].clk38div) {
1803 if (div > 2)
1804 div = 2;
1805 } else {
1806 div = 1;
1807 }
1808 } else if ((clock == PRCMU_SGACLK) && (div == 3)) {
1809 u64 r = (src_rate * 10);
1810
1811 (void)do_div(r, 25);
1812 if (r <= rate)
1813 return (unsigned long)r;
1814 }
1815 rounded_rate = (src_rate / min(div, (u32)31));
1816
1817 return rounded_rate;
1818}
1819
1820#define MIN_PLL_VCO_RATE 600000000ULL
1821#define MAX_PLL_VCO_RATE 1680640000ULL
1822
1823static long round_plldsi_rate(unsigned long rate)
1824{
1825 long rounded_rate = 0;
1826 unsigned long src_rate;
1827 unsigned long rem;
1828 u32 r;
1829
1830 src_rate = clock_rate(PRCMU_HDMICLK);
1831 rem = rate;
1832
1833 for (r = 7; (rem > 0) && (r > 0); r--) {
1834 u64 d;
1835
1836 d = (r * rate);
1837 (void)do_div(d, src_rate);
1838 if (d < 6)
1839 d = 6;
1840 else if (d > 255)
1841 d = 255;
1842 d *= src_rate;
1843 if (((2 * d) < (r * MIN_PLL_VCO_RATE)) ||
1844 ((r * MAX_PLL_VCO_RATE) < (2 * d)))
1845 continue;
1846 (void)do_div(d, r);
1847 if (rate < d) {
1848 if (rounded_rate == 0)
1849 rounded_rate = (long)d;
1850 break;
1851 }
1852 if ((rate - d) < rem) {
1853 rem = (rate - d);
1854 rounded_rate = (long)d;
1855 }
1856 }
1857 return rounded_rate;
1858}
1859
1860static long round_dsiclk_rate(unsigned long rate)
1861{
1862 u32 div;
1863 unsigned long src_rate;
1864 long rounded_rate;
1865
1866 src_rate = pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1867 PLL_RAW);
1868 div = clock_divider(src_rate, rate);
1869 rounded_rate = (src_rate / ((div > 2) ? 4 : div));
1870
1871 return rounded_rate;
1872}
1873
1874static long round_dsiescclk_rate(unsigned long rate)
1875{
1876 u32 div;
1877 unsigned long src_rate;
1878 long rounded_rate;
1879
1880 src_rate = clock_rate(PRCMU_TVCLK);
1881 div = clock_divider(src_rate, rate);
1882 rounded_rate = (src_rate / min(div, (u32)255));
1883
1884 return rounded_rate;
1885}
1886
1887long prcmu_round_clock_rate(u8 clock, unsigned long rate)
1888{
1889 if (clock < PRCMU_NUM_REG_CLOCKS)
1890 return round_clock_rate(clock, rate);
1891 else if (clock == PRCMU_PLLDSI)
1892 return round_plldsi_rate(rate);
1893 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1894 return round_dsiclk_rate(rate);
1895 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1896 return round_dsiescclk_rate(rate);
1897 else
1898 return (long)prcmu_clock_rate(clock);
1899}
1900
1901static void set_clock_rate(u8 clock, unsigned long rate)
1902{
1903 u32 val;
1904 u32 div;
1905 unsigned long src_rate;
1906 unsigned long flags;
1907
1908 spin_lock_irqsave(&clk_mgt_lock, flags);
1909
1910 /* Grab the HW semaphore. */
1911 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
1912 cpu_relax();
1913
1914 val = readl(clk_mgt[clock].reg);
1915 src_rate = clock_source_rate((val | clk_mgt[clock].pllsw),
1916 clk_mgt[clock].branch);
1917 div = clock_divider(src_rate, rate);
1918 if (val & PRCM_CLK_MGT_CLK38) {
1919 if (clk_mgt[clock].clk38div) {
1920 if (div > 1)
1921 val |= PRCM_CLK_MGT_CLK38DIV;
1922 else
1923 val &= ~PRCM_CLK_MGT_CLK38DIV;
1924 }
1925 } else if (clock == PRCMU_SGACLK) {
1926 val &= ~(PRCM_CLK_MGT_CLKPLLDIV_MASK |
1927 PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN);
1928 if (div == 3) {
1929 u64 r = (src_rate * 10);
1930
1931 (void)do_div(r, 25);
1932 if (r <= rate) {
1933 val |= PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN;
1934 div = 0;
1935 }
1936 }
1937 val |= min(div, (u32)31);
1938 } else {
1939 val &= ~PRCM_CLK_MGT_CLKPLLDIV_MASK;
1940 val |= min(div, (u32)31);
1941 }
1942 writel(val, clk_mgt[clock].reg);
1943
1944 /* Release the HW semaphore. */
1945 writel(0, PRCM_SEM);
1946
1947 spin_unlock_irqrestore(&clk_mgt_lock, flags);
1948}
1949
1950static int set_plldsi_rate(unsigned long rate)
1951{
1952 unsigned long src_rate;
1953 unsigned long rem;
1954 u32 pll_freq = 0;
1955 u32 r;
1956
1957 src_rate = clock_rate(PRCMU_HDMICLK);
1958 rem = rate;
1959
1960 for (r = 7; (rem > 0) && (r > 0); r--) {
1961 u64 d;
1962 u64 hwrate;
1963
1964 d = (r * rate);
1965 (void)do_div(d, src_rate);
1966 if (d < 6)
1967 d = 6;
1968 else if (d > 255)
1969 d = 255;
1970 hwrate = (d * src_rate);
1971 if (((2 * hwrate) < (r * MIN_PLL_VCO_RATE)) ||
1972 ((r * MAX_PLL_VCO_RATE) < (2 * hwrate)))
1973 continue;
1974 (void)do_div(hwrate, r);
1975 if (rate < hwrate) {
1976 if (pll_freq == 0)
1977 pll_freq = (((u32)d << PRCM_PLL_FREQ_D_SHIFT) |
1978 (r << PRCM_PLL_FREQ_R_SHIFT));
1979 break;
1980 }
1981 if ((rate - hwrate) < rem) {
1982 rem = (rate - hwrate);
1983 pll_freq = (((u32)d << PRCM_PLL_FREQ_D_SHIFT) |
1984 (r << PRCM_PLL_FREQ_R_SHIFT));
1985 }
1986 }
1987 if (pll_freq == 0)
1988 return -EINVAL;
1989
1990 pll_freq |= (1 << PRCM_PLL_FREQ_N_SHIFT);
1991 writel(pll_freq, PRCM_PLLDSI_FREQ);
1992
1993 return 0;
1994}
1995
1996static void set_dsiclk_rate(u8 n, unsigned long rate)
1997{
1998 u32 val;
1999 u32 div;
2000
2001 div = clock_divider(pll_rate(PRCM_PLLDSI_FREQ,
2002 clock_rate(PRCMU_HDMICLK), PLL_RAW), rate);
2003
2004 dsiclk[n].divsel = (div == 1) ? PRCM_DSI_PLLOUT_SEL_PHI :
2005 (div == 2) ? PRCM_DSI_PLLOUT_SEL_PHI_2 :
2006 /* else */ PRCM_DSI_PLLOUT_SEL_PHI_4;
2007
2008 val = readl(PRCM_DSI_PLLOUT_SEL);
2009 val &= ~dsiclk[n].divsel_mask;
2010 val |= (dsiclk[n].divsel << dsiclk[n].divsel_shift);
2011 writel(val, PRCM_DSI_PLLOUT_SEL);
2012}
2013
2014static void set_dsiescclk_rate(u8 n, unsigned long rate)
2015{
2016 u32 val;
2017 u32 div;
2018
2019 div = clock_divider(clock_rate(PRCMU_TVCLK), rate);
2020 val = readl(PRCM_DSITVCLK_DIV);
2021 val &= ~dsiescclk[n].div_mask;
2022 val |= (min(div, (u32)255) << dsiescclk[n].div_shift);
2023 writel(val, PRCM_DSITVCLK_DIV);
2024}
2025
2026int prcmu_set_clock_rate(u8 clock, unsigned long rate)
2027{
2028 if (clock < PRCMU_NUM_REG_CLOCKS)
2029 set_clock_rate(clock, rate);
2030 else if (clock == PRCMU_PLLDSI)
2031 return set_plldsi_rate(rate);
2032 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
2033 set_dsiclk_rate((clock - PRCMU_DSI0CLK), rate);
2034 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
2035 set_dsiescclk_rate((clock - PRCMU_DSI0ESCCLK), rate);
2036 return 0;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002037}
2038
Mattias Nilsson73180f82011-08-12 10:28:10 +02002039int db8500_prcmu_config_esram0_deep_sleep(u8 state)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002040{
2041 if ((state > ESRAM0_DEEP_SLEEP_STATE_RET) ||
2042 (state < ESRAM0_DEEP_SLEEP_STATE_OFF))
2043 return -EINVAL;
2044
2045 mutex_lock(&mb4_transfer.lock);
2046
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002047 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002048 cpu_relax();
2049
2050 writeb(MB4H_MEM_ST, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2051 writeb(((DDR_PWR_STATE_OFFHIGHLAT << 4) | DDR_PWR_STATE_ON),
2052 (tcdm_base + PRCM_REQ_MB4_DDR_ST_AP_SLEEP_IDLE));
2053 writeb(DDR_PWR_STATE_ON,
2054 (tcdm_base + PRCM_REQ_MB4_DDR_ST_AP_DEEP_IDLE));
2055 writeb(state, (tcdm_base + PRCM_REQ_MB4_ESRAM0_ST));
2056
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002057 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002058 wait_for_completion(&mb4_transfer.work);
2059
2060 mutex_unlock(&mb4_transfer.lock);
2061
2062 return 0;
2063}
2064
Mattias Nilsson05089012012-01-13 16:20:20 +01002065int db8500_prcmu_config_hotdog(u8 threshold)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002066{
2067 mutex_lock(&mb4_transfer.lock);
2068
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002069 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002070 cpu_relax();
2071
2072 writeb(threshold, (tcdm_base + PRCM_REQ_MB4_HOTDOG_THRESHOLD));
2073 writeb(MB4H_HOTDOG, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2074
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002075 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002076 wait_for_completion(&mb4_transfer.work);
2077
2078 mutex_unlock(&mb4_transfer.lock);
2079
2080 return 0;
2081}
2082
Mattias Nilsson05089012012-01-13 16:20:20 +01002083int db8500_prcmu_config_hotmon(u8 low, u8 high)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002084{
2085 mutex_lock(&mb4_transfer.lock);
2086
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002087 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002088 cpu_relax();
2089
2090 writeb(low, (tcdm_base + PRCM_REQ_MB4_HOTMON_LOW));
2091 writeb(high, (tcdm_base + PRCM_REQ_MB4_HOTMON_HIGH));
2092 writeb((HOTMON_CONFIG_LOW | HOTMON_CONFIG_HIGH),
2093 (tcdm_base + PRCM_REQ_MB4_HOTMON_CONFIG));
2094 writeb(MB4H_HOTMON, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2095
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002096 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002097 wait_for_completion(&mb4_transfer.work);
2098
2099 mutex_unlock(&mb4_transfer.lock);
2100
2101 return 0;
2102}
2103
2104static int config_hot_period(u16 val)
2105{
2106 mutex_lock(&mb4_transfer.lock);
2107
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002108 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002109 cpu_relax();
2110
2111 writew(val, (tcdm_base + PRCM_REQ_MB4_HOT_PERIOD));
2112 writeb(MB4H_HOT_PERIOD, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2113
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002114 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002115 wait_for_completion(&mb4_transfer.work);
2116
2117 mutex_unlock(&mb4_transfer.lock);
2118
2119 return 0;
2120}
2121
Mattias Nilsson05089012012-01-13 16:20:20 +01002122int db8500_prcmu_start_temp_sense(u16 cycles32k)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002123{
2124 if (cycles32k == 0xFFFF)
2125 return -EINVAL;
2126
2127 return config_hot_period(cycles32k);
2128}
2129
Mattias Nilsson05089012012-01-13 16:20:20 +01002130int db8500_prcmu_stop_temp_sense(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002131{
2132 return config_hot_period(0xFFFF);
2133}
2134
Jonas Aberg84165b82011-08-12 10:28:33 +02002135static int prcmu_a9wdog(u8 cmd, u8 d0, u8 d1, u8 d2, u8 d3)
2136{
2137
2138 mutex_lock(&mb4_transfer.lock);
2139
2140 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
2141 cpu_relax();
2142
2143 writeb(d0, (tcdm_base + PRCM_REQ_MB4_A9WDOG_0));
2144 writeb(d1, (tcdm_base + PRCM_REQ_MB4_A9WDOG_1));
2145 writeb(d2, (tcdm_base + PRCM_REQ_MB4_A9WDOG_2));
2146 writeb(d3, (tcdm_base + PRCM_REQ_MB4_A9WDOG_3));
2147
2148 writeb(cmd, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2149
2150 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
2151 wait_for_completion(&mb4_transfer.work);
2152
2153 mutex_unlock(&mb4_transfer.lock);
2154
2155 return 0;
2156
2157}
2158
Mattias Nilsson05089012012-01-13 16:20:20 +01002159int db8500_prcmu_config_a9wdog(u8 num, bool sleep_auto_off)
Jonas Aberg84165b82011-08-12 10:28:33 +02002160{
2161 BUG_ON(num == 0 || num > 0xf);
2162 return prcmu_a9wdog(MB4H_A9WDOG_CONF, num, 0, 0,
2163 sleep_auto_off ? A9WDOG_AUTO_OFF_EN :
2164 A9WDOG_AUTO_OFF_DIS);
2165}
2166
Mattias Nilsson05089012012-01-13 16:20:20 +01002167int db8500_prcmu_enable_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002168{
2169 return prcmu_a9wdog(MB4H_A9WDOG_EN, id, 0, 0, 0);
2170}
2171
Mattias Nilsson05089012012-01-13 16:20:20 +01002172int db8500_prcmu_disable_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002173{
2174 return prcmu_a9wdog(MB4H_A9WDOG_DIS, id, 0, 0, 0);
2175}
2176
Mattias Nilsson05089012012-01-13 16:20:20 +01002177int db8500_prcmu_kick_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002178{
2179 return prcmu_a9wdog(MB4H_A9WDOG_KICK, id, 0, 0, 0);
2180}
2181
2182/*
2183 * timeout is 28 bit, in ms.
2184 */
Mattias Nilsson05089012012-01-13 16:20:20 +01002185int db8500_prcmu_load_a9wdog(u8 id, u32 timeout)
Jonas Aberg84165b82011-08-12 10:28:33 +02002186{
Jonas Aberg84165b82011-08-12 10:28:33 +02002187 return prcmu_a9wdog(MB4H_A9WDOG_LOAD,
2188 (id & A9WDOG_ID_MASK) |
2189 /*
2190 * Put the lowest 28 bits of timeout at
2191 * offset 4. Four first bits are used for id.
2192 */
2193 (u8)((timeout << 4) & 0xf0),
2194 (u8)((timeout >> 4) & 0xff),
2195 (u8)((timeout >> 12) & 0xff),
2196 (u8)((timeout >> 20) & 0xff));
2197}
2198
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002199/**
Linus Walleije3726fc2010-08-19 12:36:01 +01002200 * prcmu_abb_read() - Read register value(s) from the ABB.
2201 * @slave: The I2C slave address.
2202 * @reg: The (start) register address.
2203 * @value: The read out value(s).
2204 * @size: The number of registers to read.
2205 *
2206 * Reads register value(s) from the ABB.
2207 * @size has to be 1 for the current firmware version.
2208 */
2209int prcmu_abb_read(u8 slave, u8 reg, u8 *value, u8 size)
2210{
2211 int r;
2212
2213 if (size != 1)
2214 return -EINVAL;
2215
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002216 mutex_lock(&mb5_transfer.lock);
Linus Walleije3726fc2010-08-19 12:36:01 +01002217
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002218 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(5))
Linus Walleije3726fc2010-08-19 12:36:01 +01002219 cpu_relax();
2220
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002221 writeb(PRCMU_I2C_READ(slave), (tcdm_base + PRCM_REQ_MB5_I2C_SLAVE_OP));
2222 writeb(PRCMU_I2C_STOP_EN, (tcdm_base + PRCM_REQ_MB5_I2C_HW_BITS));
2223 writeb(reg, (tcdm_base + PRCM_REQ_MB5_I2C_REG));
2224 writeb(0, (tcdm_base + PRCM_REQ_MB5_I2C_VAL));
Linus Walleije3726fc2010-08-19 12:36:01 +01002225
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002226 writel(MBOX_BIT(5), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002227
Linus Walleije3726fc2010-08-19 12:36:01 +01002228 if (!wait_for_completion_timeout(&mb5_transfer.work,
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002229 msecs_to_jiffies(20000))) {
2230 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
2231 __func__);
Linus Walleije3726fc2010-08-19 12:36:01 +01002232 r = -EIO;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002233 } else {
2234 r = ((mb5_transfer.ack.status == I2C_RD_OK) ? 0 : -EIO);
Linus Walleije3726fc2010-08-19 12:36:01 +01002235 }
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002236
Linus Walleije3726fc2010-08-19 12:36:01 +01002237 if (!r)
2238 *value = mb5_transfer.ack.value;
2239
Linus Walleije3726fc2010-08-19 12:36:01 +01002240 mutex_unlock(&mb5_transfer.lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002241
Linus Walleije3726fc2010-08-19 12:36:01 +01002242 return r;
2243}
Linus Walleije3726fc2010-08-19 12:36:01 +01002244
2245/**
2246 * prcmu_abb_write() - Write register value(s) to the ABB.
2247 * @slave: The I2C slave address.
2248 * @reg: The (start) register address.
2249 * @value: The value(s) to write.
2250 * @size: The number of registers to write.
2251 *
2252 * Reads register value(s) from the ABB.
2253 * @size has to be 1 for the current firmware version.
2254 */
2255int prcmu_abb_write(u8 slave, u8 reg, u8 *value, u8 size)
2256{
2257 int r;
2258
2259 if (size != 1)
2260 return -EINVAL;
2261
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002262 mutex_lock(&mb5_transfer.lock);
Linus Walleije3726fc2010-08-19 12:36:01 +01002263
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002264 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(5))
Linus Walleije3726fc2010-08-19 12:36:01 +01002265 cpu_relax();
2266
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002267 writeb(PRCMU_I2C_WRITE(slave), (tcdm_base + PRCM_REQ_MB5_I2C_SLAVE_OP));
2268 writeb(PRCMU_I2C_STOP_EN, (tcdm_base + PRCM_REQ_MB5_I2C_HW_BITS));
2269 writeb(reg, (tcdm_base + PRCM_REQ_MB5_I2C_REG));
2270 writeb(*value, (tcdm_base + PRCM_REQ_MB5_I2C_VAL));
Linus Walleije3726fc2010-08-19 12:36:01 +01002271
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002272 writel(MBOX_BIT(5), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002273
Linus Walleije3726fc2010-08-19 12:36:01 +01002274 if (!wait_for_completion_timeout(&mb5_transfer.work,
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002275 msecs_to_jiffies(20000))) {
2276 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
2277 __func__);
Linus Walleije3726fc2010-08-19 12:36:01 +01002278 r = -EIO;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002279 } else {
2280 r = ((mb5_transfer.ack.status == I2C_WR_OK) ? 0 : -EIO);
Linus Walleije3726fc2010-08-19 12:36:01 +01002281 }
Linus Walleije3726fc2010-08-19 12:36:01 +01002282
Linus Walleije3726fc2010-08-19 12:36:01 +01002283 mutex_unlock(&mb5_transfer.lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002284
Linus Walleije3726fc2010-08-19 12:36:01 +01002285 return r;
2286}
Linus Walleije3726fc2010-08-19 12:36:01 +01002287
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002288/**
2289 * prcmu_ac_wake_req - should be called whenever ARM wants to wakeup Modem
2290 */
2291void prcmu_ac_wake_req(void)
Martin Perssone0befb22010-12-08 15:13:28 +01002292{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002293 u32 val;
Mattias Nilssond6e30022011-08-12 10:28:43 +02002294 u32 status;
Martin Perssone0befb22010-12-08 15:13:28 +01002295
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002296 mutex_lock(&mb0_transfer.ac_wake_lock);
Martin Perssone0befb22010-12-08 15:13:28 +01002297
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002298 val = readl(PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002299 if (val & PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ)
2300 goto unlock_and_return;
2301
2302 atomic_set(&ac_wake_req_state, 1);
2303
Mattias Nilssond6e30022011-08-12 10:28:43 +02002304retry:
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002305 writel((val | PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ), PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002306
2307 if (!wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
Mattias Nilssond6e30022011-08-12 10:28:43 +02002308 msecs_to_jiffies(5000))) {
Linus Walleij57265bc2011-10-10 13:04:44 +02002309 pr_crit("prcmu: %s timed out (5 s) waiting for a reply.\n",
Mattias Nilssond6e30022011-08-12 10:28:43 +02002310 __func__);
2311 goto unlock_and_return;
2312 }
2313
2314 /*
2315 * The modem can generate an AC_WAKE_ACK, and then still go to sleep.
2316 * As a workaround, we wait, and then check that the modem is indeed
2317 * awake (in terms of the value of the PRCM_MOD_AWAKE_STATUS
2318 * register, which may not be the whole truth).
2319 */
2320 udelay(400);
2321 status = (readl(PRCM_MOD_AWAKE_STATUS) & BITS(0, 2));
2322 if (status != (PRCM_MOD_AWAKE_STATUS_PRCM_MOD_AAPD_AWAKE |
2323 PRCM_MOD_AWAKE_STATUS_PRCM_MOD_COREPD_AWAKE)) {
2324 pr_err("prcmu: %s received ack, but modem not awake (0x%X).\n",
2325 __func__, status);
2326 udelay(1200);
2327 writel(val, PRCM_HOSTACCESS_REQ);
2328 if (wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
2329 msecs_to_jiffies(5000)))
2330 goto retry;
Linus Walleij57265bc2011-10-10 13:04:44 +02002331 pr_crit("prcmu: %s timed out (5 s) waiting for AC_SLEEP_ACK.\n",
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002332 __func__);
2333 }
2334
2335unlock_and_return:
2336 mutex_unlock(&mb0_transfer.ac_wake_lock);
2337}
2338
2339/**
2340 * prcmu_ac_sleep_req - called when ARM no longer needs to talk to modem
2341 */
2342void prcmu_ac_sleep_req()
2343{
2344 u32 val;
2345
2346 mutex_lock(&mb0_transfer.ac_wake_lock);
2347
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002348 val = readl(PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002349 if (!(val & PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ))
2350 goto unlock_and_return;
2351
2352 writel((val & ~PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ),
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002353 PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002354
2355 if (!wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
Mattias Nilssond6e30022011-08-12 10:28:43 +02002356 msecs_to_jiffies(5000))) {
Linus Walleij57265bc2011-10-10 13:04:44 +02002357 pr_crit("prcmu: %s timed out (5 s) waiting for a reply.\n",
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002358 __func__);
2359 }
2360
2361 atomic_set(&ac_wake_req_state, 0);
2362
2363unlock_and_return:
2364 mutex_unlock(&mb0_transfer.ac_wake_lock);
2365}
2366
Mattias Nilsson73180f82011-08-12 10:28:10 +02002367bool db8500_prcmu_is_ac_wake_requested(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002368{
2369 return (atomic_read(&ac_wake_req_state) != 0);
2370}
2371
2372/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02002373 * db8500_prcmu_system_reset - System reset
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002374 *
Mattias Nilsson73180f82011-08-12 10:28:10 +02002375 * Saves the reset reason code and then sets the APE_SOFTRST register which
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002376 * fires interrupt to fw
2377 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02002378void db8500_prcmu_system_reset(u16 reset_code)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002379{
2380 writew(reset_code, (tcdm_base + PRCM_SW_RST_REASON));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002381 writel(1, PRCM_APE_SOFTRST);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002382}
2383
2384/**
Sebastian Rasmussen597045d2011-08-12 10:28:53 +02002385 * db8500_prcmu_get_reset_code - Retrieve SW reset reason code
2386 *
2387 * Retrieves the reset reason code stored by prcmu_system_reset() before
2388 * last restart.
2389 */
2390u16 db8500_prcmu_get_reset_code(void)
2391{
2392 return readw(tcdm_base + PRCM_SW_RST_REASON);
2393}
2394
2395/**
Mattias Nilsson05089012012-01-13 16:20:20 +01002396 * db8500_prcmu_reset_modem - ask the PRCMU to reset modem
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002397 */
Mattias Nilsson05089012012-01-13 16:20:20 +01002398void db8500_prcmu_modem_reset(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002399{
Martin Perssone0befb22010-12-08 15:13:28 +01002400 mutex_lock(&mb1_transfer.lock);
2401
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002402 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Martin Perssone0befb22010-12-08 15:13:28 +01002403 cpu_relax();
2404
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002405 writeb(MB1H_RESET_MODEM, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002406 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Martin Perssone0befb22010-12-08 15:13:28 +01002407 wait_for_completion(&mb1_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002408
2409 /*
2410 * No need to check return from PRCMU as modem should go in reset state
2411 * This state is already managed by upper layer
2412 */
Martin Perssone0befb22010-12-08 15:13:28 +01002413
2414 mutex_unlock(&mb1_transfer.lock);
Martin Perssone0befb22010-12-08 15:13:28 +01002415}
2416
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002417static void ack_dbb_wakeup(void)
Martin Perssone0befb22010-12-08 15:13:28 +01002418{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002419 unsigned long flags;
Martin Perssone0befb22010-12-08 15:13:28 +01002420
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002421 spin_lock_irqsave(&mb0_transfer.lock, flags);
Martin Perssone0befb22010-12-08 15:13:28 +01002422
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002423 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002424 cpu_relax();
Martin Perssone0befb22010-12-08 15:13:28 +01002425
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002426 writeb(MB0H_READ_WAKEUP_ACK, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002427 writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
Martin Perssone0befb22010-12-08 15:13:28 +01002428
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002429 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
Martin Perssone0befb22010-12-08 15:13:28 +01002430}
2431
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002432static inline void print_unknown_header_warning(u8 n, u8 header)
Linus Walleije3726fc2010-08-19 12:36:01 +01002433{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002434 pr_warning("prcmu: Unknown message header (%d) in mailbox %d.\n",
2435 header, n);
Linus Walleije3726fc2010-08-19 12:36:01 +01002436}
2437
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002438static bool read_mailbox_0(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002439{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002440 bool r;
2441 u32 ev;
2442 unsigned int n;
2443 u8 header;
2444
2445 header = readb(tcdm_base + PRCM_MBOX_HEADER_ACK_MB0);
2446 switch (header) {
2447 case MB0H_WAKEUP_EXE:
2448 case MB0H_WAKEUP_SLEEP:
2449 if (readb(tcdm_base + PRCM_ACK_MB0_READ_POINTER) & 1)
2450 ev = readl(tcdm_base + PRCM_ACK_MB0_WAKEUP_1_8500);
2451 else
2452 ev = readl(tcdm_base + PRCM_ACK_MB0_WAKEUP_0_8500);
2453
2454 if (ev & (WAKEUP_BIT_AC_WAKE_ACK | WAKEUP_BIT_AC_SLEEP_ACK))
2455 complete(&mb0_transfer.ac_wake_work);
2456 if (ev & WAKEUP_BIT_SYSCLK_OK)
2457 complete(&mb3_transfer.sysclk_work);
2458
2459 ev &= mb0_transfer.req.dbb_irqs;
2460
2461 for (n = 0; n < NUM_PRCMU_WAKEUPS; n++) {
2462 if (ev & prcmu_irq_bit[n])
2463 generic_handle_irq(IRQ_PRCMU_BASE + n);
2464 }
2465 r = true;
2466 break;
2467 default:
2468 print_unknown_header_warning(0, header);
2469 r = false;
2470 break;
2471 }
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002472 writel(MBOX_BIT(0), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002473 return r;
2474}
2475
2476static bool read_mailbox_1(void)
2477{
2478 mb1_transfer.ack.header = readb(tcdm_base + PRCM_MBOX_HEADER_REQ_MB1);
2479 mb1_transfer.ack.arm_opp = readb(tcdm_base +
2480 PRCM_ACK_MB1_CURRENT_ARM_OPP);
2481 mb1_transfer.ack.ape_opp = readb(tcdm_base +
2482 PRCM_ACK_MB1_CURRENT_APE_OPP);
2483 mb1_transfer.ack.ape_voltage_status = readb(tcdm_base +
2484 PRCM_ACK_MB1_APE_VOLTAGE_STATUS);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002485 writel(MBOX_BIT(1), PRCM_ARM_IT1_CLR);
Martin Perssone0befb22010-12-08 15:13:28 +01002486 complete(&mb1_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002487 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002488}
2489
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002490static bool read_mailbox_2(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002491{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002492 mb2_transfer.ack.status = readb(tcdm_base + PRCM_ACK_MB2_DPS_STATUS);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002493 writel(MBOX_BIT(2), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002494 complete(&mb2_transfer.work);
2495 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002496}
2497
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002498static bool read_mailbox_3(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002499{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002500 writel(MBOX_BIT(3), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002501 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002502}
2503
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002504static bool read_mailbox_4(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002505{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002506 u8 header;
2507 bool do_complete = true;
2508
2509 header = readb(tcdm_base + PRCM_MBOX_HEADER_REQ_MB4);
2510 switch (header) {
2511 case MB4H_MEM_ST:
2512 case MB4H_HOTDOG:
2513 case MB4H_HOTMON:
2514 case MB4H_HOT_PERIOD:
Mattias Nilssona592c2e2011-08-12 10:27:41 +02002515 case MB4H_A9WDOG_CONF:
2516 case MB4H_A9WDOG_EN:
2517 case MB4H_A9WDOG_DIS:
2518 case MB4H_A9WDOG_LOAD:
2519 case MB4H_A9WDOG_KICK:
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002520 break;
2521 default:
2522 print_unknown_header_warning(4, header);
2523 do_complete = false;
2524 break;
2525 }
2526
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002527 writel(MBOX_BIT(4), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002528
2529 if (do_complete)
2530 complete(&mb4_transfer.work);
2531
2532 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002533}
2534
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002535static bool read_mailbox_5(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002536{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002537 mb5_transfer.ack.status = readb(tcdm_base + PRCM_ACK_MB5_I2C_STATUS);
2538 mb5_transfer.ack.value = readb(tcdm_base + PRCM_ACK_MB5_I2C_VAL);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002539 writel(MBOX_BIT(5), PRCM_ARM_IT1_CLR);
Linus Walleije3726fc2010-08-19 12:36:01 +01002540 complete(&mb5_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002541 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002542}
2543
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002544static bool read_mailbox_6(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002545{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002546 writel(MBOX_BIT(6), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002547 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002548}
2549
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002550static bool read_mailbox_7(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002551{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002552 writel(MBOX_BIT(7), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002553 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002554}
2555
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002556static bool (* const read_mailbox[NUM_MB])(void) = {
Linus Walleije3726fc2010-08-19 12:36:01 +01002557 read_mailbox_0,
2558 read_mailbox_1,
2559 read_mailbox_2,
2560 read_mailbox_3,
2561 read_mailbox_4,
2562 read_mailbox_5,
2563 read_mailbox_6,
2564 read_mailbox_7
2565};
2566
2567static irqreturn_t prcmu_irq_handler(int irq, void *data)
2568{
2569 u32 bits;
2570 u8 n;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002571 irqreturn_t r;
Linus Walleije3726fc2010-08-19 12:36:01 +01002572
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002573 bits = (readl(PRCM_ARM_IT1_VAL) & ALL_MBOX_BITS);
Linus Walleije3726fc2010-08-19 12:36:01 +01002574 if (unlikely(!bits))
2575 return IRQ_NONE;
2576
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002577 r = IRQ_HANDLED;
Linus Walleije3726fc2010-08-19 12:36:01 +01002578 for (n = 0; bits; n++) {
2579 if (bits & MBOX_BIT(n)) {
2580 bits -= MBOX_BIT(n);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002581 if (read_mailbox[n]())
2582 r = IRQ_WAKE_THREAD;
Linus Walleije3726fc2010-08-19 12:36:01 +01002583 }
2584 }
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002585 return r;
2586}
2587
2588static irqreturn_t prcmu_irq_thread_fn(int irq, void *data)
2589{
2590 ack_dbb_wakeup();
Linus Walleije3726fc2010-08-19 12:36:01 +01002591 return IRQ_HANDLED;
2592}
2593
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002594static void prcmu_mask_work(struct work_struct *work)
2595{
2596 unsigned long flags;
2597
2598 spin_lock_irqsave(&mb0_transfer.lock, flags);
2599
2600 config_wakeups();
2601
2602 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
2603}
2604
2605static void prcmu_irq_mask(struct irq_data *d)
2606{
2607 unsigned long flags;
2608
2609 spin_lock_irqsave(&mb0_transfer.dbb_irqs_lock, flags);
2610
2611 mb0_transfer.req.dbb_irqs &= ~prcmu_irq_bit[d->irq - IRQ_PRCMU_BASE];
2612
2613 spin_unlock_irqrestore(&mb0_transfer.dbb_irqs_lock, flags);
2614
2615 if (d->irq != IRQ_PRCMU_CA_SLEEP)
2616 schedule_work(&mb0_transfer.mask_work);
2617}
2618
2619static void prcmu_irq_unmask(struct irq_data *d)
2620{
2621 unsigned long flags;
2622
2623 spin_lock_irqsave(&mb0_transfer.dbb_irqs_lock, flags);
2624
2625 mb0_transfer.req.dbb_irqs |= prcmu_irq_bit[d->irq - IRQ_PRCMU_BASE];
2626
2627 spin_unlock_irqrestore(&mb0_transfer.dbb_irqs_lock, flags);
2628
2629 if (d->irq != IRQ_PRCMU_CA_SLEEP)
2630 schedule_work(&mb0_transfer.mask_work);
2631}
2632
2633static void noop(struct irq_data *d)
2634{
2635}
2636
2637static struct irq_chip prcmu_irq_chip = {
2638 .name = "prcmu",
2639 .irq_disable = prcmu_irq_mask,
2640 .irq_ack = noop,
2641 .irq_mask = prcmu_irq_mask,
2642 .irq_unmask = prcmu_irq_unmask,
2643};
2644
Mattias Nilssonb58d12f2012-01-13 16:20:10 +01002645static char *fw_project_name(u8 project)
2646{
2647 switch (project) {
2648 case PRCMU_FW_PROJECT_U8500:
2649 return "U8500";
2650 case PRCMU_FW_PROJECT_U8500_C2:
2651 return "U8500 C2";
2652 case PRCMU_FW_PROJECT_U9500:
2653 return "U9500";
2654 case PRCMU_FW_PROJECT_U9500_C2:
2655 return "U9500 C2";
2656 default:
2657 return "Unknown";
2658 }
2659}
2660
Mattias Nilsson73180f82011-08-12 10:28:10 +02002661void __init db8500_prcmu_early_init(void)
Mattias Wallinfcbd4582010-12-02 16:20:42 +01002662{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002663 unsigned int i;
Linus Walleij3e2762c2012-01-02 14:17:40 +01002664 if (cpu_is_u8500v2()) {
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002665 void *tcpm_base = ioremap_nocache(U8500_PRCMU_TCPM_BASE, SZ_4K);
2666
2667 if (tcpm_base != NULL) {
Linus Walleij3e2762c2012-01-02 14:17:40 +01002668 u32 version;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002669 version = readl(tcpm_base + PRCMU_FW_VERSION_OFFSET);
Mattias Nilssonb58d12f2012-01-13 16:20:10 +01002670 fw_info.version.project = version & 0xFF;
2671 fw_info.version.api_version = (version >> 8) & 0xFF;
2672 fw_info.version.func_version = (version >> 16) & 0xFF;
2673 fw_info.version.errata = (version >> 24) & 0xFF;
2674 fw_info.valid = true;
2675 pr_info("PRCMU firmware: %s, version %d.%d.%d\n",
2676 fw_project_name(fw_info.version.project),
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002677 (version >> 8) & 0xFF, (version >> 16) & 0xFF,
2678 (version >> 24) & 0xFF);
2679 iounmap(tcpm_base);
2680 }
2681
Mattias Wallinfcbd4582010-12-02 16:20:42 +01002682 tcdm_base = __io_address(U8500_PRCMU_TCDM_BASE);
2683 } else {
2684 pr_err("prcmu: Unsupported chip version\n");
2685 BUG();
2686 }
Mattias Wallinfcbd4582010-12-02 16:20:42 +01002687
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002688 spin_lock_init(&mb0_transfer.lock);
2689 spin_lock_init(&mb0_transfer.dbb_irqs_lock);
2690 mutex_init(&mb0_transfer.ac_wake_lock);
2691 init_completion(&mb0_transfer.ac_wake_work);
Martin Perssone0befb22010-12-08 15:13:28 +01002692 mutex_init(&mb1_transfer.lock);
2693 init_completion(&mb1_transfer.work);
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01002694 mb1_transfer.ape_opp = APE_NO_CHANGE;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002695 mutex_init(&mb2_transfer.lock);
2696 init_completion(&mb2_transfer.work);
2697 spin_lock_init(&mb2_transfer.auto_pm_lock);
2698 spin_lock_init(&mb3_transfer.lock);
2699 mutex_init(&mb3_transfer.sysclk_lock);
2700 init_completion(&mb3_transfer.sysclk_work);
2701 mutex_init(&mb4_transfer.lock);
2702 init_completion(&mb4_transfer.work);
Linus Walleije3726fc2010-08-19 12:36:01 +01002703 mutex_init(&mb5_transfer.lock);
2704 init_completion(&mb5_transfer.work);
2705
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002706 INIT_WORK(&mb0_transfer.mask_work, prcmu_mask_work);
Linus Walleije3726fc2010-08-19 12:36:01 +01002707
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002708 /* Initalize irqs. */
2709 for (i = 0; i < NUM_PRCMU_WAKEUPS; i++) {
2710 unsigned int irq;
2711
2712 irq = IRQ_PRCMU_BASE + i;
2713 irq_set_chip_and_handler(irq, &prcmu_irq_chip,
2714 handle_simple_irq);
2715 set_irq_flags(irq, IRQF_VALID);
2716 }
Linus Walleije3726fc2010-08-19 12:36:01 +01002717}
2718
Mattias Nilsson05089012012-01-13 16:20:20 +01002719static void __init init_prcm_registers(void)
Mattias Nilssond65e12d2011-08-12 10:27:50 +02002720{
2721 u32 val;
2722
2723 val = readl(PRCM_A9PL_FORCE_CLKEN);
2724 val &= ~(PRCM_A9PL_FORCE_CLKEN_PRCM_A9PL_FORCE_CLKEN |
2725 PRCM_A9PL_FORCE_CLKEN_PRCM_A9AXI_FORCE_CLKEN);
2726 writel(val, (PRCM_A9PL_FORCE_CLKEN));
2727}
2728
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002729/*
2730 * Power domain switches (ePODs) modeled as regulators for the DB8500 SoC
2731 */
2732static struct regulator_consumer_supply db8500_vape_consumers[] = {
2733 REGULATOR_SUPPLY("v-ape", NULL),
2734 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.0"),
2735 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.1"),
2736 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.2"),
2737 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.3"),
2738 /* "v-mmc" changed to "vcore" in the mainline kernel */
2739 REGULATOR_SUPPLY("vcore", "sdi0"),
2740 REGULATOR_SUPPLY("vcore", "sdi1"),
2741 REGULATOR_SUPPLY("vcore", "sdi2"),
2742 REGULATOR_SUPPLY("vcore", "sdi3"),
2743 REGULATOR_SUPPLY("vcore", "sdi4"),
2744 REGULATOR_SUPPLY("v-dma", "dma40.0"),
2745 REGULATOR_SUPPLY("v-ape", "ab8500-usb.0"),
2746 /* "v-uart" changed to "vcore" in the mainline kernel */
2747 REGULATOR_SUPPLY("vcore", "uart0"),
2748 REGULATOR_SUPPLY("vcore", "uart1"),
2749 REGULATOR_SUPPLY("vcore", "uart2"),
2750 REGULATOR_SUPPLY("v-ape", "nmk-ske-keypad.0"),
Bengt Jonsson992b1332012-01-13 16:20:36 +01002751 REGULATOR_SUPPLY("v-hsi", "ste_hsi.0"),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002752};
2753
2754static struct regulator_consumer_supply db8500_vsmps2_consumers[] = {
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002755 REGULATOR_SUPPLY("musb_1v8", "ab8500-usb.0"),
2756 /* AV8100 regulator */
2757 REGULATOR_SUPPLY("hdmi_1v8", "0-0070"),
2758};
2759
2760static struct regulator_consumer_supply db8500_b2r2_mcde_consumers[] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002761 REGULATOR_SUPPLY("vsupply", "b2r2_bus"),
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002762 REGULATOR_SUPPLY("vsupply", "mcde"),
2763};
2764
2765/* SVA MMDSP regulator switch */
2766static struct regulator_consumer_supply db8500_svammdsp_consumers[] = {
2767 REGULATOR_SUPPLY("sva-mmdsp", "cm_control"),
2768};
2769
2770/* SVA pipe regulator switch */
2771static struct regulator_consumer_supply db8500_svapipe_consumers[] = {
2772 REGULATOR_SUPPLY("sva-pipe", "cm_control"),
2773};
2774
2775/* SIA MMDSP regulator switch */
2776static struct regulator_consumer_supply db8500_siammdsp_consumers[] = {
2777 REGULATOR_SUPPLY("sia-mmdsp", "cm_control"),
2778};
2779
2780/* SIA pipe regulator switch */
2781static struct regulator_consumer_supply db8500_siapipe_consumers[] = {
2782 REGULATOR_SUPPLY("sia-pipe", "cm_control"),
2783};
2784
2785static struct regulator_consumer_supply db8500_sga_consumers[] = {
2786 REGULATOR_SUPPLY("v-mali", NULL),
2787};
2788
2789/* ESRAM1 and 2 regulator switch */
2790static struct regulator_consumer_supply db8500_esram12_consumers[] = {
2791 REGULATOR_SUPPLY("esram12", "cm_control"),
2792};
2793
2794/* ESRAM3 and 4 regulator switch */
2795static struct regulator_consumer_supply db8500_esram34_consumers[] = {
2796 REGULATOR_SUPPLY("v-esram34", "mcde"),
2797 REGULATOR_SUPPLY("esram34", "cm_control"),
Bengt Jonsson992b1332012-01-13 16:20:36 +01002798 REGULATOR_SUPPLY("lcla_esram", "dma40.0"),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002799};
2800
2801static struct regulator_init_data db8500_regulators[DB8500_NUM_REGULATORS] = {
2802 [DB8500_REGULATOR_VAPE] = {
2803 .constraints = {
2804 .name = "db8500-vape",
2805 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2806 },
2807 .consumer_supplies = db8500_vape_consumers,
2808 .num_consumer_supplies = ARRAY_SIZE(db8500_vape_consumers),
2809 },
2810 [DB8500_REGULATOR_VARM] = {
2811 .constraints = {
2812 .name = "db8500-varm",
2813 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2814 },
2815 },
2816 [DB8500_REGULATOR_VMODEM] = {
2817 .constraints = {
2818 .name = "db8500-vmodem",
2819 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2820 },
2821 },
2822 [DB8500_REGULATOR_VPLL] = {
2823 .constraints = {
2824 .name = "db8500-vpll",
2825 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2826 },
2827 },
2828 [DB8500_REGULATOR_VSMPS1] = {
2829 .constraints = {
2830 .name = "db8500-vsmps1",
2831 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2832 },
2833 },
2834 [DB8500_REGULATOR_VSMPS2] = {
2835 .constraints = {
2836 .name = "db8500-vsmps2",
2837 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2838 },
2839 .consumer_supplies = db8500_vsmps2_consumers,
2840 .num_consumer_supplies = ARRAY_SIZE(db8500_vsmps2_consumers),
2841 },
2842 [DB8500_REGULATOR_VSMPS3] = {
2843 .constraints = {
2844 .name = "db8500-vsmps3",
2845 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2846 },
2847 },
2848 [DB8500_REGULATOR_VRF1] = {
2849 .constraints = {
2850 .name = "db8500-vrf1",
2851 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2852 },
2853 },
2854 [DB8500_REGULATOR_SWITCH_SVAMMDSP] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002855 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002856 .constraints = {
2857 .name = "db8500-sva-mmdsp",
2858 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2859 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002860 .consumer_supplies = db8500_svammdsp_consumers,
2861 .num_consumer_supplies = ARRAY_SIZE(db8500_svammdsp_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002862 },
2863 [DB8500_REGULATOR_SWITCH_SVAMMDSPRET] = {
2864 .constraints = {
2865 /* "ret" means "retention" */
2866 .name = "db8500-sva-mmdsp-ret",
2867 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2868 },
2869 },
2870 [DB8500_REGULATOR_SWITCH_SVAPIPE] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002871 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002872 .constraints = {
2873 .name = "db8500-sva-pipe",
2874 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2875 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002876 .consumer_supplies = db8500_svapipe_consumers,
2877 .num_consumer_supplies = ARRAY_SIZE(db8500_svapipe_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002878 },
2879 [DB8500_REGULATOR_SWITCH_SIAMMDSP] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002880 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002881 .constraints = {
2882 .name = "db8500-sia-mmdsp",
2883 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2884 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002885 .consumer_supplies = db8500_siammdsp_consumers,
2886 .num_consumer_supplies = ARRAY_SIZE(db8500_siammdsp_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002887 },
2888 [DB8500_REGULATOR_SWITCH_SIAMMDSPRET] = {
2889 .constraints = {
2890 .name = "db8500-sia-mmdsp-ret",
2891 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2892 },
2893 },
2894 [DB8500_REGULATOR_SWITCH_SIAPIPE] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002895 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002896 .constraints = {
2897 .name = "db8500-sia-pipe",
2898 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2899 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002900 .consumer_supplies = db8500_siapipe_consumers,
2901 .num_consumer_supplies = ARRAY_SIZE(db8500_siapipe_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002902 },
2903 [DB8500_REGULATOR_SWITCH_SGA] = {
2904 .supply_regulator = "db8500-vape",
2905 .constraints = {
2906 .name = "db8500-sga",
2907 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2908 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002909 .consumer_supplies = db8500_sga_consumers,
2910 .num_consumer_supplies = ARRAY_SIZE(db8500_sga_consumers),
2911
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002912 },
2913 [DB8500_REGULATOR_SWITCH_B2R2_MCDE] = {
2914 .supply_regulator = "db8500-vape",
2915 .constraints = {
2916 .name = "db8500-b2r2-mcde",
2917 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2918 },
2919 .consumer_supplies = db8500_b2r2_mcde_consumers,
2920 .num_consumer_supplies = ARRAY_SIZE(db8500_b2r2_mcde_consumers),
2921 },
2922 [DB8500_REGULATOR_SWITCH_ESRAM12] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002923 /*
2924 * esram12 is set in retention and supplied by Vsafe when Vape is off,
2925 * no need to hold Vape
2926 */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002927 .constraints = {
2928 .name = "db8500-esram12",
2929 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2930 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002931 .consumer_supplies = db8500_esram12_consumers,
2932 .num_consumer_supplies = ARRAY_SIZE(db8500_esram12_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002933 },
2934 [DB8500_REGULATOR_SWITCH_ESRAM12RET] = {
2935 .constraints = {
2936 .name = "db8500-esram12-ret",
2937 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2938 },
2939 },
2940 [DB8500_REGULATOR_SWITCH_ESRAM34] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002941 /*
2942 * esram34 is set in retention and supplied by Vsafe when Vape is off,
2943 * no need to hold Vape
2944 */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002945 .constraints = {
2946 .name = "db8500-esram34",
2947 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2948 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002949 .consumer_supplies = db8500_esram34_consumers,
2950 .num_consumer_supplies = ARRAY_SIZE(db8500_esram34_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002951 },
2952 [DB8500_REGULATOR_SWITCH_ESRAM34RET] = {
2953 .constraints = {
2954 .name = "db8500-esram34-ret",
2955 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2956 },
2957 },
2958};
2959
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002960static struct mfd_cell db8500_prcmu_devs[] = {
2961 {
2962 .name = "db8500-prcmu-regulators",
Mattias Wallin1ed78912011-05-27 11:49:43 +02002963 .platform_data = &db8500_regulators,
2964 .pdata_size = sizeof(db8500_regulators),
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002965 },
2966 {
2967 .name = "cpufreq-u8500",
2968 },
2969};
2970
2971/**
2972 * prcmu_fw_init - arch init call for the Linux PRCMU fw init logic
2973 *
2974 */
2975static int __init db8500_prcmu_probe(struct platform_device *pdev)
2976{
2977 int err = 0;
2978
2979 if (ux500_is_svp())
2980 return -ENODEV;
2981
Mattias Nilsson05089012012-01-13 16:20:20 +01002982 init_prcm_registers();
Mattias Nilssond65e12d2011-08-12 10:27:50 +02002983
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002984 /* Clean up the mailbox interrupts after pre-kernel code. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002985 writel(ALL_MBOX_BITS, PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002986
2987 err = request_threaded_irq(IRQ_DB8500_PRCMU1, prcmu_irq_handler,
2988 prcmu_irq_thread_fn, IRQF_NO_SUSPEND, "prcmu", NULL);
2989 if (err < 0) {
2990 pr_err("prcmu: Failed to allocate IRQ_DB8500_PRCMU1.\n");
2991 err = -EBUSY;
2992 goto no_irq_return;
2993 }
2994
2995 if (cpu_is_u8500v20_or_later())
2996 prcmu_config_esram0_deep_sleep(ESRAM0_DEEP_SLEEP_STATE_RET);
2997
2998 err = mfd_add_devices(&pdev->dev, 0, db8500_prcmu_devs,
2999 ARRAY_SIZE(db8500_prcmu_devs), NULL,
3000 0);
3001
3002 if (err)
3003 pr_err("prcmu: Failed to add subdevices\n");
3004 else
3005 pr_info("DB8500 PRCMU initialized\n");
3006
3007no_irq_return:
3008 return err;
3009}
3010
3011static struct platform_driver db8500_prcmu_driver = {
3012 .driver = {
3013 .name = "db8500-prcmu",
3014 .owner = THIS_MODULE,
3015 },
3016};
3017
3018static int __init db8500_prcmu_init(void)
3019{
3020 return platform_driver_probe(&db8500_prcmu_driver, db8500_prcmu_probe);
3021}
3022
3023arch_initcall(db8500_prcmu_init);
3024
3025MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com>");
3026MODULE_DESCRIPTION("DB8500 PRCM Unit driver");
3027MODULE_LICENSE("GPL v2");