ARM: uniphier: remove #include <net.h> again from micro-support-card.c
[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 <common.h>
9 #include <dm/of.h>
10 #include <fdt_support.h>
11 #include <log.h>
12 #include <linux/ctype.h>
13 #include <linux/delay.h>
14 #include <linux/io.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_SMC911X)
111 #include <netdev.h>
112
113 int board_eth_init(bd_t *bis)
114 {
115         if (!support_card_found)
116                 return 0;
117
118         return smc911x_initialize(0, (unsigned long)support_card_base + SMC911X_OFFSET);
119 }
120 #endif
121
122 #if defined(CONFIG_MTD_NOR_FLASH)
123
124 #include <mtd/cfi_flash.h>
125
126 struct memory_bank {
127         phys_addr_t base;
128         unsigned long size;
129 };
130
131 static int mem_is_flash(const struct memory_bank *mem)
132 {
133         const int loop = 128;
134         u32 *scratch_addr;
135         u32 saved_value;
136         int ret = 1;
137         int i;
138
139         /* just in case, use the tail of the memory bank */
140         scratch_addr = map_physmem(mem->base + mem->size - sizeof(u32) * loop,
141                                    sizeof(u32) * loop, MAP_NOCACHE);
142
143         for (i = 0; i < loop; i++, scratch_addr++) {
144                 saved_value = readl(scratch_addr);
145                 writel(~saved_value, scratch_addr);
146                 if (readl(scratch_addr) != saved_value) {
147                         /* We assume no memory or SRAM here. */
148                         writel(saved_value, scratch_addr);
149                         ret = 0;
150                         break;
151                 }
152         }
153
154         unmap_physmem(scratch_addr, MAP_NOCACHE);
155
156         return ret;
157 }
158
159 /* {address, size} */
160 static const struct memory_bank memory_banks[] = {
161         {0x42000000, 0x01f00000},
162 };
163
164 static const struct memory_bank
165 *flash_banks_list[CONFIG_SYS_MAX_FLASH_BANKS_DETECT];
166
167 phys_addr_t cfi_flash_bank_addr(int i)
168 {
169         return flash_banks_list[i]->base;
170 }
171
172 unsigned long cfi_flash_bank_size(int i)
173 {
174         return flash_banks_list[i]->size;
175 }
176
177 static void detect_num_flash_banks(void)
178 {
179         const struct memory_bank *memory_bank, *end;
180
181         cfi_flash_num_flash_banks = 0;
182
183         memory_bank = memory_banks;
184         end = memory_bank + ARRAY_SIZE(memory_banks);
185
186         for (; memory_bank < end; memory_bank++) {
187                 if (cfi_flash_num_flash_banks >=
188                     CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
189                         break;
190
191                 if (mem_is_flash(memory_bank)) {
192                         flash_banks_list[cfi_flash_num_flash_banks] =
193                                                                 memory_bank;
194
195                         debug("flash bank found: base = 0x%lx, size = 0x%lx\n",
196                               (unsigned long)memory_bank->base,
197                               (unsigned long)memory_bank->size);
198                         cfi_flash_num_flash_banks++;
199                 }
200         }
201
202         debug("number of flash banks: %d\n", cfi_flash_num_flash_banks);
203 }
204 #else /* CONFIG_MTD_NOR_FLASH */
205 static void detect_num_flash_banks(void)
206 {
207 };
208 #endif /* CONFIG_MTD_NOR_FLASH */
209
210 void support_card_late_init(void)
211 {
212         if (!support_card_found)
213                 return;
214
215         detect_num_flash_banks();
216 }
217
218 static const u8 ledval_num[] = {
219         0x7e, /* 0 */
220         0x0c, /* 1 */
221         0xb6, /* 2 */
222         0x9e, /* 3 */
223         0xcc, /* 4 */
224         0xda, /* 5 */
225         0xfa, /* 6 */
226         0x4e, /* 7 */
227         0xfe, /* 8 */
228         0xde, /* 9 */
229 };
230
231 static const u8 ledval_alpha[] = {
232         0xee, /* A */
233         0xf8, /* B */
234         0x72, /* C */
235         0xbc, /* D */
236         0xf2, /* E */
237         0xe2, /* F */
238         0x7a, /* G */
239         0xe8, /* H */
240         0x08, /* I */
241         0x3c, /* J */
242         0xea, /* K */
243         0x70, /* L */
244         0x6e, /* M */
245         0xa8, /* N */
246         0xb8, /* O */
247         0xe6, /* P */
248         0xce, /* Q */
249         0xa0, /* R */
250         0xc8, /* S */
251         0x8c, /* T */
252         0x7c, /* U */
253         0x54, /* V */
254         0xfc, /* W */
255         0xec, /* X */
256         0xdc, /* Y */
257         0xa4, /* Z */
258 };
259
260 static u8 char2ledval(char c)
261 {
262         if (isdigit(c))
263                 return ledval_num[c - '0'];
264         else if (isalpha(c))
265                 return ledval_alpha[toupper(c) - 'A'];
266
267         return 0;
268 }
269
270 void led_puts(const char *s)
271 {
272         int i;
273         u32 val = 0;
274
275         if (!support_card_found)
276                 return;
277
278         if (!s)
279                 return;
280
281         for (i = 0; i < 4; i++) {
282                 val <<= 8;
283                 val |= char2ledval(*s);
284                 if (*s != '\0')
285                         s++;
286         }
287
288         writel(~val, support_card_base + LED_OFFSET);
289 }