Paul Burton | b6d5e47 | 2016-08-26 15:17:34 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 Imagination Technologies |
| 3 | * Author: Paul Burton <paul.burton@imgtec.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License as published by the |
| 7 | * Free Software Foundation; either version 2 of the License, or (at your |
| 8 | * option) any later version. |
| 9 | */ |
| 10 | |
| 11 | #define pr_fmt(fmt) "sead3-dtshim: " fmt |
| 12 | |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/libfdt.h> |
| 15 | #include <linux/printk.h> |
| 16 | |
| 17 | #include <asm/io.h> |
| 18 | |
| 19 | #define SEAD_CONFIG CKSEG1ADDR(0x1b100110) |
| 20 | #define SEAD_CONFIG_GIC_PRESENT BIT(1) |
| 21 | |
| 22 | static unsigned char fdt_buf[16 << 10] __initdata; |
| 23 | |
| 24 | static int remove_gic(void *fdt) |
| 25 | { |
| 26 | int gic_off, cpu_off, err; |
| 27 | uint32_t cfg, cpu_phandle; |
| 28 | |
| 29 | /* leave the GIC node intact if a GIC is present */ |
| 30 | cfg = __raw_readl((uint32_t *)SEAD_CONFIG); |
| 31 | if (cfg & SEAD_CONFIG_GIC_PRESENT) |
| 32 | return 0; |
| 33 | |
| 34 | gic_off = fdt_node_offset_by_compatible(fdt, -1, "mti,gic"); |
| 35 | if (gic_off < 0) { |
| 36 | pr_err("unable to find DT GIC node: %d\n", gic_off); |
| 37 | return gic_off; |
| 38 | } |
| 39 | |
| 40 | err = fdt_nop_node(fdt, gic_off); |
| 41 | if (err) { |
| 42 | pr_err("unable to nop GIC node\n"); |
| 43 | return err; |
| 44 | } |
| 45 | |
| 46 | cpu_off = fdt_node_offset_by_compatible(fdt, -1, |
| 47 | "mti,cpu-interrupt-controller"); |
| 48 | if (cpu_off < 0) { |
| 49 | pr_err("unable to find CPU intc node: %d\n", cpu_off); |
| 50 | return cpu_off; |
| 51 | } |
| 52 | |
| 53 | cpu_phandle = fdt_get_phandle(fdt, cpu_off); |
| 54 | if (!cpu_phandle) { |
| 55 | pr_err("unable to get CPU intc phandle\n"); |
| 56 | return -EINVAL; |
| 57 | } |
| 58 | |
| 59 | err = fdt_setprop_u32(fdt, 0, "interrupt-parent", cpu_phandle); |
| 60 | if (err) { |
| 61 | pr_err("unable to set root interrupt-parent: %d\n", err); |
| 62 | return err; |
| 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | void __init *sead3_dt_shim(void *fdt) |
| 69 | { |
| 70 | int err; |
| 71 | |
| 72 | if (fdt_check_header(fdt)) |
| 73 | panic("Corrupt DT"); |
| 74 | |
| 75 | /* if this isn't SEAD3, leave the DT alone */ |
| 76 | if (fdt_node_check_compatible(fdt, 0, "mti,sead-3")) |
| 77 | return fdt; |
| 78 | |
| 79 | err = fdt_open_into(fdt, fdt_buf, sizeof(fdt_buf)); |
| 80 | if (err) |
| 81 | panic("Unable to open FDT: %d", err); |
| 82 | |
| 83 | err = remove_gic(fdt_buf); |
| 84 | if (err) |
| 85 | panic("Unable to patch FDT: %d", err); |
| 86 | |
| 87 | err = fdt_pack(fdt_buf); |
| 88 | if (err) |
| 89 | panic("Unable to pack FDT: %d\n", err); |
| 90 | |
| 91 | return fdt_buf; |
| 92 | } |