Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ OBJECTS := \
fb.o font.o font_retina.o \
firmware.o \
gapf.o \
gpio.o \
gxf.o gxf_asm.o \
heapblock.o \
hv.o hv_vm.o hv_exc.o hv_sptm.o \
Expand All @@ -159,7 +160,7 @@ OBJECTS := \
iodev.o \
iova.o \
isp.o \
kboot.o kboot_atc.o kboot_utils.o \
kboot.o kboot_atc.o kboot_audio.o kboot_utils.o \
kboot_t6020_compat.o \
main.o \
mitigations.o \
Expand Down
89 changes: 89 additions & 0 deletions src/adt.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include <string.h>

#include "adt.h"
#include "utils.h"
#include "xnuboot.h"

/* This API is designed to match libfdt's read-only API */
Expand All @@ -9,3 +12,89 @@ u32 adt_get_size(void)
{
return cur_boot_args.devtree_size;
}

bool adt_getprop_exact(const void *dt, int nodeoffset, const char *name, void *out,
size_t expected_len)
{
u32 len = 0;
const void *p = adt_getprop(dt, nodeoffset, name, &len);
if (!p || len != expected_len)
return false;
memcpy(out, p, expected_len);
return true;
}

static int adt_find_phandle_rec(const void *dt, int parent, u32 target, int *adt_path, size_t depth,
size_t max_depth, char *path_buf, size_t path_buf_size,
size_t prefix_len)
{
if (depth + 1 >= max_depth)
return -1;

int child = parent;
ADT_FOREACH_CHILD(dt, child)
{
const char *name = adt_get_name(dt, child);
size_t next_len = prefix_len;
if (path_buf && path_buf_size > 0) {
if (!name)
continue;
int n = snprintf(path_buf + prefix_len, path_buf_size - prefix_len, "/%s", name);
if (n < 0 || prefix_len + (size_t)n >= path_buf_size)
continue;
next_len = prefix_len + (size_t)n;
}

adt_path[depth] = child;
adt_path[depth + 1] = 0;

u32 phandle = 0;
if (adt_getprop_exact(dt, child, "AAPL,phandle", &phandle, sizeof(phandle)) &&
phandle == target)
return child;

int found = adt_find_phandle_rec(dt, child, target, adt_path, depth + 1, max_depth,
path_buf, path_buf_size, next_len);
if (found >= 0)
return found;
}

adt_path[depth] = 0;
if (path_buf && prefix_len < path_buf_size)
path_buf[prefix_len] = '\0';
return -1;
}

int adt_find_phandle_path(const void *dt, const char *root_path, u32 phandle, int *adt_path,
size_t max_depth, char *path_buf, size_t path_buf_size)
{
if (!root_path || !adt_path || max_depth < 3)
return -1;

int trace[8] = {0};
int root = adt_path_offset_trace(dt, root_path, trace);
if (root < 0)
return -1;

size_t depth = 0;
while (depth < ARRAY_SIZE(trace) && trace[depth] != 0) {
if (depth + 1 >= max_depth)
return -1;
adt_path[depth] = trace[depth];
depth++;
}
adt_path[depth] = 0;

size_t prefix_len = 0;
if (path_buf && path_buf_size > 0) {
int n = snprintf(path_buf, path_buf_size, "%s", root_path);
if (n < 0 || (size_t)n >= path_buf_size)
return -1;
prefix_len = (size_t)n;
while (prefix_len > 0 && path_buf[prefix_len - 1] == '/')
prefix_len--;
}

return adt_find_phandle_rec(dt, root, phandle, adt_path, depth, max_depth, path_buf,
path_buf_size, prefix_len);
}
4 changes: 4 additions & 0 deletions src/adt.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ const void *adt_getprop_by_offset(const void *adt, int offset, const char **name
const void *adt_getprop(const void *adt, int nodeoffset, const char *name, u32 *lenp);
int adt_setprop(void *adt, int nodeoffset, const char *name, void *value, size_t len);
int adt_getprop_copy(const void *adt, int nodeoffset, const char *name, void *out, size_t len);
bool adt_getprop_exact(const void *adt, int nodeoffset, const char *name, void *out,
size_t expected_len);
int adt_find_phandle_path(const void *adt, const char *root_path, u32 phandle, int *adt_path,
size_t max_depth, char *path_buf, size_t path_buf_size);

#define ADT_GETPROP(adt, nodeoffset, name, val) \
adt_getprop_copy(adt, nodeoffset, name, (val), sizeof(*(val)))
Expand Down
67 changes: 67 additions & 0 deletions src/gpio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include "gpio.h"
#include "adt.h"
#include "pmgr.h"
#include "utils.h"

#define APPLE_GPIO_DATA BIT(0)
#define APPLE_GPIO_MODE_MASK (7U << 1)
#define APPLE_GPIO_MODE_IN_OFF (7U << 1)
#define APPLE_GPIO_PERIPH_MASK (3U << 5)
#define APPLE_GPIO_INPUT_ENABLE BIT(9)
#define APPLE_GPIO_LOCK BIT(21)

int gpio_sample_pin(u64 base, u64 size, u32 pin, u32 flags)
{
if ((u64)pin >= size / 4)
return -1;

u64 reg = base + (u64)pin * 4;
u32 orig = read32(reg);
u32 cfg = (orig & ~(APPLE_GPIO_MODE_MASK | APPLE_GPIO_PERIPH_MASK)) | APPLE_GPIO_MODE_IN_OFF |
APPLE_GPIO_INPUT_ENABLE;
bool modified = false;

if (cfg != orig && !(orig & APPLE_GPIO_LOCK)) {
write32(reg, cfg);
(void)read32(reg); /* MMIO write barrier before settle delay */
udelay(10);
modified = true;
}

int val = !!(read32(reg) & APPLE_GPIO_DATA);
if (flags & 1)
val ^= 1;

if (modified) {
write32(reg, orig);
(void)read32(reg);
}

return val;
}

int gpio_adt_sample_fn(const struct adt_gpio_fn *fn)
{
int adt_path[8] = {0};
char pmgr_path[128];

int node = adt_find_phandle_path(adt, "/arm-io", fn->phandle, adt_path, ARRAY_SIZE(adt_path),
pmgr_path, sizeof(pmgr_path));
if (node < 0)
return -1;

u32 npins = 0;
if (adt_getprop_exact(adt, node, "#gpio-pins", &npins, sizeof(npins)) && fn->pin >= npins)
return -1;

u64 base = 0, size = 0;
if (adt_get_reg(adt, adt_path, "reg", 0, &base, &size) < 0)
return -1;

if (adt_getprop(adt, node, "clock-gates", NULL) && pmgr_adt_power_enable(pmgr_path) < 0)
return -1;

return gpio_sample_pin(base, size, fn->pin, fn->flags);
}
18 changes: 18 additions & 0 deletions src/gpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */

#ifndef GPIO_H
#define GPIO_H

#include "types.h"

struct adt_gpio_fn {
u32 phandle;
u32 fourcc;
u32 pin;
u32 flags;
} PACKED;

int gpio_sample_pin(u64 base, u64 size, u32 pin, u32 flags);
int gpio_adt_sample_fn(const struct adt_gpio_fn *fn);

#endif
3 changes: 3 additions & 0 deletions src/kboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "iodev.h"
#include "isp.h"
#include "kboot_atc.h"
#include "kboot_audio.h"
#include "kboot_utils.h"
#include "malloc.h"
#include "mcc.h"
Expand Down Expand Up @@ -3165,6 +3166,8 @@ int kboot_prepare_dt(void *fdt)
return -1;
if (dt_set_ipd())
return -1;
if (kboot_setup_audio(dt))
return -1;
if (dt_disable_missing_devs("usb-drd", "usb@", 8))
return -1;
if (dt_disable_missing_devs("i2c", "i2c@", 8))
Expand Down
Loading
Loading