123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- /*
- * Copyright 2008 Extreme Engineering Solutions, Inc.
- *
- * SPDX-License-Identifier: GPL-2.0
- */
- /*
- * Driver for DS4510, a CPU supervisor with integrated EEPROM, SRAM,
- * and 4 programmable non-volatile GPIO pins.
- */
- #include <common.h>
- #include <i2c.h>
- #include <command.h>
- #include <ds4510.h>
- /* Default to an address that hopefully won't corrupt other i2c devices */
- #ifndef CONFIG_SYS_I2C_DS4510_ADDR
- #define CONFIG_SYS_I2C_DS4510_ADDR (~0)
- #endif
- enum {
- DS4510_CMD_INFO,
- DS4510_CMD_DEVICE,
- DS4510_CMD_NV,
- DS4510_CMD_RSTDELAY,
- DS4510_CMD_OUTPUT,
- DS4510_CMD_INPUT,
- DS4510_CMD_PULLUP,
- DS4510_CMD_EEPROM,
- DS4510_CMD_SEEPROM,
- DS4510_CMD_SRAM,
- };
- /*
- * Write to DS4510, taking page boundaries into account
- */
- int ds4510_mem_write(uint8_t chip, int offset, uint8_t *buf, int count)
- {
- int wrlen;
- int i = 0;
- do {
- wrlen = DS4510_EEPROM_PAGE_SIZE -
- DS4510_EEPROM_PAGE_OFFSET(offset);
- if (count < wrlen)
- wrlen = count;
- if (i2c_write(chip, offset, 1, &buf[i], wrlen))
- return -1;
- /*
- * This delay isn't needed for SRAM writes but shouldn't delay
- * things too much, so do it unconditionally for simplicity
- */
- udelay(DS4510_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
- count -= wrlen;
- offset += wrlen;
- i += wrlen;
- } while (count > 0);
- return 0;
- }
- /*
- * General read from DS4510
- */
- int ds4510_mem_read(uint8_t chip, int offset, uint8_t *buf, int count)
- {
- return i2c_read(chip, offset, 1, buf, count);
- }
- /*
- * Write SEE bit in config register.
- * nv = 0 - Writes to SEEPROM registers behave like EEPROM
- * nv = 1 - Writes to SEEPROM registers behave like SRAM
- */
- int ds4510_see_write(uint8_t chip, uint8_t nv)
- {
- uint8_t data;
- if (i2c_read(chip, DS4510_CFG, 1, &data, 1))
- return -1;
- if (nv) /* Treat SEEPROM bits as EEPROM */
- data &= ~DS4510_CFG_SEE;
- else /* Treat SEEPROM bits as SRAM */
- data |= DS4510_CFG_SEE;
- return ds4510_mem_write(chip, DS4510_CFG, &data, 1);
- }
- /*
- * Write de-assertion of reset signal delay
- */
- int ds4510_rstdelay_write(uint8_t chip, uint8_t delay)
- {
- uint8_t data;
- if (i2c_read(chip, DS4510_RSTDELAY, 1, &data, 1))
- return -1;
- data &= ~DS4510_RSTDELAY_MASK;
- data |= delay & DS4510_RSTDELAY_MASK;
- return ds4510_mem_write(chip, DS4510_RSTDELAY, &data, 1);
- }
- /*
- * Write pullup characteristics of IO pins
- */
- int ds4510_pullup_write(uint8_t chip, uint8_t val)
- {
- val &= DS4510_IO_MASK;
- return ds4510_mem_write(chip, DS4510_PULLUP, (uint8_t *)&val, 1);
- }
- /*
- * Read pullup characteristics of IO pins
- */
- int ds4510_pullup_read(uint8_t chip)
- {
- uint8_t val;
- if (i2c_read(chip, DS4510_PULLUP, 1, &val, 1))
- return -1;
- return val & DS4510_IO_MASK;
- }
- /*
- * Write drive level of IO pins
- */
- int ds4510_gpio_write(uint8_t chip, uint8_t val)
- {
- uint8_t data;
- int i;
- for (i = 0; i < DS4510_NUM_IO; i++) {
- if (i2c_read(chip, DS4510_IO0 - i, 1, &data, 1))
- return -1;
- if (val & (0x1 << i))
- data |= 0x1;
- else
- data &= ~0x1;
- if (ds4510_mem_write(chip, DS4510_IO0 - i, &data, 1))
- return -1;
- }
- return 0;
- }
- /*
- * Read drive level of IO pins
- */
- int ds4510_gpio_read(uint8_t chip)
- {
- uint8_t data;
- int val = 0;
- int i;
- for (i = 0; i < DS4510_NUM_IO; i++) {
- if (i2c_read(chip, DS4510_IO0 - i, 1, &data, 1))
- return -1;
- if (data & 1)
- val |= (1 << i);
- }
- return val;
- }
- /*
- * Read physical level of IO pins
- */
- int ds4510_gpio_read_val(uint8_t chip)
- {
- uint8_t val;
- if (i2c_read(chip, DS4510_IO_STATUS, 1, &val, 1))
- return -1;
- return val & DS4510_IO_MASK;
- }
- #ifdef CONFIG_CMD_DS4510
- #ifdef CONFIG_CMD_DS4510_INFO
- /*
- * Display DS4510 information
- */
- static int ds4510_info(uint8_t chip)
- {
- int i;
- int tmp;
- uint8_t data;
- printf("DS4510 @ 0x%x:\n\n", chip);
- if (i2c_read(chip, DS4510_RSTDELAY, 1, &data, 1))
- return -1;
- printf("rstdelay = 0x%x\n\n", data & DS4510_RSTDELAY_MASK);
- if (i2c_read(chip, DS4510_CFG, 1, &data, 1))
- return -1;
- printf("config = 0x%x\n", data);
- printf(" /ready = %d\n", data & DS4510_CFG_READY ? 1 : 0);
- printf(" trip pt = %d\n", data & DS4510_CFG_TRIP_POINT ? 1 : 0);
- printf(" rst sts = %d\n", data & DS4510_CFG_RESET ? 1 : 0);
- printf(" /see = %d\n", data & DS4510_CFG_SEE ? 1 : 0);
- printf(" swrst = %d\n\n", data & DS4510_CFG_SWRST ? 1 : 0);
- printf("gpio pins: 3210\n");
- printf("---------------\n");
- printf("pullup ");
- tmp = ds4510_pullup_read(chip);
- if (tmp == -1)
- return tmp;
- for (i = DS4510_NUM_IO - 1; i >= 0; i--)
- printf("%d", (tmp & (1 << i)) ? 1 : 0);
- printf("\n");
- printf("driven ");
- tmp = ds4510_gpio_read(chip);
- if (tmp == -1)
- return -1;
- for (i = DS4510_NUM_IO - 1; i >= 0; i--)
- printf("%d", (tmp & (1 << i)) ? 1 : 0);
- printf("\n");
- printf("read ");
- tmp = ds4510_gpio_read_val(chip);
- if (tmp == -1)
- return -1;
- for (i = DS4510_NUM_IO - 1; i >= 0; i--)
- printf("%d", (tmp & (1 << i)) ? 1 : 0);
- printf("\n");
- return 0;
- }
- #endif /* CONFIG_CMD_DS4510_INFO */
- cmd_tbl_t cmd_ds4510[] = {
- U_BOOT_CMD_MKENT(device, 3, 0, (void *)DS4510_CMD_DEVICE, "", ""),
- U_BOOT_CMD_MKENT(nv, 3, 0, (void *)DS4510_CMD_NV, "", ""),
- U_BOOT_CMD_MKENT(output, 4, 0, (void *)DS4510_CMD_OUTPUT, "", ""),
- U_BOOT_CMD_MKENT(input, 3, 0, (void *)DS4510_CMD_INPUT, "", ""),
- U_BOOT_CMD_MKENT(pullup, 4, 0, (void *)DS4510_CMD_PULLUP, "", ""),
- #ifdef CONFIG_CMD_DS4510_INFO
- U_BOOT_CMD_MKENT(info, 2, 0, (void *)DS4510_CMD_INFO, "", ""),
- #endif
- #ifdef CONFIG_CMD_DS4510_RST
- U_BOOT_CMD_MKENT(rstdelay, 3, 0, (void *)DS4510_CMD_RSTDELAY, "", ""),
- #endif
- #ifdef CONFIG_CMD_DS4510_MEM
- U_BOOT_CMD_MKENT(eeprom, 6, 0, (void *)DS4510_CMD_EEPROM, "", ""),
- U_BOOT_CMD_MKENT(seeprom, 6, 0, (void *)DS4510_CMD_SEEPROM, "", ""),
- U_BOOT_CMD_MKENT(sram, 6, 0, (void *)DS4510_CMD_SRAM, "", ""),
- #endif
- };
- int do_ds4510(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
- {
- static uint8_t chip = CONFIG_SYS_I2C_DS4510_ADDR;
- cmd_tbl_t *c;
- ulong ul_arg2 = 0;
- ulong ul_arg3 = 0;
- int tmp;
- #ifdef CONFIG_CMD_DS4510_MEM
- ulong addr;
- ulong off;
- ulong cnt;
- int end;
- int (*rw_func)(uint8_t, int, uint8_t *, int);
- #endif
- c = find_cmd_tbl(argv[1], cmd_ds4510, ARRAY_SIZE(cmd_ds4510));
- /* All commands but "device" require 'maxargs' arguments */
- if (!c || !((argc == (c->maxargs)) ||
- (((int)c->cmd == DS4510_CMD_DEVICE) &&
- (argc == (c->maxargs - 1))))) {
- return cmd_usage(cmdtp);
- }
- /* arg2 used as chip addr and pin number */
- if (argc > 2)
- ul_arg2 = simple_strtoul(argv[2], NULL, 16);
- /* arg3 used as output/pullup value */
- if (argc > 3)
- ul_arg3 = simple_strtoul(argv[3], NULL, 16);
- switch ((int)c->cmd) {
- case DS4510_CMD_DEVICE:
- if (argc == 3)
- chip = ul_arg2;
- printf("Current device address: 0x%x\n", chip);
- return 0;
- case DS4510_CMD_NV:
- return ds4510_see_write(chip, ul_arg2);
- case DS4510_CMD_OUTPUT:
- tmp = ds4510_gpio_read(chip);
- if (tmp == -1)
- return -1;
- if (ul_arg3)
- tmp |= (1 << ul_arg2);
- else
- tmp &= ~(1 << ul_arg2);
- return ds4510_gpio_write(chip, tmp);
- case DS4510_CMD_INPUT:
- tmp = ds4510_gpio_read_val(chip);
- if (tmp == -1)
- return -1;
- return (tmp & (1 << ul_arg2)) != 0;
- case DS4510_CMD_PULLUP:
- tmp = ds4510_pullup_read(chip);
- if (tmp == -1)
- return -1;
- if (ul_arg3)
- tmp |= (1 << ul_arg2);
- else
- tmp &= ~(1 << ul_arg2);
- return ds4510_pullup_write(chip, tmp);
- #ifdef CONFIG_CMD_DS4510_INFO
- case DS4510_CMD_INFO:
- return ds4510_info(chip);
- #endif
- #ifdef CONFIG_CMD_DS4510_RST
- case DS4510_CMD_RSTDELAY:
- return ds4510_rstdelay_write(chip, ul_arg2);
- #endif
- #ifdef CONFIG_CMD_DS4510_MEM
- case DS4510_CMD_EEPROM:
- end = DS4510_EEPROM + DS4510_EEPROM_SIZE;
- off = DS4510_EEPROM;
- break;
- case DS4510_CMD_SEEPROM:
- end = DS4510_SEEPROM + DS4510_SEEPROM_SIZE;
- off = DS4510_SEEPROM;
- break;
- case DS4510_CMD_SRAM:
- end = DS4510_SRAM + DS4510_SRAM_SIZE;
- off = DS4510_SRAM;
- break;
- #endif
- default:
- /* We should never get here... */
- return 1;
- }
- #ifdef CONFIG_CMD_DS4510_MEM
- /* Only eeprom, seeprom, and sram commands should make it here */
- if (strcmp(argv[2], "read") == 0)
- rw_func = ds4510_mem_read;
- else if (strcmp(argv[2], "write") == 0)
- rw_func = ds4510_mem_write;
- else
- return cmd_usage(cmdtp);
- addr = simple_strtoul(argv[3], NULL, 16);
- off += simple_strtoul(argv[4], NULL, 16);
- cnt = simple_strtoul(argv[5], NULL, 16);
- if ((off + cnt) > end) {
- printf("ERROR: invalid len\n");
- return -1;
- }
- return rw_func(chip, off, (uint8_t *)addr, cnt);
- #endif
- }
- U_BOOT_CMD(
- ds4510, 6, 1, do_ds4510,
- "ds4510 eeprom/seeprom/sram/gpio access",
- "device [dev]\n"
- " - show or set current device address\n"
- #ifdef CONFIG_CMD_DS4510_INFO
- "ds4510 info\n"
- " - display ds4510 info\n"
- #endif
- "ds4510 output pin 0|1\n"
- " - set pin low or high-Z\n"
- "ds4510 input pin\n"
- " - read value of pin\n"
- "ds4510 pullup pin 0|1\n"
- " - disable/enable pullup on specified pin\n"
- "ds4510 nv 0|1\n"
- " - make gpio and seeprom writes volatile/non-volatile"
- #ifdef CONFIG_CMD_DS4510_RST
- "\n"
- "ds4510 rstdelay 0-3\n"
- " - set reset output delay"
- #endif
- #ifdef CONFIG_CMD_DS4510_MEM
- "\n"
- "ds4510 eeprom read addr off cnt\n"
- "ds4510 eeprom write addr off cnt\n"
- " - read/write 'cnt' bytes at EEPROM offset 'off'\n"
- "ds4510 seeprom read addr off cnt\n"
- "ds4510 seeprom write addr off cnt\n"
- " - read/write 'cnt' bytes at SRAM-shadowed EEPROM offset 'off'\n"
- "ds4510 sram read addr off cnt\n"
- "ds4510 sram write addr off cnt\n"
- " - read/write 'cnt' bytes at SRAM offset 'off'"
- #endif
- );
- #endif /* CONFIG_CMD_DS4510 */
|