blob: d2244dc5d3b16d54eee188999ddc5e3974950b4a [file] [log] [blame]
Linus Walleije3726fc2010-08-19 12:36:01 +01001/*
Martin Perssone0befb22010-12-08 15:13:28 +01002 * Copyright (C) STMicroelectronics 2009
3 * Copyright (C) ST-Ericsson SA 2010
Linus Walleije3726fc2010-08-19 12:36:01 +01004 *
5 * License Terms: GNU General Public License v2
Martin Perssone0befb22010-12-08 15:13:28 +01006 * Author: Kumar Sanghvi <kumar.sanghvi@stericsson.com>
7 * Author: Sundar Iyer <sundar.iyer@stericsson.com>
Linus Walleije3726fc2010-08-19 12:36:01 +01008 * Author: Mattias Nilsson <mattias.i.nilsson@stericsson.com>
9 *
Martin Perssone0befb22010-12-08 15:13:28 +010010 * U8500 PRCM Unit interface driver
11 *
Linus Walleije3726fc2010-08-19 12:36:01 +010012 */
Linus Walleije3726fc2010-08-19 12:36:01 +010013#include <linux/module.h>
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020014#include <linux/kernel.h>
15#include <linux/delay.h>
Linus Walleije3726fc2010-08-19 12:36:01 +010016#include <linux/errno.h>
17#include <linux/err.h>
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020018#include <linux/spinlock.h>
Linus Walleije3726fc2010-08-19 12:36:01 +010019#include <linux/io.h>
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020020#include <linux/slab.h>
Linus Walleije3726fc2010-08-19 12:36:01 +010021#include <linux/mutex.h>
22#include <linux/completion.h>
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020023#include <linux/irq.h>
Linus Walleije3726fc2010-08-19 12:36:01 +010024#include <linux/jiffies.h>
25#include <linux/bitops.h>
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020026#include <linux/fs.h>
27#include <linux/platform_device.h>
28#include <linux/uaccess.h>
29#include <linux/mfd/core.h>
Mattias Nilsson73180f82011-08-12 10:28:10 +020030#include <linux/mfd/dbx500-prcmu.h>
Bengt Jonsson1032fbf2011-04-01 14:43:33 +020031#include <linux/regulator/db8500-prcmu.h>
32#include <linux/regulator/machine.h>
Linus Walleije3726fc2010-08-19 12:36:01 +010033#include <mach/hardware.h>
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020034#include <mach/irqs.h>
35#include <mach/db8500-regs.h>
36#include <mach/id.h>
Mattias Nilsson73180f82011-08-12 10:28:10 +020037#include "dbx500-prcmu-regs.h"
Linus Walleije3726fc2010-08-19 12:36:01 +010038
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020039/* Offset for the firmware version within the TCPM */
40#define PRCMU_FW_VERSION_OFFSET 0xA4
Linus Walleije3726fc2010-08-19 12:36:01 +010041
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020042/* Index of different voltages to be used when accessing AVSData */
43#define PRCM_AVS_BASE 0x2FC
44#define PRCM_AVS_VBB_RET (PRCM_AVS_BASE + 0x0)
45#define PRCM_AVS_VBB_MAX_OPP (PRCM_AVS_BASE + 0x1)
46#define PRCM_AVS_VBB_100_OPP (PRCM_AVS_BASE + 0x2)
47#define PRCM_AVS_VBB_50_OPP (PRCM_AVS_BASE + 0x3)
48#define PRCM_AVS_VARM_MAX_OPP (PRCM_AVS_BASE + 0x4)
49#define PRCM_AVS_VARM_100_OPP (PRCM_AVS_BASE + 0x5)
50#define PRCM_AVS_VARM_50_OPP (PRCM_AVS_BASE + 0x6)
51#define PRCM_AVS_VARM_RET (PRCM_AVS_BASE + 0x7)
52#define PRCM_AVS_VAPE_100_OPP (PRCM_AVS_BASE + 0x8)
53#define PRCM_AVS_VAPE_50_OPP (PRCM_AVS_BASE + 0x9)
54#define PRCM_AVS_VMOD_100_OPP (PRCM_AVS_BASE + 0xA)
55#define PRCM_AVS_VMOD_50_OPP (PRCM_AVS_BASE + 0xB)
56#define PRCM_AVS_VSAFE (PRCM_AVS_BASE + 0xC)
Martin Perssone0befb22010-12-08 15:13:28 +010057
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020058#define PRCM_AVS_VOLTAGE 0
59#define PRCM_AVS_VOLTAGE_MASK 0x3f
60#define PRCM_AVS_ISSLOWSTARTUP 6
61#define PRCM_AVS_ISSLOWSTARTUP_MASK (1 << PRCM_AVS_ISSLOWSTARTUP)
Martin Perssone0befb22010-12-08 15:13:28 +010062#define PRCM_AVS_ISMODEENABLE 7
63#define PRCM_AVS_ISMODEENABLE_MASK (1 << PRCM_AVS_ISMODEENABLE)
64
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020065#define PRCM_BOOT_STATUS 0xFFF
66#define PRCM_ROMCODE_A2P 0xFFE
67#define PRCM_ROMCODE_P2A 0xFFD
68#define PRCM_XP70_CUR_PWR_STATE 0xFFC /* 4 BYTES */
Linus Walleije3726fc2010-08-19 12:36:01 +010069
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020070#define PRCM_SW_RST_REASON 0xFF8 /* 2 bytes */
71
72#define _PRCM_MBOX_HEADER 0xFE8 /* 16 bytes */
73#define PRCM_MBOX_HEADER_REQ_MB0 (_PRCM_MBOX_HEADER + 0x0)
74#define PRCM_MBOX_HEADER_REQ_MB1 (_PRCM_MBOX_HEADER + 0x1)
75#define PRCM_MBOX_HEADER_REQ_MB2 (_PRCM_MBOX_HEADER + 0x2)
76#define PRCM_MBOX_HEADER_REQ_MB3 (_PRCM_MBOX_HEADER + 0x3)
77#define PRCM_MBOX_HEADER_REQ_MB4 (_PRCM_MBOX_HEADER + 0x4)
78#define PRCM_MBOX_HEADER_REQ_MB5 (_PRCM_MBOX_HEADER + 0x5)
79#define PRCM_MBOX_HEADER_ACK_MB0 (_PRCM_MBOX_HEADER + 0x8)
80
81/* Req Mailboxes */
82#define PRCM_REQ_MB0 0xFDC /* 12 bytes */
83#define PRCM_REQ_MB1 0xFD0 /* 12 bytes */
84#define PRCM_REQ_MB2 0xFC0 /* 16 bytes */
85#define PRCM_REQ_MB3 0xE4C /* 372 bytes */
86#define PRCM_REQ_MB4 0xE48 /* 4 bytes */
87#define PRCM_REQ_MB5 0xE44 /* 4 bytes */
88
89/* Ack Mailboxes */
90#define PRCM_ACK_MB0 0xE08 /* 52 bytes */
91#define PRCM_ACK_MB1 0xE04 /* 4 bytes */
92#define PRCM_ACK_MB2 0xE00 /* 4 bytes */
93#define PRCM_ACK_MB3 0xDFC /* 4 bytes */
94#define PRCM_ACK_MB4 0xDF8 /* 4 bytes */
95#define PRCM_ACK_MB5 0xDF4 /* 4 bytes */
96
97/* Mailbox 0 headers */
98#define MB0H_POWER_STATE_TRANS 0
99#define MB0H_CONFIG_WAKEUPS_EXE 1
100#define MB0H_READ_WAKEUP_ACK 3
101#define MB0H_CONFIG_WAKEUPS_SLEEP 4
102
103#define MB0H_WAKEUP_EXE 2
104#define MB0H_WAKEUP_SLEEP 5
105
106/* Mailbox 0 REQs */
107#define PRCM_REQ_MB0_AP_POWER_STATE (PRCM_REQ_MB0 + 0x0)
108#define PRCM_REQ_MB0_AP_PLL_STATE (PRCM_REQ_MB0 + 0x1)
109#define PRCM_REQ_MB0_ULP_CLOCK_STATE (PRCM_REQ_MB0 + 0x2)
110#define PRCM_REQ_MB0_DO_NOT_WFI (PRCM_REQ_MB0 + 0x3)
111#define PRCM_REQ_MB0_WAKEUP_8500 (PRCM_REQ_MB0 + 0x4)
112#define PRCM_REQ_MB0_WAKEUP_4500 (PRCM_REQ_MB0 + 0x8)
113
114/* Mailbox 0 ACKs */
115#define PRCM_ACK_MB0_AP_PWRSTTR_STATUS (PRCM_ACK_MB0 + 0x0)
116#define PRCM_ACK_MB0_READ_POINTER (PRCM_ACK_MB0 + 0x1)
117#define PRCM_ACK_MB0_WAKEUP_0_8500 (PRCM_ACK_MB0 + 0x4)
118#define PRCM_ACK_MB0_WAKEUP_0_4500 (PRCM_ACK_MB0 + 0x8)
119#define PRCM_ACK_MB0_WAKEUP_1_8500 (PRCM_ACK_MB0 + 0x1C)
120#define PRCM_ACK_MB0_WAKEUP_1_4500 (PRCM_ACK_MB0 + 0x20)
121#define PRCM_ACK_MB0_EVENT_4500_NUMBERS 20
122
123/* Mailbox 1 headers */
124#define MB1H_ARM_APE_OPP 0x0
125#define MB1H_RESET_MODEM 0x2
126#define MB1H_REQUEST_APE_OPP_100_VOLT 0x3
127#define MB1H_RELEASE_APE_OPP_100_VOLT 0x4
128#define MB1H_RELEASE_USB_WAKEUP 0x5
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200129#define MB1H_PLL_ON_OFF 0x6
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200130
131/* Mailbox 1 Requests */
132#define PRCM_REQ_MB1_ARM_OPP (PRCM_REQ_MB1 + 0x0)
133#define PRCM_REQ_MB1_APE_OPP (PRCM_REQ_MB1 + 0x1)
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200134#define PRCM_REQ_MB1_PLL_ON_OFF (PRCM_REQ_MB1 + 0x4)
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100135#define PLL_SOC0_OFF 0x1
136#define PLL_SOC0_ON 0x2
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200137#define PLL_SOC1_OFF 0x4
138#define PLL_SOC1_ON 0x8
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200139
140/* Mailbox 1 ACKs */
141#define PRCM_ACK_MB1_CURRENT_ARM_OPP (PRCM_ACK_MB1 + 0x0)
142#define PRCM_ACK_MB1_CURRENT_APE_OPP (PRCM_ACK_MB1 + 0x1)
143#define PRCM_ACK_MB1_APE_VOLTAGE_STATUS (PRCM_ACK_MB1 + 0x2)
144#define PRCM_ACK_MB1_DVFS_STATUS (PRCM_ACK_MB1 + 0x3)
145
146/* Mailbox 2 headers */
147#define MB2H_DPS 0x0
148#define MB2H_AUTO_PWR 0x1
149
150/* Mailbox 2 REQs */
151#define PRCM_REQ_MB2_SVA_MMDSP (PRCM_REQ_MB2 + 0x0)
152#define PRCM_REQ_MB2_SVA_PIPE (PRCM_REQ_MB2 + 0x1)
153#define PRCM_REQ_MB2_SIA_MMDSP (PRCM_REQ_MB2 + 0x2)
154#define PRCM_REQ_MB2_SIA_PIPE (PRCM_REQ_MB2 + 0x3)
155#define PRCM_REQ_MB2_SGA (PRCM_REQ_MB2 + 0x4)
156#define PRCM_REQ_MB2_B2R2_MCDE (PRCM_REQ_MB2 + 0x5)
157#define PRCM_REQ_MB2_ESRAM12 (PRCM_REQ_MB2 + 0x6)
158#define PRCM_REQ_MB2_ESRAM34 (PRCM_REQ_MB2 + 0x7)
159#define PRCM_REQ_MB2_AUTO_PM_SLEEP (PRCM_REQ_MB2 + 0x8)
160#define PRCM_REQ_MB2_AUTO_PM_IDLE (PRCM_REQ_MB2 + 0xC)
161
162/* Mailbox 2 ACKs */
163#define PRCM_ACK_MB2_DPS_STATUS (PRCM_ACK_MB2 + 0x0)
164#define HWACC_PWR_ST_OK 0xFE
165
166/* Mailbox 3 headers */
167#define MB3H_ANC 0x0
168#define MB3H_SIDETONE 0x1
169#define MB3H_SYSCLK 0xE
170
171/* Mailbox 3 Requests */
172#define PRCM_REQ_MB3_ANC_FIR_COEFF (PRCM_REQ_MB3 + 0x0)
173#define PRCM_REQ_MB3_ANC_IIR_COEFF (PRCM_REQ_MB3 + 0x20)
174#define PRCM_REQ_MB3_ANC_SHIFTER (PRCM_REQ_MB3 + 0x60)
175#define PRCM_REQ_MB3_ANC_WARP (PRCM_REQ_MB3 + 0x64)
176#define PRCM_REQ_MB3_SIDETONE_FIR_GAIN (PRCM_REQ_MB3 + 0x68)
177#define PRCM_REQ_MB3_SIDETONE_FIR_COEFF (PRCM_REQ_MB3 + 0x6C)
178#define PRCM_REQ_MB3_SYSCLK_MGT (PRCM_REQ_MB3 + 0x16C)
179
180/* Mailbox 4 headers */
181#define MB4H_DDR_INIT 0x0
182#define MB4H_MEM_ST 0x1
183#define MB4H_HOTDOG 0x12
184#define MB4H_HOTMON 0x13
185#define MB4H_HOT_PERIOD 0x14
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200186#define MB4H_A9WDOG_CONF 0x16
187#define MB4H_A9WDOG_EN 0x17
188#define MB4H_A9WDOG_DIS 0x18
189#define MB4H_A9WDOG_LOAD 0x19
190#define MB4H_A9WDOG_KICK 0x20
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200191
192/* Mailbox 4 Requests */
193#define PRCM_REQ_MB4_DDR_ST_AP_SLEEP_IDLE (PRCM_REQ_MB4 + 0x0)
194#define PRCM_REQ_MB4_DDR_ST_AP_DEEP_IDLE (PRCM_REQ_MB4 + 0x1)
195#define PRCM_REQ_MB4_ESRAM0_ST (PRCM_REQ_MB4 + 0x3)
196#define PRCM_REQ_MB4_HOTDOG_THRESHOLD (PRCM_REQ_MB4 + 0x0)
197#define PRCM_REQ_MB4_HOTMON_LOW (PRCM_REQ_MB4 + 0x0)
198#define PRCM_REQ_MB4_HOTMON_HIGH (PRCM_REQ_MB4 + 0x1)
199#define PRCM_REQ_MB4_HOTMON_CONFIG (PRCM_REQ_MB4 + 0x2)
200#define PRCM_REQ_MB4_HOT_PERIOD (PRCM_REQ_MB4 + 0x0)
201#define HOTMON_CONFIG_LOW BIT(0)
202#define HOTMON_CONFIG_HIGH BIT(1)
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200203#define PRCM_REQ_MB4_A9WDOG_0 (PRCM_REQ_MB4 + 0x0)
204#define PRCM_REQ_MB4_A9WDOG_1 (PRCM_REQ_MB4 + 0x1)
205#define PRCM_REQ_MB4_A9WDOG_2 (PRCM_REQ_MB4 + 0x2)
206#define PRCM_REQ_MB4_A9WDOG_3 (PRCM_REQ_MB4 + 0x3)
207#define A9WDOG_AUTO_OFF_EN BIT(7)
208#define A9WDOG_AUTO_OFF_DIS 0
209#define A9WDOG_ID_MASK 0xf
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200210
211/* Mailbox 5 Requests */
212#define PRCM_REQ_MB5_I2C_SLAVE_OP (PRCM_REQ_MB5 + 0x0)
213#define PRCM_REQ_MB5_I2C_HW_BITS (PRCM_REQ_MB5 + 0x1)
214#define PRCM_REQ_MB5_I2C_REG (PRCM_REQ_MB5 + 0x2)
215#define PRCM_REQ_MB5_I2C_VAL (PRCM_REQ_MB5 + 0x3)
216#define PRCMU_I2C_WRITE(slave) \
217 (((slave) << 1) | (cpu_is_u8500v2() ? BIT(6) : 0))
218#define PRCMU_I2C_READ(slave) \
219 (((slave) << 1) | BIT(0) | (cpu_is_u8500v2() ? BIT(6) : 0))
220#define PRCMU_I2C_STOP_EN BIT(3)
221
222/* Mailbox 5 ACKs */
223#define PRCM_ACK_MB5_I2C_STATUS (PRCM_ACK_MB5 + 0x1)
224#define PRCM_ACK_MB5_I2C_VAL (PRCM_ACK_MB5 + 0x3)
225#define I2C_WR_OK 0x1
226#define I2C_RD_OK 0x2
227
228#define NUM_MB 8
229#define MBOX_BIT BIT
230#define ALL_MBOX_BITS (MBOX_BIT(NUM_MB) - 1)
231
232/*
233 * Wakeups/IRQs
234 */
235
236#define WAKEUP_BIT_RTC BIT(0)
237#define WAKEUP_BIT_RTT0 BIT(1)
238#define WAKEUP_BIT_RTT1 BIT(2)
239#define WAKEUP_BIT_HSI0 BIT(3)
240#define WAKEUP_BIT_HSI1 BIT(4)
241#define WAKEUP_BIT_CA_WAKE BIT(5)
242#define WAKEUP_BIT_USB BIT(6)
243#define WAKEUP_BIT_ABB BIT(7)
244#define WAKEUP_BIT_ABB_FIFO BIT(8)
245#define WAKEUP_BIT_SYSCLK_OK BIT(9)
246#define WAKEUP_BIT_CA_SLEEP BIT(10)
247#define WAKEUP_BIT_AC_WAKE_ACK BIT(11)
248#define WAKEUP_BIT_SIDE_TONE_OK BIT(12)
249#define WAKEUP_BIT_ANC_OK BIT(13)
250#define WAKEUP_BIT_SW_ERROR BIT(14)
251#define WAKEUP_BIT_AC_SLEEP_ACK BIT(15)
252#define WAKEUP_BIT_ARM BIT(17)
253#define WAKEUP_BIT_HOTMON_LOW BIT(18)
254#define WAKEUP_BIT_HOTMON_HIGH BIT(19)
255#define WAKEUP_BIT_MODEM_SW_RESET_REQ BIT(20)
256#define WAKEUP_BIT_GPIO0 BIT(23)
257#define WAKEUP_BIT_GPIO1 BIT(24)
258#define WAKEUP_BIT_GPIO2 BIT(25)
259#define WAKEUP_BIT_GPIO3 BIT(26)
260#define WAKEUP_BIT_GPIO4 BIT(27)
261#define WAKEUP_BIT_GPIO5 BIT(28)
262#define WAKEUP_BIT_GPIO6 BIT(29)
263#define WAKEUP_BIT_GPIO7 BIT(30)
264#define WAKEUP_BIT_GPIO8 BIT(31)
265
Mattias Nilssonb58d12f2012-01-13 16:20:10 +0100266static struct {
267 bool valid;
268 struct prcmu_fw_version version;
269} fw_info;
270
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200271/*
272 * This vector maps irq numbers to the bits in the bit field used in
273 * communication with the PRCMU firmware.
274 *
275 * The reason for having this is to keep the irq numbers contiguous even though
276 * the bits in the bit field are not. (The bits also have a tendency to move
277 * around, to further complicate matters.)
278 */
279#define IRQ_INDEX(_name) ((IRQ_PRCMU_##_name) - IRQ_PRCMU_BASE)
280#define IRQ_ENTRY(_name)[IRQ_INDEX(_name)] = (WAKEUP_BIT_##_name)
281static u32 prcmu_irq_bit[NUM_PRCMU_WAKEUPS] = {
282 IRQ_ENTRY(RTC),
283 IRQ_ENTRY(RTT0),
284 IRQ_ENTRY(RTT1),
285 IRQ_ENTRY(HSI0),
286 IRQ_ENTRY(HSI1),
287 IRQ_ENTRY(CA_WAKE),
288 IRQ_ENTRY(USB),
289 IRQ_ENTRY(ABB),
290 IRQ_ENTRY(ABB_FIFO),
291 IRQ_ENTRY(CA_SLEEP),
292 IRQ_ENTRY(ARM),
293 IRQ_ENTRY(HOTMON_LOW),
294 IRQ_ENTRY(HOTMON_HIGH),
295 IRQ_ENTRY(MODEM_SW_RESET_REQ),
296 IRQ_ENTRY(GPIO0),
297 IRQ_ENTRY(GPIO1),
298 IRQ_ENTRY(GPIO2),
299 IRQ_ENTRY(GPIO3),
300 IRQ_ENTRY(GPIO4),
301 IRQ_ENTRY(GPIO5),
302 IRQ_ENTRY(GPIO6),
303 IRQ_ENTRY(GPIO7),
304 IRQ_ENTRY(GPIO8)
Martin Perssone0befb22010-12-08 15:13:28 +0100305};
306
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200307#define VALID_WAKEUPS (BIT(NUM_PRCMU_WAKEUP_INDICES) - 1)
308#define WAKEUP_ENTRY(_name)[PRCMU_WAKEUP_INDEX_##_name] = (WAKEUP_BIT_##_name)
309static u32 prcmu_wakeup_bit[NUM_PRCMU_WAKEUP_INDICES] = {
310 WAKEUP_ENTRY(RTC),
311 WAKEUP_ENTRY(RTT0),
312 WAKEUP_ENTRY(RTT1),
313 WAKEUP_ENTRY(HSI0),
314 WAKEUP_ENTRY(HSI1),
315 WAKEUP_ENTRY(USB),
316 WAKEUP_ENTRY(ABB),
317 WAKEUP_ENTRY(ABB_FIFO),
318 WAKEUP_ENTRY(ARM)
319};
320
321/*
322 * mb0_transfer - state needed for mailbox 0 communication.
323 * @lock: The transaction lock.
324 * @dbb_events_lock: A lock used to handle concurrent access to (parts of)
325 * the request data.
326 * @mask_work: Work structure used for (un)masking wakeup interrupts.
327 * @req: Request data that need to persist between requests.
328 */
329static struct {
330 spinlock_t lock;
331 spinlock_t dbb_irqs_lock;
332 struct work_struct mask_work;
333 struct mutex ac_wake_lock;
334 struct completion ac_wake_work;
335 struct {
336 u32 dbb_irqs;
337 u32 dbb_wakeups;
338 u32 abb_events;
339 } req;
340} mb0_transfer;
341
342/*
343 * mb1_transfer - state needed for mailbox 1 communication.
344 * @lock: The transaction lock.
345 * @work: The transaction completion structure.
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100346 * @ape_opp: The current APE OPP.
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200347 * @ack: Reply ("acknowledge") data.
348 */
Martin Perssone0befb22010-12-08 15:13:28 +0100349static struct {
350 struct mutex lock;
351 struct completion work;
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100352 u8 ape_opp;
Martin Perssone0befb22010-12-08 15:13:28 +0100353 struct {
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200354 u8 header;
Martin Perssone0befb22010-12-08 15:13:28 +0100355 u8 arm_opp;
356 u8 ape_opp;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200357 u8 ape_voltage_status;
Martin Perssone0befb22010-12-08 15:13:28 +0100358 } ack;
359} mb1_transfer;
360
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200361/*
362 * mb2_transfer - state needed for mailbox 2 communication.
363 * @lock: The transaction lock.
364 * @work: The transaction completion structure.
365 * @auto_pm_lock: The autonomous power management configuration lock.
366 * @auto_pm_enabled: A flag indicating whether autonomous PM is enabled.
367 * @req: Request data that need to persist between requests.
368 * @ack: Reply ("acknowledge") data.
369 */
Linus Walleije3726fc2010-08-19 12:36:01 +0100370static struct {
371 struct mutex lock;
372 struct completion work;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200373 spinlock_t auto_pm_lock;
374 bool auto_pm_enabled;
375 struct {
376 u8 status;
377 } ack;
378} mb2_transfer;
379
380/*
381 * mb3_transfer - state needed for mailbox 3 communication.
382 * @lock: The request lock.
383 * @sysclk_lock: A lock used to handle concurrent sysclk requests.
384 * @sysclk_work: Work structure used for sysclk requests.
385 */
386static struct {
387 spinlock_t lock;
388 struct mutex sysclk_lock;
389 struct completion sysclk_work;
390} mb3_transfer;
391
392/*
393 * mb4_transfer - state needed for mailbox 4 communication.
394 * @lock: The transaction lock.
395 * @work: The transaction completion structure.
396 */
397static struct {
398 struct mutex lock;
399 struct completion work;
400} mb4_transfer;
401
402/*
403 * mb5_transfer - state needed for mailbox 5 communication.
404 * @lock: The transaction lock.
405 * @work: The transaction completion structure.
406 * @ack: Reply ("acknowledge") data.
407 */
408static struct {
409 struct mutex lock;
410 struct completion work;
Linus Walleije3726fc2010-08-19 12:36:01 +0100411 struct {
412 u8 status;
413 u8 value;
414 } ack;
415} mb5_transfer;
416
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200417static atomic_t ac_wake_req_state = ATOMIC_INIT(0);
418
419/* Spinlocks */
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100420static DEFINE_SPINLOCK(prcmu_lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200421static DEFINE_SPINLOCK(clkout_lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200422
423/* Global var to runtime determine TCDM base for v2 or v1 */
424static __iomem void *tcdm_base;
425
426struct clk_mgt {
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100427 void __iomem *reg;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200428 u32 pllsw;
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100429 int branch;
430 bool clk38div;
431};
432
433enum {
434 PLL_RAW,
435 PLL_FIX,
436 PLL_DIV
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200437};
438
439static DEFINE_SPINLOCK(clk_mgt_lock);
440
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100441#define CLK_MGT_ENTRY(_name, _branch, _clk38div)[PRCMU_##_name] = \
442 { (PRCM_##_name##_MGT), 0 , _branch, _clk38div}
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200443struct clk_mgt clk_mgt[PRCMU_NUM_REG_CLOCKS] = {
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100444 CLK_MGT_ENTRY(SGACLK, PLL_DIV, false),
445 CLK_MGT_ENTRY(UARTCLK, PLL_FIX, true),
446 CLK_MGT_ENTRY(MSP02CLK, PLL_FIX, true),
447 CLK_MGT_ENTRY(MSP1CLK, PLL_FIX, true),
448 CLK_MGT_ENTRY(I2CCLK, PLL_FIX, true),
449 CLK_MGT_ENTRY(SDMMCCLK, PLL_DIV, true),
450 CLK_MGT_ENTRY(SLIMCLK, PLL_FIX, true),
451 CLK_MGT_ENTRY(PER1CLK, PLL_DIV, true),
452 CLK_MGT_ENTRY(PER2CLK, PLL_DIV, true),
453 CLK_MGT_ENTRY(PER3CLK, PLL_DIV, true),
454 CLK_MGT_ENTRY(PER5CLK, PLL_DIV, true),
455 CLK_MGT_ENTRY(PER6CLK, PLL_DIV, true),
456 CLK_MGT_ENTRY(PER7CLK, PLL_DIV, true),
457 CLK_MGT_ENTRY(LCDCLK, PLL_FIX, true),
458 CLK_MGT_ENTRY(BMLCLK, PLL_DIV, true),
459 CLK_MGT_ENTRY(HSITXCLK, PLL_DIV, true),
460 CLK_MGT_ENTRY(HSIRXCLK, PLL_DIV, true),
461 CLK_MGT_ENTRY(HDMICLK, PLL_FIX, false),
462 CLK_MGT_ENTRY(APEATCLK, PLL_DIV, true),
463 CLK_MGT_ENTRY(APETRACECLK, PLL_DIV, true),
464 CLK_MGT_ENTRY(MCDECLK, PLL_DIV, true),
465 CLK_MGT_ENTRY(IPI2CCLK, PLL_FIX, true),
466 CLK_MGT_ENTRY(DSIALTCLK, PLL_FIX, false),
467 CLK_MGT_ENTRY(DMACLK, PLL_DIV, true),
468 CLK_MGT_ENTRY(B2R2CLK, PLL_DIV, true),
469 CLK_MGT_ENTRY(TVCLK, PLL_FIX, true),
470 CLK_MGT_ENTRY(SSPCLK, PLL_FIX, true),
471 CLK_MGT_ENTRY(RNGCLK, PLL_FIX, true),
472 CLK_MGT_ENTRY(UICCCLK, PLL_FIX, false),
473};
474
475struct dsiclk {
476 u32 divsel_mask;
477 u32 divsel_shift;
478 u32 divsel;
479};
480
481static struct dsiclk dsiclk[2] = {
482 {
483 .divsel_mask = PRCM_DSI_PLLOUT_SEL_DSI0_PLLOUT_DIVSEL_MASK,
484 .divsel_shift = PRCM_DSI_PLLOUT_SEL_DSI0_PLLOUT_DIVSEL_SHIFT,
485 .divsel = PRCM_DSI_PLLOUT_SEL_PHI,
486 },
487 {
488 .divsel_mask = PRCM_DSI_PLLOUT_SEL_DSI1_PLLOUT_DIVSEL_MASK,
489 .divsel_shift = PRCM_DSI_PLLOUT_SEL_DSI1_PLLOUT_DIVSEL_SHIFT,
490 .divsel = PRCM_DSI_PLLOUT_SEL_PHI,
491 }
492};
493
494struct dsiescclk {
495 u32 en;
496 u32 div_mask;
497 u32 div_shift;
498};
499
500static struct dsiescclk dsiescclk[3] = {
501 {
502 .en = PRCM_DSITVCLK_DIV_DSI0_ESC_CLK_EN,
503 .div_mask = PRCM_DSITVCLK_DIV_DSI0_ESC_CLK_DIV_MASK,
504 .div_shift = PRCM_DSITVCLK_DIV_DSI0_ESC_CLK_DIV_SHIFT,
505 },
506 {
507 .en = PRCM_DSITVCLK_DIV_DSI1_ESC_CLK_EN,
508 .div_mask = PRCM_DSITVCLK_DIV_DSI1_ESC_CLK_DIV_MASK,
509 .div_shift = PRCM_DSITVCLK_DIV_DSI1_ESC_CLK_DIV_SHIFT,
510 },
511 {
512 .en = PRCM_DSITVCLK_DIV_DSI2_ESC_CLK_EN,
513 .div_mask = PRCM_DSITVCLK_DIV_DSI2_ESC_CLK_DIV_MASK,
514 .div_shift = PRCM_DSITVCLK_DIV_DSI2_ESC_CLK_DIV_SHIFT,
515 }
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200516};
517
Mattias Nilsson0837bb72011-08-12 10:28:18 +0200518static struct regulator *hwacc_regulator[NUM_HW_ACC];
519static struct regulator *hwacc_ret_regulator[NUM_HW_ACC];
520
521static bool hwacc_enabled[NUM_HW_ACC];
522static bool hwacc_ret_enabled[NUM_HW_ACC];
523
524static const char *hwacc_regulator_name[NUM_HW_ACC] = {
525 [HW_ACC_SVAMMDSP] = "hwacc-sva-mmdsp",
526 [HW_ACC_SVAPIPE] = "hwacc-sva-pipe",
527 [HW_ACC_SIAMMDSP] = "hwacc-sia-mmdsp",
528 [HW_ACC_SIAPIPE] = "hwacc-sia-pipe",
529 [HW_ACC_SGA] = "hwacc-sga",
530 [HW_ACC_B2R2] = "hwacc-b2r2",
531 [HW_ACC_MCDE] = "hwacc-mcde",
532 [HW_ACC_ESRAM1] = "hwacc-esram1",
533 [HW_ACC_ESRAM2] = "hwacc-esram2",
534 [HW_ACC_ESRAM3] = "hwacc-esram3",
535 [HW_ACC_ESRAM4] = "hwacc-esram4",
536};
537
538static const char *hwacc_ret_regulator_name[NUM_HW_ACC] = {
539 [HW_ACC_SVAMMDSP] = "hwacc-sva-mmdsp-ret",
540 [HW_ACC_SIAMMDSP] = "hwacc-sia-mmdsp-ret",
541 [HW_ACC_ESRAM1] = "hwacc-esram1-ret",
542 [HW_ACC_ESRAM2] = "hwacc-esram2-ret",
543 [HW_ACC_ESRAM3] = "hwacc-esram3-ret",
544 [HW_ACC_ESRAM4] = "hwacc-esram4-ret",
545};
546
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200547/*
548* Used by MCDE to setup all necessary PRCMU registers
549*/
550#define PRCMU_RESET_DSIPLL 0x00004000
551#define PRCMU_UNCLAMP_DSIPLL 0x00400800
552
553#define PRCMU_CLK_PLL_DIV_SHIFT 0
554#define PRCMU_CLK_PLL_SW_SHIFT 5
555#define PRCMU_CLK_38 (1 << 9)
556#define PRCMU_CLK_38_SRC (1 << 10)
557#define PRCMU_CLK_38_DIV (1 << 11)
558
559/* PLLDIV=12, PLLSW=4 (PLLDDR) */
560#define PRCMU_DSI_CLOCK_SETTING 0x0000008C
561
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200562/* DPI 50000000 Hz */
563#define PRCMU_DPI_CLOCK_SETTING ((1 << PRCMU_CLK_PLL_SW_SHIFT) | \
564 (16 << PRCMU_CLK_PLL_DIV_SHIFT))
565#define PRCMU_DSI_LP_CLOCK_SETTING 0x00000E00
566
567/* D=101, N=1, R=4, SELDIV2=0 */
568#define PRCMU_PLLDSI_FREQ_SETTING 0x00040165
569
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200570#define PRCMU_ENABLE_PLLDSI 0x00000001
571#define PRCMU_DISABLE_PLLDSI 0x00000000
572#define PRCMU_RELEASE_RESET_DSS 0x0000400C
573#define PRCMU_DSI_PLLOUT_SEL_SETTING 0x00000202
574/* ESC clk, div0=1, div1=1, div2=3 */
575#define PRCMU_ENABLE_ESCAPE_CLOCK_DIV 0x07030101
576#define PRCMU_DISABLE_ESCAPE_CLOCK_DIV 0x00030101
577#define PRCMU_DSI_RESET_SW 0x00000007
578
579#define PRCMU_PLLDSI_LOCKP_LOCKED 0x3
580
Mattias Nilsson73180f82011-08-12 10:28:10 +0200581int db8500_prcmu_enable_dsipll(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200582{
583 int i;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200584
585 /* Clear DSIPLL_RESETN */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200586 writel(PRCMU_RESET_DSIPLL, PRCM_APE_RESETN_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200587 /* Unclamp DSIPLL in/out */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200588 writel(PRCMU_UNCLAMP_DSIPLL, PRCM_MMIP_LS_CLAMP_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200589
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200590 /* Set DSI PLL FREQ */
Daniel Willerudc72fe852012-01-13 16:20:03 +0100591 writel(PRCMU_PLLDSI_FREQ_SETTING, PRCM_PLLDSI_FREQ);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200592 writel(PRCMU_DSI_PLLOUT_SEL_SETTING, PRCM_DSI_PLLOUT_SEL);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200593 /* Enable Escape clocks */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200594 writel(PRCMU_ENABLE_ESCAPE_CLOCK_DIV, PRCM_DSITVCLK_DIV);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200595
596 /* Start DSI PLL */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200597 writel(PRCMU_ENABLE_PLLDSI, PRCM_PLLDSI_ENABLE);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200598 /* Reset DSI PLL */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200599 writel(PRCMU_DSI_RESET_SW, PRCM_DSI_SW_RESET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200600 for (i = 0; i < 10; i++) {
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200601 if ((readl(PRCM_PLLDSI_LOCKP) & PRCMU_PLLDSI_LOCKP_LOCKED)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200602 == PRCMU_PLLDSI_LOCKP_LOCKED)
603 break;
604 udelay(100);
605 }
606 /* Set DSIPLL_RESETN */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200607 writel(PRCMU_RESET_DSIPLL, PRCM_APE_RESETN_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200608 return 0;
609}
610
Mattias Nilsson73180f82011-08-12 10:28:10 +0200611int db8500_prcmu_disable_dsipll(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200612{
613 /* Disable dsi pll */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200614 writel(PRCMU_DISABLE_PLLDSI, PRCM_PLLDSI_ENABLE);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200615 /* Disable escapeclock */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200616 writel(PRCMU_DISABLE_ESCAPE_CLOCK_DIV, PRCM_DSITVCLK_DIV);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200617 return 0;
618}
619
Mattias Nilsson73180f82011-08-12 10:28:10 +0200620int db8500_prcmu_set_display_clocks(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200621{
622 unsigned long flags;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200623
624 spin_lock_irqsave(&clk_mgt_lock, flags);
625
626 /* Grab the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200627 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200628 cpu_relax();
629
Daniel Willerudc72fe852012-01-13 16:20:03 +0100630 writel(PRCMU_DSI_CLOCK_SETTING, PRCM_HDMICLK_MGT);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200631 writel(PRCMU_DSI_LP_CLOCK_SETTING, PRCM_TVCLK_MGT);
632 writel(PRCMU_DPI_CLOCK_SETTING, PRCM_LCDCLK_MGT);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200633
634 /* Release the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200635 writel(0, PRCM_SEM);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200636
637 spin_unlock_irqrestore(&clk_mgt_lock, flags);
638
639 return 0;
640}
641
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100642u32 db8500_prcmu_read(unsigned int reg)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200643{
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100644 return readl(_PRCMU_BASE + reg);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200645}
646
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100647void db8500_prcmu_write(unsigned int reg, u32 value)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200648{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200649 unsigned long flags;
650
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100651 spin_lock_irqsave(&prcmu_lock, flags);
652 writel(value, (_PRCMU_BASE + reg));
653 spin_unlock_irqrestore(&prcmu_lock, flags);
654}
655
656void db8500_prcmu_write_masked(unsigned int reg, u32 mask, u32 value)
657{
658 u32 val;
659 unsigned long flags;
660
661 spin_lock_irqsave(&prcmu_lock, flags);
662 val = readl(_PRCMU_BASE + reg);
663 val = ((val & ~mask) | (value & mask));
664 writel(val, (_PRCMU_BASE + reg));
665 spin_unlock_irqrestore(&prcmu_lock, flags);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200666}
667
Mattias Nilssonb58d12f2012-01-13 16:20:10 +0100668struct prcmu_fw_version *prcmu_get_fw_version(void)
669{
670 return fw_info.valid ? &fw_info.version : NULL;
671}
672
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200673bool prcmu_has_arm_maxopp(void)
674{
675 return (readb(tcdm_base + PRCM_AVS_VARM_MAX_OPP) &
676 PRCM_AVS_ISMODEENABLE_MASK) == PRCM_AVS_ISMODEENABLE_MASK;
677}
678
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200679/**
680 * prcmu_get_boot_status - PRCMU boot status checking
681 * Returns: the current PRCMU boot status
682 */
683int prcmu_get_boot_status(void)
684{
685 return readb(tcdm_base + PRCM_BOOT_STATUS);
686}
687
688/**
689 * prcmu_set_rc_a2p - This function is used to run few power state sequences
690 * @val: Value to be set, i.e. transition requested
691 * Returns: 0 on success, -EINVAL on invalid argument
692 *
693 * This function is used to run the following power state sequences -
694 * any state to ApReset, ApDeepSleep to ApExecute, ApExecute to ApDeepSleep
695 */
696int prcmu_set_rc_a2p(enum romcode_write val)
697{
698 if (val < RDY_2_DS || val > RDY_2_XP70_RST)
699 return -EINVAL;
700 writeb(val, (tcdm_base + PRCM_ROMCODE_A2P));
701 return 0;
702}
703
704/**
705 * prcmu_get_rc_p2a - This function is used to get power state sequences
706 * Returns: the power transition that has last happened
707 *
708 * This function can return the following transitions-
709 * any state to ApReset, ApDeepSleep to ApExecute, ApExecute to ApDeepSleep
710 */
711enum romcode_read prcmu_get_rc_p2a(void)
712{
713 return readb(tcdm_base + PRCM_ROMCODE_P2A);
714}
715
716/**
717 * prcmu_get_current_mode - Return the current XP70 power mode
718 * Returns: Returns the current AP(ARM) power mode: init,
719 * apBoot, apExecute, apDeepSleep, apSleep, apIdle, apReset
720 */
721enum ap_pwrst prcmu_get_xp70_current_state(void)
722{
723 return readb(tcdm_base + PRCM_XP70_CUR_PWR_STATE);
724}
725
726/**
727 * prcmu_config_clkout - Configure one of the programmable clock outputs.
728 * @clkout: The CLKOUT number (0 or 1).
729 * @source: The clock to be used (one of the PRCMU_CLKSRC_*).
730 * @div: The divider to be applied.
731 *
732 * Configures one of the programmable clock outputs (CLKOUTs).
733 * @div should be in the range [1,63] to request a configuration, or 0 to
734 * inform that the configuration is no longer requested.
735 */
736int prcmu_config_clkout(u8 clkout, u8 source, u8 div)
737{
738 static int requests[2];
739 int r = 0;
740 unsigned long flags;
741 u32 val;
742 u32 bits;
743 u32 mask;
744 u32 div_mask;
745
746 BUG_ON(clkout > 1);
747 BUG_ON(div > 63);
748 BUG_ON((clkout == 0) && (source > PRCMU_CLKSRC_CLK009));
749
750 if (!div && !requests[clkout])
751 return -EINVAL;
752
753 switch (clkout) {
754 case 0:
755 div_mask = PRCM_CLKOCR_CLKODIV0_MASK;
756 mask = (PRCM_CLKOCR_CLKODIV0_MASK | PRCM_CLKOCR_CLKOSEL0_MASK);
757 bits = ((source << PRCM_CLKOCR_CLKOSEL0_SHIFT) |
758 (div << PRCM_CLKOCR_CLKODIV0_SHIFT));
759 break;
760 case 1:
761 div_mask = PRCM_CLKOCR_CLKODIV1_MASK;
762 mask = (PRCM_CLKOCR_CLKODIV1_MASK | PRCM_CLKOCR_CLKOSEL1_MASK |
763 PRCM_CLKOCR_CLK1TYPE);
764 bits = ((source << PRCM_CLKOCR_CLKOSEL1_SHIFT) |
765 (div << PRCM_CLKOCR_CLKODIV1_SHIFT));
766 break;
767 }
768 bits &= mask;
769
770 spin_lock_irqsave(&clkout_lock, flags);
771
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200772 val = readl(PRCM_CLKOCR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200773 if (val & div_mask) {
774 if (div) {
775 if ((val & mask) != bits) {
776 r = -EBUSY;
777 goto unlock_and_return;
778 }
779 } else {
780 if ((val & mask & ~div_mask) != bits) {
781 r = -EINVAL;
782 goto unlock_and_return;
783 }
784 }
785 }
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200786 writel((bits | (val & ~mask)), PRCM_CLKOCR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200787 requests[clkout] += (div ? 1 : -1);
788
789unlock_and_return:
790 spin_unlock_irqrestore(&clkout_lock, flags);
791
792 return r;
793}
794
Mattias Nilsson73180f82011-08-12 10:28:10 +0200795int db8500_prcmu_set_power_state(u8 state, bool keep_ulp_clk, bool keep_ap_pll)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200796{
797 unsigned long flags;
798
799 BUG_ON((state < PRCMU_AP_SLEEP) || (PRCMU_AP_DEEP_IDLE < state));
800
801 spin_lock_irqsave(&mb0_transfer.lock, flags);
802
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200803 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200804 cpu_relax();
805
806 writeb(MB0H_POWER_STATE_TRANS, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
807 writeb(state, (tcdm_base + PRCM_REQ_MB0_AP_POWER_STATE));
808 writeb((keep_ap_pll ? 1 : 0), (tcdm_base + PRCM_REQ_MB0_AP_PLL_STATE));
809 writeb((keep_ulp_clk ? 1 : 0),
810 (tcdm_base + PRCM_REQ_MB0_ULP_CLOCK_STATE));
811 writeb(0, (tcdm_base + PRCM_REQ_MB0_DO_NOT_WFI));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200812 writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200813
814 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
815
816 return 0;
817}
818
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100819u8 db8500_prcmu_get_power_state_result(void)
820{
821 return readb(tcdm_base + PRCM_ACK_MB0_AP_PWRSTTR_STATUS);
822}
823
Daniel Lezcano485540d2012-02-20 12:30:26 +0100824/* This function decouple the gic from the prcmu */
825int db8500_prcmu_gic_decouple(void)
826{
Daniel Lezcano801448e2012-02-28 22:46:05 +0100827 u32 val = readl(PRCM_A9_MASK_REQ);
Daniel Lezcano485540d2012-02-20 12:30:26 +0100828
829 /* Set bit 0 register value to 1 */
Daniel Lezcano801448e2012-02-28 22:46:05 +0100830 writel(val | PRCM_A9_MASK_REQ_PRCM_A9_MASK_REQ,
831 PRCM_A9_MASK_REQ);
Daniel Lezcano485540d2012-02-20 12:30:26 +0100832
833 /* Make sure the register is updated */
Daniel Lezcano801448e2012-02-28 22:46:05 +0100834 readl(PRCM_A9_MASK_REQ);
Daniel Lezcano485540d2012-02-20 12:30:26 +0100835
836 /* Wait a few cycles for the gic mask completion */
Daniel Lezcano801448e2012-02-28 22:46:05 +0100837 udelay(1);
Daniel Lezcano485540d2012-02-20 12:30:26 +0100838
839 return 0;
840}
841
842/* This function recouple the gic with the prcmu */
843int db8500_prcmu_gic_recouple(void)
844{
Daniel Lezcano801448e2012-02-28 22:46:05 +0100845 u32 val = readl(PRCM_A9_MASK_REQ);
Daniel Lezcano485540d2012-02-20 12:30:26 +0100846
847 /* Set bit 0 register value to 0 */
Daniel Lezcano801448e2012-02-28 22:46:05 +0100848 writel(val & ~PRCM_A9_MASK_REQ_PRCM_A9_MASK_REQ, PRCM_A9_MASK_REQ);
Daniel Lezcano485540d2012-02-20 12:30:26 +0100849
850 return 0;
851}
852
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200853/* This function should only be called while mb0_transfer.lock is held. */
854static void config_wakeups(void)
855{
856 const u8 header[2] = {
857 MB0H_CONFIG_WAKEUPS_EXE,
858 MB0H_CONFIG_WAKEUPS_SLEEP
859 };
860 static u32 last_dbb_events;
861 static u32 last_abb_events;
862 u32 dbb_events;
863 u32 abb_events;
864 unsigned int i;
865
866 dbb_events = mb0_transfer.req.dbb_irqs | mb0_transfer.req.dbb_wakeups;
867 dbb_events |= (WAKEUP_BIT_AC_WAKE_ACK | WAKEUP_BIT_AC_SLEEP_ACK);
868
869 abb_events = mb0_transfer.req.abb_events;
870
871 if ((dbb_events == last_dbb_events) && (abb_events == last_abb_events))
872 return;
873
874 for (i = 0; i < 2; i++) {
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200875 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200876 cpu_relax();
877 writel(dbb_events, (tcdm_base + PRCM_REQ_MB0_WAKEUP_8500));
878 writel(abb_events, (tcdm_base + PRCM_REQ_MB0_WAKEUP_4500));
879 writeb(header[i], (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200880 writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200881 }
882 last_dbb_events = dbb_events;
883 last_abb_events = abb_events;
884}
885
Mattias Nilsson73180f82011-08-12 10:28:10 +0200886void db8500_prcmu_enable_wakeups(u32 wakeups)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200887{
888 unsigned long flags;
889 u32 bits;
890 int i;
891
892 BUG_ON(wakeups != (wakeups & VALID_WAKEUPS));
893
894 for (i = 0, bits = 0; i < NUM_PRCMU_WAKEUP_INDICES; i++) {
895 if (wakeups & BIT(i))
896 bits |= prcmu_wakeup_bit[i];
897 }
898
899 spin_lock_irqsave(&mb0_transfer.lock, flags);
900
901 mb0_transfer.req.dbb_wakeups = bits;
902 config_wakeups();
903
904 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
905}
906
Mattias Nilsson73180f82011-08-12 10:28:10 +0200907void db8500_prcmu_config_abb_event_readout(u32 abb_events)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200908{
909 unsigned long flags;
910
911 spin_lock_irqsave(&mb0_transfer.lock, flags);
912
913 mb0_transfer.req.abb_events = abb_events;
914 config_wakeups();
915
916 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
917}
918
Mattias Nilsson73180f82011-08-12 10:28:10 +0200919void db8500_prcmu_get_abb_event_buffer(void __iomem **buf)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200920{
921 if (readb(tcdm_base + PRCM_ACK_MB0_READ_POINTER) & 1)
922 *buf = (tcdm_base + PRCM_ACK_MB0_WAKEUP_1_4500);
923 else
924 *buf = (tcdm_base + PRCM_ACK_MB0_WAKEUP_0_4500);
925}
926
927/**
Mattias Nilsson73180f82011-08-12 10:28:10 +0200928 * db8500_prcmu_set_arm_opp - set the appropriate ARM OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200929 * @opp: The new ARM operating point to which transition is to be made
930 * Returns: 0 on success, non-zero on failure
931 *
932 * This function sets the the operating point of the ARM.
933 */
Mattias Nilsson73180f82011-08-12 10:28:10 +0200934int db8500_prcmu_set_arm_opp(u8 opp)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200935{
936 int r;
937
938 if (opp < ARM_NO_CHANGE || opp > ARM_EXTCLK)
939 return -EINVAL;
940
941 r = 0;
942
943 mutex_lock(&mb1_transfer.lock);
944
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200945 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200946 cpu_relax();
947
948 writeb(MB1H_ARM_APE_OPP, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
949 writeb(opp, (tcdm_base + PRCM_REQ_MB1_ARM_OPP));
950 writeb(APE_NO_CHANGE, (tcdm_base + PRCM_REQ_MB1_APE_OPP));
951
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200952 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200953 wait_for_completion(&mb1_transfer.work);
954
955 if ((mb1_transfer.ack.header != MB1H_ARM_APE_OPP) ||
956 (mb1_transfer.ack.arm_opp != opp))
957 r = -EIO;
958
959 mutex_unlock(&mb1_transfer.lock);
960
961 return r;
962}
963
964/**
Mattias Nilsson73180f82011-08-12 10:28:10 +0200965 * db8500_prcmu_get_arm_opp - get the current ARM OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200966 *
967 * Returns: the current ARM OPP
968 */
Mattias Nilsson73180f82011-08-12 10:28:10 +0200969int db8500_prcmu_get_arm_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200970{
971 return readb(tcdm_base + PRCM_ACK_MB1_CURRENT_ARM_OPP);
972}
973
974/**
Mattias Nilsson05089012012-01-13 16:20:20 +0100975 * db8500_prcmu_get_ddr_opp - get the current DDR OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200976 *
977 * Returns: the current DDR OPP
978 */
Mattias Nilsson05089012012-01-13 16:20:20 +0100979int db8500_prcmu_get_ddr_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200980{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200981 return readb(PRCM_DDR_SUBSYS_APE_MINBW);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200982}
983
984/**
Mattias Nilsson05089012012-01-13 16:20:20 +0100985 * db8500_set_ddr_opp - set the appropriate DDR OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200986 * @opp: The new DDR operating point to which transition is to be made
987 * Returns: 0 on success, non-zero on failure
988 *
989 * This function sets the operating point of the DDR.
990 */
Mattias Nilsson05089012012-01-13 16:20:20 +0100991int db8500_prcmu_set_ddr_opp(u8 opp)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200992{
993 if (opp < DDR_100_OPP || opp > DDR_25_OPP)
994 return -EINVAL;
995 /* Changing the DDR OPP can hang the hardware pre-v21 */
996 if (cpu_is_u8500v20_or_later() && !cpu_is_u8500v20())
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200997 writeb(opp, PRCM_DDR_SUBSYS_APE_MINBW);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200998
999 return 0;
1000}
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001001
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001002/* Divide the frequency of certain clocks by 2 for APE_50_PARTLY_25_OPP. */
1003static void request_even_slower_clocks(bool enable)
1004{
1005 void __iomem *clock_reg[] = {
1006 PRCM_ACLK_MGT,
1007 PRCM_DMACLK_MGT
1008 };
1009 unsigned long flags;
1010 unsigned int i;
1011
1012 spin_lock_irqsave(&clk_mgt_lock, flags);
1013
1014 /* Grab the HW semaphore. */
1015 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
1016 cpu_relax();
1017
1018 for (i = 0; i < ARRAY_SIZE(clock_reg); i++) {
1019 u32 val;
1020 u32 div;
1021
1022 val = readl(clock_reg[i]);
1023 div = (val & PRCM_CLK_MGT_CLKPLLDIV_MASK);
1024 if (enable) {
1025 if ((div <= 1) || (div > 15)) {
1026 pr_err("prcmu: Bad clock divider %d in %s\n",
1027 div, __func__);
1028 goto unlock_and_return;
1029 }
1030 div <<= 1;
1031 } else {
1032 if (div <= 2)
1033 goto unlock_and_return;
1034 div >>= 1;
1035 }
1036 val = ((val & ~PRCM_CLK_MGT_CLKPLLDIV_MASK) |
1037 (div & PRCM_CLK_MGT_CLKPLLDIV_MASK));
1038 writel(val, clock_reg[i]);
1039 }
1040
1041unlock_and_return:
1042 /* Release the HW semaphore. */
1043 writel(0, PRCM_SEM);
1044
1045 spin_unlock_irqrestore(&clk_mgt_lock, flags);
1046}
1047
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001048/**
Mattias Nilsson05089012012-01-13 16:20:20 +01001049 * db8500_set_ape_opp - set the appropriate APE OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001050 * @opp: The new APE operating point to which transition is to be made
1051 * Returns: 0 on success, non-zero on failure
1052 *
1053 * This function sets the operating point of the APE.
1054 */
Mattias Nilsson05089012012-01-13 16:20:20 +01001055int db8500_prcmu_set_ape_opp(u8 opp)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001056{
1057 int r = 0;
1058
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001059 if (opp == mb1_transfer.ape_opp)
1060 return 0;
1061
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001062 mutex_lock(&mb1_transfer.lock);
1063
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001064 if (mb1_transfer.ape_opp == APE_50_PARTLY_25_OPP)
1065 request_even_slower_clocks(false);
1066
1067 if ((opp != APE_100_OPP) && (mb1_transfer.ape_opp != APE_100_OPP))
1068 goto skip_message;
1069
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001070 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001071 cpu_relax();
1072
1073 writeb(MB1H_ARM_APE_OPP, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1074 writeb(ARM_NO_CHANGE, (tcdm_base + PRCM_REQ_MB1_ARM_OPP));
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001075 writeb(((opp == APE_50_PARTLY_25_OPP) ? APE_50_OPP : opp),
1076 (tcdm_base + PRCM_REQ_MB1_APE_OPP));
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001077
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001078 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001079 wait_for_completion(&mb1_transfer.work);
1080
1081 if ((mb1_transfer.ack.header != MB1H_ARM_APE_OPP) ||
1082 (mb1_transfer.ack.ape_opp != opp))
1083 r = -EIO;
1084
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001085skip_message:
1086 if ((!r && (opp == APE_50_PARTLY_25_OPP)) ||
1087 (r && (mb1_transfer.ape_opp == APE_50_PARTLY_25_OPP)))
1088 request_even_slower_clocks(true);
1089 if (!r)
1090 mb1_transfer.ape_opp = opp;
1091
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001092 mutex_unlock(&mb1_transfer.lock);
1093
1094 return r;
1095}
1096
1097/**
Mattias Nilsson05089012012-01-13 16:20:20 +01001098 * db8500_prcmu_get_ape_opp - get the current APE OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001099 *
1100 * Returns: the current APE OPP
1101 */
Mattias Nilsson05089012012-01-13 16:20:20 +01001102int db8500_prcmu_get_ape_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001103{
1104 return readb(tcdm_base + PRCM_ACK_MB1_CURRENT_APE_OPP);
1105}
1106
1107/**
1108 * prcmu_request_ape_opp_100_voltage - Request APE OPP 100% voltage
1109 * @enable: true to request the higher voltage, false to drop a request.
1110 *
1111 * Calls to this function to enable and disable requests must be balanced.
1112 */
1113int prcmu_request_ape_opp_100_voltage(bool enable)
1114{
1115 int r = 0;
1116 u8 header;
1117 static unsigned int requests;
1118
1119 mutex_lock(&mb1_transfer.lock);
1120
1121 if (enable) {
1122 if (0 != requests++)
1123 goto unlock_and_return;
1124 header = MB1H_REQUEST_APE_OPP_100_VOLT;
1125 } else {
1126 if (requests == 0) {
1127 r = -EIO;
1128 goto unlock_and_return;
1129 } else if (1 != requests--) {
1130 goto unlock_and_return;
1131 }
1132 header = MB1H_RELEASE_APE_OPP_100_VOLT;
1133 }
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(header, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1139
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001140 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001141 wait_for_completion(&mb1_transfer.work);
1142
1143 if ((mb1_transfer.ack.header != header) ||
1144 ((mb1_transfer.ack.ape_voltage_status & BIT(0)) != 0))
1145 r = -EIO;
1146
1147unlock_and_return:
1148 mutex_unlock(&mb1_transfer.lock);
1149
1150 return r;
1151}
1152
1153/**
1154 * prcmu_release_usb_wakeup_state - release the state required by a USB wakeup
1155 *
1156 * This function releases the power state requirements of a USB wakeup.
1157 */
1158int prcmu_release_usb_wakeup_state(void)
1159{
1160 int r = 0;
1161
1162 mutex_lock(&mb1_transfer.lock);
1163
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001164 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001165 cpu_relax();
1166
1167 writeb(MB1H_RELEASE_USB_WAKEUP,
1168 (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1169
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001170 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001171 wait_for_completion(&mb1_transfer.work);
1172
1173 if ((mb1_transfer.ack.header != MB1H_RELEASE_USB_WAKEUP) ||
1174 ((mb1_transfer.ack.ape_voltage_status & BIT(0)) != 0))
1175 r = -EIO;
1176
1177 mutex_unlock(&mb1_transfer.lock);
1178
1179 return r;
1180}
1181
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001182static int request_pll(u8 clock, bool enable)
1183{
1184 int r = 0;
1185
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001186 if (clock == PRCMU_PLLSOC0)
1187 clock = (enable ? PLL_SOC0_ON : PLL_SOC0_OFF);
1188 else if (clock == PRCMU_PLLSOC1)
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001189 clock = (enable ? PLL_SOC1_ON : PLL_SOC1_OFF);
1190 else
1191 return -EINVAL;
1192
1193 mutex_lock(&mb1_transfer.lock);
1194
1195 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
1196 cpu_relax();
1197
1198 writeb(MB1H_PLL_ON_OFF, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1199 writeb(clock, (tcdm_base + PRCM_REQ_MB1_PLL_ON_OFF));
1200
1201 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
1202 wait_for_completion(&mb1_transfer.work);
1203
1204 if (mb1_transfer.ack.header != MB1H_PLL_ON_OFF)
1205 r = -EIO;
1206
1207 mutex_unlock(&mb1_transfer.lock);
1208
1209 return r;
1210}
1211
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001212/**
Bengt Jonsson0b9199e2011-08-12 10:28:25 +02001213 * prcmu_set_hwacc - set the power state of a h/w accelerator
1214 * @hwacc_dev: The hardware accelerator (enum hw_acc_dev).
1215 * @state: The new power state (enum hw_acc_state).
1216 *
1217 * This function sets the power state of a hardware accelerator.
1218 * This function should not be called from interrupt context.
1219 *
1220 * NOTE! Deprecated, to be removed when all users switched over to use the
1221 * regulator framework API.
1222 */
1223int prcmu_set_hwacc(u16 hwacc_dev, u8 state)
1224{
1225 int r = 0;
1226 bool ram_retention = false;
1227 bool enable, enable_ret;
1228
1229 /* check argument */
1230 BUG_ON(hwacc_dev >= NUM_HW_ACC);
1231
1232 /* get state of switches */
1233 enable = hwacc_enabled[hwacc_dev];
1234 enable_ret = hwacc_ret_enabled[hwacc_dev];
1235
1236 /* set flag if retention is possible */
1237 switch (hwacc_dev) {
1238 case HW_ACC_SVAMMDSP:
1239 case HW_ACC_SIAMMDSP:
1240 case HW_ACC_ESRAM1:
1241 case HW_ACC_ESRAM2:
1242 case HW_ACC_ESRAM3:
1243 case HW_ACC_ESRAM4:
1244 ram_retention = true;
1245 break;
1246 }
1247
1248 /* check argument */
1249 BUG_ON(state > HW_ON);
1250 BUG_ON(state == HW_OFF_RAMRET && !ram_retention);
1251
1252 /* modify enable flags */
1253 switch (state) {
1254 case HW_OFF:
1255 enable_ret = false;
1256 enable = false;
1257 break;
1258 case HW_ON:
1259 enable = true;
1260 break;
1261 case HW_OFF_RAMRET:
1262 enable_ret = true;
1263 enable = false;
1264 break;
1265 }
1266
1267 /* get regulator (lazy) */
1268 if (hwacc_regulator[hwacc_dev] == NULL) {
1269 hwacc_regulator[hwacc_dev] = regulator_get(NULL,
1270 hwacc_regulator_name[hwacc_dev]);
1271 if (IS_ERR(hwacc_regulator[hwacc_dev])) {
1272 pr_err("prcmu: failed to get supply %s\n",
1273 hwacc_regulator_name[hwacc_dev]);
1274 r = PTR_ERR(hwacc_regulator[hwacc_dev]);
1275 goto out;
1276 }
1277 }
1278
1279 if (ram_retention) {
1280 if (hwacc_ret_regulator[hwacc_dev] == NULL) {
1281 hwacc_ret_regulator[hwacc_dev] = regulator_get(NULL,
1282 hwacc_ret_regulator_name[hwacc_dev]);
1283 if (IS_ERR(hwacc_ret_regulator[hwacc_dev])) {
1284 pr_err("prcmu: failed to get supply %s\n",
1285 hwacc_ret_regulator_name[hwacc_dev]);
1286 r = PTR_ERR(hwacc_ret_regulator[hwacc_dev]);
1287 goto out;
1288 }
1289 }
1290 }
1291
1292 /* set regulators */
1293 if (ram_retention) {
1294 if (enable_ret && !hwacc_ret_enabled[hwacc_dev]) {
1295 r = regulator_enable(hwacc_ret_regulator[hwacc_dev]);
1296 if (r < 0) {
1297 pr_err("prcmu_set_hwacc: ret enable failed\n");
1298 goto out;
1299 }
1300 hwacc_ret_enabled[hwacc_dev] = true;
1301 }
1302 }
1303
1304 if (enable && !hwacc_enabled[hwacc_dev]) {
1305 r = regulator_enable(hwacc_regulator[hwacc_dev]);
1306 if (r < 0) {
1307 pr_err("prcmu_set_hwacc: enable failed\n");
1308 goto out;
1309 }
1310 hwacc_enabled[hwacc_dev] = true;
1311 }
1312
1313 if (!enable && hwacc_enabled[hwacc_dev]) {
1314 r = regulator_disable(hwacc_regulator[hwacc_dev]);
1315 if (r < 0) {
1316 pr_err("prcmu_set_hwacc: disable failed\n");
1317 goto out;
1318 }
1319 hwacc_enabled[hwacc_dev] = false;
1320 }
1321
1322 if (ram_retention) {
1323 if (!enable_ret && hwacc_ret_enabled[hwacc_dev]) {
1324 r = regulator_disable(hwacc_ret_regulator[hwacc_dev]);
1325 if (r < 0) {
1326 pr_err("prcmu_set_hwacc: ret disable failed\n");
1327 goto out;
1328 }
1329 hwacc_ret_enabled[hwacc_dev] = false;
1330 }
1331 }
1332
1333out:
1334 return r;
1335}
1336EXPORT_SYMBOL(prcmu_set_hwacc);
1337
1338/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02001339 * db8500_prcmu_set_epod - set the state of a EPOD (power domain)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001340 * @epod_id: The EPOD to set
1341 * @epod_state: The new EPOD state
1342 *
1343 * This function sets the state of a EPOD (power domain). It may not be called
1344 * from interrupt context.
1345 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02001346int db8500_prcmu_set_epod(u16 epod_id, u8 epod_state)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001347{
1348 int r = 0;
1349 bool ram_retention = false;
1350 int i;
1351
1352 /* check argument */
1353 BUG_ON(epod_id >= NUM_EPOD_ID);
1354
1355 /* set flag if retention is possible */
1356 switch (epod_id) {
1357 case EPOD_ID_SVAMMDSP:
1358 case EPOD_ID_SIAMMDSP:
1359 case EPOD_ID_ESRAM12:
1360 case EPOD_ID_ESRAM34:
1361 ram_retention = true;
1362 break;
1363 }
1364
1365 /* check argument */
1366 BUG_ON(epod_state > EPOD_STATE_ON);
1367 BUG_ON(epod_state == EPOD_STATE_RAMRET && !ram_retention);
1368
1369 /* get lock */
1370 mutex_lock(&mb2_transfer.lock);
1371
1372 /* wait for mailbox */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001373 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(2))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001374 cpu_relax();
1375
1376 /* fill in mailbox */
1377 for (i = 0; i < NUM_EPOD_ID; i++)
1378 writeb(EPOD_STATE_NO_CHANGE, (tcdm_base + PRCM_REQ_MB2 + i));
1379 writeb(epod_state, (tcdm_base + PRCM_REQ_MB2 + epod_id));
1380
1381 writeb(MB2H_DPS, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB2));
1382
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001383 writel(MBOX_BIT(2), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001384
1385 /*
1386 * The current firmware version does not handle errors correctly,
1387 * and we cannot recover if there is an error.
1388 * This is expected to change when the firmware is updated.
1389 */
1390 if (!wait_for_completion_timeout(&mb2_transfer.work,
1391 msecs_to_jiffies(20000))) {
1392 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
1393 __func__);
1394 r = -EIO;
1395 goto unlock_and_return;
1396 }
1397
1398 if (mb2_transfer.ack.status != HWACC_PWR_ST_OK)
1399 r = -EIO;
1400
1401unlock_and_return:
1402 mutex_unlock(&mb2_transfer.lock);
1403 return r;
1404}
1405
1406/**
1407 * prcmu_configure_auto_pm - Configure autonomous power management.
1408 * @sleep: Configuration for ApSleep.
1409 * @idle: Configuration for ApIdle.
1410 */
1411void prcmu_configure_auto_pm(struct prcmu_auto_pm_config *sleep,
1412 struct prcmu_auto_pm_config *idle)
1413{
1414 u32 sleep_cfg;
1415 u32 idle_cfg;
1416 unsigned long flags;
1417
1418 BUG_ON((sleep == NULL) || (idle == NULL));
1419
1420 sleep_cfg = (sleep->sva_auto_pm_enable & 0xF);
1421 sleep_cfg = ((sleep_cfg << 4) | (sleep->sia_auto_pm_enable & 0xF));
1422 sleep_cfg = ((sleep_cfg << 8) | (sleep->sva_power_on & 0xFF));
1423 sleep_cfg = ((sleep_cfg << 8) | (sleep->sia_power_on & 0xFF));
1424 sleep_cfg = ((sleep_cfg << 4) | (sleep->sva_policy & 0xF));
1425 sleep_cfg = ((sleep_cfg << 4) | (sleep->sia_policy & 0xF));
1426
1427 idle_cfg = (idle->sva_auto_pm_enable & 0xF);
1428 idle_cfg = ((idle_cfg << 4) | (idle->sia_auto_pm_enable & 0xF));
1429 idle_cfg = ((idle_cfg << 8) | (idle->sva_power_on & 0xFF));
1430 idle_cfg = ((idle_cfg << 8) | (idle->sia_power_on & 0xFF));
1431 idle_cfg = ((idle_cfg << 4) | (idle->sva_policy & 0xF));
1432 idle_cfg = ((idle_cfg << 4) | (idle->sia_policy & 0xF));
1433
1434 spin_lock_irqsave(&mb2_transfer.auto_pm_lock, flags);
1435
1436 /*
1437 * The autonomous power management configuration is done through
1438 * fields in mailbox 2, but these fields are only used as shared
1439 * variables - i.e. there is no need to send a message.
1440 */
1441 writel(sleep_cfg, (tcdm_base + PRCM_REQ_MB2_AUTO_PM_SLEEP));
1442 writel(idle_cfg, (tcdm_base + PRCM_REQ_MB2_AUTO_PM_IDLE));
1443
1444 mb2_transfer.auto_pm_enabled =
1445 ((sleep->sva_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1446 (sleep->sia_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1447 (idle->sva_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1448 (idle->sia_auto_pm_enable == PRCMU_AUTO_PM_ON));
1449
1450 spin_unlock_irqrestore(&mb2_transfer.auto_pm_lock, flags);
1451}
1452EXPORT_SYMBOL(prcmu_configure_auto_pm);
1453
1454bool prcmu_is_auto_pm_enabled(void)
1455{
1456 return mb2_transfer.auto_pm_enabled;
1457}
1458
1459static int request_sysclk(bool enable)
1460{
1461 int r;
1462 unsigned long flags;
1463
1464 r = 0;
1465
1466 mutex_lock(&mb3_transfer.sysclk_lock);
1467
1468 spin_lock_irqsave(&mb3_transfer.lock, flags);
1469
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001470 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(3))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001471 cpu_relax();
1472
1473 writeb((enable ? ON : OFF), (tcdm_base + PRCM_REQ_MB3_SYSCLK_MGT));
1474
1475 writeb(MB3H_SYSCLK, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB3));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001476 writel(MBOX_BIT(3), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001477
1478 spin_unlock_irqrestore(&mb3_transfer.lock, flags);
1479
1480 /*
1481 * The firmware only sends an ACK if we want to enable the
1482 * SysClk, and it succeeds.
1483 */
1484 if (enable && !wait_for_completion_timeout(&mb3_transfer.sysclk_work,
1485 msecs_to_jiffies(20000))) {
1486 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
1487 __func__);
1488 r = -EIO;
1489 }
1490
1491 mutex_unlock(&mb3_transfer.sysclk_lock);
1492
1493 return r;
1494}
1495
1496static int request_timclk(bool enable)
1497{
1498 u32 val = (PRCM_TCR_DOZE_MODE | PRCM_TCR_TENSEL_MASK);
1499
1500 if (!enable)
1501 val |= PRCM_TCR_STOP_TIMERS;
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001502 writel(val, PRCM_TCR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001503
1504 return 0;
1505}
1506
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001507static int request_clock(u8 clock, bool enable)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001508{
1509 u32 val;
1510 unsigned long flags;
1511
1512 spin_lock_irqsave(&clk_mgt_lock, flags);
1513
1514 /* Grab the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001515 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001516 cpu_relax();
1517
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001518 val = readl(clk_mgt[clock].reg);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001519 if (enable) {
1520 val |= (PRCM_CLK_MGT_CLKEN | clk_mgt[clock].pllsw);
1521 } else {
1522 clk_mgt[clock].pllsw = (val & PRCM_CLK_MGT_CLKPLLSW_MASK);
1523 val &= ~(PRCM_CLK_MGT_CLKEN | PRCM_CLK_MGT_CLKPLLSW_MASK);
1524 }
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001525 writel(val, clk_mgt[clock].reg);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001526
1527 /* Release the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001528 writel(0, PRCM_SEM);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001529
1530 spin_unlock_irqrestore(&clk_mgt_lock, flags);
1531
1532 return 0;
1533}
1534
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001535static int request_sga_clock(u8 clock, bool enable)
1536{
1537 u32 val;
1538 int ret;
1539
1540 if (enable) {
1541 val = readl(PRCM_CGATING_BYPASS);
1542 writel(val | PRCM_CGATING_BYPASS_ICN2, PRCM_CGATING_BYPASS);
1543 }
1544
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001545 ret = request_clock(clock, enable);
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001546
1547 if (!ret && !enable) {
1548 val = readl(PRCM_CGATING_BYPASS);
1549 writel(val & ~PRCM_CGATING_BYPASS_ICN2, PRCM_CGATING_BYPASS);
1550 }
1551
1552 return ret;
1553}
1554
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001555static inline bool plldsi_locked(void)
1556{
1557 return (readl(PRCM_PLLDSI_LOCKP) &
1558 (PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP10 |
1559 PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP3)) ==
1560 (PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP10 |
1561 PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP3);
1562}
1563
1564static int request_plldsi(bool enable)
1565{
1566 int r = 0;
1567 u32 val;
1568
1569 writel((PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMP |
1570 PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMPI), (enable ?
1571 PRCM_MMIP_LS_CLAMP_CLR : PRCM_MMIP_LS_CLAMP_SET));
1572
1573 val = readl(PRCM_PLLDSI_ENABLE);
1574 if (enable)
1575 val |= PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1576 else
1577 val &= ~PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1578 writel(val, PRCM_PLLDSI_ENABLE);
1579
1580 if (enable) {
1581 unsigned int i;
1582 bool locked = plldsi_locked();
1583
1584 for (i = 10; !locked && (i > 0); --i) {
1585 udelay(100);
1586 locked = plldsi_locked();
1587 }
1588 if (locked) {
1589 writel(PRCM_APE_RESETN_DSIPLL_RESETN,
1590 PRCM_APE_RESETN_SET);
1591 } else {
1592 writel((PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMP |
1593 PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMPI),
1594 PRCM_MMIP_LS_CLAMP_SET);
1595 val &= ~PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1596 writel(val, PRCM_PLLDSI_ENABLE);
1597 r = -EAGAIN;
1598 }
1599 } else {
1600 writel(PRCM_APE_RESETN_DSIPLL_RESETN, PRCM_APE_RESETN_CLR);
1601 }
1602 return r;
1603}
1604
1605static int request_dsiclk(u8 n, bool enable)
1606{
1607 u32 val;
1608
1609 val = readl(PRCM_DSI_PLLOUT_SEL);
1610 val &= ~dsiclk[n].divsel_mask;
1611 val |= ((enable ? dsiclk[n].divsel : PRCM_DSI_PLLOUT_SEL_OFF) <<
1612 dsiclk[n].divsel_shift);
1613 writel(val, PRCM_DSI_PLLOUT_SEL);
1614 return 0;
1615}
1616
1617static int request_dsiescclk(u8 n, bool enable)
1618{
1619 u32 val;
1620
1621 val = readl(PRCM_DSITVCLK_DIV);
1622 enable ? (val |= dsiescclk[n].en) : (val &= ~dsiescclk[n].en);
1623 writel(val, PRCM_DSITVCLK_DIV);
1624 return 0;
1625}
1626
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001627/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02001628 * db8500_prcmu_request_clock() - Request for a clock to be enabled or disabled.
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001629 * @clock: The clock for which the request is made.
1630 * @enable: Whether the clock should be enabled (true) or disabled (false).
1631 *
1632 * This function should only be used by the clock implementation.
1633 * Do not use it from any other place!
1634 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02001635int db8500_prcmu_request_clock(u8 clock, bool enable)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001636{
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001637 if (clock == PRCMU_SGACLK)
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001638 return request_sga_clock(clock, enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001639 else if (clock < PRCMU_NUM_REG_CLOCKS)
1640 return request_clock(clock, enable);
1641 else if (clock == PRCMU_TIMCLK)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001642 return request_timclk(enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001643 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1644 return request_dsiclk((clock - PRCMU_DSI0CLK), enable);
1645 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1646 return request_dsiescclk((clock - PRCMU_DSI0ESCCLK), enable);
1647 else if (clock == PRCMU_PLLDSI)
1648 return request_plldsi(enable);
1649 else if (clock == PRCMU_SYSCLK)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001650 return request_sysclk(enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001651 else if ((clock == PRCMU_PLLSOC0) || (clock == PRCMU_PLLSOC1))
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001652 return request_pll(clock, enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001653 else
1654 return -EINVAL;
1655}
1656
1657static unsigned long pll_rate(void __iomem *reg, unsigned long src_rate,
1658 int branch)
1659{
1660 u64 rate;
1661 u32 val;
1662 u32 d;
1663 u32 div = 1;
1664
1665 val = readl(reg);
1666
1667 rate = src_rate;
1668 rate *= ((val & PRCM_PLL_FREQ_D_MASK) >> PRCM_PLL_FREQ_D_SHIFT);
1669
1670 d = ((val & PRCM_PLL_FREQ_N_MASK) >> PRCM_PLL_FREQ_N_SHIFT);
1671 if (d > 1)
1672 div *= d;
1673
1674 d = ((val & PRCM_PLL_FREQ_R_MASK) >> PRCM_PLL_FREQ_R_SHIFT);
1675 if (d > 1)
1676 div *= d;
1677
1678 if (val & PRCM_PLL_FREQ_SELDIV2)
1679 div *= 2;
1680
1681 if ((branch == PLL_FIX) || ((branch == PLL_DIV) &&
1682 (val & PRCM_PLL_FREQ_DIV2EN) &&
1683 ((reg == PRCM_PLLSOC0_FREQ) ||
1684 (reg == PRCM_PLLDDR_FREQ))))
1685 div *= 2;
1686
1687 (void)do_div(rate, div);
1688
1689 return (unsigned long)rate;
1690}
1691
1692#define ROOT_CLOCK_RATE 38400000
1693
1694static unsigned long clock_rate(u8 clock)
1695{
1696 u32 val;
1697 u32 pllsw;
1698 unsigned long rate = ROOT_CLOCK_RATE;
1699
1700 val = readl(clk_mgt[clock].reg);
1701
1702 if (val & PRCM_CLK_MGT_CLK38) {
1703 if (clk_mgt[clock].clk38div && (val & PRCM_CLK_MGT_CLK38DIV))
1704 rate /= 2;
1705 return rate;
Linus Walleije62ccf32011-10-10 12:14:14 +02001706 }
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001707
1708 val |= clk_mgt[clock].pllsw;
1709 pllsw = (val & PRCM_CLK_MGT_CLKPLLSW_MASK);
1710
1711 if (pllsw == PRCM_CLK_MGT_CLKPLLSW_SOC0)
1712 rate = pll_rate(PRCM_PLLSOC0_FREQ, rate, clk_mgt[clock].branch);
1713 else if (pllsw == PRCM_CLK_MGT_CLKPLLSW_SOC1)
1714 rate = pll_rate(PRCM_PLLSOC1_FREQ, rate, clk_mgt[clock].branch);
1715 else if (pllsw == PRCM_CLK_MGT_CLKPLLSW_DDR)
1716 rate = pll_rate(PRCM_PLLDDR_FREQ, rate, clk_mgt[clock].branch);
1717 else
1718 return 0;
1719
1720 if ((clock == PRCMU_SGACLK) &&
1721 (val & PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN)) {
1722 u64 r = (rate * 10);
1723
1724 (void)do_div(r, 25);
1725 return (unsigned long)r;
1726 }
1727 val &= PRCM_CLK_MGT_CLKPLLDIV_MASK;
1728 if (val)
1729 return rate / val;
1730 else
1731 return 0;
1732}
1733
1734static unsigned long dsiclk_rate(u8 n)
1735{
1736 u32 divsel;
1737 u32 div = 1;
1738
1739 divsel = readl(PRCM_DSI_PLLOUT_SEL);
1740 divsel = ((divsel & dsiclk[n].divsel_mask) >> dsiclk[n].divsel_shift);
1741
1742 if (divsel == PRCM_DSI_PLLOUT_SEL_OFF)
1743 divsel = dsiclk[n].divsel;
1744
1745 switch (divsel) {
1746 case PRCM_DSI_PLLOUT_SEL_PHI_4:
1747 div *= 2;
1748 case PRCM_DSI_PLLOUT_SEL_PHI_2:
1749 div *= 2;
1750 case PRCM_DSI_PLLOUT_SEL_PHI:
1751 return pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1752 PLL_RAW) / div;
1753 default:
1754 return 0;
1755 }
1756}
1757
1758static unsigned long dsiescclk_rate(u8 n)
1759{
1760 u32 div;
1761
1762 div = readl(PRCM_DSITVCLK_DIV);
1763 div = ((div & dsiescclk[n].div_mask) >> (dsiescclk[n].div_shift));
1764 return clock_rate(PRCMU_TVCLK) / max((u32)1, div);
1765}
1766
1767unsigned long prcmu_clock_rate(u8 clock)
1768{
Linus Walleije62ccf32011-10-10 12:14:14 +02001769 if (clock < PRCMU_NUM_REG_CLOCKS)
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001770 return clock_rate(clock);
1771 else if (clock == PRCMU_TIMCLK)
1772 return ROOT_CLOCK_RATE / 16;
1773 else if (clock == PRCMU_SYSCLK)
1774 return ROOT_CLOCK_RATE;
1775 else if (clock == PRCMU_PLLSOC0)
1776 return pll_rate(PRCM_PLLSOC0_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
1777 else if (clock == PRCMU_PLLSOC1)
1778 return pll_rate(PRCM_PLLSOC1_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
1779 else if (clock == PRCMU_PLLDDR)
1780 return pll_rate(PRCM_PLLDDR_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
1781 else if (clock == PRCMU_PLLDSI)
1782 return pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1783 PLL_RAW);
1784 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1785 return dsiclk_rate(clock - PRCMU_DSI0CLK);
1786 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1787 return dsiescclk_rate(clock - PRCMU_DSI0ESCCLK);
1788 else
1789 return 0;
1790}
1791
1792static unsigned long clock_source_rate(u32 clk_mgt_val, int branch)
1793{
1794 if (clk_mgt_val & PRCM_CLK_MGT_CLK38)
1795 return ROOT_CLOCK_RATE;
1796 clk_mgt_val &= PRCM_CLK_MGT_CLKPLLSW_MASK;
1797 if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_SOC0)
1798 return pll_rate(PRCM_PLLSOC0_FREQ, ROOT_CLOCK_RATE, branch);
1799 else if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_SOC1)
1800 return pll_rate(PRCM_PLLSOC1_FREQ, ROOT_CLOCK_RATE, branch);
1801 else if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_DDR)
1802 return pll_rate(PRCM_PLLDDR_FREQ, ROOT_CLOCK_RATE, branch);
1803 else
1804 return 0;
1805}
1806
1807static u32 clock_divider(unsigned long src_rate, unsigned long rate)
1808{
1809 u32 div;
1810
1811 div = (src_rate / rate);
1812 if (div == 0)
1813 return 1;
1814 if (rate < (src_rate / div))
1815 div++;
1816 return div;
1817}
1818
1819static long round_clock_rate(u8 clock, unsigned long rate)
1820{
1821 u32 val;
1822 u32 div;
1823 unsigned long src_rate;
1824 long rounded_rate;
1825
1826 val = readl(clk_mgt[clock].reg);
1827 src_rate = clock_source_rate((val | clk_mgt[clock].pllsw),
1828 clk_mgt[clock].branch);
1829 div = clock_divider(src_rate, rate);
1830 if (val & PRCM_CLK_MGT_CLK38) {
1831 if (clk_mgt[clock].clk38div) {
1832 if (div > 2)
1833 div = 2;
1834 } else {
1835 div = 1;
1836 }
1837 } else if ((clock == PRCMU_SGACLK) && (div == 3)) {
1838 u64 r = (src_rate * 10);
1839
1840 (void)do_div(r, 25);
1841 if (r <= rate)
1842 return (unsigned long)r;
1843 }
1844 rounded_rate = (src_rate / min(div, (u32)31));
1845
1846 return rounded_rate;
1847}
1848
1849#define MIN_PLL_VCO_RATE 600000000ULL
1850#define MAX_PLL_VCO_RATE 1680640000ULL
1851
1852static long round_plldsi_rate(unsigned long rate)
1853{
1854 long rounded_rate = 0;
1855 unsigned long src_rate;
1856 unsigned long rem;
1857 u32 r;
1858
1859 src_rate = clock_rate(PRCMU_HDMICLK);
1860 rem = rate;
1861
1862 for (r = 7; (rem > 0) && (r > 0); r--) {
1863 u64 d;
1864
1865 d = (r * rate);
1866 (void)do_div(d, src_rate);
1867 if (d < 6)
1868 d = 6;
1869 else if (d > 255)
1870 d = 255;
1871 d *= src_rate;
1872 if (((2 * d) < (r * MIN_PLL_VCO_RATE)) ||
1873 ((r * MAX_PLL_VCO_RATE) < (2 * d)))
1874 continue;
1875 (void)do_div(d, r);
1876 if (rate < d) {
1877 if (rounded_rate == 0)
1878 rounded_rate = (long)d;
1879 break;
1880 }
1881 if ((rate - d) < rem) {
1882 rem = (rate - d);
1883 rounded_rate = (long)d;
1884 }
1885 }
1886 return rounded_rate;
1887}
1888
1889static long round_dsiclk_rate(unsigned long rate)
1890{
1891 u32 div;
1892 unsigned long src_rate;
1893 long rounded_rate;
1894
1895 src_rate = pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1896 PLL_RAW);
1897 div = clock_divider(src_rate, rate);
1898 rounded_rate = (src_rate / ((div > 2) ? 4 : div));
1899
1900 return rounded_rate;
1901}
1902
1903static long round_dsiescclk_rate(unsigned long rate)
1904{
1905 u32 div;
1906 unsigned long src_rate;
1907 long rounded_rate;
1908
1909 src_rate = clock_rate(PRCMU_TVCLK);
1910 div = clock_divider(src_rate, rate);
1911 rounded_rate = (src_rate / min(div, (u32)255));
1912
1913 return rounded_rate;
1914}
1915
1916long prcmu_round_clock_rate(u8 clock, unsigned long rate)
1917{
1918 if (clock < PRCMU_NUM_REG_CLOCKS)
1919 return round_clock_rate(clock, rate);
1920 else if (clock == PRCMU_PLLDSI)
1921 return round_plldsi_rate(rate);
1922 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1923 return round_dsiclk_rate(rate);
1924 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1925 return round_dsiescclk_rate(rate);
1926 else
1927 return (long)prcmu_clock_rate(clock);
1928}
1929
1930static void set_clock_rate(u8 clock, unsigned long rate)
1931{
1932 u32 val;
1933 u32 div;
1934 unsigned long src_rate;
1935 unsigned long flags;
1936
1937 spin_lock_irqsave(&clk_mgt_lock, flags);
1938
1939 /* Grab the HW semaphore. */
1940 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
1941 cpu_relax();
1942
1943 val = readl(clk_mgt[clock].reg);
1944 src_rate = clock_source_rate((val | clk_mgt[clock].pllsw),
1945 clk_mgt[clock].branch);
1946 div = clock_divider(src_rate, rate);
1947 if (val & PRCM_CLK_MGT_CLK38) {
1948 if (clk_mgt[clock].clk38div) {
1949 if (div > 1)
1950 val |= PRCM_CLK_MGT_CLK38DIV;
1951 else
1952 val &= ~PRCM_CLK_MGT_CLK38DIV;
1953 }
1954 } else if (clock == PRCMU_SGACLK) {
1955 val &= ~(PRCM_CLK_MGT_CLKPLLDIV_MASK |
1956 PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN);
1957 if (div == 3) {
1958 u64 r = (src_rate * 10);
1959
1960 (void)do_div(r, 25);
1961 if (r <= rate) {
1962 val |= PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN;
1963 div = 0;
1964 }
1965 }
1966 val |= min(div, (u32)31);
1967 } else {
1968 val &= ~PRCM_CLK_MGT_CLKPLLDIV_MASK;
1969 val |= min(div, (u32)31);
1970 }
1971 writel(val, clk_mgt[clock].reg);
1972
1973 /* Release the HW semaphore. */
1974 writel(0, PRCM_SEM);
1975
1976 spin_unlock_irqrestore(&clk_mgt_lock, flags);
1977}
1978
1979static int set_plldsi_rate(unsigned long rate)
1980{
1981 unsigned long src_rate;
1982 unsigned long rem;
1983 u32 pll_freq = 0;
1984 u32 r;
1985
1986 src_rate = clock_rate(PRCMU_HDMICLK);
1987 rem = rate;
1988
1989 for (r = 7; (rem > 0) && (r > 0); r--) {
1990 u64 d;
1991 u64 hwrate;
1992
1993 d = (r * rate);
1994 (void)do_div(d, src_rate);
1995 if (d < 6)
1996 d = 6;
1997 else if (d > 255)
1998 d = 255;
1999 hwrate = (d * src_rate);
2000 if (((2 * hwrate) < (r * MIN_PLL_VCO_RATE)) ||
2001 ((r * MAX_PLL_VCO_RATE) < (2 * hwrate)))
2002 continue;
2003 (void)do_div(hwrate, r);
2004 if (rate < hwrate) {
2005 if (pll_freq == 0)
2006 pll_freq = (((u32)d << PRCM_PLL_FREQ_D_SHIFT) |
2007 (r << PRCM_PLL_FREQ_R_SHIFT));
2008 break;
2009 }
2010 if ((rate - hwrate) < rem) {
2011 rem = (rate - hwrate);
2012 pll_freq = (((u32)d << PRCM_PLL_FREQ_D_SHIFT) |
2013 (r << PRCM_PLL_FREQ_R_SHIFT));
2014 }
2015 }
2016 if (pll_freq == 0)
2017 return -EINVAL;
2018
2019 pll_freq |= (1 << PRCM_PLL_FREQ_N_SHIFT);
2020 writel(pll_freq, PRCM_PLLDSI_FREQ);
2021
2022 return 0;
2023}
2024
2025static void set_dsiclk_rate(u8 n, unsigned long rate)
2026{
2027 u32 val;
2028 u32 div;
2029
2030 div = clock_divider(pll_rate(PRCM_PLLDSI_FREQ,
2031 clock_rate(PRCMU_HDMICLK), PLL_RAW), rate);
2032
2033 dsiclk[n].divsel = (div == 1) ? PRCM_DSI_PLLOUT_SEL_PHI :
2034 (div == 2) ? PRCM_DSI_PLLOUT_SEL_PHI_2 :
2035 /* else */ PRCM_DSI_PLLOUT_SEL_PHI_4;
2036
2037 val = readl(PRCM_DSI_PLLOUT_SEL);
2038 val &= ~dsiclk[n].divsel_mask;
2039 val |= (dsiclk[n].divsel << dsiclk[n].divsel_shift);
2040 writel(val, PRCM_DSI_PLLOUT_SEL);
2041}
2042
2043static void set_dsiescclk_rate(u8 n, unsigned long rate)
2044{
2045 u32 val;
2046 u32 div;
2047
2048 div = clock_divider(clock_rate(PRCMU_TVCLK), rate);
2049 val = readl(PRCM_DSITVCLK_DIV);
2050 val &= ~dsiescclk[n].div_mask;
2051 val |= (min(div, (u32)255) << dsiescclk[n].div_shift);
2052 writel(val, PRCM_DSITVCLK_DIV);
2053}
2054
2055int prcmu_set_clock_rate(u8 clock, unsigned long rate)
2056{
2057 if (clock < PRCMU_NUM_REG_CLOCKS)
2058 set_clock_rate(clock, rate);
2059 else if (clock == PRCMU_PLLDSI)
2060 return set_plldsi_rate(rate);
2061 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
2062 set_dsiclk_rate((clock - PRCMU_DSI0CLK), rate);
2063 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
2064 set_dsiescclk_rate((clock - PRCMU_DSI0ESCCLK), rate);
2065 return 0;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002066}
2067
Mattias Nilsson73180f82011-08-12 10:28:10 +02002068int db8500_prcmu_config_esram0_deep_sleep(u8 state)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002069{
2070 if ((state > ESRAM0_DEEP_SLEEP_STATE_RET) ||
2071 (state < ESRAM0_DEEP_SLEEP_STATE_OFF))
2072 return -EINVAL;
2073
2074 mutex_lock(&mb4_transfer.lock);
2075
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002076 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002077 cpu_relax();
2078
2079 writeb(MB4H_MEM_ST, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2080 writeb(((DDR_PWR_STATE_OFFHIGHLAT << 4) | DDR_PWR_STATE_ON),
2081 (tcdm_base + PRCM_REQ_MB4_DDR_ST_AP_SLEEP_IDLE));
2082 writeb(DDR_PWR_STATE_ON,
2083 (tcdm_base + PRCM_REQ_MB4_DDR_ST_AP_DEEP_IDLE));
2084 writeb(state, (tcdm_base + PRCM_REQ_MB4_ESRAM0_ST));
2085
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002086 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002087 wait_for_completion(&mb4_transfer.work);
2088
2089 mutex_unlock(&mb4_transfer.lock);
2090
2091 return 0;
2092}
2093
Mattias Nilsson05089012012-01-13 16:20:20 +01002094int db8500_prcmu_config_hotdog(u8 threshold)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002095{
2096 mutex_lock(&mb4_transfer.lock);
2097
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002098 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002099 cpu_relax();
2100
2101 writeb(threshold, (tcdm_base + PRCM_REQ_MB4_HOTDOG_THRESHOLD));
2102 writeb(MB4H_HOTDOG, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2103
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002104 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002105 wait_for_completion(&mb4_transfer.work);
2106
2107 mutex_unlock(&mb4_transfer.lock);
2108
2109 return 0;
2110}
2111
Mattias Nilsson05089012012-01-13 16:20:20 +01002112int db8500_prcmu_config_hotmon(u8 low, u8 high)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002113{
2114 mutex_lock(&mb4_transfer.lock);
2115
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002116 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002117 cpu_relax();
2118
2119 writeb(low, (tcdm_base + PRCM_REQ_MB4_HOTMON_LOW));
2120 writeb(high, (tcdm_base + PRCM_REQ_MB4_HOTMON_HIGH));
2121 writeb((HOTMON_CONFIG_LOW | HOTMON_CONFIG_HIGH),
2122 (tcdm_base + PRCM_REQ_MB4_HOTMON_CONFIG));
2123 writeb(MB4H_HOTMON, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2124
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002125 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002126 wait_for_completion(&mb4_transfer.work);
2127
2128 mutex_unlock(&mb4_transfer.lock);
2129
2130 return 0;
2131}
2132
2133static int config_hot_period(u16 val)
2134{
2135 mutex_lock(&mb4_transfer.lock);
2136
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002137 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002138 cpu_relax();
2139
2140 writew(val, (tcdm_base + PRCM_REQ_MB4_HOT_PERIOD));
2141 writeb(MB4H_HOT_PERIOD, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2142
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002143 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002144 wait_for_completion(&mb4_transfer.work);
2145
2146 mutex_unlock(&mb4_transfer.lock);
2147
2148 return 0;
2149}
2150
Mattias Nilsson05089012012-01-13 16:20:20 +01002151int db8500_prcmu_start_temp_sense(u16 cycles32k)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002152{
2153 if (cycles32k == 0xFFFF)
2154 return -EINVAL;
2155
2156 return config_hot_period(cycles32k);
2157}
2158
Mattias Nilsson05089012012-01-13 16:20:20 +01002159int db8500_prcmu_stop_temp_sense(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002160{
2161 return config_hot_period(0xFFFF);
2162}
2163
Jonas Aberg84165b82011-08-12 10:28:33 +02002164static int prcmu_a9wdog(u8 cmd, u8 d0, u8 d1, u8 d2, u8 d3)
2165{
2166
2167 mutex_lock(&mb4_transfer.lock);
2168
2169 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
2170 cpu_relax();
2171
2172 writeb(d0, (tcdm_base + PRCM_REQ_MB4_A9WDOG_0));
2173 writeb(d1, (tcdm_base + PRCM_REQ_MB4_A9WDOG_1));
2174 writeb(d2, (tcdm_base + PRCM_REQ_MB4_A9WDOG_2));
2175 writeb(d3, (tcdm_base + PRCM_REQ_MB4_A9WDOG_3));
2176
2177 writeb(cmd, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2178
2179 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
2180 wait_for_completion(&mb4_transfer.work);
2181
2182 mutex_unlock(&mb4_transfer.lock);
2183
2184 return 0;
2185
2186}
2187
Mattias Nilsson05089012012-01-13 16:20:20 +01002188int db8500_prcmu_config_a9wdog(u8 num, bool sleep_auto_off)
Jonas Aberg84165b82011-08-12 10:28:33 +02002189{
2190 BUG_ON(num == 0 || num > 0xf);
2191 return prcmu_a9wdog(MB4H_A9WDOG_CONF, num, 0, 0,
2192 sleep_auto_off ? A9WDOG_AUTO_OFF_EN :
2193 A9WDOG_AUTO_OFF_DIS);
2194}
2195
Mattias Nilsson05089012012-01-13 16:20:20 +01002196int db8500_prcmu_enable_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002197{
2198 return prcmu_a9wdog(MB4H_A9WDOG_EN, id, 0, 0, 0);
2199}
2200
Mattias Nilsson05089012012-01-13 16:20:20 +01002201int db8500_prcmu_disable_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002202{
2203 return prcmu_a9wdog(MB4H_A9WDOG_DIS, id, 0, 0, 0);
2204}
2205
Mattias Nilsson05089012012-01-13 16:20:20 +01002206int db8500_prcmu_kick_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002207{
2208 return prcmu_a9wdog(MB4H_A9WDOG_KICK, id, 0, 0, 0);
2209}
2210
2211/*
2212 * timeout is 28 bit, in ms.
2213 */
Mattias Nilsson05089012012-01-13 16:20:20 +01002214int db8500_prcmu_load_a9wdog(u8 id, u32 timeout)
Jonas Aberg84165b82011-08-12 10:28:33 +02002215{
Jonas Aberg84165b82011-08-12 10:28:33 +02002216 return prcmu_a9wdog(MB4H_A9WDOG_LOAD,
2217 (id & A9WDOG_ID_MASK) |
2218 /*
2219 * Put the lowest 28 bits of timeout at
2220 * offset 4. Four first bits are used for id.
2221 */
2222 (u8)((timeout << 4) & 0xf0),
2223 (u8)((timeout >> 4) & 0xff),
2224 (u8)((timeout >> 12) & 0xff),
2225 (u8)((timeout >> 20) & 0xff));
2226}
2227
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002228/**
Linus Walleije3726fc2010-08-19 12:36:01 +01002229 * prcmu_abb_read() - Read register value(s) from the ABB.
2230 * @slave: The I2C slave address.
2231 * @reg: The (start) register address.
2232 * @value: The read out value(s).
2233 * @size: The number of registers to read.
2234 *
2235 * Reads register value(s) from the ABB.
2236 * @size has to be 1 for the current firmware version.
2237 */
2238int prcmu_abb_read(u8 slave, u8 reg, u8 *value, u8 size)
2239{
2240 int r;
2241
2242 if (size != 1)
2243 return -EINVAL;
2244
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002245 mutex_lock(&mb5_transfer.lock);
Linus Walleije3726fc2010-08-19 12:36:01 +01002246
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002247 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(5))
Linus Walleije3726fc2010-08-19 12:36:01 +01002248 cpu_relax();
2249
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002250 writeb(PRCMU_I2C_READ(slave), (tcdm_base + PRCM_REQ_MB5_I2C_SLAVE_OP));
2251 writeb(PRCMU_I2C_STOP_EN, (tcdm_base + PRCM_REQ_MB5_I2C_HW_BITS));
2252 writeb(reg, (tcdm_base + PRCM_REQ_MB5_I2C_REG));
2253 writeb(0, (tcdm_base + PRCM_REQ_MB5_I2C_VAL));
Linus Walleije3726fc2010-08-19 12:36:01 +01002254
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002255 writel(MBOX_BIT(5), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002256
Linus Walleije3726fc2010-08-19 12:36:01 +01002257 if (!wait_for_completion_timeout(&mb5_transfer.work,
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002258 msecs_to_jiffies(20000))) {
2259 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
2260 __func__);
Linus Walleije3726fc2010-08-19 12:36:01 +01002261 r = -EIO;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002262 } else {
2263 r = ((mb5_transfer.ack.status == I2C_RD_OK) ? 0 : -EIO);
Linus Walleije3726fc2010-08-19 12:36:01 +01002264 }
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002265
Linus Walleije3726fc2010-08-19 12:36:01 +01002266 if (!r)
2267 *value = mb5_transfer.ack.value;
2268
Linus Walleije3726fc2010-08-19 12:36:01 +01002269 mutex_unlock(&mb5_transfer.lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002270
Linus Walleije3726fc2010-08-19 12:36:01 +01002271 return r;
2272}
Linus Walleije3726fc2010-08-19 12:36:01 +01002273
2274/**
2275 * prcmu_abb_write() - Write register value(s) to the ABB.
2276 * @slave: The I2C slave address.
2277 * @reg: The (start) register address.
2278 * @value: The value(s) to write.
2279 * @size: The number of registers to write.
2280 *
2281 * Reads register value(s) from the ABB.
2282 * @size has to be 1 for the current firmware version.
2283 */
2284int prcmu_abb_write(u8 slave, u8 reg, u8 *value, u8 size)
2285{
2286 int r;
2287
2288 if (size != 1)
2289 return -EINVAL;
2290
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002291 mutex_lock(&mb5_transfer.lock);
Linus Walleije3726fc2010-08-19 12:36:01 +01002292
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002293 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(5))
Linus Walleije3726fc2010-08-19 12:36:01 +01002294 cpu_relax();
2295
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002296 writeb(PRCMU_I2C_WRITE(slave), (tcdm_base + PRCM_REQ_MB5_I2C_SLAVE_OP));
2297 writeb(PRCMU_I2C_STOP_EN, (tcdm_base + PRCM_REQ_MB5_I2C_HW_BITS));
2298 writeb(reg, (tcdm_base + PRCM_REQ_MB5_I2C_REG));
2299 writeb(*value, (tcdm_base + PRCM_REQ_MB5_I2C_VAL));
Linus Walleije3726fc2010-08-19 12:36:01 +01002300
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002301 writel(MBOX_BIT(5), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002302
Linus Walleije3726fc2010-08-19 12:36:01 +01002303 if (!wait_for_completion_timeout(&mb5_transfer.work,
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002304 msecs_to_jiffies(20000))) {
2305 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
2306 __func__);
Linus Walleije3726fc2010-08-19 12:36:01 +01002307 r = -EIO;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002308 } else {
2309 r = ((mb5_transfer.ack.status == I2C_WR_OK) ? 0 : -EIO);
Linus Walleije3726fc2010-08-19 12:36:01 +01002310 }
Linus Walleije3726fc2010-08-19 12:36:01 +01002311
Linus Walleije3726fc2010-08-19 12:36:01 +01002312 mutex_unlock(&mb5_transfer.lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002313
Linus Walleije3726fc2010-08-19 12:36:01 +01002314 return r;
2315}
Linus Walleije3726fc2010-08-19 12:36:01 +01002316
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002317/**
2318 * prcmu_ac_wake_req - should be called whenever ARM wants to wakeup Modem
2319 */
2320void prcmu_ac_wake_req(void)
Martin Perssone0befb22010-12-08 15:13:28 +01002321{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002322 u32 val;
Mattias Nilssond6e30022011-08-12 10:28:43 +02002323 u32 status;
Martin Perssone0befb22010-12-08 15:13:28 +01002324
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002325 mutex_lock(&mb0_transfer.ac_wake_lock);
Martin Perssone0befb22010-12-08 15:13:28 +01002326
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002327 val = readl(PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002328 if (val & PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ)
2329 goto unlock_and_return;
2330
2331 atomic_set(&ac_wake_req_state, 1);
2332
Mattias Nilssond6e30022011-08-12 10:28:43 +02002333retry:
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002334 writel((val | PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ), PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002335
2336 if (!wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
Mattias Nilssond6e30022011-08-12 10:28:43 +02002337 msecs_to_jiffies(5000))) {
Linus Walleij57265bc2011-10-10 13:04:44 +02002338 pr_crit("prcmu: %s timed out (5 s) waiting for a reply.\n",
Mattias Nilssond6e30022011-08-12 10:28:43 +02002339 __func__);
2340 goto unlock_and_return;
2341 }
2342
2343 /*
2344 * The modem can generate an AC_WAKE_ACK, and then still go to sleep.
2345 * As a workaround, we wait, and then check that the modem is indeed
2346 * awake (in terms of the value of the PRCM_MOD_AWAKE_STATUS
2347 * register, which may not be the whole truth).
2348 */
2349 udelay(400);
2350 status = (readl(PRCM_MOD_AWAKE_STATUS) & BITS(0, 2));
2351 if (status != (PRCM_MOD_AWAKE_STATUS_PRCM_MOD_AAPD_AWAKE |
2352 PRCM_MOD_AWAKE_STATUS_PRCM_MOD_COREPD_AWAKE)) {
2353 pr_err("prcmu: %s received ack, but modem not awake (0x%X).\n",
2354 __func__, status);
2355 udelay(1200);
2356 writel(val, PRCM_HOSTACCESS_REQ);
2357 if (wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
2358 msecs_to_jiffies(5000)))
2359 goto retry;
Linus Walleij57265bc2011-10-10 13:04:44 +02002360 pr_crit("prcmu: %s timed out (5 s) waiting for AC_SLEEP_ACK.\n",
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002361 __func__);
2362 }
2363
2364unlock_and_return:
2365 mutex_unlock(&mb0_transfer.ac_wake_lock);
2366}
2367
2368/**
2369 * prcmu_ac_sleep_req - called when ARM no longer needs to talk to modem
2370 */
2371void prcmu_ac_sleep_req()
2372{
2373 u32 val;
2374
2375 mutex_lock(&mb0_transfer.ac_wake_lock);
2376
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002377 val = readl(PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002378 if (!(val & PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ))
2379 goto unlock_and_return;
2380
2381 writel((val & ~PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ),
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002382 PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002383
2384 if (!wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
Mattias Nilssond6e30022011-08-12 10:28:43 +02002385 msecs_to_jiffies(5000))) {
Linus Walleij57265bc2011-10-10 13:04:44 +02002386 pr_crit("prcmu: %s timed out (5 s) waiting for a reply.\n",
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002387 __func__);
2388 }
2389
2390 atomic_set(&ac_wake_req_state, 0);
2391
2392unlock_and_return:
2393 mutex_unlock(&mb0_transfer.ac_wake_lock);
2394}
2395
Mattias Nilsson73180f82011-08-12 10:28:10 +02002396bool db8500_prcmu_is_ac_wake_requested(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002397{
2398 return (atomic_read(&ac_wake_req_state) != 0);
2399}
2400
2401/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02002402 * db8500_prcmu_system_reset - System reset
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002403 *
Mattias Nilsson73180f82011-08-12 10:28:10 +02002404 * Saves the reset reason code and then sets the APE_SOFTRST register which
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002405 * fires interrupt to fw
2406 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02002407void db8500_prcmu_system_reset(u16 reset_code)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002408{
2409 writew(reset_code, (tcdm_base + PRCM_SW_RST_REASON));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002410 writel(1, PRCM_APE_SOFTRST);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002411}
2412
2413/**
Sebastian Rasmussen597045d2011-08-12 10:28:53 +02002414 * db8500_prcmu_get_reset_code - Retrieve SW reset reason code
2415 *
2416 * Retrieves the reset reason code stored by prcmu_system_reset() before
2417 * last restart.
2418 */
2419u16 db8500_prcmu_get_reset_code(void)
2420{
2421 return readw(tcdm_base + PRCM_SW_RST_REASON);
2422}
2423
2424/**
Mattias Nilsson05089012012-01-13 16:20:20 +01002425 * db8500_prcmu_reset_modem - ask the PRCMU to reset modem
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002426 */
Mattias Nilsson05089012012-01-13 16:20:20 +01002427void db8500_prcmu_modem_reset(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002428{
Martin Perssone0befb22010-12-08 15:13:28 +01002429 mutex_lock(&mb1_transfer.lock);
2430
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002431 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Martin Perssone0befb22010-12-08 15:13:28 +01002432 cpu_relax();
2433
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002434 writeb(MB1H_RESET_MODEM, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002435 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Martin Perssone0befb22010-12-08 15:13:28 +01002436 wait_for_completion(&mb1_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002437
2438 /*
2439 * No need to check return from PRCMU as modem should go in reset state
2440 * This state is already managed by upper layer
2441 */
Martin Perssone0befb22010-12-08 15:13:28 +01002442
2443 mutex_unlock(&mb1_transfer.lock);
Martin Perssone0befb22010-12-08 15:13:28 +01002444}
2445
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002446static void ack_dbb_wakeup(void)
Martin Perssone0befb22010-12-08 15:13:28 +01002447{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002448 unsigned long flags;
Martin Perssone0befb22010-12-08 15:13:28 +01002449
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002450 spin_lock_irqsave(&mb0_transfer.lock, flags);
Martin Perssone0befb22010-12-08 15:13:28 +01002451
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002452 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002453 cpu_relax();
Martin Perssone0befb22010-12-08 15:13:28 +01002454
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002455 writeb(MB0H_READ_WAKEUP_ACK, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002456 writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
Martin Perssone0befb22010-12-08 15:13:28 +01002457
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002458 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
Martin Perssone0befb22010-12-08 15:13:28 +01002459}
2460
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002461static inline void print_unknown_header_warning(u8 n, u8 header)
Linus Walleije3726fc2010-08-19 12:36:01 +01002462{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002463 pr_warning("prcmu: Unknown message header (%d) in mailbox %d.\n",
2464 header, n);
Linus Walleije3726fc2010-08-19 12:36:01 +01002465}
2466
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002467static bool read_mailbox_0(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002468{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002469 bool r;
2470 u32 ev;
2471 unsigned int n;
2472 u8 header;
2473
2474 header = readb(tcdm_base + PRCM_MBOX_HEADER_ACK_MB0);
2475 switch (header) {
2476 case MB0H_WAKEUP_EXE:
2477 case MB0H_WAKEUP_SLEEP:
2478 if (readb(tcdm_base + PRCM_ACK_MB0_READ_POINTER) & 1)
2479 ev = readl(tcdm_base + PRCM_ACK_MB0_WAKEUP_1_8500);
2480 else
2481 ev = readl(tcdm_base + PRCM_ACK_MB0_WAKEUP_0_8500);
2482
2483 if (ev & (WAKEUP_BIT_AC_WAKE_ACK | WAKEUP_BIT_AC_SLEEP_ACK))
2484 complete(&mb0_transfer.ac_wake_work);
2485 if (ev & WAKEUP_BIT_SYSCLK_OK)
2486 complete(&mb3_transfer.sysclk_work);
2487
2488 ev &= mb0_transfer.req.dbb_irqs;
2489
2490 for (n = 0; n < NUM_PRCMU_WAKEUPS; n++) {
2491 if (ev & prcmu_irq_bit[n])
2492 generic_handle_irq(IRQ_PRCMU_BASE + n);
2493 }
2494 r = true;
2495 break;
2496 default:
2497 print_unknown_header_warning(0, header);
2498 r = false;
2499 break;
2500 }
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002501 writel(MBOX_BIT(0), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002502 return r;
2503}
2504
2505static bool read_mailbox_1(void)
2506{
2507 mb1_transfer.ack.header = readb(tcdm_base + PRCM_MBOX_HEADER_REQ_MB1);
2508 mb1_transfer.ack.arm_opp = readb(tcdm_base +
2509 PRCM_ACK_MB1_CURRENT_ARM_OPP);
2510 mb1_transfer.ack.ape_opp = readb(tcdm_base +
2511 PRCM_ACK_MB1_CURRENT_APE_OPP);
2512 mb1_transfer.ack.ape_voltage_status = readb(tcdm_base +
2513 PRCM_ACK_MB1_APE_VOLTAGE_STATUS);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002514 writel(MBOX_BIT(1), PRCM_ARM_IT1_CLR);
Martin Perssone0befb22010-12-08 15:13:28 +01002515 complete(&mb1_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002516 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002517}
2518
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002519static bool read_mailbox_2(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002520{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002521 mb2_transfer.ack.status = readb(tcdm_base + PRCM_ACK_MB2_DPS_STATUS);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002522 writel(MBOX_BIT(2), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002523 complete(&mb2_transfer.work);
2524 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002525}
2526
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002527static bool read_mailbox_3(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002528{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002529 writel(MBOX_BIT(3), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002530 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002531}
2532
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002533static bool read_mailbox_4(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002534{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002535 u8 header;
2536 bool do_complete = true;
2537
2538 header = readb(tcdm_base + PRCM_MBOX_HEADER_REQ_MB4);
2539 switch (header) {
2540 case MB4H_MEM_ST:
2541 case MB4H_HOTDOG:
2542 case MB4H_HOTMON:
2543 case MB4H_HOT_PERIOD:
Mattias Nilssona592c2e2011-08-12 10:27:41 +02002544 case MB4H_A9WDOG_CONF:
2545 case MB4H_A9WDOG_EN:
2546 case MB4H_A9WDOG_DIS:
2547 case MB4H_A9WDOG_LOAD:
2548 case MB4H_A9WDOG_KICK:
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002549 break;
2550 default:
2551 print_unknown_header_warning(4, header);
2552 do_complete = false;
2553 break;
2554 }
2555
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002556 writel(MBOX_BIT(4), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002557
2558 if (do_complete)
2559 complete(&mb4_transfer.work);
2560
2561 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002562}
2563
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002564static bool read_mailbox_5(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002565{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002566 mb5_transfer.ack.status = readb(tcdm_base + PRCM_ACK_MB5_I2C_STATUS);
2567 mb5_transfer.ack.value = readb(tcdm_base + PRCM_ACK_MB5_I2C_VAL);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002568 writel(MBOX_BIT(5), PRCM_ARM_IT1_CLR);
Linus Walleije3726fc2010-08-19 12:36:01 +01002569 complete(&mb5_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002570 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002571}
2572
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002573static bool read_mailbox_6(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002574{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002575 writel(MBOX_BIT(6), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002576 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002577}
2578
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002579static bool read_mailbox_7(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002580{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002581 writel(MBOX_BIT(7), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002582 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002583}
2584
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002585static bool (* const read_mailbox[NUM_MB])(void) = {
Linus Walleije3726fc2010-08-19 12:36:01 +01002586 read_mailbox_0,
2587 read_mailbox_1,
2588 read_mailbox_2,
2589 read_mailbox_3,
2590 read_mailbox_4,
2591 read_mailbox_5,
2592 read_mailbox_6,
2593 read_mailbox_7
2594};
2595
2596static irqreturn_t prcmu_irq_handler(int irq, void *data)
2597{
2598 u32 bits;
2599 u8 n;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002600 irqreturn_t r;
Linus Walleije3726fc2010-08-19 12:36:01 +01002601
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002602 bits = (readl(PRCM_ARM_IT1_VAL) & ALL_MBOX_BITS);
Linus Walleije3726fc2010-08-19 12:36:01 +01002603 if (unlikely(!bits))
2604 return IRQ_NONE;
2605
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002606 r = IRQ_HANDLED;
Linus Walleije3726fc2010-08-19 12:36:01 +01002607 for (n = 0; bits; n++) {
2608 if (bits & MBOX_BIT(n)) {
2609 bits -= MBOX_BIT(n);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002610 if (read_mailbox[n]())
2611 r = IRQ_WAKE_THREAD;
Linus Walleije3726fc2010-08-19 12:36:01 +01002612 }
2613 }
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002614 return r;
2615}
2616
2617static irqreturn_t prcmu_irq_thread_fn(int irq, void *data)
2618{
2619 ack_dbb_wakeup();
Linus Walleije3726fc2010-08-19 12:36:01 +01002620 return IRQ_HANDLED;
2621}
2622
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002623static void prcmu_mask_work(struct work_struct *work)
2624{
2625 unsigned long flags;
2626
2627 spin_lock_irqsave(&mb0_transfer.lock, flags);
2628
2629 config_wakeups();
2630
2631 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
2632}
2633
2634static void prcmu_irq_mask(struct irq_data *d)
2635{
2636 unsigned long flags;
2637
2638 spin_lock_irqsave(&mb0_transfer.dbb_irqs_lock, flags);
2639
2640 mb0_transfer.req.dbb_irqs &= ~prcmu_irq_bit[d->irq - IRQ_PRCMU_BASE];
2641
2642 spin_unlock_irqrestore(&mb0_transfer.dbb_irqs_lock, flags);
2643
2644 if (d->irq != IRQ_PRCMU_CA_SLEEP)
2645 schedule_work(&mb0_transfer.mask_work);
2646}
2647
2648static void prcmu_irq_unmask(struct irq_data *d)
2649{
2650 unsigned long flags;
2651
2652 spin_lock_irqsave(&mb0_transfer.dbb_irqs_lock, flags);
2653
2654 mb0_transfer.req.dbb_irqs |= prcmu_irq_bit[d->irq - IRQ_PRCMU_BASE];
2655
2656 spin_unlock_irqrestore(&mb0_transfer.dbb_irqs_lock, flags);
2657
2658 if (d->irq != IRQ_PRCMU_CA_SLEEP)
2659 schedule_work(&mb0_transfer.mask_work);
2660}
2661
2662static void noop(struct irq_data *d)
2663{
2664}
2665
2666static struct irq_chip prcmu_irq_chip = {
2667 .name = "prcmu",
2668 .irq_disable = prcmu_irq_mask,
2669 .irq_ack = noop,
2670 .irq_mask = prcmu_irq_mask,
2671 .irq_unmask = prcmu_irq_unmask,
2672};
2673
Mattias Nilssonb58d12f2012-01-13 16:20:10 +01002674static char *fw_project_name(u8 project)
2675{
2676 switch (project) {
2677 case PRCMU_FW_PROJECT_U8500:
2678 return "U8500";
2679 case PRCMU_FW_PROJECT_U8500_C2:
2680 return "U8500 C2";
2681 case PRCMU_FW_PROJECT_U9500:
2682 return "U9500";
2683 case PRCMU_FW_PROJECT_U9500_C2:
2684 return "U9500 C2";
2685 default:
2686 return "Unknown";
2687 }
2688}
2689
Mattias Nilsson73180f82011-08-12 10:28:10 +02002690void __init db8500_prcmu_early_init(void)
Mattias Wallinfcbd4582010-12-02 16:20:42 +01002691{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002692 unsigned int i;
Linus Walleij3e2762c2012-01-02 14:17:40 +01002693 if (cpu_is_u8500v2()) {
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002694 void *tcpm_base = ioremap_nocache(U8500_PRCMU_TCPM_BASE, SZ_4K);
2695
2696 if (tcpm_base != NULL) {
Linus Walleij3e2762c2012-01-02 14:17:40 +01002697 u32 version;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002698 version = readl(tcpm_base + PRCMU_FW_VERSION_OFFSET);
Mattias Nilssonb58d12f2012-01-13 16:20:10 +01002699 fw_info.version.project = version & 0xFF;
2700 fw_info.version.api_version = (version >> 8) & 0xFF;
2701 fw_info.version.func_version = (version >> 16) & 0xFF;
2702 fw_info.version.errata = (version >> 24) & 0xFF;
2703 fw_info.valid = true;
2704 pr_info("PRCMU firmware: %s, version %d.%d.%d\n",
2705 fw_project_name(fw_info.version.project),
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002706 (version >> 8) & 0xFF, (version >> 16) & 0xFF,
2707 (version >> 24) & 0xFF);
2708 iounmap(tcpm_base);
2709 }
2710
Mattias Wallinfcbd4582010-12-02 16:20:42 +01002711 tcdm_base = __io_address(U8500_PRCMU_TCDM_BASE);
2712 } else {
2713 pr_err("prcmu: Unsupported chip version\n");
2714 BUG();
2715 }
Mattias Wallinfcbd4582010-12-02 16:20:42 +01002716
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002717 spin_lock_init(&mb0_transfer.lock);
2718 spin_lock_init(&mb0_transfer.dbb_irqs_lock);
2719 mutex_init(&mb0_transfer.ac_wake_lock);
2720 init_completion(&mb0_transfer.ac_wake_work);
Martin Perssone0befb22010-12-08 15:13:28 +01002721 mutex_init(&mb1_transfer.lock);
2722 init_completion(&mb1_transfer.work);
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01002723 mb1_transfer.ape_opp = APE_NO_CHANGE;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002724 mutex_init(&mb2_transfer.lock);
2725 init_completion(&mb2_transfer.work);
2726 spin_lock_init(&mb2_transfer.auto_pm_lock);
2727 spin_lock_init(&mb3_transfer.lock);
2728 mutex_init(&mb3_transfer.sysclk_lock);
2729 init_completion(&mb3_transfer.sysclk_work);
2730 mutex_init(&mb4_transfer.lock);
2731 init_completion(&mb4_transfer.work);
Linus Walleije3726fc2010-08-19 12:36:01 +01002732 mutex_init(&mb5_transfer.lock);
2733 init_completion(&mb5_transfer.work);
2734
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002735 INIT_WORK(&mb0_transfer.mask_work, prcmu_mask_work);
Linus Walleije3726fc2010-08-19 12:36:01 +01002736
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002737 /* Initalize irqs. */
2738 for (i = 0; i < NUM_PRCMU_WAKEUPS; i++) {
2739 unsigned int irq;
2740
2741 irq = IRQ_PRCMU_BASE + i;
2742 irq_set_chip_and_handler(irq, &prcmu_irq_chip,
2743 handle_simple_irq);
2744 set_irq_flags(irq, IRQF_VALID);
2745 }
Linus Walleije3726fc2010-08-19 12:36:01 +01002746}
2747
Mattias Nilsson05089012012-01-13 16:20:20 +01002748static void __init init_prcm_registers(void)
Mattias Nilssond65e12d2011-08-12 10:27:50 +02002749{
2750 u32 val;
2751
2752 val = readl(PRCM_A9PL_FORCE_CLKEN);
2753 val &= ~(PRCM_A9PL_FORCE_CLKEN_PRCM_A9PL_FORCE_CLKEN |
2754 PRCM_A9PL_FORCE_CLKEN_PRCM_A9AXI_FORCE_CLKEN);
2755 writel(val, (PRCM_A9PL_FORCE_CLKEN));
2756}
2757
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002758/*
2759 * Power domain switches (ePODs) modeled as regulators for the DB8500 SoC
2760 */
2761static struct regulator_consumer_supply db8500_vape_consumers[] = {
2762 REGULATOR_SUPPLY("v-ape", NULL),
2763 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.0"),
2764 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.1"),
2765 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.2"),
2766 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.3"),
2767 /* "v-mmc" changed to "vcore" in the mainline kernel */
2768 REGULATOR_SUPPLY("vcore", "sdi0"),
2769 REGULATOR_SUPPLY("vcore", "sdi1"),
2770 REGULATOR_SUPPLY("vcore", "sdi2"),
2771 REGULATOR_SUPPLY("vcore", "sdi3"),
2772 REGULATOR_SUPPLY("vcore", "sdi4"),
2773 REGULATOR_SUPPLY("v-dma", "dma40.0"),
2774 REGULATOR_SUPPLY("v-ape", "ab8500-usb.0"),
2775 /* "v-uart" changed to "vcore" in the mainline kernel */
2776 REGULATOR_SUPPLY("vcore", "uart0"),
2777 REGULATOR_SUPPLY("vcore", "uart1"),
2778 REGULATOR_SUPPLY("vcore", "uart2"),
2779 REGULATOR_SUPPLY("v-ape", "nmk-ske-keypad.0"),
Bengt Jonsson992b1332012-01-13 16:20:36 +01002780 REGULATOR_SUPPLY("v-hsi", "ste_hsi.0"),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002781};
2782
2783static struct regulator_consumer_supply db8500_vsmps2_consumers[] = {
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002784 REGULATOR_SUPPLY("musb_1v8", "ab8500-usb.0"),
2785 /* AV8100 regulator */
2786 REGULATOR_SUPPLY("hdmi_1v8", "0-0070"),
2787};
2788
2789static struct regulator_consumer_supply db8500_b2r2_mcde_consumers[] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002790 REGULATOR_SUPPLY("vsupply", "b2r2_bus"),
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002791 REGULATOR_SUPPLY("vsupply", "mcde"),
2792};
2793
2794/* SVA MMDSP regulator switch */
2795static struct regulator_consumer_supply db8500_svammdsp_consumers[] = {
2796 REGULATOR_SUPPLY("sva-mmdsp", "cm_control"),
2797};
2798
2799/* SVA pipe regulator switch */
2800static struct regulator_consumer_supply db8500_svapipe_consumers[] = {
2801 REGULATOR_SUPPLY("sva-pipe", "cm_control"),
2802};
2803
2804/* SIA MMDSP regulator switch */
2805static struct regulator_consumer_supply db8500_siammdsp_consumers[] = {
2806 REGULATOR_SUPPLY("sia-mmdsp", "cm_control"),
2807};
2808
2809/* SIA pipe regulator switch */
2810static struct regulator_consumer_supply db8500_siapipe_consumers[] = {
2811 REGULATOR_SUPPLY("sia-pipe", "cm_control"),
2812};
2813
2814static struct regulator_consumer_supply db8500_sga_consumers[] = {
2815 REGULATOR_SUPPLY("v-mali", NULL),
2816};
2817
2818/* ESRAM1 and 2 regulator switch */
2819static struct regulator_consumer_supply db8500_esram12_consumers[] = {
2820 REGULATOR_SUPPLY("esram12", "cm_control"),
2821};
2822
2823/* ESRAM3 and 4 regulator switch */
2824static struct regulator_consumer_supply db8500_esram34_consumers[] = {
2825 REGULATOR_SUPPLY("v-esram34", "mcde"),
2826 REGULATOR_SUPPLY("esram34", "cm_control"),
Bengt Jonsson992b1332012-01-13 16:20:36 +01002827 REGULATOR_SUPPLY("lcla_esram", "dma40.0"),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002828};
2829
2830static struct regulator_init_data db8500_regulators[DB8500_NUM_REGULATORS] = {
2831 [DB8500_REGULATOR_VAPE] = {
2832 .constraints = {
2833 .name = "db8500-vape",
2834 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2835 },
2836 .consumer_supplies = db8500_vape_consumers,
2837 .num_consumer_supplies = ARRAY_SIZE(db8500_vape_consumers),
2838 },
2839 [DB8500_REGULATOR_VARM] = {
2840 .constraints = {
2841 .name = "db8500-varm",
2842 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2843 },
2844 },
2845 [DB8500_REGULATOR_VMODEM] = {
2846 .constraints = {
2847 .name = "db8500-vmodem",
2848 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2849 },
2850 },
2851 [DB8500_REGULATOR_VPLL] = {
2852 .constraints = {
2853 .name = "db8500-vpll",
2854 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2855 },
2856 },
2857 [DB8500_REGULATOR_VSMPS1] = {
2858 .constraints = {
2859 .name = "db8500-vsmps1",
2860 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2861 },
2862 },
2863 [DB8500_REGULATOR_VSMPS2] = {
2864 .constraints = {
2865 .name = "db8500-vsmps2",
2866 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2867 },
2868 .consumer_supplies = db8500_vsmps2_consumers,
2869 .num_consumer_supplies = ARRAY_SIZE(db8500_vsmps2_consumers),
2870 },
2871 [DB8500_REGULATOR_VSMPS3] = {
2872 .constraints = {
2873 .name = "db8500-vsmps3",
2874 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2875 },
2876 },
2877 [DB8500_REGULATOR_VRF1] = {
2878 .constraints = {
2879 .name = "db8500-vrf1",
2880 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2881 },
2882 },
2883 [DB8500_REGULATOR_SWITCH_SVAMMDSP] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002884 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002885 .constraints = {
2886 .name = "db8500-sva-mmdsp",
2887 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2888 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002889 .consumer_supplies = db8500_svammdsp_consumers,
2890 .num_consumer_supplies = ARRAY_SIZE(db8500_svammdsp_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002891 },
2892 [DB8500_REGULATOR_SWITCH_SVAMMDSPRET] = {
2893 .constraints = {
2894 /* "ret" means "retention" */
2895 .name = "db8500-sva-mmdsp-ret",
2896 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2897 },
2898 },
2899 [DB8500_REGULATOR_SWITCH_SVAPIPE] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002900 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002901 .constraints = {
2902 .name = "db8500-sva-pipe",
2903 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2904 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002905 .consumer_supplies = db8500_svapipe_consumers,
2906 .num_consumer_supplies = ARRAY_SIZE(db8500_svapipe_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002907 },
2908 [DB8500_REGULATOR_SWITCH_SIAMMDSP] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002909 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002910 .constraints = {
2911 .name = "db8500-sia-mmdsp",
2912 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2913 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002914 .consumer_supplies = db8500_siammdsp_consumers,
2915 .num_consumer_supplies = ARRAY_SIZE(db8500_siammdsp_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002916 },
2917 [DB8500_REGULATOR_SWITCH_SIAMMDSPRET] = {
2918 .constraints = {
2919 .name = "db8500-sia-mmdsp-ret",
2920 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2921 },
2922 },
2923 [DB8500_REGULATOR_SWITCH_SIAPIPE] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002924 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002925 .constraints = {
2926 .name = "db8500-sia-pipe",
2927 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2928 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002929 .consumer_supplies = db8500_siapipe_consumers,
2930 .num_consumer_supplies = ARRAY_SIZE(db8500_siapipe_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002931 },
2932 [DB8500_REGULATOR_SWITCH_SGA] = {
2933 .supply_regulator = "db8500-vape",
2934 .constraints = {
2935 .name = "db8500-sga",
2936 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2937 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002938 .consumer_supplies = db8500_sga_consumers,
2939 .num_consumer_supplies = ARRAY_SIZE(db8500_sga_consumers),
2940
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002941 },
2942 [DB8500_REGULATOR_SWITCH_B2R2_MCDE] = {
2943 .supply_regulator = "db8500-vape",
2944 .constraints = {
2945 .name = "db8500-b2r2-mcde",
2946 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2947 },
2948 .consumer_supplies = db8500_b2r2_mcde_consumers,
2949 .num_consumer_supplies = ARRAY_SIZE(db8500_b2r2_mcde_consumers),
2950 },
2951 [DB8500_REGULATOR_SWITCH_ESRAM12] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002952 /*
2953 * esram12 is set in retention and supplied by Vsafe when Vape is off,
2954 * no need to hold Vape
2955 */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002956 .constraints = {
2957 .name = "db8500-esram12",
2958 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2959 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002960 .consumer_supplies = db8500_esram12_consumers,
2961 .num_consumer_supplies = ARRAY_SIZE(db8500_esram12_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002962 },
2963 [DB8500_REGULATOR_SWITCH_ESRAM12RET] = {
2964 .constraints = {
2965 .name = "db8500-esram12-ret",
2966 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2967 },
2968 },
2969 [DB8500_REGULATOR_SWITCH_ESRAM34] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002970 /*
2971 * esram34 is set in retention and supplied by Vsafe when Vape is off,
2972 * no need to hold Vape
2973 */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002974 .constraints = {
2975 .name = "db8500-esram34",
2976 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2977 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002978 .consumer_supplies = db8500_esram34_consumers,
2979 .num_consumer_supplies = ARRAY_SIZE(db8500_esram34_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002980 },
2981 [DB8500_REGULATOR_SWITCH_ESRAM34RET] = {
2982 .constraints = {
2983 .name = "db8500-esram34-ret",
2984 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2985 },
2986 },
2987};
2988
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002989static struct mfd_cell db8500_prcmu_devs[] = {
2990 {
2991 .name = "db8500-prcmu-regulators",
Mattias Wallin1ed78912011-05-27 11:49:43 +02002992 .platform_data = &db8500_regulators,
2993 .pdata_size = sizeof(db8500_regulators),
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002994 },
2995 {
2996 .name = "cpufreq-u8500",
2997 },
2998};
2999
3000/**
3001 * prcmu_fw_init - arch init call for the Linux PRCMU fw init logic
3002 *
3003 */
3004static int __init db8500_prcmu_probe(struct platform_device *pdev)
3005{
3006 int err = 0;
3007
3008 if (ux500_is_svp())
3009 return -ENODEV;
3010
Mattias Nilsson05089012012-01-13 16:20:20 +01003011 init_prcm_registers();
Mattias Nilssond65e12d2011-08-12 10:27:50 +02003012
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003013 /* Clean up the mailbox interrupts after pre-kernel code. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02003014 writel(ALL_MBOX_BITS, PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003015
3016 err = request_threaded_irq(IRQ_DB8500_PRCMU1, prcmu_irq_handler,
3017 prcmu_irq_thread_fn, IRQF_NO_SUSPEND, "prcmu", NULL);
3018 if (err < 0) {
3019 pr_err("prcmu: Failed to allocate IRQ_DB8500_PRCMU1.\n");
3020 err = -EBUSY;
3021 goto no_irq_return;
3022 }
3023
3024 if (cpu_is_u8500v20_or_later())
3025 prcmu_config_esram0_deep_sleep(ESRAM0_DEEP_SLEEP_STATE_RET);
3026
3027 err = mfd_add_devices(&pdev->dev, 0, db8500_prcmu_devs,
3028 ARRAY_SIZE(db8500_prcmu_devs), NULL,
3029 0);
3030
3031 if (err)
3032 pr_err("prcmu: Failed to add subdevices\n");
3033 else
3034 pr_info("DB8500 PRCMU initialized\n");
3035
3036no_irq_return:
3037 return err;
3038}
3039
3040static struct platform_driver db8500_prcmu_driver = {
3041 .driver = {
3042 .name = "db8500-prcmu",
3043 .owner = THIS_MODULE,
3044 },
3045};
3046
3047static int __init db8500_prcmu_init(void)
3048{
3049 return platform_driver_probe(&db8500_prcmu_driver, db8500_prcmu_probe);
3050}
3051
3052arch_initcall(db8500_prcmu_init);
3053
3054MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com>");
3055MODULE_DESCRIPTION("DB8500 PRCM Unit driver");
3056MODULE_LICENSE("GPL v2");