Aparna Mallavarapu | 52b0a72 | 2014-03-28 16:49:36 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2012, 2014, The Linux Foundation. All rights reserved. |
Deepa Dinamani | c2a9b36 | 2012-02-23 15:15:54 -0800 | [diff] [blame] | 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
Duy Truong | f3ac7b3 | 2013-02-13 01:07:28 -0800 | [diff] [blame] | 12 | * * Neither the name of The Linux Foundation nor the names of its |
Deepa Dinamani | c2a9b36 | 2012-02-23 15:15:54 -0800 | [diff] [blame] | 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <debug.h> |
| 30 | #include <reg.h> |
| 31 | #include <spmi.h> |
Channagoud Kadabi | 6edec1e | 2014-04-16 15:02:43 -0700 | [diff] [blame^] | 32 | #include <bits.h> |
Deepa Dinamani | c2a9b36 | 2012-02-23 15:15:54 -0800 | [diff] [blame] | 33 | #include <platform/iomap.h> |
| 34 | #include <platform/irqs.h> |
| 35 | #include <platform/interrupts.h> |
| 36 | |
Channagoud Kadabi | 6edec1e | 2014-04-16 15:02:43 -0700 | [diff] [blame^] | 37 | #define PMIC_ARB_V2 0x20010000 |
| 38 | #define CHNL_IDX(sid, pid) ((sid << 8) | pid) |
| 39 | |
Deepa Dinamani | c2a9b36 | 2012-02-23 15:15:54 -0800 | [diff] [blame] | 40 | static uint32_t pmic_arb_chnl_num; |
| 41 | static uint32_t pmic_arb_owner_id; |
| 42 | static uint8_t pmic_irq_perph_id; |
| 43 | static spmi_callback callback; |
Channagoud Kadabi | 6edec1e | 2014-04-16 15:02:43 -0700 | [diff] [blame^] | 44 | static uint32_t pmic_arb_ver; |
| 45 | static uint8_t *chnl_tbl; |
| 46 | |
| 47 | static void spmi_lookup_chnl_number() |
| 48 | { |
| 49 | int i; |
| 50 | uint8_t slave_id; |
| 51 | uint8_t ppid_address; |
| 52 | /* We need a max of sid (4 bits) + pid (8bits) of uint8_t's */ |
| 53 | uint32_t chnl_tbl_sz = BIT(12) * sizeof(uint8_t); |
| 54 | |
| 55 | /* Allocate the channel table */ |
| 56 | chnl_tbl = (uint8_t *) malloc(chnl_tbl_sz); |
| 57 | ASSERT(chnl_tbl); |
| 58 | |
| 59 | for(i = 0; i < MAX_PERIPH ; i++) |
| 60 | { |
| 61 | #if SPMI_CORE_V2 |
| 62 | slave_id = (readl(PMIC_ARB_REG_CHLN(i)) & 0xf0000) >> 16; |
| 63 | ppid_address = (readl(PMIC_ARB_REG_CHLN(i)) & 0xff00) >> 8; |
| 64 | #endif |
| 65 | chnl_tbl[CHNL_IDX(slave_id, ppid_address)] = i; |
| 66 | } |
| 67 | } |
Deepa Dinamani | c2a9b36 | 2012-02-23 15:15:54 -0800 | [diff] [blame] | 68 | |
| 69 | /* Function to initialize SPMI controller. |
| 70 | * chnl_num : Channel number to be used by this EE. |
| 71 | */ |
| 72 | void spmi_init(uint32_t chnl_num, uint32_t owner_id) |
| 73 | { |
Channagoud Kadabi | 6edec1e | 2014-04-16 15:02:43 -0700 | [diff] [blame^] | 74 | /* Read the version numver */ |
| 75 | pmic_arb_ver = readl(PMIC_ARB_SPMI_HW_VERSION); |
| 76 | |
| 77 | if (pmic_arb_ver < PMIC_ARB_V2) |
| 78 | { |
| 79 | /* Initialize PMIC Arbiter Channel Number to |
| 80 | * 0 by default of V1 HW |
| 81 | */ |
| 82 | pmic_arb_chnl_num = chnl_num; |
| 83 | pmic_arb_owner_id = owner_id; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | spmi_lookup_chnl_number(); |
| 88 | } |
Deepa Dinamani | c2a9b36 | 2012-02-23 15:15:54 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void write_wdata_from_array(uint8_t *array, |
| 92 | uint8_t reg_num, |
| 93 | uint8_t array_size, |
| 94 | uint8_t* bytes_written) |
| 95 | { |
| 96 | uint32_t shift_value[] = {0, 8, 16, 24}; |
| 97 | int i; |
| 98 | int j; |
| 99 | uint32_t val = 0; |
| 100 | |
| 101 | /* Write to WDATA */ |
| 102 | for (i = 0; (*bytes_written < array_size) && (i < 4); i++) |
| 103 | { |
| 104 | val |= (uint32_t)(array[*bytes_written]) << shift_value[i]; |
| 105 | (*bytes_written)++; |
| 106 | } |
| 107 | |
| 108 | writel(val, PMIC_ARB_CHNLn_WDATA(pmic_arb_chnl_num, reg_num)); |
| 109 | } |
| 110 | |
Deepa Dinamani | c2a9b36 | 2012-02-23 15:15:54 -0800 | [diff] [blame] | 111 | /* Initiate a write cmd by writing to cmd register. |
| 112 | * Commands are written according to cmd parameters |
| 113 | * cmd->opcode : SPMI opcode for the command |
| 114 | * cmd->priority : Priority of the command |
| 115 | * High priority : 1 |
| 116 | * Low Priority : 0 |
| 117 | * cmd->address : SPMI Peripheral Address. |
| 118 | * cmd->offset : Offset Address for the command. |
| 119 | * cmd->bytecnt : Number of bytes to be written. |
| 120 | * |
| 121 | * param is the parameter to the command |
| 122 | * param->buffer : Value to be written |
| 123 | * param->size : Size of the buffer. |
| 124 | * |
| 125 | * return value : 0 if success, the error bit set on error |
| 126 | */ |
| 127 | unsigned int pmic_arb_write_cmd(struct pmic_arb_cmd *cmd, |
| 128 | struct pmic_arb_param *param) |
| 129 | { |
| 130 | uint32_t bytes_written = 0; |
| 131 | uint32_t error; |
| 132 | uint32_t val = 0; |
| 133 | |
Channagoud Kadabi | 6edec1e | 2014-04-16 15:02:43 -0700 | [diff] [blame^] | 134 | /* Look up for pmic channel only for V2 hardware |
| 135 | * For V1-HW we dont care for channel number & always |
| 136 | * use '0' |
| 137 | */ |
| 138 | if (pmic_arb_ver >= PMIC_ARB_V2) |
Aparna Mallavarapu | 52b0a72 | 2014-03-28 16:49:36 +0530 | [diff] [blame] | 139 | { |
Channagoud Kadabi | 6edec1e | 2014-04-16 15:02:43 -0700 | [diff] [blame^] | 140 | pmic_arb_chnl_num = chnl_tbl[CHNL_IDX(cmd->slave_id, cmd->address)]; |
Aparna Mallavarapu | 52b0a72 | 2014-03-28 16:49:36 +0530 | [diff] [blame] | 141 | } |
Channagoud Kadabi | 6edec1e | 2014-04-16 15:02:43 -0700 | [diff] [blame^] | 142 | |
Deepa Dinamani | c2a9b36 | 2012-02-23 15:15:54 -0800 | [diff] [blame] | 143 | /* Disable IRQ mode for the current channel*/ |
| 144 | writel(0x0, PMIC_ARB_CHNLn_CONFIG(pmic_arb_chnl_num)); |
Deepa Dinamani | c2a9b36 | 2012-02-23 15:15:54 -0800 | [diff] [blame] | 145 | /* Write parameters for the cmd */ |
| 146 | if (cmd == NULL) |
| 147 | { |
| 148 | dprintf(CRITICAL,"PMIC arbiter error, no command provided\n"); |
| 149 | return 1; |
| 150 | } |
| 151 | |
| 152 | /* Write the data bytes according to the param->size |
| 153 | * Can write upto 8 bytes. |
| 154 | */ |
| 155 | |
| 156 | /* Write first 4 bytes to WDATA0 */ |
| 157 | write_wdata_from_array(param->buffer, 0, param->size, &bytes_written); |
| 158 | |
| 159 | if (bytes_written < param->size) |
| 160 | { |
| 161 | /* Write next 4 bytes to WDATA1 */ |
| 162 | write_wdata_from_array(param->buffer, 1, param->size, &bytes_written); |
| 163 | } |
| 164 | |
| 165 | /* Fill in the byte count for the command |
| 166 | * Note: Byte count is one less than the number of bytes transferred. |
| 167 | */ |
| 168 | cmd->byte_cnt = param->size - 1; |
| 169 | /* Fill in the Write cmd opcode. */ |
| 170 | cmd->opcode = SPMI_CMD_EXT_REG_WRTIE_LONG; |
| 171 | |
| 172 | /* Write the command */ |
| 173 | val = 0; |
| 174 | val |= ((uint32_t)(cmd->opcode) << PMIC_ARB_CMD_OPCODE_SHIFT); |
| 175 | val |= ((uint32_t)(cmd->priority) << PMIC_ARB_CMD_PRIORITY_SHIFT); |
Aparna Mallavarapu | 52b0a72 | 2014-03-28 16:49:36 +0530 | [diff] [blame] | 176 | #ifndef SPMI_CORE_V2 |
Deepa Dinamani | c2a9b36 | 2012-02-23 15:15:54 -0800 | [diff] [blame] | 177 | val |= ((uint32_t)(cmd->slave_id) << PMIC_ARB_CMD_SLAVE_ID_SHIFT); |
| 178 | val |= ((uint32_t)(cmd->address) << PMIC_ARB_CMD_ADDR_SHIFT); |
Aparna Mallavarapu | 52b0a72 | 2014-03-28 16:49:36 +0530 | [diff] [blame] | 179 | #endif |
Deepa Dinamani | c2a9b36 | 2012-02-23 15:15:54 -0800 | [diff] [blame] | 180 | val |= ((uint32_t)(cmd->offset) << PMIC_ARB_CMD_ADDR_OFFSET_SHIFT); |
| 181 | val |= ((uint32_t)(cmd->byte_cnt)); |
| 182 | |
| 183 | writel(val, PMIC_ARB_CHNLn_CMD0(pmic_arb_chnl_num)); |
| 184 | |
| 185 | /* Wait till CMD DONE status */ |
| 186 | while (!(val = readl(PMIC_ARB_CHNLn_STATUS(pmic_arb_chnl_num)))); |
| 187 | |
| 188 | /* Check for errors */ |
| 189 | error = val ^ (1 << PMIC_ARB_CMD_DONE); |
| 190 | if (error) |
| 191 | { |
| 192 | dprintf(CRITICAL, "SPMI write command failure: \ |
| 193 | cmd_id = %u, error = %u\n", cmd->opcode, error); |
| 194 | return error; |
| 195 | } |
| 196 | else |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static void read_rdata_into_array(uint8_t *array, |
| 201 | uint8_t reg_num, |
| 202 | uint8_t array_size, |
| 203 | uint8_t* bytes_read) |
| 204 | { |
| 205 | uint32_t val = 0; |
| 206 | uint32_t mask_value[] = {0xFF, 0xFF00, 0xFF0000, 0xFF000000}; |
| 207 | uint8_t shift_value[] = {0, 8, 16, 24}; |
| 208 | int i; |
| 209 | |
| 210 | val = readl(PMIC_ARB_CHNLn_RDATA(pmic_arb_chnl_num, reg_num)); |
| 211 | |
| 212 | /* Read at most 4 bytes */ |
| 213 | for (i = 0; (i < 4) && (*bytes_read < array_size); i++) |
| 214 | { |
| 215 | array[*bytes_read] = (val & mask_value[i]) >> shift_value[i]; |
| 216 | (*bytes_read)++; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /* Initiate a read cmd by writing to cmd register. |
| 221 | * Commands are written according to cmd parameters |
| 222 | * cmd->opcode : SPMI opcode for the command |
| 223 | * cmd->priority : Priority of the command |
| 224 | * High priority : 1 |
| 225 | * Low Priority : 0 |
| 226 | * cmd->address : SPMI Peripheral Address. |
| 227 | * cmd->offset : Offset Address for the command. |
| 228 | * cmd->bytecnt : Number of bytes to be read. |
| 229 | * |
| 230 | * param is the buffer to the save command data. |
| 231 | * param->buffer : Buffer to store the bytes returned. |
| 232 | * param->size : Size of the buffer. |
| 233 | * |
| 234 | * return value : 0 if success, the error bit set on error |
| 235 | */ |
| 236 | unsigned int pmic_arb_read_cmd(struct pmic_arb_cmd *cmd, |
| 237 | struct pmic_arb_param *param) |
| 238 | { |
| 239 | uint32_t val = 0; |
| 240 | uint32_t error; |
| 241 | uint32_t addr; |
| 242 | uint8_t bytes_read = 0; |
| 243 | |
Channagoud Kadabi | 6edec1e | 2014-04-16 15:02:43 -0700 | [diff] [blame^] | 244 | /* Look up for pmic channel only for V2 hardware |
| 245 | * For V1-HW we dont care for channel number & always |
| 246 | * use '0' |
| 247 | */ |
| 248 | if (pmic_arb_ver >= PMIC_ARB_V2) |
Aparna Mallavarapu | 52b0a72 | 2014-03-28 16:49:36 +0530 | [diff] [blame] | 249 | { |
Channagoud Kadabi | 6edec1e | 2014-04-16 15:02:43 -0700 | [diff] [blame^] | 250 | pmic_arb_chnl_num = chnl_tbl[CHNL_IDX(cmd->slave_id, cmd->address)]; |
Aparna Mallavarapu | 52b0a72 | 2014-03-28 16:49:36 +0530 | [diff] [blame] | 251 | } |
Channagoud Kadabi | 6edec1e | 2014-04-16 15:02:43 -0700 | [diff] [blame^] | 252 | |
| 253 | /* Disable IRQ mode for the current channel*/ |
Deepa Dinamani | c2a9b36 | 2012-02-23 15:15:54 -0800 | [diff] [blame] | 254 | writel(0x0, PMIC_ARB_CHNLn_CONFIG(pmic_arb_chnl_num)); |
| 255 | |
| 256 | /* Fill in the byte count for the command |
| 257 | * Note: Byte count is one less than the number of bytes transferred. |
| 258 | */ |
| 259 | cmd->byte_cnt = param->size - 1; |
| 260 | /* Fill in the Write cmd opcode. */ |
| 261 | cmd->opcode = SPMI_CMD_EXT_REG_READ_LONG; |
| 262 | |
| 263 | val |= ((uint32_t)(cmd->opcode) << PMIC_ARB_CMD_OPCODE_SHIFT); |
| 264 | val |= ((uint32_t)(cmd->priority) << PMIC_ARB_CMD_PRIORITY_SHIFT); |
Aparna Mallavarapu | 52b0a72 | 2014-03-28 16:49:36 +0530 | [diff] [blame] | 265 | #ifndef SPMI_CORE_V2 |
Deepa Dinamani | c2a9b36 | 2012-02-23 15:15:54 -0800 | [diff] [blame] | 266 | val |= ((uint32_t)(cmd->slave_id) << PMIC_ARB_CMD_SLAVE_ID_SHIFT); |
| 267 | val |= ((uint32_t)(cmd->address) << PMIC_ARB_CMD_ADDR_SHIFT); |
Aparna Mallavarapu | 52b0a72 | 2014-03-28 16:49:36 +0530 | [diff] [blame] | 268 | #endif |
Deepa Dinamani | c2a9b36 | 2012-02-23 15:15:54 -0800 | [diff] [blame] | 269 | val |= ((uint32_t)(cmd->offset) << PMIC_ARB_CMD_ADDR_OFFSET_SHIFT); |
| 270 | val |= ((uint32_t)(cmd->byte_cnt)); |
| 271 | |
| 272 | writel(val, PMIC_ARB_CHNLn_CMD0(pmic_arb_chnl_num)); |
| 273 | |
| 274 | /* Wait till CMD DONE status */ |
| 275 | while (!(val = readl(PMIC_ARB_CHNLn_STATUS(pmic_arb_chnl_num)))); |
| 276 | |
| 277 | /* Check for errors */ |
| 278 | error = val ^ (1 << PMIC_ARB_CMD_DONE); |
| 279 | |
| 280 | if (error) |
| 281 | { |
| 282 | dprintf(CRITICAL, "SPMI read command failure: \ |
| 283 | cmd_id = %u, error = %u\n", cmd->opcode, error); |
| 284 | return error; |
| 285 | } |
| 286 | |
| 287 | /* Read the RDATA0 */ |
| 288 | read_rdata_into_array(param->buffer, 0, param->size , &bytes_read); |
| 289 | |
| 290 | if (bytes_read < param->size) |
| 291 | { |
| 292 | /* Read the RDATA1 */ |
| 293 | read_rdata_into_array(param->buffer, 1, param->size , &bytes_read); |
| 294 | |
| 295 | } |
| 296 | |
| 297 | if (bytes_read < param->size) |
| 298 | { |
| 299 | /* Read the RDATA2 */ |
| 300 | read_rdata_into_array(param->buffer, 2, param->size , &bytes_read); |
| 301 | |
| 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | |
| 308 | /* Funtion to determine if the peripheral that caused the interrupt |
| 309 | * is of interest. |
| 310 | * Also handles callback function and interrupt clearing if the |
| 311 | * correct interrupt is fired. |
| 312 | * periph_acc_irq: SPMI_PIC_OWNERm_ACC_STATUSn register id. |
| 313 | * status: Bits of the periph_acc_irq. |
| 314 | * return 1 if the peripheral is of interest, |
| 315 | * 0 otherwise. |
| 316 | */ |
| 317 | int spmi_acc_irq(uint32_t periph_acc_irq, uint32_t status) |
| 318 | { |
| 319 | uint8_t reg_id; |
| 320 | uint8_t offset; |
| 321 | |
| 322 | /* Narrow down the correct register for the peripheral*/ |
| 323 | reg_id = pmic_irq_perph_id / 32; |
| 324 | if (periph_acc_irq * 8 != reg_id) |
| 325 | return 0; |
| 326 | |
| 327 | /* Narrow down the correct interrupt within the register */ |
| 328 | offset = pmic_irq_perph_id & 31; |
| 329 | if ((status & offset)) |
| 330 | { |
| 331 | /* Clear the interrupt */ |
| 332 | writel(offset ^ status, SPMI_PIC_IRQ_CLEARn(reg_id)); |
| 333 | |
| 334 | /* Confirm that the interrupt has been cleared */ |
| 335 | while(readl(SPMI_PIC_IRQ_STATUSn(reg_id)) & offset); |
| 336 | |
| 337 | /* Call the callback */ |
| 338 | callback(); |
| 339 | return 1; |
| 340 | } |
| 341 | else |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | void spmi_irq() |
| 346 | { |
| 347 | int i; |
| 348 | uint32_t status; |
| 349 | |
| 350 | /* Go through the Peripheral list to figure out the periperal |
| 351 | * that caused the interrupt |
| 352 | */ |
| 353 | for (i = 0; i < 8; i++) |
| 354 | { |
| 355 | status = readl(SPMI_PIC_OWNERm_ACC_STATUSn(pmic_arb_owner_id, i)); |
| 356 | if (status) |
| 357 | if (!spmi_acc_irq(i, status)) |
| 358 | /* Not the correct interrupt, continue to wait */ |
| 359 | return; |
| 360 | } |
| 361 | mask_interrupt(EE0_KRAIT_HLOS_SPMI_PERIPH_IRQ); |
| 362 | } |
| 363 | |
| 364 | /* Enable interrupts on a particular peripheral: periph_id */ |
| 365 | void spmi_enable_periph_interrupts(uint8_t periph_id) |
| 366 | { |
| 367 | pmic_irq_perph_id = periph_id; |
| 368 | |
| 369 | register_int_handler(EE0_KRAIT_HLOS_SPMI_PERIPH_IRQ , spmi_irq, 0); |
| 370 | unmask_interrupt(EE0_KRAIT_HLOS_SPMI_PERIPH_IRQ); |
| 371 | |
| 372 | } |
| 373 | |
| 374 | void spmi_uninit() |
| 375 | { |
| 376 | mask_interrupt(EE0_KRAIT_HLOS_SPMI_PERIPH_IRQ); |
| 377 | } |