]> git.ipfire.org Git - thirdparty/u-boot.git/blame - arch/arm/mach-uniphier/micro-support-card.c
common: Drop net.h from common header
[thirdparty/u-boot.git] / arch / arm / mach-uniphier / micro-support-card.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
5894ca00 2/*
499c8679 3 * Copyright (C) 2012-2015 Panasonic Corporation
2ba1d398 4 * Copyright (C) 2015-2020 Socionext Inc.
499c8679 5 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
5894ca00
MY
6 */
7
8#include <common.h>
2ba1d398 9#include <fdt_support.h>
90526e9f
SG
10#include <net.h>
11#include <dm/of.h>
8469700b 12#include <linux/ctype.h>
f6e7f07c 13#include <linux/io.h>
107b3fb4
MY
14
15#include "micro-support-card.h"
5894ca00 16
2ba1d398
MY
17#define SMC911X_OFFSET 0x00000
18#define LED_OFFSET 0x90000
19#define NS16550A_OFFSET 0xb0000
20#define MICRO_SUPPORT_CARD_RESET 0xd0034
21#define MICRO_SUPPORT_CARD_REVISION 0xd00e0
d7728aa4 22
92d6524b 23static bool support_card_found;
2ba1d398 24static void __iomem *support_card_base;
92d6524b
MY
25
26static void support_card_detect(void)
27{
28 DECLARE_GLOBAL_DATA_PTR;
29 const void *fdt = gd->fdt_blob;
30 int offset;
2ba1d398 31 u64 addr, addr2;
92d6524b
MY
32
33 offset = fdt_node_offset_by_compatible(fdt, 0, "smsc,lan9118");
34 if (offset < 0)
35 return;
36
2ba1d398
MY
37 addr = fdt_get_base_address(fdt, offset);
38 if (addr == OF_BAD_ADDR)
39 return;
40 addr -= SMC911X_OFFSET;
41
92d6524b
MY
42 offset = fdt_node_offset_by_compatible(fdt, 0, "ns16550a");
43 if (offset < 0)
44 return;
45
2ba1d398
MY
46 addr2 = fdt_get_base_address(fdt, offset);
47 if (addr2 == OF_BAD_ADDR)
48 return;
49 addr2 -= NS16550A_OFFSET;
50
51 /* sanity check */
52 if (addr != addr2)
53 return;
54
55 support_card_base = ioremap(addr, 0x100000);
56
92d6524b
MY
57 support_card_found = true;
58}
59
5894ca00
MY
60/*
61 * 0: reset deassert, 1: reset
62 *
63 * bit[0]: LAN, I2C, LED
64 * bit[1]: UART
65 */
ef07a99b 66static void support_card_reset_deassert(void)
5894ca00 67{
2ba1d398 68 writel(0x00010000, support_card_base + MICRO_SUPPORT_CARD_RESET);
5894ca00
MY
69}
70
ef07a99b 71static void support_card_reset(void)
5894ca00 72{
2ba1d398 73 writel(0x00020003, support_card_base + MICRO_SUPPORT_CARD_RESET);
5894ca00
MY
74}
75
76static int support_card_show_revision(void)
77{
78 u32 revision;
79
2ba1d398 80 revision = readl(support_card_base + MICRO_SUPPORT_CARD_REVISION);
499c8679
MY
81 revision &= 0xff;
82
83 /* revision 3.6.x card changed the revision format */
6fc84914
MY
84 printf("SC: Micro Support Card (CPLD version %s%d.%d)\n",
85 revision >> 4 == 6 ? "3." : "",
499c8679
MY
86 revision >> 4, revision & 0xf);
87
5894ca00
MY
88 return 0;
89}
5894ca00
MY
90
91void support_card_init(void)
92{
92d6524b
MY
93 support_card_detect();
94
95 if (!support_card_found)
96 return;
97
ef07a99b 98 support_card_reset();
5894ca00
MY
99 /*
100 * After power on, we need to keep the LAN controller in reset state
101 * for a while. (200 usec)
5894ca00 102 */
66e3efeb 103 udelay(200);
5894ca00 104 support_card_reset_deassert();
6fc84914
MY
105
106 support_card_show_revision();
5894ca00
MY
107}
108
5894ca00
MY
109#if defined(CONFIG_SMC911X)
110#include <netdev.h>
111
112int board_eth_init(bd_t *bis)
113{
92d6524b
MY
114 if (!support_card_found)
115 return 0;
116
2ba1d398 117 return smc911x_initialize(0, (unsigned long)support_card_base + SMC911X_OFFSET);
5894ca00
MY
118}
119#endif
120
e856bdcf 121#if defined(CONFIG_MTD_NOR_FLASH)
5894ca00
MY
122
123#include <mtd/cfi_flash.h>
124
7a3620b2
MY
125struct memory_bank {
126 phys_addr_t base;
127 unsigned long size;
128};
5894ca00 129
7a3620b2 130static int mem_is_flash(const struct memory_bank *mem)
5894ca00
MY
131{
132 const int loop = 128;
133 u32 *scratch_addr;
134 u32 saved_value;
135 int ret = 1;
136 int i;
137
7a3620b2
MY
138 /* just in case, use the tail of the memory bank */
139 scratch_addr = map_physmem(mem->base + mem->size - sizeof(u32) * loop,
140 sizeof(u32) * loop, MAP_NOCACHE);
5894ca00
MY
141
142 for (i = 0; i < loop; i++, scratch_addr++) {
143 saved_value = readl(scratch_addr);
144 writel(~saved_value, scratch_addr);
145 if (readl(scratch_addr) != saved_value) {
146 /* We assume no memory or SRAM here. */
147 writel(saved_value, scratch_addr);
148 ret = 0;
149 break;
150 }
151 }
152
153 unmap_physmem(scratch_addr, MAP_NOCACHE);
154
155 return ret;
156}
157
9879842c
MY
158/* {address, size} */
159static const struct memory_bank memory_banks[] = {
d5ed8c57 160 {0x42000000, 0x01f00000},
7a3620b2
MY
161};
162
7a3620b2
MY
163static const struct memory_bank
164*flash_banks_list[CONFIG_SYS_MAX_FLASH_BANKS_DETECT];
165
166phys_addr_t cfi_flash_bank_addr(int i)
5894ca00 167{
7a3620b2
MY
168 return flash_banks_list[i]->base;
169}
5894ca00 170
7a3620b2
MY
171unsigned long cfi_flash_bank_size(int i)
172{
173 return flash_banks_list[i]->size;
174}
175
176static void detect_num_flash_banks(void)
177{
178 const struct memory_bank *memory_bank, *end;
179
180 cfi_flash_num_flash_banks = 0;
181
9879842c
MY
182 memory_bank = memory_banks;
183 end = memory_bank + ARRAY_SIZE(memory_banks);
7a3620b2
MY
184
185 for (; memory_bank < end; memory_bank++) {
186 if (cfi_flash_num_flash_banks >=
187 CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
188 break;
189
190 if (mem_is_flash(memory_bank)) {
191 flash_banks_list[cfi_flash_num_flash_banks] =
192 memory_bank;
193
194 debug("flash bank found: base = 0x%lx, size = 0x%lx\n",
11d3ede4
MY
195 (unsigned long)memory_bank->base,
196 (unsigned long)memory_bank->size);
7a3620b2 197 cfi_flash_num_flash_banks++;
5894ca00 198 }
5894ca00
MY
199 }
200
7a3620b2
MY
201 debug("number of flash banks: %d\n", cfi_flash_num_flash_banks);
202}
e856bdcf 203#else /* CONFIG_MTD_NOR_FLASH */
ef07a99b 204static void detect_num_flash_banks(void)
7a3620b2
MY
205{
206};
e856bdcf 207#endif /* CONFIG_MTD_NOR_FLASH */
7a3620b2
MY
208
209void support_card_late_init(void)
210{
92d6524b
MY
211 if (!support_card_found)
212 return;
213
7a3620b2 214 detect_num_flash_banks();
5894ca00 215}
8469700b
MY
216
217static const u8 ledval_num[] = {
218 0x7e, /* 0 */
219 0x0c, /* 1 */
220 0xb6, /* 2 */
221 0x9e, /* 3 */
222 0xcc, /* 4 */
223 0xda, /* 5 */
224 0xfa, /* 6 */
225 0x4e, /* 7 */
226 0xfe, /* 8 */
227 0xde, /* 9 */
228};
229
230static const u8 ledval_alpha[] = {
231 0xee, /* A */
232 0xf8, /* B */
233 0x72, /* C */
234 0xbc, /* D */
235 0xf2, /* E */
236 0xe2, /* F */
237 0x7a, /* G */
238 0xe8, /* H */
239 0x08, /* I */
240 0x3c, /* J */
241 0xea, /* K */
242 0x70, /* L */
243 0x6e, /* M */
244 0xa8, /* N */
245 0xb8, /* O */
246 0xe6, /* P */
247 0xce, /* Q */
248 0xa0, /* R */
249 0xc8, /* S */
250 0x8c, /* T */
251 0x7c, /* U */
252 0x54, /* V */
253 0xfc, /* W */
254 0xec, /* X */
255 0xdc, /* Y */
256 0xa4, /* Z */
257};
258
259static u8 char2ledval(char c)
260{
261 if (isdigit(c))
262 return ledval_num[c - '0'];
263 else if (isalpha(c))
264 return ledval_alpha[toupper(c) - 'A'];
265
266 return 0;
267}
268
269void led_puts(const char *s)
270{
271 int i;
272 u32 val = 0;
273
92d6524b
MY
274 if (!support_card_found)
275 return;
276
8469700b
MY
277 if (!s)
278 return;
279
280 for (i = 0; i < 4; i++) {
281 val <<= 8;
282 val |= char2ledval(*s);
283 if (*s != '\0')
284 s++;
285 }
286
2ba1d398 287 writel(~val, support_card_base + LED_OFFSET);
8469700b 288}