blob: 934428fb89f738345ab46c44a025eb0c243e3cb3 [file] [log] [blame]
John Rigbyafbf8892011-04-19 10:42:42 +00001/*
2 * Copyright (C) 2009 ST-Ericsson SA
3 *
4 * Adapted from the Linux version:
5 * Author: Kumar Sanghvi <kumar.sanghvi@stericsson.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License version 2
22 * as published by the Free Software Foundation.
23 */
24
25/*
26 * NOTE: This currently does not support the I2C workaround access method.
27 */
28
29#include <common.h>
30#include <config.h>
31#include <asm/io.h>
32#include <asm/arch/hardware.h>
33#include <asm/types.h>
34#include <asm/io.h>
35#include <asm/errno.h>
Mathieu J. Poirier42cb8fb2012-07-31 08:59:24 +000036#include <asm/arch/prcmu.h>
John Rigbyafbf8892011-04-19 10:42:42 +000037
38/* CPU mailbox registers */
Mathieu J. Poirier101a7692012-07-31 08:59:27 +000039#define PRCMU_I2C_WRITE(slave) \
40 (((slave) << 1) | I2CWRITE | (1 << 6))
41#define PRCMU_I2C_READ(slave) \
42 (((slave) << 1) | I2CREAD | (1 << 6))
John Rigbyafbf8892011-04-19 10:42:42 +000043
Mathieu J. Poirier9652de72012-07-31 08:59:25 +000044#define I2C_MBOX_BIT (1 << 5)
45
John Rigbyafbf8892011-04-19 10:42:42 +000046static int prcmu_is_ready(void)
47{
48 int ready = readb(PRCM_XP70_CUR_PWR_STATE) == AP_EXECUTE;
49 if (!ready)
50 printf("PRCMU firmware not ready\n");
51 return ready;
52}
53
Mathieu J. Poirier101a7692012-07-31 08:59:27 +000054static int wait_for_i2c_mbx_rdy(void)
John Rigbyafbf8892011-04-19 10:42:42 +000055{
Mathieu J. Poirier101a7692012-07-31 08:59:27 +000056 int timeout = 10000;
John Rigbyafbf8892011-04-19 10:42:42 +000057
Mathieu J. Poirier101a7692012-07-31 08:59:27 +000058 if (readl(PRCM_ARM_IT1_VAL) & I2C_MBOX_BIT) {
59 printf("prcmu: warning i2c mailbox was not acked\n");
60 /* clear mailbox 5 ack irq */
61 writel(I2C_MBOX_BIT, PRCM_ARM_IT1_CLEAR);
62 }
63
64 /* check any already on-going transaction */
65 while ((readl(PRCM_MBOX_CPU_VAL) & I2C_MBOX_BIT) && timeout)
Mathieu J. Poirier42cb8fb2012-07-31 08:59:24 +000066 timeout--;
John Rigbyafbf8892011-04-19 10:42:42 +000067
Mathieu J. Poirier101a7692012-07-31 08:59:27 +000068 if (timeout == 0)
69 return -1;
70
71 return 0;
72}
73
74static int wait_for_i2c_req_done(void)
75{
76 int timeout = 10000;
John Rigbyafbf8892011-04-19 10:42:42 +000077
78 /* Set an interrupt to XP70 */
Mathieu J. Poirier101a7692012-07-31 08:59:27 +000079 writel(I2C_MBOX_BIT, PRCM_MBOX_CPU_SET);
John Rigbyafbf8892011-04-19 10:42:42 +000080
Mathieu J. Poirier101a7692012-07-31 08:59:27 +000081 /* wait for mailbox 5 (i2c) ack */
82 while (!(readl(PRCM_ARM_IT1_VAL) & I2C_MBOX_BIT) && timeout)
Mathieu J. Poirier42cb8fb2012-07-31 08:59:24 +000083 timeout--;
John Rigbyafbf8892011-04-19 10:42:42 +000084
Mathieu J. Poirier101a7692012-07-31 08:59:27 +000085 if (timeout == 0)
John Rigbyafbf8892011-04-19 10:42:42 +000086 return -1;
John Rigbyafbf8892011-04-19 10:42:42 +000087
88 return 0;
89}
90
91/**
92 * prcmu_i2c_read - PRCMU - 4500 communication using PRCMU I2C
93 * @reg: - db8500 register bank to be accessed
94 * @slave: - db8500 register to be accessed
95 * Returns: ACK_MB5 value containing the status
96 */
97int prcmu_i2c_read(u8 reg, u16 slave)
98{
99 uint8_t i2c_status;
100 uint8_t i2c_val;
Mathieu J. Poirier101a7692012-07-31 08:59:27 +0000101 int ret;
John Rigbyafbf8892011-04-19 10:42:42 +0000102
103 if (!prcmu_is_ready())
104 return -1;
105
106 debug("\nprcmu_4500_i2c_read:bank=%x;reg=%x;\n",
107 reg, slave);
108
Mathieu J. Poirier101a7692012-07-31 08:59:27 +0000109 ret = wait_for_i2c_mbx_rdy();
110 if (ret) {
111 printf("prcmu_i2c_read: mailbox became not ready\n");
112 return ret;
113 }
114
John Rigbyafbf8892011-04-19 10:42:42 +0000115 /* prepare the data for mailbox 5 */
Mathieu J. Poirier101a7692012-07-31 08:59:27 +0000116 writeb(PRCMU_I2C_READ(reg), PRCM_REQ_MB5_I2COPTYPE_REG);
John Rigbyafbf8892011-04-19 10:42:42 +0000117 writeb((1 << 3) | 0x0, PRCM_REQ_MB5_BIT_FIELDS);
118 writeb(slave, PRCM_REQ_MB5_I2CSLAVE);
119 writeb(0, PRCM_REQ_MB5_I2CVAL);
120
Mathieu J. Poirier101a7692012-07-31 08:59:27 +0000121 ret = wait_for_i2c_req_done();
122 if (ret) {
123 printf("prcmu_i2c_read: mailbox request timed out\n");
124 return ret;
125 }
John Rigbyafbf8892011-04-19 10:42:42 +0000126
127 /* retrieve values */
128 debug("ack-mb5:transfer status = %x\n",
129 readb(PRCM_ACK_MB5_STATUS));
130 debug("ack-mb5:reg bank = %x\n", readb(PRCM_ACK_MB5) >> 1);
131 debug("ack-mb5:slave_add = %x\n",
132 readb(PRCM_ACK_MB5_SLAVE));
133 debug("ack-mb5:reg_val = %d\n", readb(PRCM_ACK_MB5_VAL));
134
135 i2c_status = readb(PRCM_ACK_MB5_STATUS);
136 i2c_val = readb(PRCM_ACK_MB5_VAL);
Mathieu J. Poirier101a7692012-07-31 08:59:27 +0000137 /* clear mailbox 5 ack irq */
138 writel(I2C_MBOX_BIT, PRCM_ARM_IT1_CLEAR);
John Rigbyafbf8892011-04-19 10:42:42 +0000139
140 if (i2c_status == I2C_RD_OK)
141 return i2c_val;
John Rigbyafbf8892011-04-19 10:42:42 +0000142
Mathieu J. Poirier101a7692012-07-31 08:59:27 +0000143 printf("prcmu_i2c_read:read return status= %d\n", i2c_status);
144 return -1;
John Rigbyafbf8892011-04-19 10:42:42 +0000145}
146
147/**
148 * prcmu_i2c_write - PRCMU-db8500 communication using PRCMU I2C
149 * @reg: - db8500 register bank to be accessed
150 * @slave: - db800 register to be written to
151 * @reg_data: - the data to write
152 * Returns: ACK_MB5 value containing the status
153 */
154int prcmu_i2c_write(u8 reg, u16 slave, u8 reg_data)
155{
156 uint8_t i2c_status;
Mathieu J. Poirier101a7692012-07-31 08:59:27 +0000157 int ret;
John Rigbyafbf8892011-04-19 10:42:42 +0000158
159 if (!prcmu_is_ready())
160 return -1;
161
162 debug("\nprcmu_4500_i2c_write:bank=%x;reg=%x;\n",
163 reg, slave);
164
Mathieu J. Poirier101a7692012-07-31 08:59:27 +0000165 ret = wait_for_i2c_mbx_rdy();
166 if (ret) {
167 printf("prcmu_i2c_write: mailbox became not ready\n");
168 return ret;
169 }
170
John Rigbyafbf8892011-04-19 10:42:42 +0000171 /* prepare the data for mailbox 5 */
Mathieu J. Poirier101a7692012-07-31 08:59:27 +0000172 writeb(PRCMU_I2C_WRITE(reg), PRCM_REQ_MB5_I2COPTYPE_REG);
John Rigbyafbf8892011-04-19 10:42:42 +0000173 writeb((1 << 3) | 0x0, PRCM_REQ_MB5_BIT_FIELDS);
174 writeb(slave, PRCM_REQ_MB5_I2CSLAVE);
175 writeb(reg_data, PRCM_REQ_MB5_I2CVAL);
176
Mathieu J. Poirier101a7692012-07-31 08:59:27 +0000177 ret = wait_for_i2c_req_done();
178 if (ret) {
179 printf("prcmu_i2c_write: mailbox request timed out\n");
180 return ret;
181 }
John Rigbyafbf8892011-04-19 10:42:42 +0000182
183 /* retrieve values */
184 debug("ack-mb5:transfer status = %x\n",
185 readb(PRCM_ACK_MB5_STATUS));
186 debug("ack-mb5:reg bank = %x\n", readb(PRCM_ACK_MB5) >> 1);
187 debug("ack-mb5:slave_add = %x\n",
188 readb(PRCM_ACK_MB5_SLAVE));
189 debug("ack-mb5:reg_val = %d\n", readb(PRCM_ACK_MB5_VAL));
190
191 i2c_status = readb(PRCM_ACK_MB5_STATUS);
192 debug("\ni2c_status = %x\n", i2c_status);
Mathieu J. Poirier101a7692012-07-31 08:59:27 +0000193 /* clear mailbox 5 ack irq */
194 writel(I2C_MBOX_BIT, PRCM_ARM_IT1_CLEAR);
195
John Rigbyafbf8892011-04-19 10:42:42 +0000196 if (i2c_status == I2C_WR_OK)
197 return 0;
Mathieu J. Poirier101a7692012-07-31 08:59:27 +0000198
199 printf("%s: i2c_status : 0x%x\n", __func__, i2c_status);
200 return -1;
John Rigbyafbf8892011-04-19 10:42:42 +0000201}
Mathieu J. Poirier9652de72012-07-31 08:59:25 +0000202
203void u8500_prcmu_enable(u32 *reg)
204{
205 writel(readl(reg) | (1 << 8), reg);
206}
207
208void db8500_prcmu_init(void)
209{
210 /* Enable timers */
211 writel(1 << 17, PRCM_TCR);
212
213 u8500_prcmu_enable((u32 *)PRCM_PER1CLK_MGT_REG);
214 u8500_prcmu_enable((u32 *)PRCM_PER2CLK_MGT_REG);
215 u8500_prcmu_enable((u32 *)PRCM_PER3CLK_MGT_REG);
216 /* PER4CLK does not exist */
217 u8500_prcmu_enable((u32 *)PRCM_PER5CLK_MGT_REG);
218 u8500_prcmu_enable((u32 *)PRCM_PER6CLK_MGT_REG);
219 /* Only exists in ED but is always ok to write to */
220 u8500_prcmu_enable((u32 *)PRCM_PER7CLK_MGT_REG);
221
222 u8500_prcmu_enable((u32 *)PRCM_UARTCLK_MGT_REG);
223 u8500_prcmu_enable((u32 *)PRCM_I2CCLK_MGT_REG);
224
225 u8500_prcmu_enable((u32 *)PRCM_SDMMCCLK_MGT_REG);
226
227 /* Clean up the mailbox interrupts after pre-u-boot code. */
228 writel(I2C_MBOX_BIT, PRCM_ARM_IT1_CLEAR);
229}