From 885fb3ed44bf31c790640fb282b45f195dba51b8 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 20 Nov 2019 11:58:36 -0700 Subject: [PATCH] Add PMC support to scratch ram, move makefile targets to new makefile --- src/board/system76/galp3-c/board.mk | 33 +++---------------- .../galp3-c/scratch/include/scratch/pmc.h | 33 +++++++++++++++++++ src/board/system76/galp3-c/scratch/main.c | 20 +++++++++-- src/board/system76/galp3-c/scratch/pmc.c | 32 ++++++++++++++++++ src/board/system76/galp3-c/scratch/scratch.mk | 27 +++++++++++++++ 5 files changed, 114 insertions(+), 31 deletions(-) create mode 100644 src/board/system76/galp3-c/scratch/include/scratch/pmc.h create mode 100644 src/board/system76/galp3-c/scratch/pmc.c create mode 100644 src/board/system76/galp3-c/scratch/scratch.mk diff --git a/src/board/system76/galp3-c/board.mk b/src/board/system76/galp3-c/board.mk index 8757d67..8ed8583 100644 --- a/src/board/system76/galp3-c/board.mk +++ b/src/board/system76/galp3-c/board.mk @@ -10,34 +10,11 @@ CFLAGS+=-DLEVEL=2 # CFLAGS+=-DI2C_DEBUGGER=0x76 # Add scratch ROM source -SCRATCH_SRC=$(wildcard $(BOARD_DIR)/scratch/*.c) -SCRATCH_OBJ=$(patsubst src/%.c,$(BUILD)/%.rel,$(SCRATCH_SRC)) -SCRATCH_CC=\ - sdcc \ - -mmcs51 \ - --model-small \ - --code-size 2048 \ - --Werror - -# Convert from binary file to C header -$(BUILD)/include/scratch.h: $(BUILD)/scratch.rom - @mkdir -p $(@D) - xxd --include < $< > $@ - -# Convert from Intel Hex file to binary file -$(BUILD)/scratch.rom: $(BUILD)/scratch.ihx - @mkdir -p $(@D) - makebin -p < $< > $@ - -# Link object files into Intel Hex file -$(BUILD)/scratch.ihx: $(SCRATCH_OBJ) - @mkdir -p $(@D) - $(SCRATCH_CC) -o $@ $^ - -# Compile C files into object files -$(SCRATCH_OBJ): $(BUILD)/%.rel: src/%.c - @mkdir -p $(@D) - $(SCRATCH_CC) -o $@ -c $< +SCRATCH_DIR=$(BOARD_DIR)/scratch +SCRATCH_SRC=$(wildcard $(SCRATCH_DIR)/*.c) +SCRATCH_INCLUDE=$(wildcard $(SCRATCH_DIR)/include/scratch/*.h) +SCRATCH_CFLAGS=-I$(SCRATCH_DIR)/include +include $(SCRATCH_DIR)/scratch.mk # Require scratch.rom to be compiled before main firmware CFLAGS+=-I$(BUILD)/include diff --git a/src/board/system76/galp3-c/scratch/include/scratch/pmc.h b/src/board/system76/galp3-c/scratch/include/scratch/pmc.h new file mode 100644 index 0000000..8e02acb --- /dev/null +++ b/src/board/system76/galp3-c/scratch/include/scratch/pmc.h @@ -0,0 +1,33 @@ +#ifndef _EC_PMC_H +#define _EC_PMC_H + +#include +#include + +struct Pmc { + // Status register + volatile uint8_t * status; + // Data out register + volatile uint8_t * data_out; + // Data in register + volatile uint8_t * data_in; + // Control register + volatile uint8_t * control; +}; + +extern struct Pmc __code PMC_1; + +#define PMC_STS_OBF (1 << 0) +#define PMC_STS_IBF (1 << 1) +#define PMC_STS_CMD (1 << 3) + +uint8_t pmc_status(struct Pmc * pmc); +uint8_t pmc_read(struct Pmc * pmc); +bool pmc_write(struct Pmc * pmc, uint8_t data, int timeout); + +volatile uint8_t __xdata __at(0x1500) PM1STS; +volatile uint8_t __xdata __at(0x1501) PM1DO; +volatile uint8_t __xdata __at(0x1504) PM1DI; +volatile uint8_t __xdata __at(0x1506) PM1CTL; + +#endif // _EC_PMC_H diff --git a/src/board/system76/galp3-c/scratch/main.c b/src/board/system76/galp3-c/scratch/main.c index ff286b0..6031201 100644 --- a/src/board/system76/galp3-c/scratch/main.c +++ b/src/board/system76/galp3-c/scratch/main.c @@ -1,6 +1,8 @@ #include #include +#include + volatile uint8_t __xdata __at(0x103B) ECINDAR0; volatile uint8_t __xdata __at(0x103C) ECINDAR1; volatile uint8_t __xdata __at(0x103D) ECINDAR2; @@ -26,9 +28,21 @@ static int flash_transaction(uint32_t offset, uint8_t * data, int length, bool r return i; } +#define PMC_TIMEOUT 1000 + +static void pmc_event(struct Pmc * pmc) { + uint8_t sts = pmc_status(pmc); + if (sts & PMC_STS_IBF) { + uint8_t data = pmc_read(pmc); + if (sts & PMC_STS_CMD) { + } else { + } + } +} + // Main program while running in scratch ROM void main(void) { - // TODO: implement flashing protocol and prevent access to flash - // data (i.e. calling functions in CSEG) during this process - for (;;) {} + for (;;) { + pmc_event(&PMC_1); + } } diff --git a/src/board/system76/galp3-c/scratch/pmc.c b/src/board/system76/galp3-c/scratch/pmc.c new file mode 100644 index 0000000..ee75943 --- /dev/null +++ b/src/board/system76/galp3-c/scratch/pmc.c @@ -0,0 +1,32 @@ +#include + +#define PMC(NUM) { \ + .status = &PM ## NUM ## STS, \ + .data_out = &PM ## NUM ## DO, \ + .data_in = &PM ## NUM ## DI, \ + .control = &PM ## NUM ## CTL, \ +} + +struct Pmc __code PMC_1 = PMC(1); + +uint8_t pmc_status(struct Pmc * pmc) { + return *(pmc->status); +} + +uint8_t pmc_read(struct Pmc * pmc) { + return *(pmc->data_in); +} + +static bool pmc_wait(struct Pmc * pmc, int timeout) { + while (pmc_status(pmc) & PMC_STS_OBF) { + if (timeout == 0) return false; + timeout -= 1; + } + return true; +} + +bool pmc_write(struct Pmc * pmc, uint8_t data, int timeout) { + if (!pmc_wait(pmc, timeout)) return false; + *(pmc->data_out) = data; + return true; +} diff --git a/src/board/system76/galp3-c/scratch/scratch.mk b/src/board/system76/galp3-c/scratch/scratch.mk new file mode 100644 index 0000000..bcfb08a --- /dev/null +++ b/src/board/system76/galp3-c/scratch/scratch.mk @@ -0,0 +1,27 @@ +SCRATCH_OBJ=$(patsubst src/%.c,$(BUILD)/%.rel,$(SCRATCH_SRC)) +SCRATCH_CC=\ + sdcc \ + -mmcs51 \ + --model-small \ + --code-size 2048 \ + --Werror + +# Convert from binary file to C header +$(BUILD)/include/scratch.h: $(BUILD)/scratch.rom + @mkdir -p $(@D) + xxd --include < $< > $@ + +# Convert from Intel Hex file to binary file +$(BUILD)/scratch.rom: $(BUILD)/scratch.ihx + @mkdir -p $(@D) + makebin -p < $< > $@ + +# Link object files into Intel Hex file +$(BUILD)/scratch.ihx: $(SCRATCH_OBJ) + @mkdir -p $(@D) + $(SCRATCH_CC) -o $@ $^ + +# Compile C files into object files +$(SCRATCH_OBJ): $(BUILD)/%.rel: src/%.c $(SCRATCH_INCLUDE) + @mkdir -p $(@D) + $(SCRATCH_CC) $(SCRATCH_CFLAGS) -o $@ -c $<