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