blob: 5970b8def5487ea3b7f40296d7da61824905df0e [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>
Lee Jonesd98a5382013-04-09 20:52:58 +010027#include <linux/of.h>
Linus Walleijf864c462014-02-04 00:35:56 +010028#include <linux/of_irq.h>
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020029#include <linux/platform_device.h>
30#include <linux/uaccess.h>
31#include <linux/mfd/core.h>
Mattias Nilsson73180f82011-08-12 10:28:10 +020032#include <linux/mfd/dbx500-prcmu.h>
Lee Jones3a8e39c2012-07-06 12:46:23 +020033#include <linux/mfd/abx500/ab8500.h>
Bengt Jonsson1032fbf2011-04-01 14:43:33 +020034#include <linux/regulator/db8500-prcmu.h>
35#include <linux/regulator/machine.h>
Fabio Baltierib3aac622013-01-18 12:40:14 +010036#include <linux/platform_data/ux500_wdt.h>
Arnd Bergmann55b175d2013-03-21 22:51:07 +010037#include <linux/platform_data/db8500_thermal.h>
Mattias Nilsson73180f82011-08-12 10:28:10 +020038#include "dbx500-prcmu-regs.h"
Linus Walleije3726fc2010-08-19 12:36:01 +010039
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020040/* Index of different voltages to be used when accessing AVSData */
41#define PRCM_AVS_BASE 0x2FC
42#define PRCM_AVS_VBB_RET (PRCM_AVS_BASE + 0x0)
43#define PRCM_AVS_VBB_MAX_OPP (PRCM_AVS_BASE + 0x1)
44#define PRCM_AVS_VBB_100_OPP (PRCM_AVS_BASE + 0x2)
45#define PRCM_AVS_VBB_50_OPP (PRCM_AVS_BASE + 0x3)
46#define PRCM_AVS_VARM_MAX_OPP (PRCM_AVS_BASE + 0x4)
47#define PRCM_AVS_VARM_100_OPP (PRCM_AVS_BASE + 0x5)
48#define PRCM_AVS_VARM_50_OPP (PRCM_AVS_BASE + 0x6)
49#define PRCM_AVS_VARM_RET (PRCM_AVS_BASE + 0x7)
50#define PRCM_AVS_VAPE_100_OPP (PRCM_AVS_BASE + 0x8)
51#define PRCM_AVS_VAPE_50_OPP (PRCM_AVS_BASE + 0x9)
52#define PRCM_AVS_VMOD_100_OPP (PRCM_AVS_BASE + 0xA)
53#define PRCM_AVS_VMOD_50_OPP (PRCM_AVS_BASE + 0xB)
54#define PRCM_AVS_VSAFE (PRCM_AVS_BASE + 0xC)
Martin Perssone0befb22010-12-08 15:13:28 +010055
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020056#define PRCM_AVS_VOLTAGE 0
57#define PRCM_AVS_VOLTAGE_MASK 0x3f
58#define PRCM_AVS_ISSLOWSTARTUP 6
59#define PRCM_AVS_ISSLOWSTARTUP_MASK (1 << PRCM_AVS_ISSLOWSTARTUP)
Martin Perssone0befb22010-12-08 15:13:28 +010060#define PRCM_AVS_ISMODEENABLE 7
61#define PRCM_AVS_ISMODEENABLE_MASK (1 << PRCM_AVS_ISMODEENABLE)
62
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020063#define PRCM_BOOT_STATUS 0xFFF
64#define PRCM_ROMCODE_A2P 0xFFE
65#define PRCM_ROMCODE_P2A 0xFFD
66#define PRCM_XP70_CUR_PWR_STATE 0xFFC /* 4 BYTES */
Linus Walleije3726fc2010-08-19 12:36:01 +010067
Mattias Nilsson3df57bc2011-05-16 00:15:05 +020068#define PRCM_SW_RST_REASON 0xFF8 /* 2 bytes */
69
70#define _PRCM_MBOX_HEADER 0xFE8 /* 16 bytes */
71#define PRCM_MBOX_HEADER_REQ_MB0 (_PRCM_MBOX_HEADER + 0x0)
72#define PRCM_MBOX_HEADER_REQ_MB1 (_PRCM_MBOX_HEADER + 0x1)
73#define PRCM_MBOX_HEADER_REQ_MB2 (_PRCM_MBOX_HEADER + 0x2)
74#define PRCM_MBOX_HEADER_REQ_MB3 (_PRCM_MBOX_HEADER + 0x3)
75#define PRCM_MBOX_HEADER_REQ_MB4 (_PRCM_MBOX_HEADER + 0x4)
76#define PRCM_MBOX_HEADER_REQ_MB5 (_PRCM_MBOX_HEADER + 0x5)
77#define PRCM_MBOX_HEADER_ACK_MB0 (_PRCM_MBOX_HEADER + 0x8)
78
79/* Req Mailboxes */
80#define PRCM_REQ_MB0 0xFDC /* 12 bytes */
81#define PRCM_REQ_MB1 0xFD0 /* 12 bytes */
82#define PRCM_REQ_MB2 0xFC0 /* 16 bytes */
83#define PRCM_REQ_MB3 0xE4C /* 372 bytes */
84#define PRCM_REQ_MB4 0xE48 /* 4 bytes */
85#define PRCM_REQ_MB5 0xE44 /* 4 bytes */
86
87/* Ack Mailboxes */
88#define PRCM_ACK_MB0 0xE08 /* 52 bytes */
89#define PRCM_ACK_MB1 0xE04 /* 4 bytes */
90#define PRCM_ACK_MB2 0xE00 /* 4 bytes */
91#define PRCM_ACK_MB3 0xDFC /* 4 bytes */
92#define PRCM_ACK_MB4 0xDF8 /* 4 bytes */
93#define PRCM_ACK_MB5 0xDF4 /* 4 bytes */
94
95/* Mailbox 0 headers */
96#define MB0H_POWER_STATE_TRANS 0
97#define MB0H_CONFIG_WAKEUPS_EXE 1
98#define MB0H_READ_WAKEUP_ACK 3
99#define MB0H_CONFIG_WAKEUPS_SLEEP 4
100
101#define MB0H_WAKEUP_EXE 2
102#define MB0H_WAKEUP_SLEEP 5
103
104/* Mailbox 0 REQs */
105#define PRCM_REQ_MB0_AP_POWER_STATE (PRCM_REQ_MB0 + 0x0)
106#define PRCM_REQ_MB0_AP_PLL_STATE (PRCM_REQ_MB0 + 0x1)
107#define PRCM_REQ_MB0_ULP_CLOCK_STATE (PRCM_REQ_MB0 + 0x2)
108#define PRCM_REQ_MB0_DO_NOT_WFI (PRCM_REQ_MB0 + 0x3)
109#define PRCM_REQ_MB0_WAKEUP_8500 (PRCM_REQ_MB0 + 0x4)
110#define PRCM_REQ_MB0_WAKEUP_4500 (PRCM_REQ_MB0 + 0x8)
111
112/* Mailbox 0 ACKs */
113#define PRCM_ACK_MB0_AP_PWRSTTR_STATUS (PRCM_ACK_MB0 + 0x0)
114#define PRCM_ACK_MB0_READ_POINTER (PRCM_ACK_MB0 + 0x1)
115#define PRCM_ACK_MB0_WAKEUP_0_8500 (PRCM_ACK_MB0 + 0x4)
116#define PRCM_ACK_MB0_WAKEUP_0_4500 (PRCM_ACK_MB0 + 0x8)
117#define PRCM_ACK_MB0_WAKEUP_1_8500 (PRCM_ACK_MB0 + 0x1C)
118#define PRCM_ACK_MB0_WAKEUP_1_4500 (PRCM_ACK_MB0 + 0x20)
119#define PRCM_ACK_MB0_EVENT_4500_NUMBERS 20
120
121/* Mailbox 1 headers */
122#define MB1H_ARM_APE_OPP 0x0
123#define MB1H_RESET_MODEM 0x2
124#define MB1H_REQUEST_APE_OPP_100_VOLT 0x3
125#define MB1H_RELEASE_APE_OPP_100_VOLT 0x4
126#define MB1H_RELEASE_USB_WAKEUP 0x5
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200127#define MB1H_PLL_ON_OFF 0x6
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200128
129/* Mailbox 1 Requests */
130#define PRCM_REQ_MB1_ARM_OPP (PRCM_REQ_MB1 + 0x0)
131#define PRCM_REQ_MB1_APE_OPP (PRCM_REQ_MB1 + 0x1)
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200132#define PRCM_REQ_MB1_PLL_ON_OFF (PRCM_REQ_MB1 + 0x4)
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100133#define PLL_SOC0_OFF 0x1
134#define PLL_SOC0_ON 0x2
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200135#define PLL_SOC1_OFF 0x4
136#define PLL_SOC1_ON 0x8
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200137
138/* Mailbox 1 ACKs */
139#define PRCM_ACK_MB1_CURRENT_ARM_OPP (PRCM_ACK_MB1 + 0x0)
140#define PRCM_ACK_MB1_CURRENT_APE_OPP (PRCM_ACK_MB1 + 0x1)
141#define PRCM_ACK_MB1_APE_VOLTAGE_STATUS (PRCM_ACK_MB1 + 0x2)
142#define PRCM_ACK_MB1_DVFS_STATUS (PRCM_ACK_MB1 + 0x3)
143
144/* Mailbox 2 headers */
145#define MB2H_DPS 0x0
146#define MB2H_AUTO_PWR 0x1
147
148/* Mailbox 2 REQs */
149#define PRCM_REQ_MB2_SVA_MMDSP (PRCM_REQ_MB2 + 0x0)
150#define PRCM_REQ_MB2_SVA_PIPE (PRCM_REQ_MB2 + 0x1)
151#define PRCM_REQ_MB2_SIA_MMDSP (PRCM_REQ_MB2 + 0x2)
152#define PRCM_REQ_MB2_SIA_PIPE (PRCM_REQ_MB2 + 0x3)
153#define PRCM_REQ_MB2_SGA (PRCM_REQ_MB2 + 0x4)
154#define PRCM_REQ_MB2_B2R2_MCDE (PRCM_REQ_MB2 + 0x5)
155#define PRCM_REQ_MB2_ESRAM12 (PRCM_REQ_MB2 + 0x6)
156#define PRCM_REQ_MB2_ESRAM34 (PRCM_REQ_MB2 + 0x7)
157#define PRCM_REQ_MB2_AUTO_PM_SLEEP (PRCM_REQ_MB2 + 0x8)
158#define PRCM_REQ_MB2_AUTO_PM_IDLE (PRCM_REQ_MB2 + 0xC)
159
160/* Mailbox 2 ACKs */
161#define PRCM_ACK_MB2_DPS_STATUS (PRCM_ACK_MB2 + 0x0)
162#define HWACC_PWR_ST_OK 0xFE
163
164/* Mailbox 3 headers */
165#define MB3H_ANC 0x0
166#define MB3H_SIDETONE 0x1
167#define MB3H_SYSCLK 0xE
168
169/* Mailbox 3 Requests */
170#define PRCM_REQ_MB3_ANC_FIR_COEFF (PRCM_REQ_MB3 + 0x0)
171#define PRCM_REQ_MB3_ANC_IIR_COEFF (PRCM_REQ_MB3 + 0x20)
172#define PRCM_REQ_MB3_ANC_SHIFTER (PRCM_REQ_MB3 + 0x60)
173#define PRCM_REQ_MB3_ANC_WARP (PRCM_REQ_MB3 + 0x64)
174#define PRCM_REQ_MB3_SIDETONE_FIR_GAIN (PRCM_REQ_MB3 + 0x68)
175#define PRCM_REQ_MB3_SIDETONE_FIR_COEFF (PRCM_REQ_MB3 + 0x6C)
176#define PRCM_REQ_MB3_SYSCLK_MGT (PRCM_REQ_MB3 + 0x16C)
177
178/* Mailbox 4 headers */
179#define MB4H_DDR_INIT 0x0
180#define MB4H_MEM_ST 0x1
181#define MB4H_HOTDOG 0x12
182#define MB4H_HOTMON 0x13
183#define MB4H_HOT_PERIOD 0x14
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200184#define MB4H_A9WDOG_CONF 0x16
185#define MB4H_A9WDOG_EN 0x17
186#define MB4H_A9WDOG_DIS 0x18
187#define MB4H_A9WDOG_LOAD 0x19
188#define MB4H_A9WDOG_KICK 0x20
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200189
190/* Mailbox 4 Requests */
191#define PRCM_REQ_MB4_DDR_ST_AP_SLEEP_IDLE (PRCM_REQ_MB4 + 0x0)
192#define PRCM_REQ_MB4_DDR_ST_AP_DEEP_IDLE (PRCM_REQ_MB4 + 0x1)
193#define PRCM_REQ_MB4_ESRAM0_ST (PRCM_REQ_MB4 + 0x3)
194#define PRCM_REQ_MB4_HOTDOG_THRESHOLD (PRCM_REQ_MB4 + 0x0)
195#define PRCM_REQ_MB4_HOTMON_LOW (PRCM_REQ_MB4 + 0x0)
196#define PRCM_REQ_MB4_HOTMON_HIGH (PRCM_REQ_MB4 + 0x1)
197#define PRCM_REQ_MB4_HOTMON_CONFIG (PRCM_REQ_MB4 + 0x2)
198#define PRCM_REQ_MB4_HOT_PERIOD (PRCM_REQ_MB4 + 0x0)
199#define HOTMON_CONFIG_LOW BIT(0)
200#define HOTMON_CONFIG_HIGH BIT(1)
Mattias Nilssona592c2e2011-08-12 10:27:41 +0200201#define PRCM_REQ_MB4_A9WDOG_0 (PRCM_REQ_MB4 + 0x0)
202#define PRCM_REQ_MB4_A9WDOG_1 (PRCM_REQ_MB4 + 0x1)
203#define PRCM_REQ_MB4_A9WDOG_2 (PRCM_REQ_MB4 + 0x2)
204#define PRCM_REQ_MB4_A9WDOG_3 (PRCM_REQ_MB4 + 0x3)
205#define A9WDOG_AUTO_OFF_EN BIT(7)
206#define A9WDOG_AUTO_OFF_DIS 0
207#define A9WDOG_ID_MASK 0xf
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200208
209/* Mailbox 5 Requests */
210#define PRCM_REQ_MB5_I2C_SLAVE_OP (PRCM_REQ_MB5 + 0x0)
211#define PRCM_REQ_MB5_I2C_HW_BITS (PRCM_REQ_MB5 + 0x1)
212#define PRCM_REQ_MB5_I2C_REG (PRCM_REQ_MB5 + 0x2)
213#define PRCM_REQ_MB5_I2C_VAL (PRCM_REQ_MB5 + 0x3)
Linus Walleij7a4f2602012-09-19 19:31:19 +0200214#define PRCMU_I2C_WRITE(slave) (((slave) << 1) | BIT(6))
215#define PRCMU_I2C_READ(slave) (((slave) << 1) | BIT(0) | BIT(6))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200216#define PRCMU_I2C_STOP_EN BIT(3)
217
218/* Mailbox 5 ACKs */
219#define PRCM_ACK_MB5_I2C_STATUS (PRCM_ACK_MB5 + 0x1)
220#define PRCM_ACK_MB5_I2C_VAL (PRCM_ACK_MB5 + 0x3)
221#define I2C_WR_OK 0x1
222#define I2C_RD_OK 0x2
223
224#define NUM_MB 8
225#define MBOX_BIT BIT
226#define ALL_MBOX_BITS (MBOX_BIT(NUM_MB) - 1)
227
228/*
229 * Wakeups/IRQs
230 */
231
232#define WAKEUP_BIT_RTC BIT(0)
233#define WAKEUP_BIT_RTT0 BIT(1)
234#define WAKEUP_BIT_RTT1 BIT(2)
235#define WAKEUP_BIT_HSI0 BIT(3)
236#define WAKEUP_BIT_HSI1 BIT(4)
237#define WAKEUP_BIT_CA_WAKE BIT(5)
238#define WAKEUP_BIT_USB BIT(6)
239#define WAKEUP_BIT_ABB BIT(7)
240#define WAKEUP_BIT_ABB_FIFO BIT(8)
241#define WAKEUP_BIT_SYSCLK_OK BIT(9)
242#define WAKEUP_BIT_CA_SLEEP BIT(10)
243#define WAKEUP_BIT_AC_WAKE_ACK BIT(11)
244#define WAKEUP_BIT_SIDE_TONE_OK BIT(12)
245#define WAKEUP_BIT_ANC_OK BIT(13)
246#define WAKEUP_BIT_SW_ERROR BIT(14)
247#define WAKEUP_BIT_AC_SLEEP_ACK BIT(15)
248#define WAKEUP_BIT_ARM BIT(17)
249#define WAKEUP_BIT_HOTMON_LOW BIT(18)
250#define WAKEUP_BIT_HOTMON_HIGH BIT(19)
251#define WAKEUP_BIT_MODEM_SW_RESET_REQ BIT(20)
252#define WAKEUP_BIT_GPIO0 BIT(23)
253#define WAKEUP_BIT_GPIO1 BIT(24)
254#define WAKEUP_BIT_GPIO2 BIT(25)
255#define WAKEUP_BIT_GPIO3 BIT(26)
256#define WAKEUP_BIT_GPIO4 BIT(27)
257#define WAKEUP_BIT_GPIO5 BIT(28)
258#define WAKEUP_BIT_GPIO6 BIT(29)
259#define WAKEUP_BIT_GPIO7 BIT(30)
260#define WAKEUP_BIT_GPIO8 BIT(31)
261
Mattias Nilssonb58d12f2012-01-13 16:20:10 +0100262static struct {
263 bool valid;
264 struct prcmu_fw_version version;
265} fw_info;
266
Lee Jonesf3f1f0a2012-09-24 09:11:46 +0100267static struct irq_domain *db8500_irq_domain;
268
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200269/*
270 * This vector maps irq numbers to the bits in the bit field used in
271 * communication with the PRCMU firmware.
272 *
273 * The reason for having this is to keep the irq numbers contiguous even though
274 * the bits in the bit field are not. (The bits also have a tendency to move
275 * around, to further complicate matters.)
276 */
Arnd Bergmann55b175d2013-03-21 22:51:07 +0100277#define IRQ_INDEX(_name) ((IRQ_PRCMU_##_name))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200278#define IRQ_ENTRY(_name)[IRQ_INDEX(_name)] = (WAKEUP_BIT_##_name)
Arnd Bergmann55b175d2013-03-21 22:51:07 +0100279
280#define IRQ_PRCMU_RTC 0
281#define IRQ_PRCMU_RTT0 1
282#define IRQ_PRCMU_RTT1 2
283#define IRQ_PRCMU_HSI0 3
284#define IRQ_PRCMU_HSI1 4
285#define IRQ_PRCMU_CA_WAKE 5
286#define IRQ_PRCMU_USB 6
287#define IRQ_PRCMU_ABB 7
288#define IRQ_PRCMU_ABB_FIFO 8
289#define IRQ_PRCMU_ARM 9
290#define IRQ_PRCMU_MODEM_SW_RESET_REQ 10
291#define IRQ_PRCMU_GPIO0 11
292#define IRQ_PRCMU_GPIO1 12
293#define IRQ_PRCMU_GPIO2 13
294#define IRQ_PRCMU_GPIO3 14
295#define IRQ_PRCMU_GPIO4 15
296#define IRQ_PRCMU_GPIO5 16
297#define IRQ_PRCMU_GPIO6 17
298#define IRQ_PRCMU_GPIO7 18
299#define IRQ_PRCMU_GPIO8 19
300#define IRQ_PRCMU_CA_SLEEP 20
301#define IRQ_PRCMU_HOTMON_LOW 21
302#define IRQ_PRCMU_HOTMON_HIGH 22
303#define NUM_PRCMU_WAKEUPS 23
304
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200305static u32 prcmu_irq_bit[NUM_PRCMU_WAKEUPS] = {
306 IRQ_ENTRY(RTC),
307 IRQ_ENTRY(RTT0),
308 IRQ_ENTRY(RTT1),
309 IRQ_ENTRY(HSI0),
310 IRQ_ENTRY(HSI1),
311 IRQ_ENTRY(CA_WAKE),
312 IRQ_ENTRY(USB),
313 IRQ_ENTRY(ABB),
314 IRQ_ENTRY(ABB_FIFO),
315 IRQ_ENTRY(CA_SLEEP),
316 IRQ_ENTRY(ARM),
317 IRQ_ENTRY(HOTMON_LOW),
318 IRQ_ENTRY(HOTMON_HIGH),
319 IRQ_ENTRY(MODEM_SW_RESET_REQ),
320 IRQ_ENTRY(GPIO0),
321 IRQ_ENTRY(GPIO1),
322 IRQ_ENTRY(GPIO2),
323 IRQ_ENTRY(GPIO3),
324 IRQ_ENTRY(GPIO4),
325 IRQ_ENTRY(GPIO5),
326 IRQ_ENTRY(GPIO6),
327 IRQ_ENTRY(GPIO7),
328 IRQ_ENTRY(GPIO8)
Martin Perssone0befb22010-12-08 15:13:28 +0100329};
330
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200331#define VALID_WAKEUPS (BIT(NUM_PRCMU_WAKEUP_INDICES) - 1)
332#define WAKEUP_ENTRY(_name)[PRCMU_WAKEUP_INDEX_##_name] = (WAKEUP_BIT_##_name)
333static u32 prcmu_wakeup_bit[NUM_PRCMU_WAKEUP_INDICES] = {
334 WAKEUP_ENTRY(RTC),
335 WAKEUP_ENTRY(RTT0),
336 WAKEUP_ENTRY(RTT1),
337 WAKEUP_ENTRY(HSI0),
338 WAKEUP_ENTRY(HSI1),
339 WAKEUP_ENTRY(USB),
340 WAKEUP_ENTRY(ABB),
341 WAKEUP_ENTRY(ABB_FIFO),
342 WAKEUP_ENTRY(ARM)
343};
344
345/*
346 * mb0_transfer - state needed for mailbox 0 communication.
347 * @lock: The transaction lock.
348 * @dbb_events_lock: A lock used to handle concurrent access to (parts of)
349 * the request data.
350 * @mask_work: Work structure used for (un)masking wakeup interrupts.
351 * @req: Request data that need to persist between requests.
352 */
353static struct {
354 spinlock_t lock;
355 spinlock_t dbb_irqs_lock;
356 struct work_struct mask_work;
357 struct mutex ac_wake_lock;
358 struct completion ac_wake_work;
359 struct {
360 u32 dbb_irqs;
361 u32 dbb_wakeups;
362 u32 abb_events;
363 } req;
364} mb0_transfer;
365
366/*
367 * mb1_transfer - state needed for mailbox 1 communication.
368 * @lock: The transaction lock.
369 * @work: The transaction completion structure.
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100370 * @ape_opp: The current APE OPP.
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200371 * @ack: Reply ("acknowledge") data.
372 */
Martin Perssone0befb22010-12-08 15:13:28 +0100373static struct {
374 struct mutex lock;
375 struct completion work;
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100376 u8 ape_opp;
Martin Perssone0befb22010-12-08 15:13:28 +0100377 struct {
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200378 u8 header;
Martin Perssone0befb22010-12-08 15:13:28 +0100379 u8 arm_opp;
380 u8 ape_opp;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200381 u8 ape_voltage_status;
Martin Perssone0befb22010-12-08 15:13:28 +0100382 } ack;
383} mb1_transfer;
384
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200385/*
386 * mb2_transfer - state needed for mailbox 2 communication.
387 * @lock: The transaction lock.
388 * @work: The transaction completion structure.
389 * @auto_pm_lock: The autonomous power management configuration lock.
390 * @auto_pm_enabled: A flag indicating whether autonomous PM is enabled.
391 * @req: Request data that need to persist between requests.
392 * @ack: Reply ("acknowledge") data.
393 */
Linus Walleije3726fc2010-08-19 12:36:01 +0100394static struct {
395 struct mutex lock;
396 struct completion work;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200397 spinlock_t auto_pm_lock;
398 bool auto_pm_enabled;
399 struct {
400 u8 status;
401 } ack;
402} mb2_transfer;
403
404/*
405 * mb3_transfer - state needed for mailbox 3 communication.
406 * @lock: The request lock.
407 * @sysclk_lock: A lock used to handle concurrent sysclk requests.
408 * @sysclk_work: Work structure used for sysclk requests.
409 */
410static struct {
411 spinlock_t lock;
412 struct mutex sysclk_lock;
413 struct completion sysclk_work;
414} mb3_transfer;
415
416/*
417 * mb4_transfer - state needed for mailbox 4 communication.
418 * @lock: The transaction lock.
419 * @work: The transaction completion structure.
420 */
421static struct {
422 struct mutex lock;
423 struct completion work;
424} mb4_transfer;
425
426/*
427 * mb5_transfer - state needed for mailbox 5 communication.
428 * @lock: The transaction lock.
429 * @work: The transaction completion structure.
430 * @ack: Reply ("acknowledge") data.
431 */
432static struct {
433 struct mutex lock;
434 struct completion work;
Linus Walleije3726fc2010-08-19 12:36:01 +0100435 struct {
436 u8 status;
437 u8 value;
438 } ack;
439} mb5_transfer;
440
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200441static atomic_t ac_wake_req_state = ATOMIC_INIT(0);
442
443/* Spinlocks */
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100444static DEFINE_SPINLOCK(prcmu_lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200445static DEFINE_SPINLOCK(clkout_lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200446
447/* Global var to runtime determine TCDM base for v2 or v1 */
448static __iomem void *tcdm_base;
Linus Walleijb047d982013-03-19 14:21:47 +0100449static __iomem void *prcmu_base;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200450
451struct clk_mgt {
Linus Walleijb047d982013-03-19 14:21:47 +0100452 u32 offset;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200453 u32 pllsw;
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100454 int branch;
455 bool clk38div;
456};
457
458enum {
459 PLL_RAW,
460 PLL_FIX,
461 PLL_DIV
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200462};
463
464static DEFINE_SPINLOCK(clk_mgt_lock);
465
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100466#define CLK_MGT_ENTRY(_name, _branch, _clk38div)[PRCMU_##_name] = \
467 { (PRCM_##_name##_MGT), 0 , _branch, _clk38div}
Sachin Kamat6746f232013-08-23 17:05:20 +0530468static struct clk_mgt clk_mgt[PRCMU_NUM_REG_CLOCKS] = {
Mattias Nilsson6b6fae22012-01-13 16:20:28 +0100469 CLK_MGT_ENTRY(SGACLK, PLL_DIV, false),
470 CLK_MGT_ENTRY(UARTCLK, PLL_FIX, true),
471 CLK_MGT_ENTRY(MSP02CLK, PLL_FIX, true),
472 CLK_MGT_ENTRY(MSP1CLK, PLL_FIX, true),
473 CLK_MGT_ENTRY(I2CCLK, PLL_FIX, true),
474 CLK_MGT_ENTRY(SDMMCCLK, PLL_DIV, true),
475 CLK_MGT_ENTRY(SLIMCLK, PLL_FIX, true),
476 CLK_MGT_ENTRY(PER1CLK, PLL_DIV, true),
477 CLK_MGT_ENTRY(PER2CLK, PLL_DIV, true),
478 CLK_MGT_ENTRY(PER3CLK, PLL_DIV, true),
479 CLK_MGT_ENTRY(PER5CLK, PLL_DIV, true),
480 CLK_MGT_ENTRY(PER6CLK, PLL_DIV, true),
481 CLK_MGT_ENTRY(PER7CLK, PLL_DIV, true),
482 CLK_MGT_ENTRY(LCDCLK, PLL_FIX, true),
483 CLK_MGT_ENTRY(BMLCLK, PLL_DIV, true),
484 CLK_MGT_ENTRY(HSITXCLK, PLL_DIV, true),
485 CLK_MGT_ENTRY(HSIRXCLK, PLL_DIV, true),
486 CLK_MGT_ENTRY(HDMICLK, PLL_FIX, false),
487 CLK_MGT_ENTRY(APEATCLK, PLL_DIV, true),
488 CLK_MGT_ENTRY(APETRACECLK, PLL_DIV, true),
489 CLK_MGT_ENTRY(MCDECLK, PLL_DIV, true),
490 CLK_MGT_ENTRY(IPI2CCLK, PLL_FIX, true),
491 CLK_MGT_ENTRY(DSIALTCLK, PLL_FIX, false),
492 CLK_MGT_ENTRY(DMACLK, PLL_DIV, true),
493 CLK_MGT_ENTRY(B2R2CLK, PLL_DIV, true),
494 CLK_MGT_ENTRY(TVCLK, PLL_FIX, true),
495 CLK_MGT_ENTRY(SSPCLK, PLL_FIX, true),
496 CLK_MGT_ENTRY(RNGCLK, PLL_FIX, true),
497 CLK_MGT_ENTRY(UICCCLK, PLL_FIX, false),
498};
499
500struct dsiclk {
501 u32 divsel_mask;
502 u32 divsel_shift;
503 u32 divsel;
504};
505
506static struct dsiclk dsiclk[2] = {
507 {
508 .divsel_mask = PRCM_DSI_PLLOUT_SEL_DSI0_PLLOUT_DIVSEL_MASK,
509 .divsel_shift = PRCM_DSI_PLLOUT_SEL_DSI0_PLLOUT_DIVSEL_SHIFT,
510 .divsel = PRCM_DSI_PLLOUT_SEL_PHI,
511 },
512 {
513 .divsel_mask = PRCM_DSI_PLLOUT_SEL_DSI1_PLLOUT_DIVSEL_MASK,
514 .divsel_shift = PRCM_DSI_PLLOUT_SEL_DSI1_PLLOUT_DIVSEL_SHIFT,
515 .divsel = PRCM_DSI_PLLOUT_SEL_PHI,
516 }
517};
518
519struct dsiescclk {
520 u32 en;
521 u32 div_mask;
522 u32 div_shift;
523};
524
525static struct dsiescclk dsiescclk[3] = {
526 {
527 .en = PRCM_DSITVCLK_DIV_DSI0_ESC_CLK_EN,
528 .div_mask = PRCM_DSITVCLK_DIV_DSI0_ESC_CLK_DIV_MASK,
529 .div_shift = PRCM_DSITVCLK_DIV_DSI0_ESC_CLK_DIV_SHIFT,
530 },
531 {
532 .en = PRCM_DSITVCLK_DIV_DSI1_ESC_CLK_EN,
533 .div_mask = PRCM_DSITVCLK_DIV_DSI1_ESC_CLK_DIV_MASK,
534 .div_shift = PRCM_DSITVCLK_DIV_DSI1_ESC_CLK_DIV_SHIFT,
535 },
536 {
537 .en = PRCM_DSITVCLK_DIV_DSI2_ESC_CLK_EN,
538 .div_mask = PRCM_DSITVCLK_DIV_DSI2_ESC_CLK_DIV_MASK,
539 .div_shift = PRCM_DSITVCLK_DIV_DSI2_ESC_CLK_DIV_SHIFT,
540 }
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200541};
542
Michel Jaouen20aee5b2012-08-31 14:21:30 +0200543
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200544/*
545* Used by MCDE to setup all necessary PRCMU registers
546*/
547#define PRCMU_RESET_DSIPLL 0x00004000
548#define PRCMU_UNCLAMP_DSIPLL 0x00400800
549
550#define PRCMU_CLK_PLL_DIV_SHIFT 0
551#define PRCMU_CLK_PLL_SW_SHIFT 5
552#define PRCMU_CLK_38 (1 << 9)
553#define PRCMU_CLK_38_SRC (1 << 10)
554#define PRCMU_CLK_38_DIV (1 << 11)
555
556/* PLLDIV=12, PLLSW=4 (PLLDDR) */
557#define PRCMU_DSI_CLOCK_SETTING 0x0000008C
558
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200559/* DPI 50000000 Hz */
560#define PRCMU_DPI_CLOCK_SETTING ((1 << PRCMU_CLK_PLL_SW_SHIFT) | \
561 (16 << PRCMU_CLK_PLL_DIV_SHIFT))
562#define PRCMU_DSI_LP_CLOCK_SETTING 0x00000E00
563
564/* D=101, N=1, R=4, SELDIV2=0 */
565#define PRCMU_PLLDSI_FREQ_SETTING 0x00040165
566
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200567#define PRCMU_ENABLE_PLLDSI 0x00000001
568#define PRCMU_DISABLE_PLLDSI 0x00000000
569#define PRCMU_RELEASE_RESET_DSS 0x0000400C
570#define PRCMU_DSI_PLLOUT_SEL_SETTING 0x00000202
571/* ESC clk, div0=1, div1=1, div2=3 */
572#define PRCMU_ENABLE_ESCAPE_CLOCK_DIV 0x07030101
573#define PRCMU_DISABLE_ESCAPE_CLOCK_DIV 0x00030101
574#define PRCMU_DSI_RESET_SW 0x00000007
575
576#define PRCMU_PLLDSI_LOCKP_LOCKED 0x3
577
Mattias Nilsson73180f82011-08-12 10:28:10 +0200578int db8500_prcmu_enable_dsipll(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200579{
580 int i;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200581
582 /* Clear DSIPLL_RESETN */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200583 writel(PRCMU_RESET_DSIPLL, PRCM_APE_RESETN_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200584 /* Unclamp DSIPLL in/out */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200585 writel(PRCMU_UNCLAMP_DSIPLL, PRCM_MMIP_LS_CLAMP_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200586
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200587 /* Set DSI PLL FREQ */
Daniel Willerudc72fe852012-01-13 16:20:03 +0100588 writel(PRCMU_PLLDSI_FREQ_SETTING, PRCM_PLLDSI_FREQ);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200589 writel(PRCMU_DSI_PLLOUT_SEL_SETTING, PRCM_DSI_PLLOUT_SEL);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200590 /* Enable Escape clocks */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200591 writel(PRCMU_ENABLE_ESCAPE_CLOCK_DIV, PRCM_DSITVCLK_DIV);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200592
593 /* Start DSI PLL */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200594 writel(PRCMU_ENABLE_PLLDSI, PRCM_PLLDSI_ENABLE);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200595 /* Reset DSI PLL */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200596 writel(PRCMU_DSI_RESET_SW, PRCM_DSI_SW_RESET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200597 for (i = 0; i < 10; i++) {
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200598 if ((readl(PRCM_PLLDSI_LOCKP) & PRCMU_PLLDSI_LOCKP_LOCKED)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200599 == PRCMU_PLLDSI_LOCKP_LOCKED)
600 break;
601 udelay(100);
602 }
603 /* Set DSIPLL_RESETN */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200604 writel(PRCMU_RESET_DSIPLL, PRCM_APE_RESETN_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200605 return 0;
606}
607
Mattias Nilsson73180f82011-08-12 10:28:10 +0200608int db8500_prcmu_disable_dsipll(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200609{
610 /* Disable dsi pll */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200611 writel(PRCMU_DISABLE_PLLDSI, PRCM_PLLDSI_ENABLE);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200612 /* Disable escapeclock */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200613 writel(PRCMU_DISABLE_ESCAPE_CLOCK_DIV, PRCM_DSITVCLK_DIV);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200614 return 0;
615}
616
Mattias Nilsson73180f82011-08-12 10:28:10 +0200617int db8500_prcmu_set_display_clocks(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200618{
619 unsigned long flags;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200620
621 spin_lock_irqsave(&clk_mgt_lock, flags);
622
623 /* Grab the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200624 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200625 cpu_relax();
626
Linus Walleijb047d982013-03-19 14:21:47 +0100627 writel(PRCMU_DSI_CLOCK_SETTING, prcmu_base + PRCM_HDMICLK_MGT);
628 writel(PRCMU_DSI_LP_CLOCK_SETTING, prcmu_base + PRCM_TVCLK_MGT);
629 writel(PRCMU_DPI_CLOCK_SETTING, prcmu_base + PRCM_LCDCLK_MGT);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200630
631 /* Release the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200632 writel(0, PRCM_SEM);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200633
634 spin_unlock_irqrestore(&clk_mgt_lock, flags);
635
636 return 0;
637}
638
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100639u32 db8500_prcmu_read(unsigned int reg)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200640{
Linus Walleijb047d982013-03-19 14:21:47 +0100641 return readl(prcmu_base + reg);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200642}
643
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100644void db8500_prcmu_write(unsigned int reg, u32 value)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200645{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200646 unsigned long flags;
647
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100648 spin_lock_irqsave(&prcmu_lock, flags);
Linus Walleijb047d982013-03-19 14:21:47 +0100649 writel(value, (prcmu_base + reg));
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100650 spin_unlock_irqrestore(&prcmu_lock, flags);
651}
652
653void db8500_prcmu_write_masked(unsigned int reg, u32 mask, u32 value)
654{
655 u32 val;
656 unsigned long flags;
657
658 spin_lock_irqsave(&prcmu_lock, flags);
Linus Walleijb047d982013-03-19 14:21:47 +0100659 val = readl(prcmu_base + reg);
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100660 val = ((val & ~mask) | (value & mask));
Linus Walleijb047d982013-03-19 14:21:47 +0100661 writel(val, (prcmu_base + reg));
Mattias Nilssonb4a6dbd2012-01-13 16:21:00 +0100662 spin_unlock_irqrestore(&prcmu_lock, flags);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200663}
664
Mattias Nilssonb58d12f2012-01-13 16:20:10 +0100665struct prcmu_fw_version *prcmu_get_fw_version(void)
666{
667 return fw_info.valid ? &fw_info.version : NULL;
668}
669
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200670bool prcmu_has_arm_maxopp(void)
671{
672 return (readb(tcdm_base + PRCM_AVS_VARM_MAX_OPP) &
673 PRCM_AVS_ISMODEENABLE_MASK) == PRCM_AVS_ISMODEENABLE_MASK;
674}
675
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200676/**
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200677 * prcmu_set_rc_a2p - This function is used to run few power state sequences
678 * @val: Value to be set, i.e. transition requested
679 * Returns: 0 on success, -EINVAL on invalid argument
680 *
681 * This function is used to run the following power state sequences -
682 * any state to ApReset, ApDeepSleep to ApExecute, ApExecute to ApDeepSleep
683 */
684int prcmu_set_rc_a2p(enum romcode_write val)
685{
686 if (val < RDY_2_DS || val > RDY_2_XP70_RST)
687 return -EINVAL;
688 writeb(val, (tcdm_base + PRCM_ROMCODE_A2P));
689 return 0;
690}
691
692/**
693 * prcmu_get_rc_p2a - This function is used to get power state sequences
694 * Returns: the power transition that has last happened
695 *
696 * This function can return the following transitions-
697 * any state to ApReset, ApDeepSleep to ApExecute, ApExecute to ApDeepSleep
698 */
699enum romcode_read prcmu_get_rc_p2a(void)
700{
701 return readb(tcdm_base + PRCM_ROMCODE_P2A);
702}
703
704/**
705 * prcmu_get_current_mode - Return the current XP70 power mode
706 * Returns: Returns the current AP(ARM) power mode: init,
707 * apBoot, apExecute, apDeepSleep, apSleep, apIdle, apReset
708 */
709enum ap_pwrst prcmu_get_xp70_current_state(void)
710{
711 return readb(tcdm_base + PRCM_XP70_CUR_PWR_STATE);
712}
713
714/**
715 * prcmu_config_clkout - Configure one of the programmable clock outputs.
716 * @clkout: The CLKOUT number (0 or 1).
717 * @source: The clock to be used (one of the PRCMU_CLKSRC_*).
718 * @div: The divider to be applied.
719 *
720 * Configures one of the programmable clock outputs (CLKOUTs).
721 * @div should be in the range [1,63] to request a configuration, or 0 to
722 * inform that the configuration is no longer requested.
723 */
724int prcmu_config_clkout(u8 clkout, u8 source, u8 div)
725{
726 static int requests[2];
727 int r = 0;
728 unsigned long flags;
729 u32 val;
730 u32 bits;
731 u32 mask;
732 u32 div_mask;
733
734 BUG_ON(clkout > 1);
735 BUG_ON(div > 63);
736 BUG_ON((clkout == 0) && (source > PRCMU_CLKSRC_CLK009));
737
738 if (!div && !requests[clkout])
739 return -EINVAL;
740
Arnd Bergmanna7e46312016-01-25 17:02:24 +0100741 if (clkout == 0) {
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200742 div_mask = PRCM_CLKOCR_CLKODIV0_MASK;
743 mask = (PRCM_CLKOCR_CLKODIV0_MASK | PRCM_CLKOCR_CLKOSEL0_MASK);
744 bits = ((source << PRCM_CLKOCR_CLKOSEL0_SHIFT) |
745 (div << PRCM_CLKOCR_CLKODIV0_SHIFT));
Arnd Bergmanna7e46312016-01-25 17:02:24 +0100746 } else {
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200747 div_mask = PRCM_CLKOCR_CLKODIV1_MASK;
748 mask = (PRCM_CLKOCR_CLKODIV1_MASK | PRCM_CLKOCR_CLKOSEL1_MASK |
749 PRCM_CLKOCR_CLK1TYPE);
750 bits = ((source << PRCM_CLKOCR_CLKOSEL1_SHIFT) |
751 (div << PRCM_CLKOCR_CLKODIV1_SHIFT));
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200752 }
753 bits &= mask;
754
755 spin_lock_irqsave(&clkout_lock, flags);
756
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200757 val = readl(PRCM_CLKOCR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200758 if (val & div_mask) {
759 if (div) {
760 if ((val & mask) != bits) {
761 r = -EBUSY;
762 goto unlock_and_return;
763 }
764 } else {
765 if ((val & mask & ~div_mask) != bits) {
766 r = -EINVAL;
767 goto unlock_and_return;
768 }
769 }
770 }
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200771 writel((bits | (val & ~mask)), PRCM_CLKOCR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200772 requests[clkout] += (div ? 1 : -1);
773
774unlock_and_return:
775 spin_unlock_irqrestore(&clkout_lock, flags);
776
777 return r;
778}
779
Mattias Nilsson73180f82011-08-12 10:28:10 +0200780int db8500_prcmu_set_power_state(u8 state, bool keep_ulp_clk, bool keep_ap_pll)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200781{
782 unsigned long flags;
783
784 BUG_ON((state < PRCMU_AP_SLEEP) || (PRCMU_AP_DEEP_IDLE < state));
785
786 spin_lock_irqsave(&mb0_transfer.lock, flags);
787
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200788 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200789 cpu_relax();
790
791 writeb(MB0H_POWER_STATE_TRANS, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
792 writeb(state, (tcdm_base + PRCM_REQ_MB0_AP_POWER_STATE));
793 writeb((keep_ap_pll ? 1 : 0), (tcdm_base + PRCM_REQ_MB0_AP_PLL_STATE));
794 writeb((keep_ulp_clk ? 1 : 0),
795 (tcdm_base + PRCM_REQ_MB0_ULP_CLOCK_STATE));
796 writeb(0, (tcdm_base + PRCM_REQ_MB0_DO_NOT_WFI));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200797 writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200798
799 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
800
801 return 0;
802}
803
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100804u8 db8500_prcmu_get_power_state_result(void)
805{
806 return readb(tcdm_base + PRCM_ACK_MB0_AP_PWRSTTR_STATUS);
807}
808
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200809/* This function should only be called while mb0_transfer.lock is held. */
810static void config_wakeups(void)
811{
812 const u8 header[2] = {
813 MB0H_CONFIG_WAKEUPS_EXE,
814 MB0H_CONFIG_WAKEUPS_SLEEP
815 };
816 static u32 last_dbb_events;
817 static u32 last_abb_events;
818 u32 dbb_events;
819 u32 abb_events;
820 unsigned int i;
821
822 dbb_events = mb0_transfer.req.dbb_irqs | mb0_transfer.req.dbb_wakeups;
823 dbb_events |= (WAKEUP_BIT_AC_WAKE_ACK | WAKEUP_BIT_AC_SLEEP_ACK);
824
825 abb_events = mb0_transfer.req.abb_events;
826
827 if ((dbb_events == last_dbb_events) && (abb_events == last_abb_events))
828 return;
829
830 for (i = 0; i < 2; i++) {
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200831 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200832 cpu_relax();
833 writel(dbb_events, (tcdm_base + PRCM_REQ_MB0_WAKEUP_8500));
834 writel(abb_events, (tcdm_base + PRCM_REQ_MB0_WAKEUP_4500));
835 writeb(header[i], (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200836 writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200837 }
838 last_dbb_events = dbb_events;
839 last_abb_events = abb_events;
840}
841
Mattias Nilsson73180f82011-08-12 10:28:10 +0200842void db8500_prcmu_enable_wakeups(u32 wakeups)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200843{
844 unsigned long flags;
845 u32 bits;
846 int i;
847
848 BUG_ON(wakeups != (wakeups & VALID_WAKEUPS));
849
850 for (i = 0, bits = 0; i < NUM_PRCMU_WAKEUP_INDICES; i++) {
851 if (wakeups & BIT(i))
852 bits |= prcmu_wakeup_bit[i];
853 }
854
855 spin_lock_irqsave(&mb0_transfer.lock, flags);
856
857 mb0_transfer.req.dbb_wakeups = bits;
858 config_wakeups();
859
860 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
861}
862
Mattias Nilsson73180f82011-08-12 10:28:10 +0200863void db8500_prcmu_config_abb_event_readout(u32 abb_events)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200864{
865 unsigned long flags;
866
867 spin_lock_irqsave(&mb0_transfer.lock, flags);
868
869 mb0_transfer.req.abb_events = abb_events;
870 config_wakeups();
871
872 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
873}
874
Mattias Nilsson73180f82011-08-12 10:28:10 +0200875void db8500_prcmu_get_abb_event_buffer(void __iomem **buf)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200876{
877 if (readb(tcdm_base + PRCM_ACK_MB0_READ_POINTER) & 1)
878 *buf = (tcdm_base + PRCM_ACK_MB0_WAKEUP_1_4500);
879 else
880 *buf = (tcdm_base + PRCM_ACK_MB0_WAKEUP_0_4500);
881}
882
883/**
Mattias Nilsson73180f82011-08-12 10:28:10 +0200884 * db8500_prcmu_set_arm_opp - set the appropriate ARM OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200885 * @opp: The new ARM operating point to which transition is to be made
886 * Returns: 0 on success, non-zero on failure
887 *
888 * This function sets the the operating point of the ARM.
889 */
Mattias Nilsson73180f82011-08-12 10:28:10 +0200890int db8500_prcmu_set_arm_opp(u8 opp)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200891{
892 int r;
893
894 if (opp < ARM_NO_CHANGE || opp > ARM_EXTCLK)
895 return -EINVAL;
896
897 r = 0;
898
899 mutex_lock(&mb1_transfer.lock);
900
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200901 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200902 cpu_relax();
903
904 writeb(MB1H_ARM_APE_OPP, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
905 writeb(opp, (tcdm_base + PRCM_REQ_MB1_ARM_OPP));
906 writeb(APE_NO_CHANGE, (tcdm_base + PRCM_REQ_MB1_APE_OPP));
907
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200908 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200909 wait_for_completion(&mb1_transfer.work);
910
911 if ((mb1_transfer.ack.header != MB1H_ARM_APE_OPP) ||
912 (mb1_transfer.ack.arm_opp != opp))
913 r = -EIO;
914
915 mutex_unlock(&mb1_transfer.lock);
916
917 return r;
918}
919
920/**
Mattias Nilsson73180f82011-08-12 10:28:10 +0200921 * db8500_prcmu_get_arm_opp - get the current ARM OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200922 *
923 * Returns: the current ARM OPP
924 */
Mattias Nilsson73180f82011-08-12 10:28:10 +0200925int db8500_prcmu_get_arm_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200926{
927 return readb(tcdm_base + PRCM_ACK_MB1_CURRENT_ARM_OPP);
928}
929
930/**
Mattias Nilsson05089012012-01-13 16:20:20 +0100931 * db8500_prcmu_get_ddr_opp - get the current DDR OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200932 *
933 * Returns: the current DDR OPP
934 */
Mattias Nilsson05089012012-01-13 16:20:20 +0100935int db8500_prcmu_get_ddr_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200936{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +0200937 return readb(PRCM_DDR_SUBSYS_APE_MINBW);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200938}
939
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100940/* Divide the frequency of certain clocks by 2 for APE_50_PARTLY_25_OPP. */
941static void request_even_slower_clocks(bool enable)
942{
Linus Walleijb047d982013-03-19 14:21:47 +0100943 u32 clock_reg[] = {
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100944 PRCM_ACLK_MGT,
945 PRCM_DMACLK_MGT
946 };
947 unsigned long flags;
948 unsigned int i;
949
950 spin_lock_irqsave(&clk_mgt_lock, flags);
951
952 /* Grab the HW semaphore. */
953 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
954 cpu_relax();
955
956 for (i = 0; i < ARRAY_SIZE(clock_reg); i++) {
957 u32 val;
958 u32 div;
959
Linus Walleijb047d982013-03-19 14:21:47 +0100960 val = readl(prcmu_base + clock_reg[i]);
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100961 div = (val & PRCM_CLK_MGT_CLKPLLDIV_MASK);
962 if (enable) {
963 if ((div <= 1) || (div > 15)) {
964 pr_err("prcmu: Bad clock divider %d in %s\n",
965 div, __func__);
966 goto unlock_and_return;
967 }
968 div <<= 1;
969 } else {
970 if (div <= 2)
971 goto unlock_and_return;
972 div >>= 1;
973 }
974 val = ((val & ~PRCM_CLK_MGT_CLKPLLDIV_MASK) |
975 (div & PRCM_CLK_MGT_CLKPLLDIV_MASK));
Linus Walleijb047d982013-03-19 14:21:47 +0100976 writel(val, prcmu_base + clock_reg[i]);
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100977 }
978
979unlock_and_return:
980 /* Release the HW semaphore. */
981 writel(0, PRCM_SEM);
982
983 spin_unlock_irqrestore(&clk_mgt_lock, flags);
984}
985
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200986/**
Mattias Nilsson05089012012-01-13 16:20:20 +0100987 * db8500_set_ape_opp - set the appropriate APE OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200988 * @opp: The new APE operating point to which transition is to be made
989 * Returns: 0 on success, non-zero on failure
990 *
991 * This function sets the operating point of the APE.
992 */
Mattias Nilsson05089012012-01-13 16:20:20 +0100993int db8500_prcmu_set_ape_opp(u8 opp)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +0200994{
995 int r = 0;
996
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +0100997 if (opp == mb1_transfer.ape_opp)
998 return 0;
999
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001000 mutex_lock(&mb1_transfer.lock);
1001
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001002 if (mb1_transfer.ape_opp == APE_50_PARTLY_25_OPP)
1003 request_even_slower_clocks(false);
1004
1005 if ((opp != APE_100_OPP) && (mb1_transfer.ape_opp != APE_100_OPP))
1006 goto skip_message;
1007
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001008 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001009 cpu_relax();
1010
1011 writeb(MB1H_ARM_APE_OPP, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1012 writeb(ARM_NO_CHANGE, (tcdm_base + PRCM_REQ_MB1_ARM_OPP));
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001013 writeb(((opp == APE_50_PARTLY_25_OPP) ? APE_50_OPP : opp),
1014 (tcdm_base + PRCM_REQ_MB1_APE_OPP));
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001015
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001016 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001017 wait_for_completion(&mb1_transfer.work);
1018
1019 if ((mb1_transfer.ack.header != MB1H_ARM_APE_OPP) ||
1020 (mb1_transfer.ack.ape_opp != opp))
1021 r = -EIO;
1022
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01001023skip_message:
1024 if ((!r && (opp == APE_50_PARTLY_25_OPP)) ||
1025 (r && (mb1_transfer.ape_opp == APE_50_PARTLY_25_OPP)))
1026 request_even_slower_clocks(true);
1027 if (!r)
1028 mb1_transfer.ape_opp = opp;
1029
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001030 mutex_unlock(&mb1_transfer.lock);
1031
1032 return r;
1033}
1034
1035/**
Mattias Nilsson05089012012-01-13 16:20:20 +01001036 * db8500_prcmu_get_ape_opp - get the current APE OPP
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001037 *
1038 * Returns: the current APE OPP
1039 */
Mattias Nilsson05089012012-01-13 16:20:20 +01001040int db8500_prcmu_get_ape_opp(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001041{
1042 return readb(tcdm_base + PRCM_ACK_MB1_CURRENT_APE_OPP);
1043}
1044
1045/**
Ulf Hansson686f8712012-09-24 16:43:17 +02001046 * db8500_prcmu_request_ape_opp_100_voltage - Request APE OPP 100% voltage
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001047 * @enable: true to request the higher voltage, false to drop a request.
1048 *
1049 * Calls to this function to enable and disable requests must be balanced.
1050 */
Ulf Hansson686f8712012-09-24 16:43:17 +02001051int db8500_prcmu_request_ape_opp_100_voltage(bool enable)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001052{
1053 int r = 0;
1054 u8 header;
1055 static unsigned int requests;
1056
1057 mutex_lock(&mb1_transfer.lock);
1058
1059 if (enable) {
1060 if (0 != requests++)
1061 goto unlock_and_return;
1062 header = MB1H_REQUEST_APE_OPP_100_VOLT;
1063 } else {
1064 if (requests == 0) {
1065 r = -EIO;
1066 goto unlock_and_return;
1067 } else if (1 != requests--) {
1068 goto unlock_and_return;
1069 }
1070 header = MB1H_RELEASE_APE_OPP_100_VOLT;
1071 }
1072
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001073 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001074 cpu_relax();
1075
1076 writeb(header, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1077
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 != header) ||
1082 ((mb1_transfer.ack.ape_voltage_status & BIT(0)) != 0))
1083 r = -EIO;
1084
1085unlock_and_return:
1086 mutex_unlock(&mb1_transfer.lock);
1087
1088 return r;
1089}
1090
1091/**
1092 * prcmu_release_usb_wakeup_state - release the state required by a USB wakeup
1093 *
1094 * This function releases the power state requirements of a USB wakeup.
1095 */
1096int prcmu_release_usb_wakeup_state(void)
1097{
1098 int r = 0;
1099
1100 mutex_lock(&mb1_transfer.lock);
1101
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001102 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001103 cpu_relax();
1104
1105 writeb(MB1H_RELEASE_USB_WAKEUP,
1106 (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1107
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001108 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001109 wait_for_completion(&mb1_transfer.work);
1110
1111 if ((mb1_transfer.ack.header != MB1H_RELEASE_USB_WAKEUP) ||
1112 ((mb1_transfer.ack.ape_voltage_status & BIT(0)) != 0))
1113 r = -EIO;
1114
1115 mutex_unlock(&mb1_transfer.lock);
1116
1117 return r;
1118}
1119
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001120static int request_pll(u8 clock, bool enable)
1121{
1122 int r = 0;
1123
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001124 if (clock == PRCMU_PLLSOC0)
1125 clock = (enable ? PLL_SOC0_ON : PLL_SOC0_OFF);
1126 else if (clock == PRCMU_PLLSOC1)
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001127 clock = (enable ? PLL_SOC1_ON : PLL_SOC1_OFF);
1128 else
1129 return -EINVAL;
1130
1131 mutex_lock(&mb1_transfer.lock);
1132
1133 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
1134 cpu_relax();
1135
1136 writeb(MB1H_PLL_ON_OFF, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
1137 writeb(clock, (tcdm_base + PRCM_REQ_MB1_PLL_ON_OFF));
1138
1139 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
1140 wait_for_completion(&mb1_transfer.work);
1141
1142 if (mb1_transfer.ack.header != MB1H_PLL_ON_OFF)
1143 r = -EIO;
1144
1145 mutex_unlock(&mb1_transfer.lock);
1146
1147 return r;
1148}
1149
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001150/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02001151 * db8500_prcmu_set_epod - set the state of a EPOD (power domain)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001152 * @epod_id: The EPOD to set
1153 * @epod_state: The new EPOD state
1154 *
1155 * This function sets the state of a EPOD (power domain). It may not be called
1156 * from interrupt context.
1157 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02001158int db8500_prcmu_set_epod(u16 epod_id, u8 epod_state)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001159{
1160 int r = 0;
1161 bool ram_retention = false;
1162 int i;
1163
1164 /* check argument */
1165 BUG_ON(epod_id >= NUM_EPOD_ID);
1166
1167 /* set flag if retention is possible */
1168 switch (epod_id) {
1169 case EPOD_ID_SVAMMDSP:
1170 case EPOD_ID_SIAMMDSP:
1171 case EPOD_ID_ESRAM12:
1172 case EPOD_ID_ESRAM34:
1173 ram_retention = true;
1174 break;
1175 }
1176
1177 /* check argument */
1178 BUG_ON(epod_state > EPOD_STATE_ON);
1179 BUG_ON(epod_state == EPOD_STATE_RAMRET && !ram_retention);
1180
1181 /* get lock */
1182 mutex_lock(&mb2_transfer.lock);
1183
1184 /* wait for mailbox */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001185 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(2))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001186 cpu_relax();
1187
1188 /* fill in mailbox */
1189 for (i = 0; i < NUM_EPOD_ID; i++)
1190 writeb(EPOD_STATE_NO_CHANGE, (tcdm_base + PRCM_REQ_MB2 + i));
1191 writeb(epod_state, (tcdm_base + PRCM_REQ_MB2 + epod_id));
1192
1193 writeb(MB2H_DPS, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB2));
1194
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001195 writel(MBOX_BIT(2), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001196
1197 /*
1198 * The current firmware version does not handle errors correctly,
1199 * and we cannot recover if there is an error.
1200 * This is expected to change when the firmware is updated.
1201 */
1202 if (!wait_for_completion_timeout(&mb2_transfer.work,
1203 msecs_to_jiffies(20000))) {
1204 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
1205 __func__);
1206 r = -EIO;
1207 goto unlock_and_return;
1208 }
1209
1210 if (mb2_transfer.ack.status != HWACC_PWR_ST_OK)
1211 r = -EIO;
1212
1213unlock_and_return:
1214 mutex_unlock(&mb2_transfer.lock);
1215 return r;
1216}
1217
1218/**
1219 * prcmu_configure_auto_pm - Configure autonomous power management.
1220 * @sleep: Configuration for ApSleep.
1221 * @idle: Configuration for ApIdle.
1222 */
1223void prcmu_configure_auto_pm(struct prcmu_auto_pm_config *sleep,
1224 struct prcmu_auto_pm_config *idle)
1225{
1226 u32 sleep_cfg;
1227 u32 idle_cfg;
1228 unsigned long flags;
1229
1230 BUG_ON((sleep == NULL) || (idle == NULL));
1231
1232 sleep_cfg = (sleep->sva_auto_pm_enable & 0xF);
1233 sleep_cfg = ((sleep_cfg << 4) | (sleep->sia_auto_pm_enable & 0xF));
1234 sleep_cfg = ((sleep_cfg << 8) | (sleep->sva_power_on & 0xFF));
1235 sleep_cfg = ((sleep_cfg << 8) | (sleep->sia_power_on & 0xFF));
1236 sleep_cfg = ((sleep_cfg << 4) | (sleep->sva_policy & 0xF));
1237 sleep_cfg = ((sleep_cfg << 4) | (sleep->sia_policy & 0xF));
1238
1239 idle_cfg = (idle->sva_auto_pm_enable & 0xF);
1240 idle_cfg = ((idle_cfg << 4) | (idle->sia_auto_pm_enable & 0xF));
1241 idle_cfg = ((idle_cfg << 8) | (idle->sva_power_on & 0xFF));
1242 idle_cfg = ((idle_cfg << 8) | (idle->sia_power_on & 0xFF));
1243 idle_cfg = ((idle_cfg << 4) | (idle->sva_policy & 0xF));
1244 idle_cfg = ((idle_cfg << 4) | (idle->sia_policy & 0xF));
1245
1246 spin_lock_irqsave(&mb2_transfer.auto_pm_lock, flags);
1247
1248 /*
1249 * The autonomous power management configuration is done through
1250 * fields in mailbox 2, but these fields are only used as shared
1251 * variables - i.e. there is no need to send a message.
1252 */
1253 writel(sleep_cfg, (tcdm_base + PRCM_REQ_MB2_AUTO_PM_SLEEP));
1254 writel(idle_cfg, (tcdm_base + PRCM_REQ_MB2_AUTO_PM_IDLE));
1255
1256 mb2_transfer.auto_pm_enabled =
1257 ((sleep->sva_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1258 (sleep->sia_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1259 (idle->sva_auto_pm_enable == PRCMU_AUTO_PM_ON) ||
1260 (idle->sia_auto_pm_enable == PRCMU_AUTO_PM_ON));
1261
1262 spin_unlock_irqrestore(&mb2_transfer.auto_pm_lock, flags);
1263}
1264EXPORT_SYMBOL(prcmu_configure_auto_pm);
1265
1266bool prcmu_is_auto_pm_enabled(void)
1267{
1268 return mb2_transfer.auto_pm_enabled;
1269}
1270
1271static int request_sysclk(bool enable)
1272{
1273 int r;
1274 unsigned long flags;
1275
1276 r = 0;
1277
1278 mutex_lock(&mb3_transfer.sysclk_lock);
1279
1280 spin_lock_irqsave(&mb3_transfer.lock, flags);
1281
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001282 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(3))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001283 cpu_relax();
1284
1285 writeb((enable ? ON : OFF), (tcdm_base + PRCM_REQ_MB3_SYSCLK_MGT));
1286
1287 writeb(MB3H_SYSCLK, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB3));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001288 writel(MBOX_BIT(3), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001289
1290 spin_unlock_irqrestore(&mb3_transfer.lock, flags);
1291
1292 /*
1293 * The firmware only sends an ACK if we want to enable the
1294 * SysClk, and it succeeds.
1295 */
1296 if (enable && !wait_for_completion_timeout(&mb3_transfer.sysclk_work,
1297 msecs_to_jiffies(20000))) {
1298 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
1299 __func__);
1300 r = -EIO;
1301 }
1302
1303 mutex_unlock(&mb3_transfer.sysclk_lock);
1304
1305 return r;
1306}
1307
1308static int request_timclk(bool enable)
1309{
1310 u32 val = (PRCM_TCR_DOZE_MODE | PRCM_TCR_TENSEL_MASK);
1311
1312 if (!enable)
1313 val |= PRCM_TCR_STOP_TIMERS;
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001314 writel(val, PRCM_TCR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001315
1316 return 0;
1317}
1318
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001319static int request_clock(u8 clock, bool enable)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001320{
1321 u32 val;
1322 unsigned long flags;
1323
1324 spin_lock_irqsave(&clk_mgt_lock, flags);
1325
1326 /* Grab the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001327 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001328 cpu_relax();
1329
Linus Walleijb047d982013-03-19 14:21:47 +01001330 val = readl(prcmu_base + clk_mgt[clock].offset);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001331 if (enable) {
1332 val |= (PRCM_CLK_MGT_CLKEN | clk_mgt[clock].pllsw);
1333 } else {
1334 clk_mgt[clock].pllsw = (val & PRCM_CLK_MGT_CLKPLLSW_MASK);
1335 val &= ~(PRCM_CLK_MGT_CLKEN | PRCM_CLK_MGT_CLKPLLSW_MASK);
1336 }
Linus Walleijb047d982013-03-19 14:21:47 +01001337 writel(val, prcmu_base + clk_mgt[clock].offset);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001338
1339 /* Release the HW semaphore. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001340 writel(0, PRCM_SEM);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001341
1342 spin_unlock_irqrestore(&clk_mgt_lock, flags);
1343
1344 return 0;
1345}
1346
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001347static int request_sga_clock(u8 clock, bool enable)
1348{
1349 u32 val;
1350 int ret;
1351
1352 if (enable) {
1353 val = readl(PRCM_CGATING_BYPASS);
1354 writel(val | PRCM_CGATING_BYPASS_ICN2, PRCM_CGATING_BYPASS);
1355 }
1356
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001357 ret = request_clock(clock, enable);
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001358
1359 if (!ret && !enable) {
1360 val = readl(PRCM_CGATING_BYPASS);
1361 writel(val & ~PRCM_CGATING_BYPASS_ICN2, PRCM_CGATING_BYPASS);
1362 }
1363
1364 return ret;
1365}
1366
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001367static inline bool plldsi_locked(void)
1368{
1369 return (readl(PRCM_PLLDSI_LOCKP) &
1370 (PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP10 |
1371 PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP3)) ==
1372 (PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP10 |
1373 PRCM_PLLDSI_LOCKP_PRCM_PLLDSI_LOCKP3);
1374}
1375
1376static int request_plldsi(bool enable)
1377{
1378 int r = 0;
1379 u32 val;
1380
1381 writel((PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMP |
1382 PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMPI), (enable ?
1383 PRCM_MMIP_LS_CLAMP_CLR : PRCM_MMIP_LS_CLAMP_SET));
1384
1385 val = readl(PRCM_PLLDSI_ENABLE);
1386 if (enable)
1387 val |= PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1388 else
1389 val &= ~PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1390 writel(val, PRCM_PLLDSI_ENABLE);
1391
1392 if (enable) {
1393 unsigned int i;
1394 bool locked = plldsi_locked();
1395
1396 for (i = 10; !locked && (i > 0); --i) {
1397 udelay(100);
1398 locked = plldsi_locked();
1399 }
1400 if (locked) {
1401 writel(PRCM_APE_RESETN_DSIPLL_RESETN,
1402 PRCM_APE_RESETN_SET);
1403 } else {
1404 writel((PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMP |
1405 PRCM_MMIP_LS_CLAMP_DSIPLL_CLAMPI),
1406 PRCM_MMIP_LS_CLAMP_SET);
1407 val &= ~PRCM_PLLDSI_ENABLE_PRCM_PLLDSI_ENABLE;
1408 writel(val, PRCM_PLLDSI_ENABLE);
1409 r = -EAGAIN;
1410 }
1411 } else {
1412 writel(PRCM_APE_RESETN_DSIPLL_RESETN, PRCM_APE_RESETN_CLR);
1413 }
1414 return r;
1415}
1416
1417static int request_dsiclk(u8 n, bool enable)
1418{
1419 u32 val;
1420
1421 val = readl(PRCM_DSI_PLLOUT_SEL);
1422 val &= ~dsiclk[n].divsel_mask;
1423 val |= ((enable ? dsiclk[n].divsel : PRCM_DSI_PLLOUT_SEL_OFF) <<
1424 dsiclk[n].divsel_shift);
1425 writel(val, PRCM_DSI_PLLOUT_SEL);
1426 return 0;
1427}
1428
1429static int request_dsiescclk(u8 n, bool enable)
1430{
1431 u32 val;
1432
1433 val = readl(PRCM_DSITVCLK_DIV);
1434 enable ? (val |= dsiescclk[n].en) : (val &= ~dsiescclk[n].en);
1435 writel(val, PRCM_DSITVCLK_DIV);
1436 return 0;
1437}
1438
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001439/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02001440 * db8500_prcmu_request_clock() - Request for a clock to be enabled or disabled.
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001441 * @clock: The clock for which the request is made.
1442 * @enable: Whether the clock should be enabled (true) or disabled (false).
1443 *
1444 * This function should only be used by the clock implementation.
1445 * Do not use it from any other place!
1446 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02001447int db8500_prcmu_request_clock(u8 clock, bool enable)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001448{
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001449 if (clock == PRCMU_SGACLK)
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001450 return request_sga_clock(clock, enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001451 else if (clock < PRCMU_NUM_REG_CLOCKS)
1452 return request_clock(clock, enable);
1453 else if (clock == PRCMU_TIMCLK)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001454 return request_timclk(enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001455 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1456 return request_dsiclk((clock - PRCMU_DSI0CLK), enable);
1457 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1458 return request_dsiescclk((clock - PRCMU_DSI0ESCCLK), enable);
1459 else if (clock == PRCMU_PLLDSI)
1460 return request_plldsi(enable);
1461 else if (clock == PRCMU_SYSCLK)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001462 return request_sysclk(enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001463 else if ((clock == PRCMU_PLLSOC0) || (clock == PRCMU_PLLSOC1))
Mattias Nilsson0837bb72011-08-12 10:28:18 +02001464 return request_pll(clock, enable);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001465 else
1466 return -EINVAL;
1467}
1468
1469static unsigned long pll_rate(void __iomem *reg, unsigned long src_rate,
1470 int branch)
1471{
1472 u64 rate;
1473 u32 val;
1474 u32 d;
1475 u32 div = 1;
1476
1477 val = readl(reg);
1478
1479 rate = src_rate;
1480 rate *= ((val & PRCM_PLL_FREQ_D_MASK) >> PRCM_PLL_FREQ_D_SHIFT);
1481
1482 d = ((val & PRCM_PLL_FREQ_N_MASK) >> PRCM_PLL_FREQ_N_SHIFT);
1483 if (d > 1)
1484 div *= d;
1485
1486 d = ((val & PRCM_PLL_FREQ_R_MASK) >> PRCM_PLL_FREQ_R_SHIFT);
1487 if (d > 1)
1488 div *= d;
1489
1490 if (val & PRCM_PLL_FREQ_SELDIV2)
1491 div *= 2;
1492
1493 if ((branch == PLL_FIX) || ((branch == PLL_DIV) &&
1494 (val & PRCM_PLL_FREQ_DIV2EN) &&
1495 ((reg == PRCM_PLLSOC0_FREQ) ||
Michel Jaouen20aee5b2012-08-31 14:21:30 +02001496 (reg == PRCM_PLLARM_FREQ) ||
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001497 (reg == PRCM_PLLDDR_FREQ))))
1498 div *= 2;
1499
1500 (void)do_div(rate, div);
1501
1502 return (unsigned long)rate;
1503}
1504
1505#define ROOT_CLOCK_RATE 38400000
1506
1507static unsigned long clock_rate(u8 clock)
1508{
1509 u32 val;
1510 u32 pllsw;
1511 unsigned long rate = ROOT_CLOCK_RATE;
1512
Linus Walleijb047d982013-03-19 14:21:47 +01001513 val = readl(prcmu_base + clk_mgt[clock].offset);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001514
1515 if (val & PRCM_CLK_MGT_CLK38) {
1516 if (clk_mgt[clock].clk38div && (val & PRCM_CLK_MGT_CLK38DIV))
1517 rate /= 2;
1518 return rate;
Linus Walleije62ccf32011-10-10 12:14:14 +02001519 }
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001520
1521 val |= clk_mgt[clock].pllsw;
1522 pllsw = (val & PRCM_CLK_MGT_CLKPLLSW_MASK);
1523
1524 if (pllsw == PRCM_CLK_MGT_CLKPLLSW_SOC0)
1525 rate = pll_rate(PRCM_PLLSOC0_FREQ, rate, clk_mgt[clock].branch);
1526 else if (pllsw == PRCM_CLK_MGT_CLKPLLSW_SOC1)
1527 rate = pll_rate(PRCM_PLLSOC1_FREQ, rate, clk_mgt[clock].branch);
1528 else if (pllsw == PRCM_CLK_MGT_CLKPLLSW_DDR)
1529 rate = pll_rate(PRCM_PLLDDR_FREQ, rate, clk_mgt[clock].branch);
1530 else
1531 return 0;
1532
1533 if ((clock == PRCMU_SGACLK) &&
1534 (val & PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN)) {
1535 u64 r = (rate * 10);
1536
1537 (void)do_div(r, 25);
1538 return (unsigned long)r;
1539 }
1540 val &= PRCM_CLK_MGT_CLKPLLDIV_MASK;
1541 if (val)
1542 return rate / val;
1543 else
1544 return 0;
1545}
Michel Jaouen20aee5b2012-08-31 14:21:30 +02001546
Ulf Hanssonb2302c82012-10-10 13:42:26 +02001547static unsigned long armss_rate(void)
Michel Jaouen20aee5b2012-08-31 14:21:30 +02001548{
1549 u32 r;
1550 unsigned long rate;
1551
1552 r = readl(PRCM_ARM_CHGCLKREQ);
1553
1554 if (r & PRCM_ARM_CHGCLKREQ_PRCM_ARM_CHGCLKREQ) {
1555 /* External ARMCLKFIX clock */
1556
1557 rate = pll_rate(PRCM_PLLDDR_FREQ, ROOT_CLOCK_RATE, PLL_FIX);
1558
1559 /* Check PRCM_ARM_CHGCLKREQ divider */
1560 if (!(r & PRCM_ARM_CHGCLKREQ_PRCM_ARM_DIVSEL))
1561 rate /= 2;
1562
1563 /* Check PRCM_ARMCLKFIX_MGT divider */
1564 r = readl(PRCM_ARMCLKFIX_MGT);
1565 r &= PRCM_CLK_MGT_CLKPLLDIV_MASK;
1566 rate /= r;
1567
1568 } else {/* ARM PLL */
1569 rate = pll_rate(PRCM_PLLARM_FREQ, ROOT_CLOCK_RATE, PLL_DIV);
1570 }
1571
Ulf Hanssonb2302c82012-10-10 13:42:26 +02001572 return rate;
Michel Jaouen20aee5b2012-08-31 14:21:30 +02001573}
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001574
1575static unsigned long dsiclk_rate(u8 n)
1576{
1577 u32 divsel;
1578 u32 div = 1;
1579
1580 divsel = readl(PRCM_DSI_PLLOUT_SEL);
1581 divsel = ((divsel & dsiclk[n].divsel_mask) >> dsiclk[n].divsel_shift);
1582
1583 if (divsel == PRCM_DSI_PLLOUT_SEL_OFF)
1584 divsel = dsiclk[n].divsel;
Ulf Hanssone9d7b4b2013-05-14 15:14:55 +02001585 else
1586 dsiclk[n].divsel = divsel;
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001587
1588 switch (divsel) {
1589 case PRCM_DSI_PLLOUT_SEL_PHI_4:
1590 div *= 2;
1591 case PRCM_DSI_PLLOUT_SEL_PHI_2:
1592 div *= 2;
1593 case PRCM_DSI_PLLOUT_SEL_PHI:
1594 return pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1595 PLL_RAW) / div;
1596 default:
1597 return 0;
1598 }
1599}
1600
1601static unsigned long dsiescclk_rate(u8 n)
1602{
1603 u32 div;
1604
1605 div = readl(PRCM_DSITVCLK_DIV);
1606 div = ((div & dsiescclk[n].div_mask) >> (dsiescclk[n].div_shift));
1607 return clock_rate(PRCMU_TVCLK) / max((u32)1, div);
1608}
1609
1610unsigned long prcmu_clock_rate(u8 clock)
1611{
Linus Walleije62ccf32011-10-10 12:14:14 +02001612 if (clock < PRCMU_NUM_REG_CLOCKS)
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001613 return clock_rate(clock);
1614 else if (clock == PRCMU_TIMCLK)
1615 return ROOT_CLOCK_RATE / 16;
1616 else if (clock == PRCMU_SYSCLK)
1617 return ROOT_CLOCK_RATE;
1618 else if (clock == PRCMU_PLLSOC0)
1619 return pll_rate(PRCM_PLLSOC0_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
1620 else if (clock == PRCMU_PLLSOC1)
1621 return pll_rate(PRCM_PLLSOC1_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
Michel Jaouen20aee5b2012-08-31 14:21:30 +02001622 else if (clock == PRCMU_ARMSS)
1623 return armss_rate();
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001624 else if (clock == PRCMU_PLLDDR)
1625 return pll_rate(PRCM_PLLDDR_FREQ, ROOT_CLOCK_RATE, PLL_RAW);
1626 else if (clock == PRCMU_PLLDSI)
1627 return pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1628 PLL_RAW);
1629 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1630 return dsiclk_rate(clock - PRCMU_DSI0CLK);
1631 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1632 return dsiescclk_rate(clock - PRCMU_DSI0ESCCLK);
1633 else
1634 return 0;
1635}
1636
1637static unsigned long clock_source_rate(u32 clk_mgt_val, int branch)
1638{
1639 if (clk_mgt_val & PRCM_CLK_MGT_CLK38)
1640 return ROOT_CLOCK_RATE;
1641 clk_mgt_val &= PRCM_CLK_MGT_CLKPLLSW_MASK;
1642 if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_SOC0)
1643 return pll_rate(PRCM_PLLSOC0_FREQ, ROOT_CLOCK_RATE, branch);
1644 else if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_SOC1)
1645 return pll_rate(PRCM_PLLSOC1_FREQ, ROOT_CLOCK_RATE, branch);
1646 else if (clk_mgt_val == PRCM_CLK_MGT_CLKPLLSW_DDR)
1647 return pll_rate(PRCM_PLLDDR_FREQ, ROOT_CLOCK_RATE, branch);
1648 else
1649 return 0;
1650}
1651
1652static u32 clock_divider(unsigned long src_rate, unsigned long rate)
1653{
1654 u32 div;
1655
1656 div = (src_rate / rate);
1657 if (div == 0)
1658 return 1;
1659 if (rate < (src_rate / div))
1660 div++;
1661 return div;
1662}
1663
1664static long round_clock_rate(u8 clock, unsigned long rate)
1665{
1666 u32 val;
1667 u32 div;
1668 unsigned long src_rate;
1669 long rounded_rate;
1670
Linus Walleijb047d982013-03-19 14:21:47 +01001671 val = readl(prcmu_base + clk_mgt[clock].offset);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001672 src_rate = clock_source_rate((val | clk_mgt[clock].pllsw),
1673 clk_mgt[clock].branch);
1674 div = clock_divider(src_rate, rate);
1675 if (val & PRCM_CLK_MGT_CLK38) {
1676 if (clk_mgt[clock].clk38div) {
1677 if (div > 2)
1678 div = 2;
1679 } else {
1680 div = 1;
1681 }
1682 } else if ((clock == PRCMU_SGACLK) && (div == 3)) {
1683 u64 r = (src_rate * 10);
1684
1685 (void)do_div(r, 25);
1686 if (r <= rate)
1687 return (unsigned long)r;
1688 }
1689 rounded_rate = (src_rate / min(div, (u32)31));
1690
1691 return rounded_rate;
1692}
1693
Linus Walleij836a1e22017-08-22 15:17:40 +02001694static const unsigned long armss_freqs[] = {
1695 200000000,
1696 400000000,
1697 800000000,
1698 998400000
Ulf Hanssonb2302c82012-10-10 13:42:26 +02001699};
1700
1701static long round_armss_rate(unsigned long rate)
1702{
Linus Walleij836a1e22017-08-22 15:17:40 +02001703 unsigned long freq = 0;
1704 int i;
Ulf Hanssonb2302c82012-10-10 13:42:26 +02001705
1706 /* Find the corresponding arm opp from the cpufreq table. */
Linus Walleij836a1e22017-08-22 15:17:40 +02001707 for (i = 0; i < ARRAY_SIZE(armss_freqs); i++) {
1708 freq = armss_freqs[i];
1709 if (rate <= freq)
Ulf Hanssonb2302c82012-10-10 13:42:26 +02001710 break;
Ulf Hanssonb2302c82012-10-10 13:42:26 +02001711 }
1712
1713 /* Return the last valid value, even if a match was not found. */
Linus Walleij836a1e22017-08-22 15:17:40 +02001714 return freq;
Ulf Hanssonb2302c82012-10-10 13:42:26 +02001715}
1716
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001717#define MIN_PLL_VCO_RATE 600000000ULL
1718#define MAX_PLL_VCO_RATE 1680640000ULL
1719
1720static long round_plldsi_rate(unsigned long rate)
1721{
1722 long rounded_rate = 0;
1723 unsigned long src_rate;
1724 unsigned long rem;
1725 u32 r;
1726
1727 src_rate = clock_rate(PRCMU_HDMICLK);
1728 rem = rate;
1729
1730 for (r = 7; (rem > 0) && (r > 0); r--) {
1731 u64 d;
1732
1733 d = (r * rate);
1734 (void)do_div(d, src_rate);
1735 if (d < 6)
1736 d = 6;
1737 else if (d > 255)
1738 d = 255;
1739 d *= src_rate;
1740 if (((2 * d) < (r * MIN_PLL_VCO_RATE)) ||
1741 ((r * MAX_PLL_VCO_RATE) < (2 * d)))
1742 continue;
1743 (void)do_div(d, r);
1744 if (rate < d) {
1745 if (rounded_rate == 0)
1746 rounded_rate = (long)d;
1747 break;
1748 }
1749 if ((rate - d) < rem) {
1750 rem = (rate - d);
1751 rounded_rate = (long)d;
1752 }
1753 }
1754 return rounded_rate;
1755}
1756
1757static long round_dsiclk_rate(unsigned long rate)
1758{
1759 u32 div;
1760 unsigned long src_rate;
1761 long rounded_rate;
1762
1763 src_rate = pll_rate(PRCM_PLLDSI_FREQ, clock_rate(PRCMU_HDMICLK),
1764 PLL_RAW);
1765 div = clock_divider(src_rate, rate);
1766 rounded_rate = (src_rate / ((div > 2) ? 4 : div));
1767
1768 return rounded_rate;
1769}
1770
1771static long round_dsiescclk_rate(unsigned long rate)
1772{
1773 u32 div;
1774 unsigned long src_rate;
1775 long rounded_rate;
1776
1777 src_rate = clock_rate(PRCMU_TVCLK);
1778 div = clock_divider(src_rate, rate);
1779 rounded_rate = (src_rate / min(div, (u32)255));
1780
1781 return rounded_rate;
1782}
1783
1784long prcmu_round_clock_rate(u8 clock, unsigned long rate)
1785{
1786 if (clock < PRCMU_NUM_REG_CLOCKS)
1787 return round_clock_rate(clock, rate);
Ulf Hanssonb2302c82012-10-10 13:42:26 +02001788 else if (clock == PRCMU_ARMSS)
1789 return round_armss_rate(rate);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001790 else if (clock == PRCMU_PLLDSI)
1791 return round_plldsi_rate(rate);
1792 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1793 return round_dsiclk_rate(rate);
1794 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1795 return round_dsiescclk_rate(rate);
1796 else
1797 return (long)prcmu_clock_rate(clock);
1798}
1799
1800static void set_clock_rate(u8 clock, unsigned long rate)
1801{
1802 u32 val;
1803 u32 div;
1804 unsigned long src_rate;
1805 unsigned long flags;
1806
1807 spin_lock_irqsave(&clk_mgt_lock, flags);
1808
1809 /* Grab the HW semaphore. */
1810 while ((readl(PRCM_SEM) & PRCM_SEM_PRCM_SEM) != 0)
1811 cpu_relax();
1812
Linus Walleijb047d982013-03-19 14:21:47 +01001813 val = readl(prcmu_base + clk_mgt[clock].offset);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001814 src_rate = clock_source_rate((val | clk_mgt[clock].pllsw),
1815 clk_mgt[clock].branch);
1816 div = clock_divider(src_rate, rate);
1817 if (val & PRCM_CLK_MGT_CLK38) {
1818 if (clk_mgt[clock].clk38div) {
1819 if (div > 1)
1820 val |= PRCM_CLK_MGT_CLK38DIV;
1821 else
1822 val &= ~PRCM_CLK_MGT_CLK38DIV;
1823 }
1824 } else if (clock == PRCMU_SGACLK) {
1825 val &= ~(PRCM_CLK_MGT_CLKPLLDIV_MASK |
1826 PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN);
1827 if (div == 3) {
1828 u64 r = (src_rate * 10);
1829
1830 (void)do_div(r, 25);
1831 if (r <= rate) {
1832 val |= PRCM_SGACLK_MGT_SGACLKDIV_BY_2_5_EN;
1833 div = 0;
1834 }
1835 }
1836 val |= min(div, (u32)31);
1837 } else {
1838 val &= ~PRCM_CLK_MGT_CLKPLLDIV_MASK;
1839 val |= min(div, (u32)31);
1840 }
Linus Walleijb047d982013-03-19 14:21:47 +01001841 writel(val, prcmu_base + clk_mgt[clock].offset);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001842
1843 /* Release the HW semaphore. */
1844 writel(0, PRCM_SEM);
1845
1846 spin_unlock_irqrestore(&clk_mgt_lock, flags);
1847}
1848
Ulf Hanssonb2302c82012-10-10 13:42:26 +02001849static int set_armss_rate(unsigned long rate)
1850{
Linus Walleij836a1e22017-08-22 15:17:40 +02001851 unsigned long freq;
1852 u8 opps[] = { ARM_EXTCLK, ARM_50_OPP, ARM_100_OPP, ARM_MAX_OPP };
1853 int i;
Ulf Hanssonb2302c82012-10-10 13:42:26 +02001854
1855 /* Find the corresponding arm opp from the cpufreq table. */
Linus Walleij836a1e22017-08-22 15:17:40 +02001856 for (i = 0; i < ARRAY_SIZE(armss_freqs); i++) {
1857 freq = armss_freqs[i];
1858 if (rate == freq)
Ulf Hanssonb2302c82012-10-10 13:42:26 +02001859 break;
Linus Walleij836a1e22017-08-22 15:17:40 +02001860 }
Ulf Hanssonb2302c82012-10-10 13:42:26 +02001861
Linus Walleij836a1e22017-08-22 15:17:40 +02001862 if (rate != freq)
Ulf Hanssonb2302c82012-10-10 13:42:26 +02001863 return -EINVAL;
1864
1865 /* Set the new arm opp. */
Linus Walleij836a1e22017-08-22 15:17:40 +02001866 pr_debug("SET ARM OPP 0x%02x\n", opps[i]);
1867 return db8500_prcmu_set_arm_opp(opps[i]);
Ulf Hanssonb2302c82012-10-10 13:42:26 +02001868}
1869
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001870static int set_plldsi_rate(unsigned long rate)
1871{
1872 unsigned long src_rate;
1873 unsigned long rem;
1874 u32 pll_freq = 0;
1875 u32 r;
1876
1877 src_rate = clock_rate(PRCMU_HDMICLK);
1878 rem = rate;
1879
1880 for (r = 7; (rem > 0) && (r > 0); r--) {
1881 u64 d;
1882 u64 hwrate;
1883
1884 d = (r * rate);
1885 (void)do_div(d, src_rate);
1886 if (d < 6)
1887 d = 6;
1888 else if (d > 255)
1889 d = 255;
1890 hwrate = (d * src_rate);
1891 if (((2 * hwrate) < (r * MIN_PLL_VCO_RATE)) ||
1892 ((r * MAX_PLL_VCO_RATE) < (2 * hwrate)))
1893 continue;
1894 (void)do_div(hwrate, r);
1895 if (rate < hwrate) {
1896 if (pll_freq == 0)
1897 pll_freq = (((u32)d << PRCM_PLL_FREQ_D_SHIFT) |
1898 (r << PRCM_PLL_FREQ_R_SHIFT));
1899 break;
1900 }
1901 if ((rate - hwrate) < rem) {
1902 rem = (rate - hwrate);
1903 pll_freq = (((u32)d << PRCM_PLL_FREQ_D_SHIFT) |
1904 (r << PRCM_PLL_FREQ_R_SHIFT));
1905 }
1906 }
1907 if (pll_freq == 0)
1908 return -EINVAL;
1909
1910 pll_freq |= (1 << PRCM_PLL_FREQ_N_SHIFT);
1911 writel(pll_freq, PRCM_PLLDSI_FREQ);
1912
1913 return 0;
1914}
1915
1916static void set_dsiclk_rate(u8 n, unsigned long rate)
1917{
1918 u32 val;
1919 u32 div;
1920
1921 div = clock_divider(pll_rate(PRCM_PLLDSI_FREQ,
1922 clock_rate(PRCMU_HDMICLK), PLL_RAW), rate);
1923
1924 dsiclk[n].divsel = (div == 1) ? PRCM_DSI_PLLOUT_SEL_PHI :
1925 (div == 2) ? PRCM_DSI_PLLOUT_SEL_PHI_2 :
1926 /* else */ PRCM_DSI_PLLOUT_SEL_PHI_4;
1927
1928 val = readl(PRCM_DSI_PLLOUT_SEL);
1929 val &= ~dsiclk[n].divsel_mask;
1930 val |= (dsiclk[n].divsel << dsiclk[n].divsel_shift);
1931 writel(val, PRCM_DSI_PLLOUT_SEL);
1932}
1933
1934static void set_dsiescclk_rate(u8 n, unsigned long rate)
1935{
1936 u32 val;
1937 u32 div;
1938
1939 div = clock_divider(clock_rate(PRCMU_TVCLK), rate);
1940 val = readl(PRCM_DSITVCLK_DIV);
1941 val &= ~dsiescclk[n].div_mask;
1942 val |= (min(div, (u32)255) << dsiescclk[n].div_shift);
1943 writel(val, PRCM_DSITVCLK_DIV);
1944}
1945
1946int prcmu_set_clock_rate(u8 clock, unsigned long rate)
1947{
1948 if (clock < PRCMU_NUM_REG_CLOCKS)
1949 set_clock_rate(clock, rate);
Ulf Hanssonb2302c82012-10-10 13:42:26 +02001950 else if (clock == PRCMU_ARMSS)
1951 return set_armss_rate(rate);
Mattias Nilsson6b6fae22012-01-13 16:20:28 +01001952 else if (clock == PRCMU_PLLDSI)
1953 return set_plldsi_rate(rate);
1954 else if ((clock == PRCMU_DSI0CLK) || (clock == PRCMU_DSI1CLK))
1955 set_dsiclk_rate((clock - PRCMU_DSI0CLK), rate);
1956 else if ((PRCMU_DSI0ESCCLK <= clock) && (clock <= PRCMU_DSI2ESCCLK))
1957 set_dsiescclk_rate((clock - PRCMU_DSI0ESCCLK), rate);
1958 return 0;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001959}
1960
Mattias Nilsson73180f82011-08-12 10:28:10 +02001961int db8500_prcmu_config_esram0_deep_sleep(u8 state)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001962{
1963 if ((state > ESRAM0_DEEP_SLEEP_STATE_RET) ||
1964 (state < ESRAM0_DEEP_SLEEP_STATE_OFF))
1965 return -EINVAL;
1966
1967 mutex_lock(&mb4_transfer.lock);
1968
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001969 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001970 cpu_relax();
1971
1972 writeb(MB4H_MEM_ST, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
1973 writeb(((DDR_PWR_STATE_OFFHIGHLAT << 4) | DDR_PWR_STATE_ON),
1974 (tcdm_base + PRCM_REQ_MB4_DDR_ST_AP_SLEEP_IDLE));
1975 writeb(DDR_PWR_STATE_ON,
1976 (tcdm_base + PRCM_REQ_MB4_DDR_ST_AP_DEEP_IDLE));
1977 writeb(state, (tcdm_base + PRCM_REQ_MB4_ESRAM0_ST));
1978
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001979 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001980 wait_for_completion(&mb4_transfer.work);
1981
1982 mutex_unlock(&mb4_transfer.lock);
1983
1984 return 0;
1985}
1986
Mattias Nilsson05089012012-01-13 16:20:20 +01001987int db8500_prcmu_config_hotdog(u8 threshold)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001988{
1989 mutex_lock(&mb4_transfer.lock);
1990
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001991 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001992 cpu_relax();
1993
1994 writeb(threshold, (tcdm_base + PRCM_REQ_MB4_HOTDOG_THRESHOLD));
1995 writeb(MB4H_HOTDOG, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
1996
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02001997 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02001998 wait_for_completion(&mb4_transfer.work);
1999
2000 mutex_unlock(&mb4_transfer.lock);
2001
2002 return 0;
2003}
2004
Mattias Nilsson05089012012-01-13 16:20:20 +01002005int db8500_prcmu_config_hotmon(u8 low, u8 high)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002006{
2007 mutex_lock(&mb4_transfer.lock);
2008
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002009 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002010 cpu_relax();
2011
2012 writeb(low, (tcdm_base + PRCM_REQ_MB4_HOTMON_LOW));
2013 writeb(high, (tcdm_base + PRCM_REQ_MB4_HOTMON_HIGH));
2014 writeb((HOTMON_CONFIG_LOW | HOTMON_CONFIG_HIGH),
2015 (tcdm_base + PRCM_REQ_MB4_HOTMON_CONFIG));
2016 writeb(MB4H_HOTMON, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2017
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002018 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002019 wait_for_completion(&mb4_transfer.work);
2020
2021 mutex_unlock(&mb4_transfer.lock);
2022
2023 return 0;
2024}
Arnd Bergmann26716ce2016-01-25 17:44:12 +01002025EXPORT_SYMBOL_GPL(db8500_prcmu_config_hotmon);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002026
2027static int config_hot_period(u16 val)
2028{
2029 mutex_lock(&mb4_transfer.lock);
2030
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002031 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002032 cpu_relax();
2033
2034 writew(val, (tcdm_base + PRCM_REQ_MB4_HOT_PERIOD));
2035 writeb(MB4H_HOT_PERIOD, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2036
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002037 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002038 wait_for_completion(&mb4_transfer.work);
2039
2040 mutex_unlock(&mb4_transfer.lock);
2041
2042 return 0;
2043}
2044
Mattias Nilsson05089012012-01-13 16:20:20 +01002045int db8500_prcmu_start_temp_sense(u16 cycles32k)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002046{
2047 if (cycles32k == 0xFFFF)
2048 return -EINVAL;
2049
2050 return config_hot_period(cycles32k);
2051}
Arnd Bergmann26716ce2016-01-25 17:44:12 +01002052EXPORT_SYMBOL_GPL(db8500_prcmu_start_temp_sense);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002053
Mattias Nilsson05089012012-01-13 16:20:20 +01002054int db8500_prcmu_stop_temp_sense(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002055{
2056 return config_hot_period(0xFFFF);
2057}
Arnd Bergmann26716ce2016-01-25 17:44:12 +01002058EXPORT_SYMBOL_GPL(db8500_prcmu_stop_temp_sense);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002059
Jonas Aberg84165b82011-08-12 10:28:33 +02002060static int prcmu_a9wdog(u8 cmd, u8 d0, u8 d1, u8 d2, u8 d3)
2061{
2062
2063 mutex_lock(&mb4_transfer.lock);
2064
2065 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(4))
2066 cpu_relax();
2067
2068 writeb(d0, (tcdm_base + PRCM_REQ_MB4_A9WDOG_0));
2069 writeb(d1, (tcdm_base + PRCM_REQ_MB4_A9WDOG_1));
2070 writeb(d2, (tcdm_base + PRCM_REQ_MB4_A9WDOG_2));
2071 writeb(d3, (tcdm_base + PRCM_REQ_MB4_A9WDOG_3));
2072
2073 writeb(cmd, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB4));
2074
2075 writel(MBOX_BIT(4), PRCM_MBOX_CPU_SET);
2076 wait_for_completion(&mb4_transfer.work);
2077
2078 mutex_unlock(&mb4_transfer.lock);
2079
2080 return 0;
2081
2082}
2083
Mattias Nilsson05089012012-01-13 16:20:20 +01002084int db8500_prcmu_config_a9wdog(u8 num, bool sleep_auto_off)
Jonas Aberg84165b82011-08-12 10:28:33 +02002085{
2086 BUG_ON(num == 0 || num > 0xf);
2087 return prcmu_a9wdog(MB4H_A9WDOG_CONF, num, 0, 0,
2088 sleep_auto_off ? A9WDOG_AUTO_OFF_EN :
2089 A9WDOG_AUTO_OFF_DIS);
2090}
Fabio Baltieri6f8cfa92013-01-18 12:40:12 +01002091EXPORT_SYMBOL(db8500_prcmu_config_a9wdog);
Jonas Aberg84165b82011-08-12 10:28:33 +02002092
Mattias Nilsson05089012012-01-13 16:20:20 +01002093int db8500_prcmu_enable_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002094{
2095 return prcmu_a9wdog(MB4H_A9WDOG_EN, id, 0, 0, 0);
2096}
Fabio Baltieri6f8cfa92013-01-18 12:40:12 +01002097EXPORT_SYMBOL(db8500_prcmu_enable_a9wdog);
Jonas Aberg84165b82011-08-12 10:28:33 +02002098
Mattias Nilsson05089012012-01-13 16:20:20 +01002099int db8500_prcmu_disable_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002100{
2101 return prcmu_a9wdog(MB4H_A9WDOG_DIS, id, 0, 0, 0);
2102}
Fabio Baltieri6f8cfa92013-01-18 12:40:12 +01002103EXPORT_SYMBOL(db8500_prcmu_disable_a9wdog);
Jonas Aberg84165b82011-08-12 10:28:33 +02002104
Mattias Nilsson05089012012-01-13 16:20:20 +01002105int db8500_prcmu_kick_a9wdog(u8 id)
Jonas Aberg84165b82011-08-12 10:28:33 +02002106{
2107 return prcmu_a9wdog(MB4H_A9WDOG_KICK, id, 0, 0, 0);
2108}
Fabio Baltieri6f8cfa92013-01-18 12:40:12 +01002109EXPORT_SYMBOL(db8500_prcmu_kick_a9wdog);
Jonas Aberg84165b82011-08-12 10:28:33 +02002110
2111/*
2112 * timeout is 28 bit, in ms.
2113 */
Mattias Nilsson05089012012-01-13 16:20:20 +01002114int db8500_prcmu_load_a9wdog(u8 id, u32 timeout)
Jonas Aberg84165b82011-08-12 10:28:33 +02002115{
Jonas Aberg84165b82011-08-12 10:28:33 +02002116 return prcmu_a9wdog(MB4H_A9WDOG_LOAD,
2117 (id & A9WDOG_ID_MASK) |
2118 /*
2119 * Put the lowest 28 bits of timeout at
2120 * offset 4. Four first bits are used for id.
2121 */
2122 (u8)((timeout << 4) & 0xf0),
2123 (u8)((timeout >> 4) & 0xff),
2124 (u8)((timeout >> 12) & 0xff),
2125 (u8)((timeout >> 20) & 0xff));
2126}
Fabio Baltieri6f8cfa92013-01-18 12:40:12 +01002127EXPORT_SYMBOL(db8500_prcmu_load_a9wdog);
Jonas Aberg84165b82011-08-12 10:28:33 +02002128
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002129/**
Linus Walleije3726fc2010-08-19 12:36:01 +01002130 * prcmu_abb_read() - Read register value(s) from the ABB.
2131 * @slave: The I2C slave address.
2132 * @reg: The (start) register address.
2133 * @value: The read out value(s).
2134 * @size: The number of registers to read.
2135 *
2136 * Reads register value(s) from the ABB.
2137 * @size has to be 1 for the current firmware version.
2138 */
2139int prcmu_abb_read(u8 slave, u8 reg, u8 *value, u8 size)
2140{
2141 int r;
2142
2143 if (size != 1)
2144 return -EINVAL;
2145
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002146 mutex_lock(&mb5_transfer.lock);
Linus Walleije3726fc2010-08-19 12:36:01 +01002147
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002148 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(5))
Linus Walleije3726fc2010-08-19 12:36:01 +01002149 cpu_relax();
2150
Mattias Nilsson3c3e4892012-03-08 14:02:05 +01002151 writeb(0, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB5));
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002152 writeb(PRCMU_I2C_READ(slave), (tcdm_base + PRCM_REQ_MB5_I2C_SLAVE_OP));
2153 writeb(PRCMU_I2C_STOP_EN, (tcdm_base + PRCM_REQ_MB5_I2C_HW_BITS));
2154 writeb(reg, (tcdm_base + PRCM_REQ_MB5_I2C_REG));
2155 writeb(0, (tcdm_base + PRCM_REQ_MB5_I2C_VAL));
Linus Walleije3726fc2010-08-19 12:36:01 +01002156
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002157 writel(MBOX_BIT(5), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002158
Linus Walleije3726fc2010-08-19 12:36:01 +01002159 if (!wait_for_completion_timeout(&mb5_transfer.work,
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002160 msecs_to_jiffies(20000))) {
2161 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
2162 __func__);
Linus Walleije3726fc2010-08-19 12:36:01 +01002163 r = -EIO;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002164 } else {
2165 r = ((mb5_transfer.ack.status == I2C_RD_OK) ? 0 : -EIO);
Linus Walleije3726fc2010-08-19 12:36:01 +01002166 }
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002167
Linus Walleije3726fc2010-08-19 12:36:01 +01002168 if (!r)
2169 *value = mb5_transfer.ack.value;
2170
Linus Walleije3726fc2010-08-19 12:36:01 +01002171 mutex_unlock(&mb5_transfer.lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002172
Linus Walleije3726fc2010-08-19 12:36:01 +01002173 return r;
2174}
Linus Walleije3726fc2010-08-19 12:36:01 +01002175
2176/**
Mattias Nilsson3c3e4892012-03-08 14:02:05 +01002177 * prcmu_abb_write_masked() - Write masked register value(s) to the ABB.
Linus Walleije3726fc2010-08-19 12:36:01 +01002178 * @slave: The I2C slave address.
2179 * @reg: The (start) register address.
2180 * @value: The value(s) to write.
Mattias Nilsson3c3e4892012-03-08 14:02:05 +01002181 * @mask: The mask(s) to use.
Linus Walleije3726fc2010-08-19 12:36:01 +01002182 * @size: The number of registers to write.
2183 *
Mattias Nilsson3c3e4892012-03-08 14:02:05 +01002184 * Writes masked register value(s) to the ABB.
2185 * For each @value, only the bits set to 1 in the corresponding @mask
2186 * will be written. The other bits are not changed.
Linus Walleije3726fc2010-08-19 12:36:01 +01002187 * @size has to be 1 for the current firmware version.
2188 */
Mattias Nilsson3c3e4892012-03-08 14:02:05 +01002189int prcmu_abb_write_masked(u8 slave, u8 reg, u8 *value, u8 *mask, u8 size)
Linus Walleije3726fc2010-08-19 12:36:01 +01002190{
2191 int r;
2192
2193 if (size != 1)
2194 return -EINVAL;
2195
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002196 mutex_lock(&mb5_transfer.lock);
Linus Walleije3726fc2010-08-19 12:36:01 +01002197
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002198 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(5))
Linus Walleije3726fc2010-08-19 12:36:01 +01002199 cpu_relax();
2200
Mattias Nilsson3c3e4892012-03-08 14:02:05 +01002201 writeb(~*mask, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB5));
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002202 writeb(PRCMU_I2C_WRITE(slave), (tcdm_base + PRCM_REQ_MB5_I2C_SLAVE_OP));
2203 writeb(PRCMU_I2C_STOP_EN, (tcdm_base + PRCM_REQ_MB5_I2C_HW_BITS));
2204 writeb(reg, (tcdm_base + PRCM_REQ_MB5_I2C_REG));
2205 writeb(*value, (tcdm_base + PRCM_REQ_MB5_I2C_VAL));
Linus Walleije3726fc2010-08-19 12:36:01 +01002206
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002207 writel(MBOX_BIT(5), PRCM_MBOX_CPU_SET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002208
Linus Walleije3726fc2010-08-19 12:36:01 +01002209 if (!wait_for_completion_timeout(&mb5_transfer.work,
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002210 msecs_to_jiffies(20000))) {
2211 pr_err("prcmu: %s timed out (20 s) waiting for a reply.\n",
2212 __func__);
Linus Walleije3726fc2010-08-19 12:36:01 +01002213 r = -EIO;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002214 } else {
2215 r = ((mb5_transfer.ack.status == I2C_WR_OK) ? 0 : -EIO);
Linus Walleije3726fc2010-08-19 12:36:01 +01002216 }
Linus Walleije3726fc2010-08-19 12:36:01 +01002217
Linus Walleije3726fc2010-08-19 12:36:01 +01002218 mutex_unlock(&mb5_transfer.lock);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002219
Linus Walleije3726fc2010-08-19 12:36:01 +01002220 return r;
2221}
Linus Walleije3726fc2010-08-19 12:36:01 +01002222
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002223/**
Mattias Nilsson3c3e4892012-03-08 14:02:05 +01002224 * prcmu_abb_write() - Write register value(s) to the ABB.
2225 * @slave: The I2C slave address.
2226 * @reg: The (start) register address.
2227 * @value: The value(s) to write.
2228 * @size: The number of registers to write.
2229 *
2230 * Writes register value(s) to the ABB.
2231 * @size has to be 1 for the current firmware version.
2232 */
2233int prcmu_abb_write(u8 slave, u8 reg, u8 *value, u8 size)
2234{
2235 u8 mask = ~0;
2236
2237 return prcmu_abb_write_masked(slave, reg, value, &mask, size);
2238}
2239
2240/**
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002241 * prcmu_ac_wake_req - should be called whenever ARM wants to wakeup Modem
2242 */
Arun Murthy5261e102012-05-21 14:28:21 +05302243int prcmu_ac_wake_req(void)
Martin Perssone0befb22010-12-08 15:13:28 +01002244{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002245 u32 val;
Arun Murthy5261e102012-05-21 14:28:21 +05302246 int ret = 0;
Martin Perssone0befb22010-12-08 15:13:28 +01002247
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002248 mutex_lock(&mb0_transfer.ac_wake_lock);
Martin Perssone0befb22010-12-08 15:13:28 +01002249
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002250 val = readl(PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002251 if (val & PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ)
2252 goto unlock_and_return;
2253
2254 atomic_set(&ac_wake_req_state, 1);
2255
Arun Murthy5261e102012-05-21 14:28:21 +05302256 /*
2257 * Force Modem Wake-up before hostaccess_req ping-pong.
2258 * It prevents Modem to enter in Sleep while acking the hostaccess
2259 * request. The 31us delay has been calculated by HWI.
2260 */
2261 val |= PRCM_HOSTACCESS_REQ_WAKE_REQ;
2262 writel(val, PRCM_HOSTACCESS_REQ);
2263
2264 udelay(31);
2265
2266 val |= PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ;
2267 writel(val, PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002268
2269 if (!wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
Mattias Nilssond6e30022011-08-12 10:28:43 +02002270 msecs_to_jiffies(5000))) {
Linus Walleij57265bc2011-10-10 13:04:44 +02002271 pr_crit("prcmu: %s timed out (5 s) waiting for a reply.\n",
Mattias Nilssond6e30022011-08-12 10:28:43 +02002272 __func__);
Arun Murthy5261e102012-05-21 14:28:21 +05302273 ret = -EFAULT;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002274 }
2275
2276unlock_and_return:
2277 mutex_unlock(&mb0_transfer.ac_wake_lock);
Arun Murthy5261e102012-05-21 14:28:21 +05302278 return ret;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002279}
2280
2281/**
2282 * prcmu_ac_sleep_req - called when ARM no longer needs to talk to modem
2283 */
Sachin Kamatffb01162013-08-23 17:05:19 +05302284void prcmu_ac_sleep_req(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002285{
2286 u32 val;
2287
2288 mutex_lock(&mb0_transfer.ac_wake_lock);
2289
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002290 val = readl(PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002291 if (!(val & PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ))
2292 goto unlock_and_return;
2293
2294 writel((val & ~PRCM_HOSTACCESS_REQ_HOSTACCESS_REQ),
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002295 PRCM_HOSTACCESS_REQ);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002296
2297 if (!wait_for_completion_timeout(&mb0_transfer.ac_wake_work,
Mattias Nilssond6e30022011-08-12 10:28:43 +02002298 msecs_to_jiffies(5000))) {
Linus Walleij57265bc2011-10-10 13:04:44 +02002299 pr_crit("prcmu: %s timed out (5 s) waiting for a reply.\n",
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002300 __func__);
2301 }
2302
2303 atomic_set(&ac_wake_req_state, 0);
2304
2305unlock_and_return:
2306 mutex_unlock(&mb0_transfer.ac_wake_lock);
2307}
2308
Mattias Nilsson73180f82011-08-12 10:28:10 +02002309bool db8500_prcmu_is_ac_wake_requested(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002310{
2311 return (atomic_read(&ac_wake_req_state) != 0);
2312}
2313
2314/**
Mattias Nilsson73180f82011-08-12 10:28:10 +02002315 * db8500_prcmu_system_reset - System reset
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002316 *
Mattias Nilsson73180f82011-08-12 10:28:10 +02002317 * Saves the reset reason code and then sets the APE_SOFTRST register which
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002318 * fires interrupt to fw
2319 */
Mattias Nilsson73180f82011-08-12 10:28:10 +02002320void db8500_prcmu_system_reset(u16 reset_code)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002321{
2322 writew(reset_code, (tcdm_base + PRCM_SW_RST_REASON));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002323 writel(1, PRCM_APE_SOFTRST);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002324}
2325
2326/**
Sebastian Rasmussen597045d2011-08-12 10:28:53 +02002327 * db8500_prcmu_get_reset_code - Retrieve SW reset reason code
2328 *
2329 * Retrieves the reset reason code stored by prcmu_system_reset() before
2330 * last restart.
2331 */
2332u16 db8500_prcmu_get_reset_code(void)
2333{
2334 return readw(tcdm_base + PRCM_SW_RST_REASON);
2335}
2336
2337/**
Mattias Nilsson05089012012-01-13 16:20:20 +01002338 * db8500_prcmu_reset_modem - ask the PRCMU to reset modem
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002339 */
Mattias Nilsson05089012012-01-13 16:20:20 +01002340void db8500_prcmu_modem_reset(void)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002341{
Martin Perssone0befb22010-12-08 15:13:28 +01002342 mutex_lock(&mb1_transfer.lock);
2343
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002344 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(1))
Martin Perssone0befb22010-12-08 15:13:28 +01002345 cpu_relax();
2346
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002347 writeb(MB1H_RESET_MODEM, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB1));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002348 writel(MBOX_BIT(1), PRCM_MBOX_CPU_SET);
Martin Perssone0befb22010-12-08 15:13:28 +01002349 wait_for_completion(&mb1_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002350
2351 /*
2352 * No need to check return from PRCMU as modem should go in reset state
2353 * This state is already managed by upper layer
2354 */
Martin Perssone0befb22010-12-08 15:13:28 +01002355
2356 mutex_unlock(&mb1_transfer.lock);
Martin Perssone0befb22010-12-08 15:13:28 +01002357}
2358
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002359static void ack_dbb_wakeup(void)
Martin Perssone0befb22010-12-08 15:13:28 +01002360{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002361 unsigned long flags;
Martin Perssone0befb22010-12-08 15:13:28 +01002362
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002363 spin_lock_irqsave(&mb0_transfer.lock, flags);
Martin Perssone0befb22010-12-08 15:13:28 +01002364
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002365 while (readl(PRCM_MBOX_CPU_VAL) & MBOX_BIT(0))
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002366 cpu_relax();
Martin Perssone0befb22010-12-08 15:13:28 +01002367
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002368 writeb(MB0H_READ_WAKEUP_ACK, (tcdm_base + PRCM_MBOX_HEADER_REQ_MB0));
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002369 writel(MBOX_BIT(0), PRCM_MBOX_CPU_SET);
Martin Perssone0befb22010-12-08 15:13:28 +01002370
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002371 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
Martin Perssone0befb22010-12-08 15:13:28 +01002372}
2373
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002374static inline void print_unknown_header_warning(u8 n, u8 header)
Linus Walleije3726fc2010-08-19 12:36:01 +01002375{
Joe Perches81d30ed2017-02-16 23:11:36 -08002376 pr_warn("prcmu: Unknown message header (%d) in mailbox %d\n",
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002377 header, n);
Linus Walleije3726fc2010-08-19 12:36:01 +01002378}
2379
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002380static bool read_mailbox_0(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002381{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002382 bool r;
2383 u32 ev;
2384 unsigned int n;
2385 u8 header;
2386
2387 header = readb(tcdm_base + PRCM_MBOX_HEADER_ACK_MB0);
2388 switch (header) {
2389 case MB0H_WAKEUP_EXE:
2390 case MB0H_WAKEUP_SLEEP:
2391 if (readb(tcdm_base + PRCM_ACK_MB0_READ_POINTER) & 1)
2392 ev = readl(tcdm_base + PRCM_ACK_MB0_WAKEUP_1_8500);
2393 else
2394 ev = readl(tcdm_base + PRCM_ACK_MB0_WAKEUP_0_8500);
2395
2396 if (ev & (WAKEUP_BIT_AC_WAKE_ACK | WAKEUP_BIT_AC_SLEEP_ACK))
2397 complete(&mb0_transfer.ac_wake_work);
2398 if (ev & WAKEUP_BIT_SYSCLK_OK)
2399 complete(&mb3_transfer.sysclk_work);
2400
2401 ev &= mb0_transfer.req.dbb_irqs;
2402
2403 for (n = 0; n < NUM_PRCMU_WAKEUPS; n++) {
2404 if (ev & prcmu_irq_bit[n])
Linus Walleij89d9b1c2012-12-20 10:20:15 +01002405 generic_handle_irq(irq_find_mapping(db8500_irq_domain, n));
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002406 }
2407 r = true;
2408 break;
2409 default:
2410 print_unknown_header_warning(0, header);
2411 r = false;
2412 break;
2413 }
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002414 writel(MBOX_BIT(0), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002415 return r;
2416}
2417
2418static bool read_mailbox_1(void)
2419{
2420 mb1_transfer.ack.header = readb(tcdm_base + PRCM_MBOX_HEADER_REQ_MB1);
2421 mb1_transfer.ack.arm_opp = readb(tcdm_base +
2422 PRCM_ACK_MB1_CURRENT_ARM_OPP);
2423 mb1_transfer.ack.ape_opp = readb(tcdm_base +
2424 PRCM_ACK_MB1_CURRENT_APE_OPP);
2425 mb1_transfer.ack.ape_voltage_status = readb(tcdm_base +
2426 PRCM_ACK_MB1_APE_VOLTAGE_STATUS);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002427 writel(MBOX_BIT(1), PRCM_ARM_IT1_CLR);
Martin Perssone0befb22010-12-08 15:13:28 +01002428 complete(&mb1_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002429 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002430}
2431
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002432static bool read_mailbox_2(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002433{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002434 mb2_transfer.ack.status = readb(tcdm_base + PRCM_ACK_MB2_DPS_STATUS);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002435 writel(MBOX_BIT(2), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002436 complete(&mb2_transfer.work);
2437 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002438}
2439
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002440static bool read_mailbox_3(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002441{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002442 writel(MBOX_BIT(3), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002443 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002444}
2445
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002446static bool read_mailbox_4(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002447{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002448 u8 header;
2449 bool do_complete = true;
2450
2451 header = readb(tcdm_base + PRCM_MBOX_HEADER_REQ_MB4);
2452 switch (header) {
2453 case MB4H_MEM_ST:
2454 case MB4H_HOTDOG:
2455 case MB4H_HOTMON:
2456 case MB4H_HOT_PERIOD:
Mattias Nilssona592c2e2011-08-12 10:27:41 +02002457 case MB4H_A9WDOG_CONF:
2458 case MB4H_A9WDOG_EN:
2459 case MB4H_A9WDOG_DIS:
2460 case MB4H_A9WDOG_LOAD:
2461 case MB4H_A9WDOG_KICK:
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002462 break;
2463 default:
2464 print_unknown_header_warning(4, header);
2465 do_complete = false;
2466 break;
2467 }
2468
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002469 writel(MBOX_BIT(4), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002470
2471 if (do_complete)
2472 complete(&mb4_transfer.work);
2473
2474 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002475}
2476
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002477static bool read_mailbox_5(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002478{
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002479 mb5_transfer.ack.status = readb(tcdm_base + PRCM_ACK_MB5_I2C_STATUS);
2480 mb5_transfer.ack.value = readb(tcdm_base + PRCM_ACK_MB5_I2C_VAL);
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002481 writel(MBOX_BIT(5), PRCM_ARM_IT1_CLR);
Linus Walleije3726fc2010-08-19 12:36:01 +01002482 complete(&mb5_transfer.work);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002483 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002484}
2485
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002486static bool read_mailbox_6(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002487{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002488 writel(MBOX_BIT(6), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002489 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002490}
2491
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002492static bool read_mailbox_7(void)
Linus Walleije3726fc2010-08-19 12:36:01 +01002493{
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002494 writel(MBOX_BIT(7), PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002495 return false;
Linus Walleije3726fc2010-08-19 12:36:01 +01002496}
2497
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002498static bool (* const read_mailbox[NUM_MB])(void) = {
Linus Walleije3726fc2010-08-19 12:36:01 +01002499 read_mailbox_0,
2500 read_mailbox_1,
2501 read_mailbox_2,
2502 read_mailbox_3,
2503 read_mailbox_4,
2504 read_mailbox_5,
2505 read_mailbox_6,
2506 read_mailbox_7
2507};
2508
2509static irqreturn_t prcmu_irq_handler(int irq, void *data)
2510{
2511 u32 bits;
2512 u8 n;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002513 irqreturn_t r;
Linus Walleije3726fc2010-08-19 12:36:01 +01002514
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02002515 bits = (readl(PRCM_ARM_IT1_VAL) & ALL_MBOX_BITS);
Linus Walleije3726fc2010-08-19 12:36:01 +01002516 if (unlikely(!bits))
2517 return IRQ_NONE;
2518
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002519 r = IRQ_HANDLED;
Linus Walleije3726fc2010-08-19 12:36:01 +01002520 for (n = 0; bits; n++) {
2521 if (bits & MBOX_BIT(n)) {
2522 bits -= MBOX_BIT(n);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002523 if (read_mailbox[n]())
2524 r = IRQ_WAKE_THREAD;
Linus Walleije3726fc2010-08-19 12:36:01 +01002525 }
2526 }
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002527 return r;
2528}
2529
2530static irqreturn_t prcmu_irq_thread_fn(int irq, void *data)
2531{
2532 ack_dbb_wakeup();
Linus Walleije3726fc2010-08-19 12:36:01 +01002533 return IRQ_HANDLED;
2534}
2535
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002536static void prcmu_mask_work(struct work_struct *work)
2537{
2538 unsigned long flags;
2539
2540 spin_lock_irqsave(&mb0_transfer.lock, flags);
2541
2542 config_wakeups();
2543
2544 spin_unlock_irqrestore(&mb0_transfer.lock, flags);
2545}
2546
2547static void prcmu_irq_mask(struct irq_data *d)
2548{
2549 unsigned long flags;
2550
2551 spin_lock_irqsave(&mb0_transfer.dbb_irqs_lock, flags);
2552
Lee Jonesf3f1f0a2012-09-24 09:11:46 +01002553 mb0_transfer.req.dbb_irqs &= ~prcmu_irq_bit[d->hwirq];
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002554
2555 spin_unlock_irqrestore(&mb0_transfer.dbb_irqs_lock, flags);
2556
2557 if (d->irq != IRQ_PRCMU_CA_SLEEP)
2558 schedule_work(&mb0_transfer.mask_work);
2559}
2560
2561static void prcmu_irq_unmask(struct irq_data *d)
2562{
2563 unsigned long flags;
2564
2565 spin_lock_irqsave(&mb0_transfer.dbb_irqs_lock, flags);
2566
Lee Jonesf3f1f0a2012-09-24 09:11:46 +01002567 mb0_transfer.req.dbb_irqs |= prcmu_irq_bit[d->hwirq];
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002568
2569 spin_unlock_irqrestore(&mb0_transfer.dbb_irqs_lock, flags);
2570
2571 if (d->irq != IRQ_PRCMU_CA_SLEEP)
2572 schedule_work(&mb0_transfer.mask_work);
2573}
2574
2575static void noop(struct irq_data *d)
2576{
2577}
2578
2579static struct irq_chip prcmu_irq_chip = {
2580 .name = "prcmu",
2581 .irq_disable = prcmu_irq_mask,
2582 .irq_ack = noop,
2583 .irq_mask = prcmu_irq_mask,
2584 .irq_unmask = prcmu_irq_unmask,
2585};
2586
Linus Walleij05ec2602013-02-07 10:17:31 +01002587static __init char *fw_project_name(u32 project)
Mattias Nilssonb58d12f2012-01-13 16:20:10 +01002588{
2589 switch (project) {
2590 case PRCMU_FW_PROJECT_U8500:
2591 return "U8500";
Linus Walleij05ec2602013-02-07 10:17:31 +01002592 case PRCMU_FW_PROJECT_U8400:
2593 return "U8400";
Mattias Nilssonb58d12f2012-01-13 16:20:10 +01002594 case PRCMU_FW_PROJECT_U9500:
2595 return "U9500";
Linus Walleij05ec2602013-02-07 10:17:31 +01002596 case PRCMU_FW_PROJECT_U8500_MBB:
2597 return "U8500 MBB";
2598 case PRCMU_FW_PROJECT_U8500_C1:
2599 return "U8500 C1";
2600 case PRCMU_FW_PROJECT_U8500_C2:
2601 return "U8500 C2";
2602 case PRCMU_FW_PROJECT_U8500_C3:
2603 return "U8500 C3";
2604 case PRCMU_FW_PROJECT_U8500_C4:
2605 return "U8500 C4";
2606 case PRCMU_FW_PROJECT_U9500_MBL:
2607 return "U9500 MBL";
2608 case PRCMU_FW_PROJECT_U8500_MBL:
2609 return "U8500 MBL";
2610 case PRCMU_FW_PROJECT_U8500_MBL2:
2611 return "U8500 MBL2";
Bengt Jonsson5f96a1a62012-03-15 19:50:40 +01002612 case PRCMU_FW_PROJECT_U8520:
Linus Walleij05ec2602013-02-07 10:17:31 +01002613 return "U8520 MBL";
Bengt Jonsson1927ddf2012-03-15 19:50:51 +01002614 case PRCMU_FW_PROJECT_U8420:
2615 return "U8420";
Linus Walleij05ec2602013-02-07 10:17:31 +01002616 case PRCMU_FW_PROJECT_U9540:
2617 return "U9540";
2618 case PRCMU_FW_PROJECT_A9420:
2619 return "A9420";
2620 case PRCMU_FW_PROJECT_L8540:
2621 return "L8540";
2622 case PRCMU_FW_PROJECT_L8580:
2623 return "L8580";
Mattias Nilssonb58d12f2012-01-13 16:20:10 +01002624 default:
2625 return "Unknown";
2626 }
2627}
2628
Lee Jonesf3f1f0a2012-09-24 09:11:46 +01002629static int db8500_irq_map(struct irq_domain *d, unsigned int virq,
2630 irq_hw_number_t hwirq)
2631{
2632 irq_set_chip_and_handler(virq, &prcmu_irq_chip,
2633 handle_simple_irq);
Lee Jonesf3f1f0a2012-09-24 09:11:46 +01002634
2635 return 0;
2636}
2637
Krzysztof Kozlowski7ce7b262015-04-27 21:54:13 +09002638static const struct irq_domain_ops db8500_irq_ops = {
Linus Walleij89d9b1c2012-12-20 10:20:15 +01002639 .map = db8500_irq_map,
2640 .xlate = irq_domain_xlate_twocell,
Lee Jonesf3f1f0a2012-09-24 09:11:46 +01002641};
2642
Linus Walleijf864c462014-02-04 00:35:56 +01002643static int db8500_irq_init(struct device_node *np)
Lee Jonesf3f1f0a2012-09-24 09:11:46 +01002644{
Linus Walleij89d9b1c2012-12-20 10:20:15 +01002645 int i;
Linus Walleija7238e42012-10-18 18:22:11 +02002646
Linus Walleija7238e42012-10-18 18:22:11 +02002647 db8500_irq_domain = irq_domain_add_simple(
Linus Walleijf864c462014-02-04 00:35:56 +01002648 np, NUM_PRCMU_WAKEUPS, 0,
Linus Walleija7238e42012-10-18 18:22:11 +02002649 &db8500_irq_ops, NULL);
Lee Jonesf3f1f0a2012-09-24 09:11:46 +01002650
2651 if (!db8500_irq_domain) {
2652 pr_err("Failed to create irqdomain\n");
2653 return -ENOSYS;
2654 }
2655
Linus Walleij89d9b1c2012-12-20 10:20:15 +01002656 /* All wakeups will be used, so create mappings for all */
2657 for (i = 0; i < NUM_PRCMU_WAKEUPS; i++)
2658 irq_create_mapping(db8500_irq_domain, i);
2659
Lee Jonesf3f1f0a2012-09-24 09:11:46 +01002660 return 0;
2661}
2662
Linus Walleij05ec2602013-02-07 10:17:31 +01002663static void dbx500_fw_version_init(struct platform_device *pdev,
2664 u32 version_offset)
2665{
2666 struct resource *res;
2667 void __iomem *tcpm_base;
Lee Jones741cdec2013-04-04 11:39:00 +01002668 u32 version;
Linus Walleij05ec2602013-02-07 10:17:31 +01002669
2670 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2671 "prcmu-tcpm");
2672 if (!res) {
2673 dev_err(&pdev->dev,
2674 "Error: no prcmu tcpm memory region provided\n");
2675 return;
2676 }
2677 tcpm_base = ioremap(res->start, resource_size(res));
Lee Jones741cdec2013-04-04 11:39:00 +01002678 if (!tcpm_base) {
2679 dev_err(&pdev->dev, "no prcmu tcpm mem region provided\n");
2680 return;
Linus Walleij05ec2602013-02-07 10:17:31 +01002681 }
Lee Jones741cdec2013-04-04 11:39:00 +01002682
2683 version = readl(tcpm_base + version_offset);
2684 fw_info.version.project = (version & 0xFF);
2685 fw_info.version.api_version = (version >> 8) & 0xFF;
2686 fw_info.version.func_version = (version >> 16) & 0xFF;
2687 fw_info.version.errata = (version >> 24) & 0xFF;
2688 strncpy(fw_info.version.project_name,
2689 fw_project_name(fw_info.version.project),
2690 PRCMU_FW_PROJECT_NAME_LEN);
2691 fw_info.valid = true;
2692 pr_info("PRCMU firmware: %s(%d), version %d.%d.%d\n",
2693 fw_info.version.project_name,
2694 fw_info.version.project,
2695 fw_info.version.api_version,
2696 fw_info.version.func_version,
2697 fw_info.version.errata);
2698 iounmap(tcpm_base);
Linus Walleij05ec2602013-02-07 10:17:31 +01002699}
2700
Linus Walleij9a47a8d2013-03-21 12:27:25 +01002701void __init db8500_prcmu_early_init(u32 phy_base, u32 size)
Mattias Wallinfcbd4582010-12-02 16:20:42 +01002702{
Linus Walleij9a47a8d2013-03-21 12:27:25 +01002703 /*
2704 * This is a temporary remap to bring up the clocks. It is
2705 * subsequently replaces with a real remap. After the merge of
2706 * the mailbox subsystem all of this early code goes away, and the
2707 * clock driver can probe independently. An early initcall will
2708 * still be needed, but it can be diverted into drivers/clk/ux500.
2709 */
2710 prcmu_base = ioremap(phy_base, size);
2711 if (!prcmu_base)
2712 pr_err("%s: ioremap() of prcmu registers failed!\n", __func__);
2713
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002714 spin_lock_init(&mb0_transfer.lock);
2715 spin_lock_init(&mb0_transfer.dbb_irqs_lock);
2716 mutex_init(&mb0_transfer.ac_wake_lock);
2717 init_completion(&mb0_transfer.ac_wake_work);
Martin Perssone0befb22010-12-08 15:13:28 +01002718 mutex_init(&mb1_transfer.lock);
2719 init_completion(&mb1_transfer.work);
Mattias Nilsson4d64d2e2012-01-13 16:20:43 +01002720 mb1_transfer.ape_opp = APE_NO_CHANGE;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002721 mutex_init(&mb2_transfer.lock);
2722 init_completion(&mb2_transfer.work);
2723 spin_lock_init(&mb2_transfer.auto_pm_lock);
2724 spin_lock_init(&mb3_transfer.lock);
2725 mutex_init(&mb3_transfer.sysclk_lock);
2726 init_completion(&mb3_transfer.sysclk_work);
2727 mutex_init(&mb4_transfer.lock);
2728 init_completion(&mb4_transfer.work);
Linus Walleije3726fc2010-08-19 12:36:01 +01002729 mutex_init(&mb5_transfer.lock);
2730 init_completion(&mb5_transfer.work);
2731
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02002732 INIT_WORK(&mb0_transfer.mask_work, prcmu_mask_work);
Linus Walleije3726fc2010-08-19 12:36:01 +01002733}
2734
Mattias Nilsson05089012012-01-13 16:20:20 +01002735static void __init init_prcm_registers(void)
Mattias Nilssond65e12d2011-08-12 10:27:50 +02002736{
2737 u32 val;
2738
2739 val = readl(PRCM_A9PL_FORCE_CLKEN);
2740 val &= ~(PRCM_A9PL_FORCE_CLKEN_PRCM_A9PL_FORCE_CLKEN |
2741 PRCM_A9PL_FORCE_CLKEN_PRCM_A9AXI_FORCE_CLKEN);
2742 writel(val, (PRCM_A9PL_FORCE_CLKEN));
2743}
2744
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002745/*
2746 * Power domain switches (ePODs) modeled as regulators for the DB8500 SoC
2747 */
2748static struct regulator_consumer_supply db8500_vape_consumers[] = {
2749 REGULATOR_SUPPLY("v-ape", NULL),
2750 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.0"),
2751 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.1"),
2752 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.2"),
2753 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.3"),
Lee Jonesae8406352012-05-04 19:23:20 +01002754 REGULATOR_SUPPLY("v-i2c", "nmk-i2c.4"),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002755 /* "v-mmc" changed to "vcore" in the mainline kernel */
2756 REGULATOR_SUPPLY("vcore", "sdi0"),
2757 REGULATOR_SUPPLY("vcore", "sdi1"),
2758 REGULATOR_SUPPLY("vcore", "sdi2"),
2759 REGULATOR_SUPPLY("vcore", "sdi3"),
2760 REGULATOR_SUPPLY("vcore", "sdi4"),
2761 REGULATOR_SUPPLY("v-dma", "dma40.0"),
2762 REGULATOR_SUPPLY("v-ape", "ab8500-usb.0"),
2763 /* "v-uart" changed to "vcore" in the mainline kernel */
2764 REGULATOR_SUPPLY("vcore", "uart0"),
2765 REGULATOR_SUPPLY("vcore", "uart1"),
2766 REGULATOR_SUPPLY("vcore", "uart2"),
2767 REGULATOR_SUPPLY("v-ape", "nmk-ske-keypad.0"),
Bengt Jonsson992b1332012-01-13 16:20:36 +01002768 REGULATOR_SUPPLY("v-hsi", "ste_hsi.0"),
Lee Jonesbc367482012-05-03 11:23:47 +01002769 REGULATOR_SUPPLY("vddvario", "smsc911x.0"),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002770};
2771
2772static struct regulator_consumer_supply db8500_vsmps2_consumers[] = {
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002773 REGULATOR_SUPPLY("musb_1v8", "ab8500-usb.0"),
2774 /* AV8100 regulator */
2775 REGULATOR_SUPPLY("hdmi_1v8", "0-0070"),
2776};
2777
2778static struct regulator_consumer_supply db8500_b2r2_mcde_consumers[] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002779 REGULATOR_SUPPLY("vsupply", "b2r2_bus"),
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002780 REGULATOR_SUPPLY("vsupply", "mcde"),
2781};
2782
2783/* SVA MMDSP regulator switch */
2784static struct regulator_consumer_supply db8500_svammdsp_consumers[] = {
2785 REGULATOR_SUPPLY("sva-mmdsp", "cm_control"),
2786};
2787
2788/* SVA pipe regulator switch */
2789static struct regulator_consumer_supply db8500_svapipe_consumers[] = {
2790 REGULATOR_SUPPLY("sva-pipe", "cm_control"),
2791};
2792
2793/* SIA MMDSP regulator switch */
2794static struct regulator_consumer_supply db8500_siammdsp_consumers[] = {
2795 REGULATOR_SUPPLY("sia-mmdsp", "cm_control"),
2796};
2797
2798/* SIA pipe regulator switch */
2799static struct regulator_consumer_supply db8500_siapipe_consumers[] = {
2800 REGULATOR_SUPPLY("sia-pipe", "cm_control"),
2801};
2802
2803static struct regulator_consumer_supply db8500_sga_consumers[] = {
2804 REGULATOR_SUPPLY("v-mali", NULL),
2805};
2806
2807/* ESRAM1 and 2 regulator switch */
2808static struct regulator_consumer_supply db8500_esram12_consumers[] = {
2809 REGULATOR_SUPPLY("esram12", "cm_control"),
2810};
2811
2812/* ESRAM3 and 4 regulator switch */
2813static struct regulator_consumer_supply db8500_esram34_consumers[] = {
2814 REGULATOR_SUPPLY("v-esram34", "mcde"),
2815 REGULATOR_SUPPLY("esram34", "cm_control"),
Bengt Jonsson992b1332012-01-13 16:20:36 +01002816 REGULATOR_SUPPLY("lcla_esram", "dma40.0"),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002817};
2818
2819static struct regulator_init_data db8500_regulators[DB8500_NUM_REGULATORS] = {
2820 [DB8500_REGULATOR_VAPE] = {
2821 .constraints = {
2822 .name = "db8500-vape",
2823 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
Mark Brown1e458602012-04-13 13:11:50 +01002824 .always_on = true,
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002825 },
2826 .consumer_supplies = db8500_vape_consumers,
2827 .num_consumer_supplies = ARRAY_SIZE(db8500_vape_consumers),
2828 },
2829 [DB8500_REGULATOR_VARM] = {
2830 .constraints = {
2831 .name = "db8500-varm",
2832 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2833 },
2834 },
2835 [DB8500_REGULATOR_VMODEM] = {
2836 .constraints = {
2837 .name = "db8500-vmodem",
2838 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2839 },
2840 },
2841 [DB8500_REGULATOR_VPLL] = {
2842 .constraints = {
2843 .name = "db8500-vpll",
2844 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2845 },
2846 },
2847 [DB8500_REGULATOR_VSMPS1] = {
2848 .constraints = {
2849 .name = "db8500-vsmps1",
2850 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2851 },
2852 },
2853 [DB8500_REGULATOR_VSMPS2] = {
2854 .constraints = {
2855 .name = "db8500-vsmps2",
2856 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2857 },
2858 .consumer_supplies = db8500_vsmps2_consumers,
2859 .num_consumer_supplies = ARRAY_SIZE(db8500_vsmps2_consumers),
2860 },
2861 [DB8500_REGULATOR_VSMPS3] = {
2862 .constraints = {
2863 .name = "db8500-vsmps3",
2864 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2865 },
2866 },
2867 [DB8500_REGULATOR_VRF1] = {
2868 .constraints = {
2869 .name = "db8500-vrf1",
2870 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2871 },
2872 },
2873 [DB8500_REGULATOR_SWITCH_SVAMMDSP] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002874 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002875 .constraints = {
2876 .name = "db8500-sva-mmdsp",
2877 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2878 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002879 .consumer_supplies = db8500_svammdsp_consumers,
2880 .num_consumer_supplies = ARRAY_SIZE(db8500_svammdsp_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002881 },
2882 [DB8500_REGULATOR_SWITCH_SVAMMDSPRET] = {
2883 .constraints = {
2884 /* "ret" means "retention" */
2885 .name = "db8500-sva-mmdsp-ret",
2886 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2887 },
2888 },
2889 [DB8500_REGULATOR_SWITCH_SVAPIPE] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002890 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002891 .constraints = {
2892 .name = "db8500-sva-pipe",
2893 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2894 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002895 .consumer_supplies = db8500_svapipe_consumers,
2896 .num_consumer_supplies = ARRAY_SIZE(db8500_svapipe_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002897 },
2898 [DB8500_REGULATOR_SWITCH_SIAMMDSP] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002899 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002900 .constraints = {
2901 .name = "db8500-sia-mmdsp",
2902 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2903 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002904 .consumer_supplies = db8500_siammdsp_consumers,
2905 .num_consumer_supplies = ARRAY_SIZE(db8500_siammdsp_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002906 },
2907 [DB8500_REGULATOR_SWITCH_SIAMMDSPRET] = {
2908 .constraints = {
2909 .name = "db8500-sia-mmdsp-ret",
2910 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2911 },
2912 },
2913 [DB8500_REGULATOR_SWITCH_SIAPIPE] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002914 /* dependency to u8500-vape is handled outside regulator framework */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002915 .constraints = {
2916 .name = "db8500-sia-pipe",
2917 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2918 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002919 .consumer_supplies = db8500_siapipe_consumers,
2920 .num_consumer_supplies = ARRAY_SIZE(db8500_siapipe_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002921 },
2922 [DB8500_REGULATOR_SWITCH_SGA] = {
2923 .supply_regulator = "db8500-vape",
2924 .constraints = {
2925 .name = "db8500-sga",
2926 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2927 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002928 .consumer_supplies = db8500_sga_consumers,
2929 .num_consumer_supplies = ARRAY_SIZE(db8500_sga_consumers),
2930
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002931 },
2932 [DB8500_REGULATOR_SWITCH_B2R2_MCDE] = {
2933 .supply_regulator = "db8500-vape",
2934 .constraints = {
2935 .name = "db8500-b2r2-mcde",
2936 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2937 },
2938 .consumer_supplies = db8500_b2r2_mcde_consumers,
2939 .num_consumer_supplies = ARRAY_SIZE(db8500_b2r2_mcde_consumers),
2940 },
2941 [DB8500_REGULATOR_SWITCH_ESRAM12] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002942 /*
2943 * esram12 is set in retention and supplied by Vsafe when Vape is off,
2944 * no need to hold Vape
2945 */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002946 .constraints = {
2947 .name = "db8500-esram12",
2948 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2949 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002950 .consumer_supplies = db8500_esram12_consumers,
2951 .num_consumer_supplies = ARRAY_SIZE(db8500_esram12_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002952 },
2953 [DB8500_REGULATOR_SWITCH_ESRAM12RET] = {
2954 .constraints = {
2955 .name = "db8500-esram12-ret",
2956 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2957 },
2958 },
2959 [DB8500_REGULATOR_SWITCH_ESRAM34] = {
Bengt Jonsson992b1332012-01-13 16:20:36 +01002960 /*
2961 * esram34 is set in retention and supplied by Vsafe when Vape is off,
2962 * no need to hold Vape
2963 */
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002964 .constraints = {
2965 .name = "db8500-esram34",
2966 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2967 },
Bengt Jonsson624e87c2011-08-12 10:29:02 +02002968 .consumer_supplies = db8500_esram34_consumers,
2969 .num_consumer_supplies = ARRAY_SIZE(db8500_esram34_consumers),
Bengt Jonsson1032fbf2011-04-01 14:43:33 +02002970 },
2971 [DB8500_REGULATOR_SWITCH_ESRAM34RET] = {
2972 .constraints = {
2973 .name = "db8500-esram34-ret",
2974 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
2975 },
2976 },
2977};
2978
Fabio Baltierib3aac622013-01-18 12:40:14 +01002979static struct ux500_wdt_data db8500_wdt_pdata = {
2980 .timeout = 600, /* 10 minutes */
2981 .has_28_bits_resolution = true,
2982};
Arnd Bergmann55b175d2013-03-21 22:51:07 +01002983/*
2984 * Thermal Sensor
2985 */
2986
2987static struct resource db8500_thsens_resources[] = {
2988 {
2989 .name = "IRQ_HOTMON_LOW",
2990 .start = IRQ_PRCMU_HOTMON_LOW,
2991 .end = IRQ_PRCMU_HOTMON_LOW,
2992 .flags = IORESOURCE_IRQ,
2993 },
2994 {
2995 .name = "IRQ_HOTMON_HIGH",
2996 .start = IRQ_PRCMU_HOTMON_HIGH,
2997 .end = IRQ_PRCMU_HOTMON_HIGH,
2998 .flags = IORESOURCE_IRQ,
2999 },
3000};
3001
3002static struct db8500_thsens_platform_data db8500_thsens_data = {
3003 .trip_points[0] = {
3004 .temp = 70000,
3005 .type = THERMAL_TRIP_ACTIVE,
3006 .cdev_name = {
3007 [0] = "thermal-cpufreq-0",
3008 },
3009 },
3010 .trip_points[1] = {
3011 .temp = 75000,
3012 .type = THERMAL_TRIP_ACTIVE,
3013 .cdev_name = {
3014 [0] = "thermal-cpufreq-0",
3015 },
3016 },
3017 .trip_points[2] = {
3018 .temp = 80000,
3019 .type = THERMAL_TRIP_ACTIVE,
3020 .cdev_name = {
3021 [0] = "thermal-cpufreq-0",
3022 },
3023 },
3024 .trip_points[3] = {
3025 .temp = 85000,
3026 .type = THERMAL_TRIP_CRITICAL,
3027 },
3028 .num_trips = 4,
3029};
Fabio Baltierib3aac622013-01-18 12:40:14 +01003030
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +01003031static const struct mfd_cell common_prcmu_devs[] = {
Lee Jonesd98a5382013-04-09 20:52:58 +01003032 {
3033 .name = "ux500_wdt",
3034 .platform_data = &db8500_wdt_pdata,
3035 .pdata_size = sizeof(db8500_wdt_pdata),
3036 .id = -1,
3037 },
3038};
3039
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +01003040static const struct mfd_cell db8500_prcmu_devs[] = {
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003041 {
3042 .name = "db8500-prcmu-regulators",
Lee Jones5d903222012-06-20 13:56:41 +01003043 .of_compatible = "stericsson,db8500-prcmu-regulator",
Mattias Wallin1ed78912011-05-27 11:49:43 +02003044 .platform_data = &db8500_regulators,
3045 .pdata_size = sizeof(db8500_regulators),
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003046 },
3047 {
Linus Walleij80253952013-07-10 15:35:26 +02003048 .name = "cpuidle-dbx500",
3049 .of_compatible = "stericsson,cpuidle-dbx500",
3050 },
3051 {
Arnd Bergmann55b175d2013-03-21 22:51:07 +01003052 .name = "db8500-thermal",
3053 .num_resources = ARRAY_SIZE(db8500_thsens_resources),
3054 .resources = db8500_thsens_resources,
3055 .platform_data = &db8500_thsens_data,
Lee Jonesa3ef0de2013-05-07 12:01:32 +01003056 .pdata_size = sizeof(db8500_thsens_data),
Lee Jones6d11d132012-06-29 17:13:35 +02003057 },
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003058};
3059
Arnd Bergmann4e657942016-03-18 16:53:31 +01003060static int db8500_prcmu_register_ab8500(struct device *parent)
Arnd Bergmann55b175d2013-03-21 22:51:07 +01003061{
Linus Walleijf864c462014-02-04 00:35:56 +01003062 struct device_node *np;
3063 struct resource ab8500_resource;
Krzysztof Kozlowski5785a972014-05-13 12:58:41 +02003064 const struct mfd_cell ab8500_cell = {
Arnd Bergmann55b175d2013-03-21 22:51:07 +01003065 .name = "ab8500-core",
3066 .of_compatible = "stericsson,ab8500",
3067 .id = AB8500_VERSION_AB8500,
Arnd Bergmann55b175d2013-03-21 22:51:07 +01003068 .resources = &ab8500_resource,
3069 .num_resources = 1,
3070 };
3071
Linus Walleijf864c462014-02-04 00:35:56 +01003072 if (!parent->of_node)
3073 return -ENODEV;
3074
3075 /* Look up the device node, sneak the IRQ out of it */
3076 for_each_child_of_node(parent->of_node, np) {
3077 if (of_device_is_compatible(np, ab8500_cell.of_compatible))
3078 break;
3079 }
3080 if (!np) {
3081 dev_info(parent, "could not find AB8500 node in the device tree\n");
3082 return -ENODEV;
3083 }
3084 of_irq_to_resource_table(np, &ab8500_resource, 1);
3085
Arnd Bergmann55b175d2013-03-21 22:51:07 +01003086 return mfd_add_devices(parent, 0, &ab8500_cell, 1, NULL, 0, NULL);
3087}
3088
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003089/**
3090 * prcmu_fw_init - arch init call for the Linux PRCMU fw init logic
3091 *
3092 */
Bill Pembertonf791be42012-11-19 13:23:04 -05003093static int db8500_prcmu_probe(struct platform_device *pdev)
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003094{
Lee Jonesca7edd12012-05-09 17:19:25 +02003095 struct device_node *np = pdev->dev.of_node;
Arnd Bergmann55b175d2013-03-21 22:51:07 +01003096 int irq = 0, err = 0;
Linus Walleij05ec2602013-02-07 10:17:31 +01003097 struct resource *res;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003098
Linus Walleijb047d982013-03-19 14:21:47 +01003099 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "prcmu");
3100 if (!res) {
3101 dev_err(&pdev->dev, "no prcmu memory region provided\n");
Lee Jones6bdf8912014-11-03 16:12:26 +00003102 return -EINVAL;
Linus Walleijb047d982013-03-19 14:21:47 +01003103 }
3104 prcmu_base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
3105 if (!prcmu_base) {
3106 dev_err(&pdev->dev,
3107 "failed to ioremap prcmu register memory\n");
Lee Jones6bdf8912014-11-03 16:12:26 +00003108 return -ENOMEM;
Linus Walleijb047d982013-03-19 14:21:47 +01003109 }
Mattias Nilsson05089012012-01-13 16:20:20 +01003110 init_prcm_registers();
Arnd Bergmann4e657942016-03-18 16:53:31 +01003111 dbx500_fw_version_init(pdev, DB8500_PRCMU_FW_VERSION_OFFSET);
Linus Walleij05ec2602013-02-07 10:17:31 +01003112 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "prcmu-tcdm");
3113 if (!res) {
3114 dev_err(&pdev->dev, "no prcmu tcdm region provided\n");
Lee Jones6bdf8912014-11-03 16:12:26 +00003115 return -EINVAL;
Linus Walleij05ec2602013-02-07 10:17:31 +01003116 }
3117 tcdm_base = devm_ioremap(&pdev->dev, res->start,
3118 resource_size(res));
Pramod Gurav51a7e022014-10-30 14:51:35 +05303119 if (!tcdm_base) {
3120 dev_err(&pdev->dev,
3121 "failed to ioremap prcmu-tcdm register memory\n");
Lee Jones6bdf8912014-11-03 16:12:26 +00003122 return -ENOMEM;
Pramod Gurav51a7e022014-10-30 14:51:35 +05303123 }
Linus Walleij05ec2602013-02-07 10:17:31 +01003124
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003125 /* Clean up the mailbox interrupts after pre-kernel code. */
Mattias Nilssonc553b3c2011-08-12 10:27:20 +02003126 writel(ALL_MBOX_BITS, PRCM_ARM_IT1_CLR);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003127
Linus Walleij05ec2602013-02-07 10:17:31 +01003128 irq = platform_get_irq(pdev, 0);
3129 if (irq <= 0) {
3130 dev_err(&pdev->dev, "no prcmu irq provided\n");
Lee Jones6bdf8912014-11-03 16:12:26 +00003131 return irq;
Linus Walleij05ec2602013-02-07 10:17:31 +01003132 }
Lee Jonesca7edd12012-05-09 17:19:25 +02003133
3134 err = request_threaded_irq(irq, prcmu_irq_handler,
3135 prcmu_irq_thread_fn, IRQF_NO_SUSPEND, "prcmu", NULL);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003136 if (err < 0) {
3137 pr_err("prcmu: Failed to allocate IRQ_DB8500_PRCMU1.\n");
Lee Jones6bdf8912014-11-03 16:12:26 +00003138 return err;
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003139 }
3140
Linus Walleijf864c462014-02-04 00:35:56 +01003141 db8500_irq_init(np);
Lee Jones3a8e39c2012-07-06 12:46:23 +02003142
Linus Walleij7a4f2602012-09-19 19:31:19 +02003143 prcmu_config_esram0_deep_sleep(ESRAM0_DEEP_SLEEP_STATE_RET);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003144
Lee Jonesd98a5382013-04-09 20:52:58 +01003145 err = mfd_add_devices(&pdev->dev, 0, common_prcmu_devs,
3146 ARRAY_SIZE(common_prcmu_devs), NULL, 0, db8500_irq_domain);
Lee Jones5d903222012-06-20 13:56:41 +01003147 if (err) {
3148 pr_err("prcmu: Failed to add subdevices\n");
3149 return err;
Lee Jonesca7edd12012-05-09 17:19:25 +02003150 }
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003151
Lee Jonesd98a5382013-04-09 20:52:58 +01003152 /* TODO: Remove restriction when clk definitions are available. */
3153 if (!of_machine_is_compatible("st-ericsson,u8540")) {
3154 err = mfd_add_devices(&pdev->dev, 0, db8500_prcmu_devs,
3155 ARRAY_SIZE(db8500_prcmu_devs), NULL, 0,
3156 db8500_irq_domain);
3157 if (err) {
3158 mfd_remove_devices(&pdev->dev);
3159 pr_err("prcmu: Failed to add subdevices\n");
Lee Jones6bdf8912014-11-03 16:12:26 +00003160 return err;
Lee Jonesd98a5382013-04-09 20:52:58 +01003161 }
3162 }
3163
Arnd Bergmann4e657942016-03-18 16:53:31 +01003164 err = db8500_prcmu_register_ab8500(&pdev->dev);
Arnd Bergmann55b175d2013-03-21 22:51:07 +01003165 if (err) {
3166 mfd_remove_devices(&pdev->dev);
3167 pr_err("prcmu: Failed to add ab8500 subdevice\n");
Lee Jones6bdf8912014-11-03 16:12:26 +00003168 return err;
Arnd Bergmann55b175d2013-03-21 22:51:07 +01003169 }
3170
Lee Jonesca7edd12012-05-09 17:19:25 +02003171 pr_info("DB8500 PRCMU initialized\n");
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003172 return err;
3173}
Lee Jones3c144762012-06-29 15:41:38 +02003174static const struct of_device_id db8500_prcmu_match[] = {
3175 { .compatible = "stericsson,db8500-prcmu"},
3176 { },
3177};
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003178
3179static struct platform_driver db8500_prcmu_driver = {
3180 .driver = {
3181 .name = "db8500-prcmu",
Lee Jones3c144762012-06-29 15:41:38 +02003182 .of_match_table = db8500_prcmu_match,
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003183 },
Lee Jones9fc63f62012-04-19 21:36:41 +01003184 .probe = db8500_prcmu_probe,
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003185};
3186
3187static int __init db8500_prcmu_init(void)
3188{
Lee Jones9fc63f62012-04-19 21:36:41 +01003189 return platform_driver_register(&db8500_prcmu_driver);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003190}
3191
Lee Jonesa661aca2012-06-11 16:24:59 +01003192core_initcall(db8500_prcmu_init);
Mattias Nilsson3df57bc2011-05-16 00:15:05 +02003193
3194MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com>");
3195MODULE_DESCRIPTION("DB8500 PRCM Unit driver");
3196MODULE_LICENSE("GPL v2");