Debian Codegraph @

src/soc/mediatek/mt8188/spi.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only OR MIT */
2
3 /*
4 * This file is created based on MT8188 Functional Specification
5 * Chapter number: 5.8, 5.19
6 */
7
8 #include <assert.h>
9 #include <console/console.h>
10 #include <device/mmio.h>
11 #include <gpio.h>
12 #include <soc/addressmap.h>
13 #include <soc/flash_controller_common.h>
14 #include <soc/spi.h>
15 #include <spi_flash.h>
16
17 struct mtk_spi_bus spi_bus[SPI_BUS_NUMBER] = {
18 {
19 .regs = (void *)SPI0_BASE,
20 .cs_gpio = GPIO(SPIM0_CSB),
21 },
22 {
23 .regs = (void *)SPI1_BASE,
24 .cs_gpio = GPIO(SPIM1_CSB),
25 },
26 {
27 .regs = (void *)SPI2_BASE,
28 .cs_gpio = GPIO(SPIM2_CSB),
29 },
30 {
31 .regs = (void *)SPI3_BASE,
32 .cs_gpio = GPIO(DPI_D12),
33 },
34 {
35 .regs = (void *)SPI4_BASE,
36 .cs_gpio = GPIO(GPIO12),
37 },
38 {
39 .regs = (void *)SPI5_BASE,
40 .cs_gpio = GPIO(GPIO00),
41 },
42 };
43
44 static const struct pad_func pad_funcs[SPI_BUS_NUMBER][4] = {
45 {
46 PAD_FUNC_DOWN(SPIM0_MISO, SPIM0_MISO),
47 PAD_FUNC_GPIO(SPIM0_CSB),
48 PAD_FUNC_DOWN(SPIM0_MOSI, SPIM0_MOSI),
49 PAD_FUNC_DOWN(SPIM0_CLK, SPIM0_CLK),
50 },
51 {
52 PAD_FUNC_DOWN(SPIM1_MISO, SPIM1_MISO),
53 PAD_FUNC_GPIO(SPIM1_CSB),
54 PAD_FUNC_DOWN(SPIM1_MOSI, SPIM1_MOSI),
55 PAD_FUNC_DOWN(SPIM1_CLK, SPIM1_CLK),
56 },
57 {
58 PAD_FUNC_DOWN(SPIM2_MISO, SPIM2_MISO),
59 PAD_FUNC_GPIO(SPIM2_CSB),
60 PAD_FUNC_DOWN(SPIM2_MOSI, SPIM2_MOSI),
61 PAD_FUNC_DOWN(SPIM2_CLK, SPIM2_CLK),
62 },
63 {
64 PAD_FUNC_DOWN(DPI_D15, SPIM3_MISO),
65 PAD_FUNC_GPIO(DPI_D12),
66 PAD_FUNC_DOWN(DPI_D14, SPIM3_MOSI),
67 PAD_FUNC_DOWN(DPI_D13, SPIM3_CLK),
68 },
69 {
70 PAD_FUNC_DOWN(GPIO15, SPIM4_MISO),
71 PAD_FUNC_GPIO(GPIO12),
72 PAD_FUNC_DOWN(GPIO14, SPIM4_MOSI),
73 PAD_FUNC_DOWN(GPIO13, SPIM4_CLK),
74 },
75 {
76 PAD_FUNC_DOWN(GPIO03, SPIM5_MISO),
77 PAD_FUNC_GPIO(GPIO00),
78 PAD_FUNC_DOWN(GPIO02, SPIM5_MOSI),
79 PAD_FUNC_DOWN(GPIO01, SPIM5_CLK),
80 },
81 };
82
83 static const struct pad_func nor_pinmux[4] = {
84 /* GPIO 125 ~ 128 */
85 PAD_FUNC(DMIC1_CLK, SPINOR_CK, GPIO_PULL_DOWN),
86 PAD_FUNC(DMIC1_DAT, SPINOR_CS, GPIO_PULL_UP),
87 PAD_FUNC(DMIC1_DAT_R, SPINOR_IO0, GPIO_PULL_DOWN),
88 PAD_FUNC(DMIC2_CLK, SPINOR_IO1, GPIO_PULL_DOWN),
89 };
90
91 void mtk_snfc_init(void)
92 {
93 const struct pad_func *ptr = NULL;
94
95 ptr = nor_pinmux;
96 for (size_t i = 0; i < ARRAY_SIZE(nor_pinmux); i++) {
97 gpio_set_pull(ptr[i].gpio, GPIO_PULL_ENABLE, ptr[i].select);
98 gpio_set_mode(ptr[i].gpio, ptr[i].func);
99
100 if (gpio_set_driving(ptr[i].gpio, GPIO_DRV_8_MA) < 0)
101 printk(BIOS_ERR,
102 "%s: failed to set pin drive to 8 mA for %d\n",
103 __func__, ptr[i].gpio.id);
104 else
105 printk(BIOS_DEBUG, "%s: got pin drive: %#x\n", __func__,
106 gpio_get_driving(ptr[i].gpio));
107 }
108 }
109
110 void mtk_spi_set_gpio_pinmux(unsigned int bus, enum spi_pad_mask pad_select)
111 {
112 assert(bus < SPI_BUS_NUMBER);
113 const struct pad_func *ptr;
114
115 ptr = pad_funcs[bus];
116
117 for (unsigned int i = 0; i < ARRAY_SIZE(pad_funcs[0]); i++)
118 gpio_set_mode(ptr[i].gpio, ptr[i].func);
119 }
120
121 static const struct spi_ctrlr spi_flash_ctrlr = {
122 .max_xfer_size = 65535,
123 .flash_probe = mtk_spi_flash_probe,
124 };
125
126 const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
127 {
128 .ctrlr = &spi_ctrlr,
129 .bus_start = 0,
130 .bus_end = SPI_BUS_NUMBER - 1,
131 },
132 {
133 .ctrlr = &spi_flash_ctrlr,
134 .bus_start = CONFIG_BOOT_DEVICE_SPI_FLASH_BUS,
135 .bus_end = CONFIG_BOOT_DEVICE_SPI_FLASH_BUS,
136 },
137 };
138
139 const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map);