axs10x.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * AXS101/AXS103 Software Development Platform
  3. *
  4. * Copyright (C) 2013-15 Synopsys, Inc. (www.synopsys.com)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/of_fdt.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/libfdt.h>
  19. #include <asm/asm-offsets.h>
  20. #include <asm/io.h>
  21. #include <asm/mach_desc.h>
  22. #include <asm/mcip.h>
  23. #define AXS_MB_CGU 0xE0010000
  24. #define AXS_MB_CREG 0xE0011000
  25. #define CREG_MB_IRQ_MUX (AXS_MB_CREG + 0x214)
  26. #define CREG_MB_SW_RESET (AXS_MB_CREG + 0x220)
  27. #define CREG_MB_VER (AXS_MB_CREG + 0x230)
  28. #define CREG_MB_CONFIG (AXS_MB_CREG + 0x234)
  29. #define AXC001_CREG 0xF0001000
  30. #define AXC001_GPIO_INTC 0xF0003000
  31. static void __init axs10x_enable_gpio_intc_wire(void)
  32. {
  33. /*
  34. * Peripherals on CPU Card and Mother Board are wired to cpu intc via
  35. * intermediate DW APB GPIO blocks (mainly for debouncing)
  36. *
  37. * ---------------------
  38. * | snps,arc700-intc |
  39. * ---------------------
  40. * | #7 | #15
  41. * ------------------- -------------------
  42. * | snps,dw-apb-gpio | | snps,dw-apb-gpio |
  43. * ------------------- -------------------
  44. * | #12 |
  45. * | [ Debug UART on cpu card ]
  46. * |
  47. * ------------------------
  48. * | snps,dw-apb-intc (MB)|
  49. * ------------------------
  50. * | | | |
  51. * [eth] [uart] [... other perip on Main Board]
  52. *
  53. * Current implementation of "irq-dw-apb-ictl" driver doesn't work well
  54. * with stacked INTCs. In particular problem happens if its master INTC
  55. * not yet instantiated. See discussion here -
  56. * https://lkml.org/lkml/2015/3/4/755
  57. *
  58. * So setup the first gpio block as a passive pass thru and hide it from
  59. * DT hardware topology - connect MB intc directly to cpu intc
  60. * The GPIO "wire" needs to be init nevertheless (here)
  61. *
  62. * One side adv is that peripheral interrupt handling avoids one nested
  63. * intc ISR hop
  64. */
  65. #define GPIO_INTEN (AXC001_GPIO_INTC + 0x30)
  66. #define GPIO_INTMASK (AXC001_GPIO_INTC + 0x34)
  67. #define GPIO_INTTYPE_LEVEL (AXC001_GPIO_INTC + 0x38)
  68. #define GPIO_INT_POLARITY (AXC001_GPIO_INTC + 0x3c)
  69. #define MB_TO_GPIO_IRQ 12
  70. iowrite32(~(1 << MB_TO_GPIO_IRQ), (void __iomem *) GPIO_INTMASK);
  71. iowrite32(0, (void __iomem *) GPIO_INTTYPE_LEVEL);
  72. iowrite32(~0, (void __iomem *) GPIO_INT_POLARITY);
  73. iowrite32(1 << MB_TO_GPIO_IRQ, (void __iomem *) GPIO_INTEN);
  74. }
  75. static inline void __init
  76. write_cgu_reg(uint32_t value, void __iomem *reg, void __iomem *lock_reg)
  77. {
  78. unsigned int loops = 128 * 1024, ctr;
  79. iowrite32(value, reg);
  80. ctr = loops;
  81. while (((ioread32(lock_reg) & 1) == 1) && ctr--) /* wait for unlock */
  82. cpu_relax();
  83. ctr = loops;
  84. while (((ioread32(lock_reg) & 1) == 0) && ctr--) /* wait for re-lock */
  85. cpu_relax();
  86. }
  87. static void __init axs10x_print_board_ver(unsigned int creg, const char *str)
  88. {
  89. union ver {
  90. struct {
  91. #ifdef CONFIG_CPU_BIG_ENDIAN
  92. unsigned int pad:11, y:12, m:4, d:5;
  93. #else
  94. unsigned int d:5, m:4, y:12, pad:11;
  95. #endif
  96. };
  97. unsigned int val;
  98. } board;
  99. board.val = ioread32((void __iomem *)creg);
  100. pr_info("AXS: %s FPGA Date: %u-%u-%u\n", str, board.d, board.m,
  101. board.y);
  102. }
  103. static void __init axs10x_early_init(void)
  104. {
  105. int mb_rev;
  106. char mb[32];
  107. /* Determine motherboard version */
  108. if (ioread32((void __iomem *) CREG_MB_CONFIG) & (1 << 28))
  109. mb_rev = 3; /* HT-3 (rev3.0) */
  110. else
  111. mb_rev = 2; /* HT-2 (rev2.0) */
  112. axs10x_enable_gpio_intc_wire();
  113. scnprintf(mb, 32, "MainBoard v%d", mb_rev);
  114. axs10x_print_board_ver(CREG_MB_VER, mb);
  115. }
  116. #ifdef CONFIG_AXS101
  117. #define CREG_CPU_ADDR_770 (AXC001_CREG + 0x20)
  118. #define CREG_CPU_ADDR_TUNN (AXC001_CREG + 0x60)
  119. #define CREG_CPU_ADDR_770_UPD (AXC001_CREG + 0x34)
  120. #define CREG_CPU_ADDR_TUNN_UPD (AXC001_CREG + 0x74)
  121. #define CREG_CPU_ARC770_IRQ_MUX (AXC001_CREG + 0x114)
  122. #define CREG_CPU_GPIO_UART_MUX (AXC001_CREG + 0x120)
  123. /*
  124. * Set up System Memory Map for ARC cpu / peripherals controllers
  125. *
  126. * Each AXI master has a 4GB memory map specified as 16 apertures of 256MB, each
  127. * of which maps to a corresponding 256MB aperture in Target slave memory map.
  128. *
  129. * e.g. ARC cpu AXI Master's aperture 8 (0x8000_0000) is mapped to aperture 0
  130. * (0x0000_0000) of DDR Port 0 (slave #1)
  131. *
  132. * Access from cpu to MB controllers such as GMAC is setup using AXI Tunnel:
  133. * which has master/slaves on both ends.
  134. * e.g. aperture 14 (0xE000_0000) of ARC cpu is mapped to aperture 14
  135. * (0xE000_0000) of CPU Card AXI Tunnel slave (slave #3) which is mapped to
  136. * MB AXI Tunnel Master, which also has a mem map setup
  137. *
  138. * In the reverse direction, MB AXI Masters (e.g. GMAC) mem map is setup
  139. * to map to MB AXI Tunnel slave which connects to CPU Card AXI Tunnel Master
  140. */
  141. struct aperture {
  142. unsigned int slave_sel:4, slave_off:4, pad:24;
  143. };
  144. /* CPU Card target slaves */
  145. #define AXC001_SLV_NONE 0
  146. #define AXC001_SLV_DDR_PORT0 1
  147. #define AXC001_SLV_SRAM 2
  148. #define AXC001_SLV_AXI_TUNNEL 3
  149. #define AXC001_SLV_AXI2APB 6
  150. #define AXC001_SLV_DDR_PORT1 7
  151. /* MB AXI Target slaves */
  152. #define AXS_MB_SLV_NONE 0
  153. #define AXS_MB_SLV_AXI_TUNNEL_CPU 1
  154. #define AXS_MB_SLV_AXI_TUNNEL_HAPS 2
  155. #define AXS_MB_SLV_SRAM 3
  156. #define AXS_MB_SLV_CONTROL 4
  157. /* MB AXI masters */
  158. #define AXS_MB_MST_TUNNEL_CPU 0
  159. #define AXS_MB_MST_USB_OHCI 10
  160. /*
  161. * memmap for ARC core on CPU Card
  162. */
  163. static const struct aperture axc001_memmap[16] = {
  164. {AXC001_SLV_AXI_TUNNEL, 0x0},
  165. {AXC001_SLV_AXI_TUNNEL, 0x1},
  166. {AXC001_SLV_SRAM, 0x0}, /* 0x2000_0000: Local SRAM */
  167. {AXC001_SLV_NONE, 0x0},
  168. {AXC001_SLV_NONE, 0x0},
  169. {AXC001_SLV_NONE, 0x0},
  170. {AXC001_SLV_NONE, 0x0},
  171. {AXC001_SLV_NONE, 0x0},
  172. {AXC001_SLV_DDR_PORT0, 0x0}, /* 0x8000_0000: DDR 0..256M */
  173. {AXC001_SLV_DDR_PORT0, 0x1}, /* 0x9000_0000: DDR 256..512M */
  174. {AXC001_SLV_DDR_PORT0, 0x2},
  175. {AXC001_SLV_DDR_PORT0, 0x3},
  176. {AXC001_SLV_NONE, 0x0},
  177. {AXC001_SLV_AXI_TUNNEL, 0xD},
  178. {AXC001_SLV_AXI_TUNNEL, 0xE}, /* MB: CREG, CGU... */
  179. {AXC001_SLV_AXI2APB, 0x0}, /* CPU Card local CREG, CGU... */
  180. };
  181. /*
  182. * memmap for CPU Card AXI Tunnel Master (for access by MB controllers)
  183. * GMAC (MB) -> MB AXI Tunnel slave -> CPU Card AXI Tunnel Master -> DDR
  184. */
  185. static const struct aperture axc001_axi_tunnel_memmap[16] = {
  186. {AXC001_SLV_AXI_TUNNEL, 0x0},
  187. {AXC001_SLV_AXI_TUNNEL, 0x1},
  188. {AXC001_SLV_SRAM, 0x0},
  189. {AXC001_SLV_NONE, 0x0},
  190. {AXC001_SLV_NONE, 0x0},
  191. {AXC001_SLV_NONE, 0x0},
  192. {AXC001_SLV_NONE, 0x0},
  193. {AXC001_SLV_NONE, 0x0},
  194. {AXC001_SLV_DDR_PORT1, 0x0},
  195. {AXC001_SLV_DDR_PORT1, 0x1},
  196. {AXC001_SLV_DDR_PORT1, 0x2},
  197. {AXC001_SLV_DDR_PORT1, 0x3},
  198. {AXC001_SLV_NONE, 0x0},
  199. {AXC001_SLV_AXI_TUNNEL, 0xD},
  200. {AXC001_SLV_AXI_TUNNEL, 0xE},
  201. {AXC001_SLV_AXI2APB, 0x0},
  202. };
  203. /*
  204. * memmap for MB AXI Masters
  205. * Same mem map for all perip controllers as well as MB AXI Tunnel Master
  206. */
  207. static const struct aperture axs_mb_memmap[16] = {
  208. {AXS_MB_SLV_SRAM, 0x0},
  209. {AXS_MB_SLV_SRAM, 0x0},
  210. {AXS_MB_SLV_NONE, 0x0},
  211. {AXS_MB_SLV_NONE, 0x0},
  212. {AXS_MB_SLV_NONE, 0x0},
  213. {AXS_MB_SLV_NONE, 0x0},
  214. {AXS_MB_SLV_NONE, 0x0},
  215. {AXS_MB_SLV_NONE, 0x0},
  216. {AXS_MB_SLV_AXI_TUNNEL_CPU, 0x8}, /* DDR on CPU Card */
  217. {AXS_MB_SLV_AXI_TUNNEL_CPU, 0x9}, /* DDR on CPU Card */
  218. {AXS_MB_SLV_AXI_TUNNEL_CPU, 0xA},
  219. {AXS_MB_SLV_AXI_TUNNEL_CPU, 0xB},
  220. {AXS_MB_SLV_NONE, 0x0},
  221. {AXS_MB_SLV_AXI_TUNNEL_HAPS, 0xD},
  222. {AXS_MB_SLV_CONTROL, 0x0}, /* MB Local CREG, CGU... */
  223. {AXS_MB_SLV_AXI_TUNNEL_CPU, 0xF},
  224. };
  225. static noinline void __init
  226. axs101_set_memmap(void __iomem *base, const struct aperture map[16])
  227. {
  228. unsigned int slave_select, slave_offset;
  229. int i;
  230. slave_select = slave_offset = 0;
  231. for (i = 0; i < 8; i++) {
  232. slave_select |= map[i].slave_sel << (i << 2);
  233. slave_offset |= map[i].slave_off << (i << 2);
  234. }
  235. iowrite32(slave_select, base + 0x0); /* SLV0 */
  236. iowrite32(slave_offset, base + 0x8); /* OFFSET0 */
  237. slave_select = slave_offset = 0;
  238. for (i = 0; i < 8; i++) {
  239. slave_select |= map[i+8].slave_sel << (i << 2);
  240. slave_offset |= map[i+8].slave_off << (i << 2);
  241. }
  242. iowrite32(slave_select, base + 0x4); /* SLV1 */
  243. iowrite32(slave_offset, base + 0xC); /* OFFSET1 */
  244. }
  245. static void __init axs101_early_init(void)
  246. {
  247. int i;
  248. /* ARC 770D memory view */
  249. axs101_set_memmap((void __iomem *) CREG_CPU_ADDR_770, axc001_memmap);
  250. iowrite32(1, (void __iomem *) CREG_CPU_ADDR_770_UPD);
  251. /* AXI tunnel memory map (incoming traffic from MB into CPU Card */
  252. axs101_set_memmap((void __iomem *) CREG_CPU_ADDR_TUNN,
  253. axc001_axi_tunnel_memmap);
  254. iowrite32(1, (void __iomem *) CREG_CPU_ADDR_TUNN_UPD);
  255. /* MB peripherals memory map */
  256. for (i = AXS_MB_MST_TUNNEL_CPU; i <= AXS_MB_MST_USB_OHCI; i++)
  257. axs101_set_memmap((void __iomem *) AXS_MB_CREG + (i << 4),
  258. axs_mb_memmap);
  259. iowrite32(0x3ff, (void __iomem *) AXS_MB_CREG + 0x100); /* Update */
  260. /* GPIO pins 18 and 19 are used as UART rx and tx, respectively. */
  261. iowrite32(0x01, (void __iomem *) CREG_CPU_GPIO_UART_MUX);
  262. /* Set up the MB interrupt system: mux interrupts to GPIO7) */
  263. iowrite32(0x01, (void __iomem *) CREG_MB_IRQ_MUX);
  264. /* reset ethernet and ULPI interfaces */
  265. iowrite32(0x18, (void __iomem *) CREG_MB_SW_RESET);
  266. /* map GPIO 14:10 to ARC 9:5 (IRQ mux change for MB v2 onwards) */
  267. iowrite32(0x52, (void __iomem *) CREG_CPU_ARC770_IRQ_MUX);
  268. axs10x_early_init();
  269. }
  270. #endif /* CONFIG_AXS101 */
  271. #ifdef CONFIG_AXS103
  272. #define AXC003_CGU 0xF0000000
  273. #define AXC003_CREG 0xF0001000
  274. #define AXC003_MST_AXI_TUNNEL 0
  275. #define AXC003_MST_HS38 1
  276. #define CREG_CPU_AXI_M0_IRQ_MUX (AXC003_CREG + 0x440)
  277. #define CREG_CPU_GPIO_UART_MUX (AXC003_CREG + 0x480)
  278. #define CREG_CPU_TUN_IO_CTRL (AXC003_CREG + 0x494)
  279. union pll_reg {
  280. struct {
  281. #ifdef CONFIG_CPU_BIG_ENDIAN
  282. unsigned int pad:17, noupd:1, bypass:1, edge:1, high:6, low:6;
  283. #else
  284. unsigned int low:6, high:6, edge:1, bypass:1, noupd:1, pad:17;
  285. #endif
  286. };
  287. unsigned int val;
  288. };
  289. static unsigned int __init axs103_get_freq(void)
  290. {
  291. union pll_reg idiv, fbdiv, odiv;
  292. unsigned int f = 33333333;
  293. idiv.val = ioread32((void __iomem *)AXC003_CGU + 0x80 + 0);
  294. fbdiv.val = ioread32((void __iomem *)AXC003_CGU + 0x80 + 4);
  295. odiv.val = ioread32((void __iomem *)AXC003_CGU + 0x80 + 8);
  296. if (idiv.bypass != 1)
  297. f = f / (idiv.low + idiv.high);
  298. if (fbdiv.bypass != 1)
  299. f = f * (fbdiv.low + fbdiv.high);
  300. if (odiv.bypass != 1)
  301. f = f / (odiv.low + odiv.high);
  302. f = (f + 500000) / 1000000; /* Rounding */
  303. return f;
  304. }
  305. static inline unsigned int __init encode_div(unsigned int id, int upd)
  306. {
  307. union pll_reg div;
  308. div.val = 0;
  309. div.noupd = !upd;
  310. div.bypass = id == 1 ? 1 : 0;
  311. div.edge = (id%2 == 0) ? 0 : 1; /* 0 = rising */
  312. div.low = (id%2 == 0) ? id >> 1 : (id >> 1)+1;
  313. div.high = id >> 1;
  314. return div.val;
  315. }
  316. noinline static void __init
  317. axs103_set_freq(unsigned int id, unsigned int fd, unsigned int od)
  318. {
  319. write_cgu_reg(encode_div(id, 0),
  320. (void __iomem *)AXC003_CGU + 0x80 + 0,
  321. (void __iomem *)AXC003_CGU + 0x110);
  322. write_cgu_reg(encode_div(fd, 0),
  323. (void __iomem *)AXC003_CGU + 0x80 + 4,
  324. (void __iomem *)AXC003_CGU + 0x110);
  325. write_cgu_reg(encode_div(od, 1),
  326. (void __iomem *)AXC003_CGU + 0x80 + 8,
  327. (void __iomem *)AXC003_CGU + 0x110);
  328. }
  329. static void __init axs103_early_init(void)
  330. {
  331. int offset = fdt_path_offset(initial_boot_params, "/cpu_card/core_clk");
  332. const struct fdt_property *prop = fdt_get_property(initial_boot_params,
  333. offset,
  334. "clock-frequency",
  335. NULL);
  336. u32 freq = be32_to_cpu(*(u32*)(prop->data)) / 1000000, orig = freq;
  337. /*
  338. * AXS103 configurations for SMP/QUAD configurations share device tree
  339. * which defaults to 90 MHz. However recent failures of Quad config
  340. * revealed P&R timing violations so clamp it down to safe 50 MHz
  341. * Instead of duplicating defconfig/DT for SMP/QUAD, add a small hack
  342. *
  343. * This hack is really hacky as of now. Fix it properly by getting the
  344. * number of cores as return value of platform's early SMP callback
  345. */
  346. #ifdef CONFIG_ARC_MCIP
  347. unsigned int num_cores = (read_aux_reg(ARC_REG_MCIP_BCR) >> 16) & 0x3F;
  348. if (num_cores > 2)
  349. freq = 50;
  350. #endif
  351. switch (freq) {
  352. case 33:
  353. axs103_set_freq(1, 1, 1);
  354. break;
  355. case 50:
  356. axs103_set_freq(1, 30, 20);
  357. break;
  358. case 75:
  359. axs103_set_freq(2, 45, 10);
  360. break;
  361. case 90:
  362. axs103_set_freq(2, 54, 10);
  363. break;
  364. case 100:
  365. axs103_set_freq(1, 30, 10);
  366. break;
  367. case 125:
  368. axs103_set_freq(2, 45, 6);
  369. break;
  370. default:
  371. /*
  372. * In this case, core_frequency derived from
  373. * DT "clock-frequency" might not match with board value.
  374. * Hence update it to match the board value.
  375. */
  376. freq = axs103_get_freq();
  377. break;
  378. }
  379. pr_info("Freq is %dMHz\n", freq);
  380. /* Patching .dtb in-place with new core clock value */
  381. if (freq != orig ) {
  382. freq = cpu_to_be32(freq * 1000000);
  383. fdt_setprop_inplace(initial_boot_params, offset,
  384. "clock-frequency", &freq, sizeof(freq));
  385. }
  386. /* Memory maps already config in pre-bootloader */
  387. /* set GPIO mux to UART */
  388. iowrite32(0x01, (void __iomem *) CREG_CPU_GPIO_UART_MUX);
  389. iowrite32((0x00100000U | 0x000C0000U | 0x00003322U),
  390. (void __iomem *) CREG_CPU_TUN_IO_CTRL);
  391. /* Set up the AXS_MB interrupt system.*/
  392. iowrite32(12, (void __iomem *) (CREG_CPU_AXI_M0_IRQ_MUX
  393. + (AXC003_MST_HS38 << 2)));
  394. /* connect ICTL - Main Board with GPIO line */
  395. iowrite32(0x01, (void __iomem *) CREG_MB_IRQ_MUX);
  396. axs10x_print_board_ver(AXC003_CREG + 4088, "AXC003 CPU Card");
  397. axs10x_early_init();
  398. }
  399. #endif
  400. #ifdef CONFIG_AXS101
  401. static const char *axs101_compat[] __initconst = {
  402. "snps,axs101",
  403. NULL,
  404. };
  405. MACHINE_START(AXS101, "axs101")
  406. .dt_compat = axs101_compat,
  407. .init_early = axs101_early_init,
  408. MACHINE_END
  409. #endif /* CONFIG_AXS101 */
  410. #ifdef CONFIG_AXS103
  411. static const char *axs103_compat[] __initconst = {
  412. "snps,axs103",
  413. NULL,
  414. };
  415. MACHINE_START(AXS103, "axs103")
  416. .dt_compat = axs103_compat,
  417. .init_early = axs103_early_init,
  418. MACHINE_END
  419. /*
  420. * For the VDK OS-kit, to get the offset to pid and command fields
  421. */
  422. char coware_swa_pid_offset[TASK_PID];
  423. char coware_swa_comm_offset[TASK_COMM];
  424. #endif /* CONFIG_AXS103 */