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 | |
Masahiro Yamada | e631441 | 2020-05-20 12:31:27 +0900 | [diff] [blame] | 8 | #include <config.h> |
Masahiro Yamada | bbec08c | 2020-05-20 12:31:24 +0900 | [diff] [blame] | 9 | #include <dm/of.h> |
Masahiro Yamada | bc9daa0 | 2020-02-14 20:54:42 +0900 | [diff] [blame] | 10 | #include <fdt_support.h> |
Masahiro Yamada | 4e5d28d | 2015-09-22 00:27:31 +0900 | [diff] [blame] | 11 | #include <linux/ctype.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 12 | #include <linux/delay.h> |
Masahiro Yamada | 663a23f | 2015-05-29 17:30:00 +0900 | [diff] [blame] | 13 | #include <linux/io.h> |
Masahiro Yamada | e631441 | 2020-05-20 12:31:27 +0900 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Masahiro Yamada | efdf340 | 2016-01-09 01:51:13 +0900 | [diff] [blame] | 15 | |
| 16 | #include "micro-support-card.h" |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 17 | |
Masahiro Yamada | bc9daa0 | 2020-02-14 20:54:42 +0900 | [diff] [blame] | 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 |
Masahiro Yamada | 56a2071 | 2015-09-22 00:27:32 +0900 | [diff] [blame] | 23 | |
Masahiro Yamada | bcd2244 | 2019-07-10 20:07:36 +0900 | [diff] [blame] | 24 | static bool support_card_found; |
Masahiro Yamada | bc9daa0 | 2020-02-14 20:54:42 +0900 | [diff] [blame] | 25 | static void __iomem *support_card_base; |
Masahiro Yamada | bcd2244 | 2019-07-10 20:07:36 +0900 | [diff] [blame] | 26 | |
| 27 | static void support_card_detect(void) |
| 28 | { |
| 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | const void *fdt = gd->fdt_blob; |
| 31 | int offset; |
Masahiro Yamada | bc9daa0 | 2020-02-14 20:54:42 +0900 | [diff] [blame] | 32 | u64 addr, addr2; |
Masahiro Yamada | bcd2244 | 2019-07-10 20:07:36 +0900 | [diff] [blame] | 33 | |
| 34 | offset = fdt_node_offset_by_compatible(fdt, 0, "smsc,lan9118"); |
| 35 | if (offset < 0) |
| 36 | return; |
| 37 | |
Masahiro Yamada | bc9daa0 | 2020-02-14 20:54:42 +0900 | [diff] [blame] | 38 | addr = fdt_get_base_address(fdt, offset); |
| 39 | if (addr == OF_BAD_ADDR) |
| 40 | return; |
| 41 | addr -= SMC911X_OFFSET; |
| 42 | |
Masahiro Yamada | bcd2244 | 2019-07-10 20:07:36 +0900 | [diff] [blame] | 43 | offset = fdt_node_offset_by_compatible(fdt, 0, "ns16550a"); |
| 44 | if (offset < 0) |
| 45 | return; |
| 46 | |
Masahiro Yamada | bc9daa0 | 2020-02-14 20:54:42 +0900 | [diff] [blame] | 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 | |
Masahiro Yamada | bcd2244 | 2019-07-10 20:07:36 +0900 | [diff] [blame] | 58 | support_card_found = true; |
| 59 | } |
| 60 | |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 61 | /* |
| 62 | * 0: reset deassert, 1: reset |
| 63 | * |
| 64 | * bit[0]: LAN, I2C, LED |
| 65 | * bit[1]: UART |
| 66 | */ |
Masahiro Yamada | 8e03270 | 2017-01-15 14:59:06 +0900 | [diff] [blame] | 67 | static void support_card_reset_deassert(void) |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 68 | { |
Masahiro Yamada | bc9daa0 | 2020-02-14 20:54:42 +0900 | [diff] [blame] | 69 | writel(0x00010000, support_card_base + MICRO_SUPPORT_CARD_RESET); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 70 | } |
| 71 | |
Masahiro Yamada | 8e03270 | 2017-01-15 14:59:06 +0900 | [diff] [blame] | 72 | static void support_card_reset(void) |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 73 | { |
Masahiro Yamada | bc9daa0 | 2020-02-14 20:54:42 +0900 | [diff] [blame] | 74 | writel(0x00020003, support_card_base + MICRO_SUPPORT_CARD_RESET); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static int support_card_show_revision(void) |
| 78 | { |
| 79 | u32 revision; |
| 80 | |
Masahiro Yamada | bc9daa0 | 2020-02-14 20:54:42 +0900 | [diff] [blame] | 81 | revision = readl(support_card_base + MICRO_SUPPORT_CARD_REVISION); |
Masahiro Yamada | d040170 | 2016-08-25 17:02:31 +0900 | [diff] [blame] | 82 | revision &= 0xff; |
| 83 | |
| 84 | /* revision 3.6.x card changed the revision format */ |
Masahiro Yamada | df0c0b2 | 2017-02-20 17:32:19 +0900 | [diff] [blame] | 85 | printf("SC: Micro Support Card (CPLD version %s%d.%d)\n", |
| 86 | revision >> 4 == 6 ? "3." : "", |
Masahiro Yamada | d040170 | 2016-08-25 17:02:31 +0900 | [diff] [blame] | 87 | revision >> 4, revision & 0xf); |
| 88 | |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 89 | return 0; |
| 90 | } |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 91 | |
| 92 | void support_card_init(void) |
| 93 | { |
Masahiro Yamada | bcd2244 | 2019-07-10 20:07:36 +0900 | [diff] [blame] | 94 | support_card_detect(); |
| 95 | |
| 96 | if (!support_card_found) |
| 97 | return; |
| 98 | |
Masahiro Yamada | 8e03270 | 2017-01-15 14:59:06 +0900 | [diff] [blame] | 99 | support_card_reset(); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 100 | /* |
| 101 | * After power on, we need to keep the LAN controller in reset state |
| 102 | * for a while. (200 usec) |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 103 | */ |
Masahiro Yamada | a53f74f | 2016-10-08 13:25:29 +0900 | [diff] [blame] | 104 | udelay(200); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 105 | support_card_reset_deassert(); |
Masahiro Yamada | df0c0b2 | 2017-02-20 17:32:19 +0900 | [diff] [blame] | 106 | |
| 107 | support_card_show_revision(); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 108 | } |
| 109 | |
Masahiro Yamada | 8cea9b5 | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 110 | #if defined(CONFIG_MTD_NOR_FLASH) |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 111 | |
| 112 | #include <mtd/cfi_flash.h> |
| 113 | |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 114 | struct memory_bank { |
| 115 | phys_addr_t base; |
| 116 | unsigned long size; |
| 117 | }; |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 118 | |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 119 | static int mem_is_flash(const struct memory_bank *mem) |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 120 | { |
| 121 | const int loop = 128; |
| 122 | u32 *scratch_addr; |
| 123 | u32 saved_value; |
| 124 | int ret = 1; |
| 125 | int i; |
| 126 | |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 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); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 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 | |
Masahiro Yamada | 4903b6d | 2015-09-11 20:17:45 +0900 | [diff] [blame] | 147 | /* {address, size} */ |
| 148 | static const struct memory_bank memory_banks[] = { |
Masahiro Yamada | 4b275ea | 2015-09-11 20:17:47 +0900 | [diff] [blame] | 149 | {0x42000000, 0x01f00000}, |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 150 | }; |
| 151 | |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 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) |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 161 | { |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 162 | return flash_banks_list[i]->size; |
| 163 | } |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 164 | |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 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 | |
Masahiro Yamada | 4903b6d | 2015-09-11 20:17:45 +0900 | [diff] [blame] | 171 | memory_bank = memory_banks; |
| 172 | end = memory_bank + ARRAY_SIZE(memory_banks); |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 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", |
Masahiro Yamada | 0935010 | 2016-02-26 18:59:45 +0900 | [diff] [blame] | 184 | (unsigned long)memory_bank->base, |
| 185 | (unsigned long)memory_bank->size); |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 186 | cfi_flash_num_flash_banks++; |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 187 | } |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 188 | } |
| 189 | |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 190 | debug("number of flash banks: %d\n", cfi_flash_num_flash_banks); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 191 | } |
Masahiro Yamada | 8cea9b5 | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 192 | #else /* CONFIG_MTD_NOR_FLASH */ |
Masahiro Yamada | 8e03270 | 2017-01-15 14:59:06 +0900 | [diff] [blame] | 193 | static void detect_num_flash_banks(void) |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 194 | { |
| 195 | }; |
Masahiro Yamada | 8cea9b5 | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 196 | #endif /* CONFIG_MTD_NOR_FLASH */ |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 197 | |
| 198 | void support_card_late_init(void) |
| 199 | { |
Masahiro Yamada | bcd2244 | 2019-07-10 20:07:36 +0900 | [diff] [blame] | 200 | if (!support_card_found) |
| 201 | return; |
| 202 | |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 203 | detect_num_flash_banks(); |
| 204 | } |
Masahiro Yamada | 4e5d28d | 2015-09-22 00:27:31 +0900 | [diff] [blame] | 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 | |
Masahiro Yamada | bcd2244 | 2019-07-10 20:07:36 +0900 | [diff] [blame] | 263 | if (!support_card_found) |
| 264 | return; |
| 265 | |
Masahiro Yamada | 4e5d28d | 2015-09-22 00:27:31 +0900 | [diff] [blame] | 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 | |
Masahiro Yamada | bc9daa0 | 2020-02-14 20:54:42 +0900 | [diff] [blame] | 276 | writel(~val, support_card_base + LED_OFFSET); |
Masahiro Yamada | 4e5d28d | 2015-09-22 00:27:31 +0900 | [diff] [blame] | 277 | } |