Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / arch / arm / mach-uniphier / micro-support-card.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2012-2015 Panasonic Corporation
4  * Copyright (C) 2015-2020 Socionext Inc.
5  *   Author: Masahiro Yamada <yamada.masahiro@socionext.com>
6  */
7
8 #include <config.h>
9 #include <dm/of.h>
10 #include <fdt_support.h>
11 #include <linux/ctype.h>
12 #include <linux/delay.h>
13 #include <linux/io.h>
14 #include <asm/global_data.h>
15
16 #include "micro-support-card.h"
17
18 #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
23
24 static bool support_card_found;
25 static void __iomem *support_card_base;
26
27 static void support_card_detect(void)
28 {
29         DECLARE_GLOBAL_DATA_PTR;
30         const void *fdt = gd->fdt_blob;
31         int offset;
32         u64 addr, addr2;
33
34         offset = fdt_node_offset_by_compatible(fdt, 0, "smsc,lan9118");
35         if (offset < 0)
36                 return;
37
38         addr = fdt_get_base_address(fdt, offset);
39         if (addr == OF_BAD_ADDR)
40                 return;
41         addr -= SMC911X_OFFSET;
42
43         offset = fdt_node_offset_by_compatible(fdt, 0, "ns16550a");
44         if (offset < 0)
45                 return;
46
47         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
58         support_card_found = true;
59 }
60
61 /*
62  * 0: reset deassert, 1: reset
63  *
64  * bit[0]: LAN, I2C, LED
65  * bit[1]: UART
66  */
67 static void support_card_reset_deassert(void)
68 {
69         writel(0x00010000, support_card_base + MICRO_SUPPORT_CARD_RESET);
70 }
71
72 static void support_card_reset(void)
73 {
74         writel(0x00020003, support_card_base + MICRO_SUPPORT_CARD_RESET);
75 }
76
77 static int support_card_show_revision(void)
78 {
79         u32 revision;
80
81         revision = readl(support_card_base + MICRO_SUPPORT_CARD_REVISION);
82         revision &= 0xff;
83
84         /* revision 3.6.x card changed the revision format */
85         printf("SC:    Micro Support Card (CPLD version %s%d.%d)\n",
86                revision >> 4 == 6 ? "3." : "",
87                revision >> 4, revision & 0xf);
88
89         return 0;
90 }
91
92 void support_card_init(void)
93 {
94         support_card_detect();
95
96         if (!support_card_found)
97                 return;
98
99         support_card_reset();
100         /*
101          * After power on, we need to keep the LAN controller in reset state
102          * for a while. (200 usec)
103          */
104         udelay(200);
105         support_card_reset_deassert();
106
107         support_card_show_revision();
108 }
109
110 #if defined(CONFIG_MTD_NOR_FLASH)
111
112 #include <mtd/cfi_flash.h>
113
114 struct memory_bank {
115         phys_addr_t base;
116         unsigned long size;
117 };
118
119 static int mem_is_flash(const struct memory_bank *mem)
120 {
121         const int loop = 128;
122         u32 *scratch_addr;
123         u32 saved_value;
124         int ret = 1;
125         int i;
126
127         /* 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);
130
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
147 /* {address, size} */
148 static const struct memory_bank memory_banks[] = {
149         {0x42000000, 0x01f00000},
150 };
151
152 static const struct memory_bank
153 *flash_banks_list[CONFIG_SYS_MAX_FLASH_BANKS_DETECT];
154
155 phys_addr_t cfi_flash_bank_addr(int i)
156 {
157         return flash_banks_list[i]->base;
158 }
159
160 unsigned long cfi_flash_bank_size(int i)
161 {
162         return flash_banks_list[i]->size;
163 }
164
165 static void detect_num_flash_banks(void)
166 {
167         const struct memory_bank *memory_bank, *end;
168
169         cfi_flash_num_flash_banks = 0;
170
171         memory_bank = memory_banks;
172         end = memory_bank + ARRAY_SIZE(memory_banks);
173
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",
184                               (unsigned long)memory_bank->base,
185                               (unsigned long)memory_bank->size);
186                         cfi_flash_num_flash_banks++;
187                 }
188         }
189
190         debug("number of flash banks: %d\n", cfi_flash_num_flash_banks);
191 }
192 #else /* CONFIG_MTD_NOR_FLASH */
193 static void detect_num_flash_banks(void)
194 {
195 };
196 #endif /* CONFIG_MTD_NOR_FLASH */
197
198 void support_card_late_init(void)
199 {
200         if (!support_card_found)
201                 return;
202
203         detect_num_flash_banks();
204 }
205
206 static 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
219 static 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
248 static 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
258 void led_puts(const char *s)
259 {
260         int i;
261         u32 val = 0;
262
263         if (!support_card_found)
264                 return;
265
266         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
276         writel(~val, support_card_base + LED_OFFSET);
277 }