coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
spi.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <assert.h>
4 #include <device/mmio.h>
5 #include <soc/addressmap.h>
7 #include <soc/gpio.h>
8 #include <soc/spi.h>
9 
11  {
12  .regs = (void *)SPI0_BASE,
13  .cs_gpio = GPIO(SPIM0_CSB),
14  },
15  {
16  .regs = (void *)SPI1_BASE,
17  .cs_gpio = GPIO(SPIM1_CSB),
18  },
19  {
20  .regs = (void *)SPI2_BASE,
21  .cs_gpio = GPIO(SPIM2_CSB),
22  },
23  {
24  .regs = (void *)SPI3_BASE,
25  .cs_gpio = GPIO(PWRAP_SPI_CSN),
26  },
27  {
28  .regs = (void *)SPI4_BASE,
29  .cs_gpio = GPIO(DGI_D2),
30  },
31  {
32  .regs = (void *)SPI5_BASE,
33  .cs_gpio = GPIO(DGI_D6),
34  },
35 };
36 
37 struct pad_func {
38  u8 pin_id;
39  u8 func;
40 };
41 
42 #define PAD_FUNC(name, func) {PAD_##name##_ID, PAD_##name##_FUNC_##func}
43 #define PAD_FUNC_GPIO(name) {PAD_##name##_ID, 0}
44 
45 static const struct pad_func pad0_funcs[SPI_BUS_NUMBER][4] = {
46  {
47  PAD_FUNC(SPIM0_MI, SPIM0_MI),
48  PAD_FUNC_GPIO(SPIM0_CSB),
49  PAD_FUNC(SPIM0_MO, SPIM0_MO),
50  PAD_FUNC(SPIM0_CLK, SPIM0_CLK),
51  },
52  {
53  PAD_FUNC(SPIM1_MI, SPIM1_MI),
54  PAD_FUNC_GPIO(SPIM1_CSB),
55  PAD_FUNC(SPIM1_MO, SPIM1_MO),
56  PAD_FUNC(SPIM1_CLK, SPIM1_CLK),
57  },
58  {
59  PAD_FUNC(SPIM2_MI, PIM2_MI),
60  PAD_FUNC_GPIO(SPIM2_CSB),
61  PAD_FUNC(SPIM2_MO, SPIM2_MO),
62  PAD_FUNC(SPIM2_CLK, SPIM2_CLK),
63  },
64  {
65  PAD_FUNC(PWRAP_SPI_MI, SPIM3_MI),
66  PAD_FUNC_GPIO(PWRAP_SPI_CSN),
67  PAD_FUNC(PWRAP_SPI_MO, SPIM3_MO),
68  PAD_FUNC(PWRAP_SPI_CK, SPIM3_CLK),
69  },
70  {
71  PAD_FUNC(DGI_D3, SPIM4_MI),
72  PAD_FUNC_GPIO(DGI_D2),
73  PAD_FUNC(DGI_D1, SPIM4_MO),
74  PAD_FUNC(DGI_D0, SPIM4_CLK),
75  },
76  {
77  PAD_FUNC(DGI_D7, SPIM5_MI),
78  PAD_FUNC_GPIO(DGI_D6),
79  PAD_FUNC(DGI_D5, SPIM5_MO),
80  PAD_FUNC(DGI_D4, SPIM5_CLK),
81  },
82 };
83 
84 void mtk_spi_set_gpio_pinmux(unsigned int bus, enum spi_pad_mask pad_select)
85 {
87  assert(pad_select == SPI_PAD0_MASK);
88  const struct pad_func *ptr = NULL;
89 
90  ptr = pad0_funcs[bus];
91  for (int i = 0; i < 4; i++)
92  gpio_set_mode((gpio_t){.id = ptr[i].pin_id}, ptr[i].func);
93 }
94 
95 static const struct spi_ctrlr spi_flash_ctrlr = {
96  .max_xfer_size = 65535,
97  .flash_probe = mtk_spi_flash_probe,
98 };
99 
100 const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
101  {
102  .ctrlr = &spi_ctrlr,
103  .bus_start = 0,
104  .bus_end = SPI_BUS_NUMBER - 1,
105  },
106  {
107  .ctrlr = &spi_flash_ctrlr,
108  .bus_start = CONFIG_BOOT_DEVICE_SPI_FLASH_BUS,
109  .bus_end = CONFIG_BOOT_DEVICE_SPI_FLASH_BUS,
110  },
111 };
112 
#define assert(statement)
Definition: assert.h:74
#define ARRAY_SIZE(a)
Definition: helpers.h:12
@ GPIO
Definition: chip.h:84
int mtk_spi_flash_probe(const struct spi_slave *spi, struct spi_flash *flash)
const struct spi_ctrlr_buses spi_ctrlr_bus_map[]
Definition: spi.c:401
const size_t spi_ctrlr_bus_map_count
Definition: spi.c:408
void gpio_set_mode(gpio_t gpio, int mode)
Definition: gpio.c:45
const struct spi_ctrlr spi_ctrlr
Definition: spi.c:261
#define SPI_BUS_NUMBER
Definition: spi.h:8
struct mtk_spi_bus spi_bus[SPI_BUS_NUMBER]
Definition: spi.c:11
void mtk_spi_set_gpio_pinmux(unsigned int bus, enum spi_pad_mask pad_select)
Definition: spi.c:18
static const struct spi_ctrlr spi_flash_ctrlr
Definition: spi.c:95
static const struct pad_func pad0_funcs[SPI_BUS_NUMBER][4]
Definition: spi.c:45
#define PAD_FUNC(name, func)
Definition: spi.c:42
#define PAD_FUNC_GPIO(name)
Definition: spi.c:43
#define SPI1_BASE
Definition: addressmap.h:14
#define SPI2_BASE
Definition: addressmap.h:15
#define SPI0_BASE
Definition: addressmap.h:13
#define SPI5_BASE
Definition: addressmap.h:43
#define SPI4_BASE
Definition: addressmap.h:42
#define SPI3_BASE
Definition: addressmap.h:41
spi_pad_mask
Definition: spi_common.h:45
@ SPI_PAD0_MASK
Definition: spi_common.h:46
#define NULL
Definition: stddef.h:19
uint8_t u8
Definition: stdint.h:45
Definition: device.h:76
gpio_t cs_gpio
Definition: spi_common.h:77
struct mtk_spi_regs * regs
Definition: spi_common.h:74
u8 func
Definition: bootblock.c:15
u8 pin_id
Definition: bootblock.c:14
const struct spi_ctrlr * ctrlr
Definition: spi-generic.h:175
uint32_t max_xfer_size
Definition: spi-generic.h:158