blob: 46879019fdaa2443b6720ebd04a91371d22640c5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada5894ca02014-10-03 19:21:06 +09002/*
Masahiro Yamada499c8672016-08-25 17:02:31 +09003 * Copyright (C) 2012-2015 Panasonic Corporation
4 * Copyright (C) 2015-2016 Socionext Inc.
5 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada5894ca02014-10-03 19:21:06 +09006 */
7
8#include <common.h>
Masahiro Yamada84697002015-09-22 00:27:31 +09009#include <linux/ctype.h>
Masahiro Yamadaf6e7f072015-05-29 17:30:00 +090010#include <linux/io.h>
Masahiro Yamada107b3fb2016-01-09 01:51:13 +090011
12#include "micro-support-card.h"
Masahiro Yamada5894ca02014-10-03 19:21:06 +090013
Masahiro Yamadad7728aa2015-09-22 00:27:32 +090014#define MICRO_SUPPORT_CARD_BASE 0x43f00000
15#define SMC911X_BASE ((MICRO_SUPPORT_CARD_BASE) + 0x00000)
16#define LED_BASE ((MICRO_SUPPORT_CARD_BASE) + 0x90000)
17#define NS16550A_BASE ((MICRO_SUPPORT_CARD_BASE) + 0xb0000)
18#define MICRO_SUPPORT_CARD_RESET ((MICRO_SUPPORT_CARD_BASE) + 0xd0034)
19#define MICRO_SUPPORT_CARD_REVISION ((MICRO_SUPPORT_CARD_BASE) + 0xd00E0)
20
Masahiro Yamada92d65242019-07-10 20:07:36 +090021static bool support_card_found;
22
23static void support_card_detect(void)
24{
25 DECLARE_GLOBAL_DATA_PTR;
26 const void *fdt = gd->fdt_blob;
27 int offset;
28
29 offset = fdt_node_offset_by_compatible(fdt, 0, "smsc,lan9118");
30 if (offset < 0)
31 return;
32
33 offset = fdt_node_offset_by_compatible(fdt, 0, "ns16550a");
34 if (offset < 0)
35 return;
36
37 support_card_found = true;
38}
39
Masahiro Yamada5894ca02014-10-03 19:21:06 +090040/*
41 * 0: reset deassert, 1: reset
42 *
43 * bit[0]: LAN, I2C, LED
44 * bit[1]: UART
45 */
Masahiro Yamadaef07a992017-01-15 14:59:06 +090046static void support_card_reset_deassert(void)
Masahiro Yamada5894ca02014-10-03 19:21:06 +090047{
Masahiro Yamada36223f52016-03-18 16:41:46 +090048 writel(0x00010000, MICRO_SUPPORT_CARD_RESET);
Masahiro Yamada5894ca02014-10-03 19:21:06 +090049}
50
Masahiro Yamadaef07a992017-01-15 14:59:06 +090051static void support_card_reset(void)
Masahiro Yamada5894ca02014-10-03 19:21:06 +090052{
Masahiro Yamada36223f52016-03-18 16:41:46 +090053 writel(0x00020003, MICRO_SUPPORT_CARD_RESET);
Masahiro Yamada5894ca02014-10-03 19:21:06 +090054}
55
56static int support_card_show_revision(void)
57{
58 u32 revision;
59
Masahiro Yamada98798422015-09-11 20:17:45 +090060 revision = readl(MICRO_SUPPORT_CARD_REVISION);
Masahiro Yamada499c8672016-08-25 17:02:31 +090061 revision &= 0xff;
62
63 /* revision 3.6.x card changed the revision format */
Masahiro Yamada6fc84912017-02-20 17:32:19 +090064 printf("SC: Micro Support Card (CPLD version %s%d.%d)\n",
65 revision >> 4 == 6 ? "3." : "",
Masahiro Yamada499c8672016-08-25 17:02:31 +090066 revision >> 4, revision & 0xf);
67
Masahiro Yamada5894ca02014-10-03 19:21:06 +090068 return 0;
69}
Masahiro Yamada5894ca02014-10-03 19:21:06 +090070
71void support_card_init(void)
72{
Masahiro Yamada92d65242019-07-10 20:07:36 +090073 support_card_detect();
74
75 if (!support_card_found)
76 return;
77
Masahiro Yamadaef07a992017-01-15 14:59:06 +090078 support_card_reset();
Masahiro Yamada5894ca02014-10-03 19:21:06 +090079 /*
80 * After power on, we need to keep the LAN controller in reset state
81 * for a while. (200 usec)
Masahiro Yamada5894ca02014-10-03 19:21:06 +090082 */
Masahiro Yamada66e3efe2016-10-08 13:25:29 +090083 udelay(200);
Masahiro Yamada5894ca02014-10-03 19:21:06 +090084 support_card_reset_deassert();
Masahiro Yamada6fc84912017-02-20 17:32:19 +090085
86 support_card_show_revision();
Masahiro Yamada5894ca02014-10-03 19:21:06 +090087}
88
Masahiro Yamada5894ca02014-10-03 19:21:06 +090089#if defined(CONFIG_SMC911X)
90#include <netdev.h>
91
92int board_eth_init(bd_t *bis)
93{
Masahiro Yamada92d65242019-07-10 20:07:36 +090094 if (!support_card_found)
95 return 0;
96
Masahiro Yamadad7728aa2015-09-22 00:27:32 +090097 return smc911x_initialize(0, SMC911X_BASE);
Masahiro Yamada5894ca02014-10-03 19:21:06 +090098}
99#endif
100
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900101#if defined(CONFIG_MTD_NOR_FLASH)
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900102
103#include <mtd/cfi_flash.h>
104
Masahiro Yamada7a3620b2014-12-06 00:03:26 +0900105struct memory_bank {
106 phys_addr_t base;
107 unsigned long size;
108};
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900109
Masahiro Yamada7a3620b2014-12-06 00:03:26 +0900110static int mem_is_flash(const struct memory_bank *mem)
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900111{
112 const int loop = 128;
113 u32 *scratch_addr;
114 u32 saved_value;
115 int ret = 1;
116 int i;
117
Masahiro Yamada7a3620b2014-12-06 00:03:26 +0900118 /* just in case, use the tail of the memory bank */
119 scratch_addr = map_physmem(mem->base + mem->size - sizeof(u32) * loop,
120 sizeof(u32) * loop, MAP_NOCACHE);
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900121
122 for (i = 0; i < loop; i++, scratch_addr++) {
123 saved_value = readl(scratch_addr);
124 writel(~saved_value, scratch_addr);
125 if (readl(scratch_addr) != saved_value) {
126 /* We assume no memory or SRAM here. */
127 writel(saved_value, scratch_addr);
128 ret = 0;
129 break;
130 }
131 }
132
133 unmap_physmem(scratch_addr, MAP_NOCACHE);
134
135 return ret;
136}
137
Masahiro Yamada98798422015-09-11 20:17:45 +0900138/* {address, size} */
139static const struct memory_bank memory_banks[] = {
Masahiro Yamadad5ed8c52015-09-11 20:17:47 +0900140 {0x42000000, 0x01f00000},
Masahiro Yamada7a3620b2014-12-06 00:03:26 +0900141};
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900142
Masahiro Yamada7a3620b2014-12-06 00:03:26 +0900143static const struct memory_bank
144*flash_banks_list[CONFIG_SYS_MAX_FLASH_BANKS_DETECT];
145
146phys_addr_t cfi_flash_bank_addr(int i)
147{
148 return flash_banks_list[i]->base;
149}
150
151unsigned long cfi_flash_bank_size(int i)
152{
153 return flash_banks_list[i]->size;
154}
155
156static void detect_num_flash_banks(void)
157{
158 const struct memory_bank *memory_bank, *end;
159
160 cfi_flash_num_flash_banks = 0;
161
Masahiro Yamada98798422015-09-11 20:17:45 +0900162 memory_bank = memory_banks;
163 end = memory_bank + ARRAY_SIZE(memory_banks);
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900164
Masahiro Yamada7a3620b2014-12-06 00:03:26 +0900165 for (; memory_bank < end; memory_bank++) {
166 if (cfi_flash_num_flash_banks >=
167 CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
168 break;
169
170 if (mem_is_flash(memory_bank)) {
171 flash_banks_list[cfi_flash_num_flash_banks] =
172 memory_bank;
173
174 debug("flash bank found: base = 0x%lx, size = 0x%lx\n",
Masahiro Yamada11d3ede2016-02-26 18:59:45 +0900175 (unsigned long)memory_bank->base,
176 (unsigned long)memory_bank->size);
Masahiro Yamada7a3620b2014-12-06 00:03:26 +0900177 cfi_flash_num_flash_banks++;
178 }
179 }
180
181 debug("number of flash banks: %d\n", cfi_flash_num_flash_banks);
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900182}
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900183#else /* CONFIG_MTD_NOR_FLASH */
Masahiro Yamadaef07a992017-01-15 14:59:06 +0900184static void detect_num_flash_banks(void)
Masahiro Yamada7a3620b2014-12-06 00:03:26 +0900185{
186};
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900187#endif /* CONFIG_MTD_NOR_FLASH */
Masahiro Yamada7a3620b2014-12-06 00:03:26 +0900188
189void support_card_late_init(void)
190{
Masahiro Yamada92d65242019-07-10 20:07:36 +0900191 if (!support_card_found)
192 return;
193
Masahiro Yamada7a3620b2014-12-06 00:03:26 +0900194 detect_num_flash_banks();
195}
Masahiro Yamada84697002015-09-22 00:27:31 +0900196
197static const u8 ledval_num[] = {
198 0x7e, /* 0 */
199 0x0c, /* 1 */
200 0xb6, /* 2 */
201 0x9e, /* 3 */
202 0xcc, /* 4 */
203 0xda, /* 5 */
204 0xfa, /* 6 */
205 0x4e, /* 7 */
206 0xfe, /* 8 */
207 0xde, /* 9 */
208};
209
210static const u8 ledval_alpha[] = {
211 0xee, /* A */
212 0xf8, /* B */
213 0x72, /* C */
214 0xbc, /* D */
215 0xf2, /* E */
216 0xe2, /* F */
217 0x7a, /* G */
218 0xe8, /* H */
219 0x08, /* I */
220 0x3c, /* J */
221 0xea, /* K */
222 0x70, /* L */
223 0x6e, /* M */
224 0xa8, /* N */
225 0xb8, /* O */
226 0xe6, /* P */
227 0xce, /* Q */
228 0xa0, /* R */
229 0xc8, /* S */
230 0x8c, /* T */
231 0x7c, /* U */
232 0x54, /* V */
233 0xfc, /* W */
234 0xec, /* X */
235 0xdc, /* Y */
236 0xa4, /* Z */
237};
238
239static u8 char2ledval(char c)
240{
241 if (isdigit(c))
242 return ledval_num[c - '0'];
243 else if (isalpha(c))
244 return ledval_alpha[toupper(c) - 'A'];
245
246 return 0;
247}
248
249void led_puts(const char *s)
250{
251 int i;
252 u32 val = 0;
253
Masahiro Yamada92d65242019-07-10 20:07:36 +0900254 if (!support_card_found)
255 return;
256
Masahiro Yamada84697002015-09-22 00:27:31 +0900257 if (!s)
258 return;
259
260 for (i = 0; i < 4; i++) {
261 val <<= 8;
262 val |= char2ledval(*s);
263 if (*s != '\0')
264 s++;
265 }
266
Masahiro Yamadad7728aa2015-09-22 00:27:32 +0900267 writel(~val, LED_BASE);
Masahiro Yamada84697002015-09-22 00:27:31 +0900268}