Stefano Babic | 6a0034d | 2010-04-04 23:08:03 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2008-2009 Freescale Semiconductor, Inc. |
| 3 | * |
| 4 | * See file CREDITS for list of people who contributed to this |
| 5 | * project. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (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., 59 Temple Place, Suite 330, Boston, |
| 20 | * MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <config.h> |
| 24 | #include <common.h> |
| 25 | #include <spi.h> |
| 26 | #include <asm/errno.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <fsl_pmic.h> |
| 29 | |
| 30 | static struct spi_slave *slave; |
| 31 | |
| 32 | struct spi_slave *pmic_spi_probe(void) |
| 33 | { |
| 34 | return spi_setup_slave(CONFIG_FSL_PMIC_BUS, |
| 35 | CONFIG_FSL_PMIC_CS, |
| 36 | CONFIG_FSL_PMIC_CLK, |
| 37 | CONFIG_FSL_PMIC_MODE); |
| 38 | } |
| 39 | |
| 40 | void pmic_spi_free(struct spi_slave *slave) |
| 41 | { |
| 42 | if (slave) |
| 43 | spi_free_slave(slave); |
| 44 | } |
| 45 | |
| 46 | u32 pmic_reg(u32 reg, u32 val, u32 write) |
| 47 | { |
| 48 | u32 pmic_tx, pmic_rx; |
Stefano Babic | 125f82a | 2010-08-20 12:05:03 +0200 | [diff] [blame^] | 49 | u32 tmp; |
Stefano Babic | 6a0034d | 2010-04-04 23:08:03 +0200 | [diff] [blame] | 50 | |
| 51 | if (!slave) { |
| 52 | slave = pmic_spi_probe(); |
| 53 | |
| 54 | if (!slave) |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | if (reg > 63 || write > 1) { |
| 59 | printf("<reg num> = %d is invalid. Should be less then 63\n", |
| 60 | reg); |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | if (spi_claim_bus(slave)) |
| 65 | return -1; |
| 66 | |
| 67 | pmic_tx = (write << 31) | (reg << 25) | (val & 0x00FFFFFF); |
| 68 | |
Stefano Babic | 125f82a | 2010-08-20 12:05:03 +0200 | [diff] [blame^] | 69 | tmp = cpu_to_be32(pmic_tx); |
| 70 | |
| 71 | if (spi_xfer(slave, 4 << 3, &tmp, &pmic_rx, |
Stefano Babic | 6a0034d | 2010-04-04 23:08:03 +0200 | [diff] [blame] | 72 | SPI_XFER_BEGIN | SPI_XFER_END)) { |
| 73 | spi_release_bus(slave); |
| 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | if (write) { |
| 78 | pmic_tx &= ~(1 << 31); |
Stefano Babic | 125f82a | 2010-08-20 12:05:03 +0200 | [diff] [blame^] | 79 | tmp = cpu_to_be32(pmic_tx); |
| 80 | if (spi_xfer(slave, 4 << 3, &tmp, &pmic_rx, |
Stefano Babic | 6a0034d | 2010-04-04 23:08:03 +0200 | [diff] [blame] | 81 | SPI_XFER_BEGIN | SPI_XFER_END)) { |
| 82 | spi_release_bus(slave); |
| 83 | return -1; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | spi_release_bus(slave); |
Stefano Babic | 125f82a | 2010-08-20 12:05:03 +0200 | [diff] [blame^] | 88 | return cpu_to_be32(pmic_rx); |
Stefano Babic | 6a0034d | 2010-04-04 23:08:03 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void pmic_reg_write(u32 reg, u32 value) |
| 92 | { |
| 93 | pmic_reg(reg, value, 1); |
| 94 | } |
| 95 | |
| 96 | u32 pmic_reg_read(u32 reg) |
| 97 | { |
| 98 | return pmic_reg(reg, 0, 0); |
| 99 | } |
| 100 | |
| 101 | void pmic_show_pmic_info(void) |
| 102 | { |
| 103 | u32 rev_id; |
| 104 | |
| 105 | rev_id = pmic_reg_read(REG_IDENTIFICATION); |
| 106 | printf("PMIC ID: 0x%08x [Rev: ", rev_id); |
| 107 | switch (rev_id & 0x1F) { |
| 108 | case 0x1: |
| 109 | puts("1.0"); |
| 110 | break; |
| 111 | case 0x9: |
| 112 | puts("1.1"); |
| 113 | break; |
| 114 | case 0xA: |
| 115 | puts("1.2"); |
| 116 | break; |
| 117 | case 0x10: |
| 118 | puts("2.0"); |
| 119 | break; |
| 120 | case 0x11: |
| 121 | puts("2.1"); |
| 122 | break; |
| 123 | case 0x18: |
| 124 | puts("3.0"); |
| 125 | break; |
| 126 | case 0x19: |
| 127 | puts("3.1"); |
| 128 | break; |
| 129 | case 0x1A: |
| 130 | puts("3.2"); |
| 131 | break; |
| 132 | case 0x2: |
| 133 | puts("3.2A"); |
| 134 | break; |
| 135 | case 0x1B: |
| 136 | puts("3.3"); |
| 137 | break; |
| 138 | case 0x1D: |
| 139 | puts("3.5"); |
| 140 | break; |
| 141 | default: |
| 142 | puts("unknown"); |
| 143 | break; |
| 144 | } |
| 145 | puts("]\n"); |
| 146 | } |
| 147 | |
| 148 | static void pmic_dump(int numregs) |
| 149 | { |
| 150 | u32 val; |
| 151 | int i; |
| 152 | |
| 153 | pmic_show_pmic_info(); |
| 154 | for (i = 0; i < numregs; i++) { |
| 155 | val = pmic_reg_read(i); |
| 156 | if (!(i % 8)) |
| 157 | printf ("\n0x%02x: ", i); |
| 158 | printf("%08x ", val); |
| 159 | } |
| 160 | puts("\n"); |
| 161 | } |
| 162 | |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 163 | int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Stefano Babic | 6a0034d | 2010-04-04 23:08:03 +0200 | [diff] [blame] | 164 | { |
| 165 | char *cmd; |
| 166 | int nregs; |
| 167 | u32 val; |
| 168 | |
| 169 | /* at least two arguments please */ |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 170 | if (argc < 2) |
| 171 | return cmd_usage(cmdtp); |
Stefano Babic | 6a0034d | 2010-04-04 23:08:03 +0200 | [diff] [blame] | 172 | |
| 173 | cmd = argv[1]; |
| 174 | if (strcmp(cmd, "dump") == 0) { |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 175 | if (argc < 3) |
| 176 | return cmd_usage(cmdtp); |
| 177 | |
Stefano Babic | 6a0034d | 2010-04-04 23:08:03 +0200 | [diff] [blame] | 178 | nregs = simple_strtoul(argv[2], NULL, 16); |
| 179 | pmic_dump(nregs); |
| 180 | return 0; |
| 181 | } |
| 182 | if (strcmp(cmd, "write") == 0) { |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 183 | if (argc < 4) |
| 184 | return cmd_usage(cmdtp); |
| 185 | |
Stefano Babic | 6a0034d | 2010-04-04 23:08:03 +0200 | [diff] [blame] | 186 | nregs = simple_strtoul(argv[2], NULL, 16); |
| 187 | val = simple_strtoul(argv[3], NULL, 16); |
| 188 | pmic_reg_write(nregs, val); |
| 189 | return 0; |
| 190 | } |
| 191 | /* No subcommand found */ |
| 192 | return 1; |
| 193 | } |
| 194 | |
| 195 | U_BOOT_CMD( |
| 196 | pmic, CONFIG_SYS_MAXARGS, 1, do_pmic, |
| 197 | "Freescale PMIC (Atlas)", |
| 198 | "dump [numregs] dump registers\n" |
| 199 | "pmic write <reg> <value> - write register" |
| 200 | ); |