John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 1 | /* |
| 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. Poirier | 42cb8fb | 2012-07-31 08:59:24 +0000 | [diff] [blame] | 36 | #include <asm/arch/prcmu.h> |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 37 | |
| 38 | /* CPU mailbox registers */ |
Mathieu J. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 39 | #define PRCMU_I2C_WRITE(slave) \ |
| 40 | (((slave) << 1) | I2CWRITE | (1 << 6)) |
| 41 | #define PRCMU_I2C_READ(slave) \ |
| 42 | (((slave) << 1) | I2CREAD | (1 << 6)) |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 43 | |
Mathieu J. Poirier | 9652de7 | 2012-07-31 08:59:25 +0000 | [diff] [blame] | 44 | #define I2C_MBOX_BIT (1 << 5) |
| 45 | |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 46 | static 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. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 54 | static int wait_for_i2c_mbx_rdy(void) |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 55 | { |
Mathieu J. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 56 | int timeout = 10000; |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 57 | |
Mathieu J. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 58 | 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. Poirier | 42cb8fb | 2012-07-31 08:59:24 +0000 | [diff] [blame] | 66 | timeout--; |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 67 | |
Mathieu J. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 68 | if (timeout == 0) |
| 69 | return -1; |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int wait_for_i2c_req_done(void) |
| 75 | { |
| 76 | int timeout = 10000; |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 77 | |
| 78 | /* Set an interrupt to XP70 */ |
Mathieu J. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 79 | writel(I2C_MBOX_BIT, PRCM_MBOX_CPU_SET); |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 80 | |
Mathieu J. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 81 | /* wait for mailbox 5 (i2c) ack */ |
| 82 | while (!(readl(PRCM_ARM_IT1_VAL) & I2C_MBOX_BIT) && timeout) |
Mathieu J. Poirier | 42cb8fb | 2012-07-31 08:59:24 +0000 | [diff] [blame] | 83 | timeout--; |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 84 | |
Mathieu J. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 85 | if (timeout == 0) |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 86 | return -1; |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 87 | |
| 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 | */ |
| 97 | int prcmu_i2c_read(u8 reg, u16 slave) |
| 98 | { |
| 99 | uint8_t i2c_status; |
| 100 | uint8_t i2c_val; |
Mathieu J. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 101 | int ret; |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 102 | |
| 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. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 109 | 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 Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 115 | /* prepare the data for mailbox 5 */ |
Mathieu J. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 116 | writeb(PRCMU_I2C_READ(reg), PRCM_REQ_MB5_I2COPTYPE_REG); |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 117 | 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. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 121 | ret = wait_for_i2c_req_done(); |
| 122 | if (ret) { |
| 123 | printf("prcmu_i2c_read: mailbox request timed out\n"); |
| 124 | return ret; |
| 125 | } |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 126 | |
| 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. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 137 | /* clear mailbox 5 ack irq */ |
| 138 | writel(I2C_MBOX_BIT, PRCM_ARM_IT1_CLEAR); |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 139 | |
| 140 | if (i2c_status == I2C_RD_OK) |
| 141 | return i2c_val; |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 142 | |
Mathieu J. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 143 | printf("prcmu_i2c_read:read return status= %d\n", i2c_status); |
| 144 | return -1; |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 145 | } |
| 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 | */ |
| 154 | int prcmu_i2c_write(u8 reg, u16 slave, u8 reg_data) |
| 155 | { |
| 156 | uint8_t i2c_status; |
Mathieu J. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 157 | int ret; |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 158 | |
| 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. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 165 | 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 Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 171 | /* prepare the data for mailbox 5 */ |
Mathieu J. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 172 | writeb(PRCMU_I2C_WRITE(reg), PRCM_REQ_MB5_I2COPTYPE_REG); |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 173 | 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. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 177 | ret = wait_for_i2c_req_done(); |
| 178 | if (ret) { |
| 179 | printf("prcmu_i2c_write: mailbox request timed out\n"); |
| 180 | return ret; |
| 181 | } |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 182 | |
| 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. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 193 | /* clear mailbox 5 ack irq */ |
| 194 | writel(I2C_MBOX_BIT, PRCM_ARM_IT1_CLEAR); |
| 195 | |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 196 | if (i2c_status == I2C_WR_OK) |
| 197 | return 0; |
Mathieu J. Poirier | 101a769 | 2012-07-31 08:59:27 +0000 | [diff] [blame] | 198 | |
| 199 | printf("%s: i2c_status : 0x%x\n", __func__, i2c_status); |
| 200 | return -1; |
John Rigby | afbf889 | 2011-04-19 10:42:42 +0000 | [diff] [blame] | 201 | } |
Mathieu J. Poirier | 9652de7 | 2012-07-31 08:59:25 +0000 | [diff] [blame] | 202 | |
| 203 | void u8500_prcmu_enable(u32 *reg) |
| 204 | { |
| 205 | writel(readl(reg) | (1 << 8), reg); |
| 206 | } |
| 207 | |
| 208 | void 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 | } |