Skip to content

Commit fa9fe76

Browse files
committed
spi: use GPIO and CCU base address from soc_info
Now that we store the base addresses of the GPIO and clock controllers for each SoC in our soc table, let's use those values instead of determining them ourselves, in the SPI flash code. This involves several changes: - removing the base offset from each register address - supporting the NCAT2 pin controller, with a larger per-port size - using info about clock and pinctrl generation from soc table This will simplify future SoC support, as we can simply put the respective base addresses in the soc table, without touching the SPI flash code. Signed-off-by: Andre Przywara <osp@andrep.de>
1 parent 7f501f3 commit fa9fe76

1 file changed

Lines changed: 38 additions & 53 deletions

File tree

fel-spiflash.c

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ void fel_writel(feldev_handle *dev, uint32_t addr, uint32_t val);
7575
#define PB (1)
7676
#define PC (2)
7777

78-
#define CCM_SPI0_CLK (0x01C20000 + 0xA0)
79-
#define CCM_AHB_GATING0 (0x01C20000 + 0x60)
78+
#define CCM_SPI0_CLK_OFF 0xa0
79+
#define CCM_AHB_GATING0_OFF 0x60
8080
#define CCM_AHB_GATE_SPI0 (1 << 20)
81-
#define SUN6I_BUS_SOFT_RST_REG0 (0x01C20000 + 0x2C0)
81+
#define SUN6I_BUS_SOFT_RST_REG0_OFF 0x2c0
8282
#define SUN6I_SPI0_RST (1 << 20)
83-
#define SUNIV_PLL6_CTL (0x01c20000 + 0x28)
84-
#define SUNIV_AHB_APB_CFG (0x01c20000 + 0x54)
83+
#define SUNIV_PLL6_CTL_OFF 0x28
84+
#define SUNIV_AHB_APB_CFG_OFF 0x54
8585

86-
#define H6_CCM_SPI0_CLK (0x03001000 + 0x940)
87-
#define H6_CCM_SPI_BGR (0x03001000 + 0x96C)
86+
#define H6_CCM_SPI0_CLK_OFF 0x940
87+
#define H6_CCM_SPI_BGR_OFF 0x96c
8888
#define H6_CCM_SPI0_GATE_RESET (1 << 0 | 1 << 16)
8989

9090
#define SUNIV_GPC_SPI0 (2)
@@ -122,20 +122,6 @@ void fel_writel(feldev_handle *dev, uint32_t addr, uint32_t val);
122122
#define CCM_SPI0_CLK_DIV_BY_6 (0x1002)
123123
#define CCM_SPI0_CLK_DIV_BY_32 (0x100f)
124124

125-
static uint32_t gpio_base(feldev_handle *dev)
126-
{
127-
soc_info_t *soc_info = dev->soc_info;
128-
switch (soc_info->soc_id) {
129-
case 0x1816: /* V536 */
130-
case 0x1817: /* V831 */
131-
case 0x1728: /* H6 */
132-
case 0x1823: /* H616 */
133-
return 0x0300B000;
134-
default:
135-
return 0x01C20800;
136-
}
137-
}
138-
139125
static uint32_t spi_base(feldev_handle *dev)
140126
{
141127
soc_info_t *soc_info = dev->soc_info;
@@ -162,13 +148,21 @@ static uint32_t spi_base(feldev_handle *dev)
162148
static void gpio_set_cfgpin(feldev_handle *dev, int port_num, int pin_num,
163149
int val)
164150
{
165-
uint32_t port_base = gpio_base(dev) + port_num * 0x24;
166-
uint32_t cfg_reg = port_base + 4 * (pin_num / 8);
167-
uint32_t pin_idx = pin_num % 8;
168-
uint32_t x = readl(cfg_reg);
169-
x &= ~(0x7 << (pin_idx * 4));
170-
x |= val << (pin_idx * 4);
171-
writel(x, cfg_reg);
151+
uint32_t cfg_reg;
152+
uint32_t pin_idx = pin_num % 8;
153+
uint32_t reg;
154+
155+
cfg_reg = dev->soc_info->gpio_base;
156+
if (dev->soc_info->flags & GPIO_NCAT2)
157+
cfg_reg += port_num * 0x30;
158+
else
159+
cfg_reg += port_num * 0x24;
160+
cfg_reg += 4 * (pin_num / 8);
161+
162+
reg = readl(cfg_reg);
163+
reg &= ~(0xf << (pin_idx * 4));
164+
reg |= val << (pin_idx * 4);
165+
writel(reg, cfg_reg);
172166
}
173167

174168
static bool spi_is_sun6i(feldev_handle *dev)
@@ -184,27 +178,15 @@ static bool spi_is_sun6i(feldev_handle *dev)
184178
}
185179
}
186180

187-
static bool soc_is_h6_style(feldev_handle *dev)
188-
{
189-
soc_info_t *soc_info = dev->soc_info;
190-
switch (soc_info->soc_id) {
191-
case 0x1816: /* V536 */
192-
case 0x1817: /* V831 */
193-
case 0x1728: /* H6 */
194-
case 0x1823: /* H616 */
195-
return true;
196-
default:
197-
return false;
198-
}
199-
}
200-
201181
/*
202182
* Init the SPI0 controller and setup pins muxing.
203183
*/
204184
static bool spi0_init(feldev_handle *dev)
205185
{
206186
uint32_t reg_val;
207187
soc_info_t *soc_info = dev->soc_info;
188+
uint32_t ccu_base;
189+
208190
if (!soc_info) {
209191
printf("Unable to fetch device information. "
210192
"Possibly unknown device.\n");
@@ -265,21 +247,22 @@ static bool spi0_init(feldev_handle *dev)
265247
return false;
266248
}
267249

268-
if (soc_is_h6_style(dev)) {
269-
reg_val = readl(H6_CCM_SPI_BGR);
250+
ccu_base = dev->soc_info->ccu_base;
251+
if (dev->soc_info->flags & H6_STYLE_CLOCKS) {
252+
reg_val = readl(ccu_base + H6_CCM_SPI_BGR_OFF);
270253
reg_val |= H6_CCM_SPI0_GATE_RESET;
271-
writel(reg_val, H6_CCM_SPI_BGR);
254+
writel(reg_val, ccu_base + H6_CCM_SPI_BGR_OFF);
272255
} else {
273256
if (spi_is_sun6i(dev)) {
274257
/* Deassert SPI0 reset */
275-
reg_val = readl(SUN6I_BUS_SOFT_RST_REG0);
258+
reg_val = readl(ccu_base + SUN6I_BUS_SOFT_RST_REG0_OFF);
276259
reg_val |= SUN6I_SPI0_RST;
277-
writel(reg_val, SUN6I_BUS_SOFT_RST_REG0);
260+
writel(reg_val, ccu_base + SUN6I_BUS_SOFT_RST_REG0_OFF);
278261
}
279262

280-
reg_val = readl(CCM_AHB_GATING0);
263+
reg_val = readl(ccu_base + CCM_AHB_GATING0_OFF);
281264
reg_val |= CCM_AHB_GATE_SPI0;
282-
writel(reg_val, CCM_AHB_GATING0);
265+
writel(reg_val, ccu_base + CCM_AHB_GATING0_OFF);
283266
}
284267

285268
if (soc_info->soc_id == 0x1663) { /* suniv F1C100s */
@@ -291,18 +274,20 @@ static bool spi0_init(feldev_handle *dev)
291274
*/
292275

293276
/* Set PLL6 to 600MHz */
294-
writel(0x80041801, SUNIV_PLL6_CTL);
277+
writel(0x80041801, ccu_base + SUNIV_PLL6_CTL_OFF);
295278
/* PLL6:AHB:APB = 6:2:1 */
296-
writel(0x00003180, SUNIV_AHB_APB_CFG);
279+
writel(0x00003180, ccu_base + SUNIV_AHB_APB_CFG_OFF);
297280
/* divide by 32 */
298281
writel(CCM_SPI0_CLK_DIV_BY_32, SUN6I_SPI0_CCTL);
299282
} else {
300283
/* divide 24MHz OSC by 4 */
301284
writel(CCM_SPI0_CLK_DIV_BY_4,
302285
spi_is_sun6i(dev) ? SUN6I_SPI0_CCTL : SUN4I_SPI0_CCTL);
303286
/* Choose 24MHz from OSC24M and enable clock */
304-
writel(1U << 31,
305-
soc_is_h6_style(dev) ? H6_CCM_SPI0_CLK : CCM_SPI0_CLK);
287+
if (dev->soc_info->flags & H6_STYLE_CLOCKS)
288+
writel(1U << 31, ccu_base + H6_CCM_SPI0_CLK_OFF);
289+
else
290+
writel(1U << 31, ccu_base + CCM_SPI0_CLK_OFF);
306291
}
307292

308293
if (spi_is_sun6i(dev)) {

0 commit comments

Comments
 (0)