blob: b09ec54e1f1834301e7ea47270e3327158d3a3d4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +09002/*
Masahiro Yamadad0401702016-08-25 17:02:31 +09003 * Copyright (C) 2012-2015 Panasonic Corporation
Masahiro Yamadabc9daa02020-02-14 20:54:42 +09004 * Copyright (C) 2015-2020 Socionext Inc.
Masahiro Yamadad0401702016-08-25 17:02:31 +09005 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +09006 */
7
Masahiro Yamadae6314412020-05-20 12:31:27 +09008#include <config.h>
Masahiro Yamadabbec08c2020-05-20 12:31:24 +09009#include <dm/of.h>
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090010#include <fdt_support.h>
Masahiro Yamada4e5d28d2015-09-22 00:27:31 +090011#include <linux/ctype.h>
Simon Glassdbd79542020-05-10 11:40:11 -060012#include <linux/delay.h>
Masahiro Yamada663a23f2015-05-29 17:30:00 +090013#include <linux/io.h>
Masahiro Yamadae6314412020-05-20 12:31:27 +090014#include <asm/global_data.h>
Masahiro Yamadaefdf3402016-01-09 01:51:13 +090015
16#include "micro-support-card.h"
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090017
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090018#define SMC911X_OFFSET 0x00000
19#define LED_OFFSET 0x90000
20#define NS16550A_OFFSET 0xb0000
21#define MICRO_SUPPORT_CARD_RESET 0xd0034
22#define MICRO_SUPPORT_CARD_REVISION 0xd00e0
Masahiro Yamada56a20712015-09-22 00:27:32 +090023
Masahiro Yamadabcd22442019-07-10 20:07:36 +090024static bool support_card_found;
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090025static void __iomem *support_card_base;
Masahiro Yamadabcd22442019-07-10 20:07:36 +090026
27static void support_card_detect(void)
28{
29 DECLARE_GLOBAL_DATA_PTR;
30 const void *fdt = gd->fdt_blob;
31 int offset;
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090032 u64 addr, addr2;
Masahiro Yamadabcd22442019-07-10 20:07:36 +090033
34 offset = fdt_node_offset_by_compatible(fdt, 0, "smsc,lan9118");
35 if (offset < 0)
36 return;
37
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090038 addr = fdt_get_base_address(fdt, offset);
39 if (addr == OF_BAD_ADDR)
40 return;
41 addr -= SMC911X_OFFSET;
42
Masahiro Yamadabcd22442019-07-10 20:07:36 +090043 offset = fdt_node_offset_by_compatible(fdt, 0, "ns16550a");
44 if (offset < 0)
45 return;
46
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090047 addr2 = fdt_get_base_address(fdt, offset);
48 if (addr2 == OF_BAD_ADDR)
49 return;
50 addr2 -= NS16550A_OFFSET;
51
52 /* sanity check */
53 if (addr != addr2)
54 return;
55
56 support_card_base = ioremap(addr, 0x100000);
57
Masahiro Yamadabcd22442019-07-10 20:07:36 +090058 support_card_found = true;
59}
60
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090061/*
62 * 0: reset deassert, 1: reset
63 *
64 * bit[0]: LAN, I2C, LED
65 * bit[1]: UART
66 */
Masahiro Yamada8e032702017-01-15 14:59:06 +090067static void support_card_reset_deassert(void)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090068{
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090069 writel(0x00010000, support_card_base + MICRO_SUPPORT_CARD_RESET);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090070}
71
Masahiro Yamada8e032702017-01-15 14:59:06 +090072static void support_card_reset(void)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090073{
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090074 writel(0x00020003, support_card_base + MICRO_SUPPORT_CARD_RESET);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090075}
76
77static int support_card_show_revision(void)
78{
79 u32 revision;
80
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090081 revision = readl(support_card_base + MICRO_SUPPORT_CARD_REVISION);
Masahiro Yamadad0401702016-08-25 17:02:31 +090082 revision &= 0xff;
83
84 /* revision 3.6.x card changed the revision format */
Masahiro Yamadadf0c0b22017-02-20 17:32:19 +090085 printf("SC: Micro Support Card (CPLD version %s%d.%d)\n",
86 revision >> 4 == 6 ? "3." : "",
Masahiro Yamadad0401702016-08-25 17:02:31 +090087 revision >> 4, revision & 0xf);
88
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090089 return 0;
90}
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090091
92void support_card_init(void)
93{
Masahiro Yamadabcd22442019-07-10 20:07:36 +090094 support_card_detect();
95
96 if (!support_card_found)
97 return;
98
Masahiro Yamada8e032702017-01-15 14:59:06 +090099 support_card_reset();
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900100 /*
101 * After power on, we need to keep the LAN controller in reset state
102 * for a while. (200 usec)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900103 */
Masahiro Yamadaa53f74f2016-10-08 13:25:29 +0900104 udelay(200);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900105 support_card_reset_deassert();
Masahiro Yamadadf0c0b22017-02-20 17:32:19 +0900106
107 support_card_show_revision();
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900108}
109
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900110#if defined(CONFIG_MTD_NOR_FLASH)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900111
112#include <mtd/cfi_flash.h>
113
Masahiro Yamada88216812014-12-06 00:03:26 +0900114struct memory_bank {
115 phys_addr_t base;
116 unsigned long size;
117};
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900118
Masahiro Yamada88216812014-12-06 00:03:26 +0900119static int mem_is_flash(const struct memory_bank *mem)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900120{
121 const int loop = 128;
122 u32 *scratch_addr;
123 u32 saved_value;
124 int ret = 1;
125 int i;
126
Masahiro Yamada88216812014-12-06 00:03:26 +0900127 /* just in case, use the tail of the memory bank */
128 scratch_addr = map_physmem(mem->base + mem->size - sizeof(u32) * loop,
129 sizeof(u32) * loop, MAP_NOCACHE);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900130
131 for (i = 0; i < loop; i++, scratch_addr++) {
132 saved_value = readl(scratch_addr);
133 writel(~saved_value, scratch_addr);
134 if (readl(scratch_addr) != saved_value) {
135 /* We assume no memory or SRAM here. */
136 writel(saved_value, scratch_addr);
137 ret = 0;
138 break;
139 }
140 }
141
142 unmap_physmem(scratch_addr, MAP_NOCACHE);
143
144 return ret;
145}
146
Masahiro Yamada4903b6d2015-09-11 20:17:45 +0900147/* {address, size} */
148static const struct memory_bank memory_banks[] = {
Masahiro Yamada4b275ea2015-09-11 20:17:47 +0900149 {0x42000000, 0x01f00000},
Masahiro Yamada88216812014-12-06 00:03:26 +0900150};
151
Masahiro Yamada88216812014-12-06 00:03:26 +0900152static const struct memory_bank
153*flash_banks_list[CONFIG_SYS_MAX_FLASH_BANKS_DETECT];
154
155phys_addr_t cfi_flash_bank_addr(int i)
156{
157 return flash_banks_list[i]->base;
158}
159
160unsigned long cfi_flash_bank_size(int i)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900161{
Masahiro Yamada88216812014-12-06 00:03:26 +0900162 return flash_banks_list[i]->size;
163}
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900164
Masahiro Yamada88216812014-12-06 00:03:26 +0900165static void detect_num_flash_banks(void)
166{
167 const struct memory_bank *memory_bank, *end;
168
169 cfi_flash_num_flash_banks = 0;
170
Masahiro Yamada4903b6d2015-09-11 20:17:45 +0900171 memory_bank = memory_banks;
172 end = memory_bank + ARRAY_SIZE(memory_banks);
Masahiro Yamada88216812014-12-06 00:03:26 +0900173
174 for (; memory_bank < end; memory_bank++) {
175 if (cfi_flash_num_flash_banks >=
176 CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
177 break;
178
179 if (mem_is_flash(memory_bank)) {
180 flash_banks_list[cfi_flash_num_flash_banks] =
181 memory_bank;
182
183 debug("flash bank found: base = 0x%lx, size = 0x%lx\n",
Masahiro Yamada09350102016-02-26 18:59:45 +0900184 (unsigned long)memory_bank->base,
185 (unsigned long)memory_bank->size);
Masahiro Yamada88216812014-12-06 00:03:26 +0900186 cfi_flash_num_flash_banks++;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900187 }
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900188 }
189
Masahiro Yamada88216812014-12-06 00:03:26 +0900190 debug("number of flash banks: %d\n", cfi_flash_num_flash_banks);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900191}
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900192#else /* CONFIG_MTD_NOR_FLASH */
Masahiro Yamada8e032702017-01-15 14:59:06 +0900193static void detect_num_flash_banks(void)
Masahiro Yamada88216812014-12-06 00:03:26 +0900194{
195};
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900196#endif /* CONFIG_MTD_NOR_FLASH */
Masahiro Yamada88216812014-12-06 00:03:26 +0900197
198void support_card_late_init(void)
199{
Masahiro Yamadabcd22442019-07-10 20:07:36 +0900200 if (!support_card_found)
201 return;
202
Masahiro Yamada88216812014-12-06 00:03:26 +0900203 detect_num_flash_banks();
204}
Masahiro Yamada4e5d28d2015-09-22 00:27:31 +0900205
206static const u8 ledval_num[] = {
207 0x7e, /* 0 */
208 0x0c, /* 1 */
209 0xb6, /* 2 */
210 0x9e, /* 3 */
211 0xcc, /* 4 */
212 0xda, /* 5 */
213 0xfa, /* 6 */
214 0x4e, /* 7 */
215 0xfe, /* 8 */
216 0xde, /* 9 */
217};
218
219static const u8 ledval_alpha[] = {
220 0xee, /* A */
221 0xf8, /* B */
222 0x72, /* C */
223 0xbc, /* D */
224 0xf2, /* E */
225 0xe2, /* F */
226 0x7a, /* G */
227 0xe8, /* H */
228 0x08, /* I */
229 0x3c, /* J */
230 0xea, /* K */
231 0x70, /* L */
232 0x6e, /* M */
233 0xa8, /* N */
234 0xb8, /* O */
235 0xe6, /* P */
236 0xce, /* Q */
237 0xa0, /* R */
238 0xc8, /* S */
239 0x8c, /* T */
240 0x7c, /* U */
241 0x54, /* V */
242 0xfc, /* W */
243 0xec, /* X */
244 0xdc, /* Y */
245 0xa4, /* Z */
246};
247
248static u8 char2ledval(char c)
249{
250 if (isdigit(c))
251 return ledval_num[c - '0'];
252 else if (isalpha(c))
253 return ledval_alpha[toupper(c) - 'A'];
254
255 return 0;
256}
257
258void led_puts(const char *s)
259{
260 int i;
261 u32 val = 0;
262
Masahiro Yamadabcd22442019-07-10 20:07:36 +0900263 if (!support_card_found)
264 return;
265
Masahiro Yamada4e5d28d2015-09-22 00:27:31 +0900266 if (!s)
267 return;
268
269 for (i = 0; i < 4; i++) {
270 val <<= 8;
271 val |= char2ledval(*s);
272 if (*s != '\0')
273 s++;
274 }
275
Masahiro Yamadabc9daa02020-02-14 20:54:42 +0900276 writel(~val, support_card_base + LED_OFFSET);
Masahiro Yamada4e5d28d2015-09-22 00:27:31 +0900277}