summaryrefslogtreecommitdiffstats
path: root/arch/arm/boards/novena/board.c
blob: b6c59aff445f0f582a2566f0c21103c0643eddf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2023 John Watts <contact@jookia.org>

#include <common.h>
#include <deep-probe.h>
#include <fs.h>
#include <libfile.h>
#include <net.h>

struct novena_eeprom {
	uint8_t signature[6]; /* 'Novena' */
	uint8_t version; /* 1 or 2, not checked */
	uint8_t page_size; /* v2 only: EEPROM read/write page */
	uint32_t serial; /* 32-bit serial number */
	uint8_t mac[6]; /* Gigabit MAC address */
	uint16_t features; /* features */
	/* ... extra fields omitted ... */
} __packed;

static void power_on_audio_codec(void)
{
	int rc = of_devices_ensure_probed_by_name("regulator-audio-codec");

	if (rc < 0)
		pr_err("Unable to power on audio codec: %s\n", strerror(-rc));
}

static struct novena_eeprom *novena_read_eeprom(void)
{
	size_t read;
	loff_t max = sizeof(struct novena_eeprom);
	void *eeprom;
	int rc;

	/*
	 * When powered off the audio codec pulls down the EEPROM's I2C line.
	 * Power it on so we can actually read data.
	 */
	power_on_audio_codec();

	rc = of_device_ensure_probed_by_alias("eeprom0");
	if (rc < 0) {
		pr_err("Unable to probe eeprom0: %s\n", strerror(-rc));
		return NULL;
	}

	rc = read_file_2("/dev/eeprom0", &read, &eeprom, max);

	if (rc < 0 && rc != -EFBIG) {
		pr_err("Unable to read Novena EEPROM: %s\n", strerror(-rc));
		return NULL;
	} else if (read != max) {
		pr_err("Short read from Novena EEPROM?\n");
		free(eeprom);
		return NULL;
	}

	return eeprom;
}

static bool novena_check_eeprom(struct novena_eeprom *eeprom)
{
	char *sig = eeprom->signature;
	size_t size = sizeof(eeprom->signature);

	if (memcmp("Novena", sig, size) != 0) {
		pr_err("Unknown Novena EEPROM signature\n");
		return false;
	}

	return true;
}

static void novena_set_mac(struct novena_eeprom *eeprom)
{
	struct device_node *dnode;

	dnode = of_find_node_by_alias(of_get_root_node(), "ethernet0");
	if (dnode)
		of_eth_register_ethaddr(dnode, eeprom->mac);
	else
		pr_err("Unable to find ethernet node\n");
}

static int novena_probe(struct device *dev)
{
	struct novena_eeprom *eeprom = novena_read_eeprom();

	if (eeprom && novena_check_eeprom(eeprom))
		novena_set_mac(eeprom);

	free(eeprom);

	return 0;
}

static const struct of_device_id novena_of_match[] = {
	{ .compatible = "kosagi,imx6q-novena", },
	{ /* sentinel */ }
};

static struct driver novena_board_driver = {
	.name = "board-novena",
	.probe = novena_probe,
	.of_compatible = novena_of_match,
};
coredevice_platform_driver(novena_board_driver);

BAREBOX_DEEP_PROBE_ENABLE(novena_of_match);