Skip to content

Commit dbc75ea

Browse files
author
Josh Tsai
committed
Add the function to read/write SPI ROM a byte
Signed-off-by: Josh Tsai <josh_tsai@compal.corp-partner.google.com>
1 parent b790da9 commit dbc75ea

2 files changed

Lines changed: 74 additions & 11 deletions

File tree

board/hx30/board.c

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "pwm_chip.h"
5454
#include "spi.h"
5555
#include "spi_chip.h"
56+
#include "spi_flash.h"
5657
#include "switch.h"
5758
#include "system.h"
5859
#include "task.h"
@@ -370,6 +371,58 @@ void board_reset_pd_mcu(void)
370371

371372
}
372373

374+
#define SPI_BLANK_OFFSET 0x3A000
375+
376+
void spi_mux_control(int enable)
377+
{
378+
if (enable) {
379+
CPRINTS("switch to spi mode");
380+
/* Disable LED drv */
381+
gpio_set_level(GPIO_TYPEC_G_DRV2_EN, 0);
382+
/* Set GPIO56 as SPI for access SPI ROM */
383+
gpio_set_alternate_function(1, 0x4000, 2);
384+
} else {
385+
CPRINTS("switch to pwm mode");
386+
/* Enable LED drv */
387+
gpio_set_level(GPIO_TYPEC_G_DRV2_EN, 1);
388+
/* Set GPIO56 as SPI for access SPI ROM */
389+
gpio_set_alternate_function(1, 0x4000, 1);
390+
}
391+
}
392+
393+
394+
void board_spi_read_byte(uint8_t offset, uint8_t *data)
395+
{
396+
int rv;
397+
398+
spi_mux_control(1);
399+
400+
rv = spi_flash_read(data, SPI_BLANK_OFFSET + offset, 0x01);
401+
if (rv != EC_SUCCESS)
402+
CPRINTS("SPI fail to read");
403+
404+
spi_mux_control(0);
405+
}
406+
407+
void board_spi_write_byte(uint8_t offset, uint8_t data)
408+
{
409+
int rv;
410+
411+
spi_mux_control(1);
412+
413+
rv = spi_flash_erase(SPI_BLANK_OFFSET, 0x1000);
414+
415+
if (rv != EC_SUCCESS)
416+
CPRINTS("SPI fail to erase");
417+
418+
rv = spi_flash_write(SPI_BLANK_OFFSET + offset, 0x01, &data);
419+
420+
if (rv != EC_SUCCESS)
421+
CPRINTS("SPI fail to write");
422+
423+
spi_mux_control(0);
424+
}
425+
373426
/**
374427
* Check the plug-in AC then power on system setting.
375428
*/
@@ -1191,24 +1244,32 @@ static int cmd_spimux(int argc, char **argv)
11911244
if (!parse_bool(argv[1], &enable))
11921245
return EC_ERROR_PARAM1;
11931246

1194-
if (enable) {
1195-
/* Disable LED drv */
1196-
gpio_set_level(GPIO_TYPEC_G_DRV2_EN, 0);
1197-
/* Set GPIO56 as SPI for access SPI ROM */
1198-
gpio_set_alternate_function(1, 0x4000, 2);
1199-
} else {
1200-
/* Enable LED drv */
1201-
gpio_set_level(GPIO_TYPEC_G_DRV2_EN, 1);
1202-
/* Set GPIO56 as SPI for access SPI ROM */
1203-
gpio_set_alternate_function(1, 0x4000, 1);
1204-
}
1247+
spi_mux_control(enable);
12051248
}
12061249
return EC_SUCCESS;
12071250
}
12081251
DECLARE_CONSOLE_COMMAND(spimux, cmd_spimux,
12091252
"[enable/disable]",
12101253
"Set if spi CLK is in SPI mode (true) or PWM mode");
12111254

1255+
1256+
static int cmd_boardspicontrol(int argc, char **argv)
1257+
{
1258+
uint8_t data;
1259+
1260+
if (!strcasecmp(argv[1], "read")) {
1261+
board_spi_read_byte(0x00, &data);
1262+
CPRINTS("DEBUG: cmd get data:0x%02x", data);
1263+
} else if (!strcasecmp(argv[1], "write")) {
1264+
board_spi_write_byte(0x00, 0xAA);
1265+
}
1266+
1267+
return EC_SUCCESS;
1268+
}
1269+
DECLARE_CONSOLE_COMMAND(boardspi, cmd_boardspicontrol,
1270+
"[read/write]",
1271+
"test");
1272+
12121273
#define FP_LOCKOUT_TIMEOUT (8 * SECOND)
12131274
static void fingerprint_ctrl_detection_deferred(void);
12141275
DECLARE_DEFERRED(fingerprint_ctrl_detection_deferred);

board/hx30/board.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,8 @@ void board_reset_pd_mcu(void);
727727
/* P sensor */
728728
void psensor_interrupt(enum gpio_signal signal);
729729

730+
void board_spi_read_byte(uint8_t offset, uint8_t *data);
731+
void board_spi_write_byte(uint8_t offset, uint8_t data);
730732

731733
/* SOC */
732734
void soc_signal_interrupt(enum gpio_signal signal);

0 commit comments

Comments
 (0)