summaryrefslogtreecommitdiffstats
path: root/arch/mips/mach-ath79/art.c
blob: 984d08736385ab64df2a173295c8c0d080899782 (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
110
111
112
// SPDX-License-Identifier: GPL-2.
/*
 * Copyright (c) 2018 Oleksij Rempel <linux@rempel-privat.de>
 */

#include <common.h>
#include <fcntl.h>
#include <init.h>
#include <libfile.h>
#include <net.h>
#include <unistd.h>

#define AR93000_EPPROM_OFFSET	0x1000

struct ar9300_eeprom {
	u8 eeprom_version;
	u8 template_version;
	u8 mac_addr[6];
};

static int art_set_mac(struct device_d *dev, struct ar9300_eeprom *eeprom)
{
	struct device_node *node = dev->device_node;
	struct device_node *rnode;

	if (!node)
		return -ENOENT;

	rnode = of_parse_phandle_from(node, NULL,
				     "barebox,provide-mac-address", 0);
	if (!rnode)
		return -ENOENT;

	of_eth_register_ethaddr(rnode, &eeprom->mac_addr[0]);

	return 0;
}

static int art_read_mac(struct device_d *dev, const char *file)
{
	int fd, rbytes;
	struct ar9300_eeprom eeprom;

	fd = open_and_lseek(file, O_RDONLY, AR93000_EPPROM_OFFSET);
	if (fd < 0) {
		dev_err(dev, "Failed to open eeprom path %s %d\n",
		       file, fd);
		return fd;
	}

	rbytes = read_full(fd, &eeprom, sizeof(eeprom));
	close(fd);
	if (rbytes < sizeof(eeprom)) {
		dev_err(dev, "Failed to read %s\n", file);
		return rbytes < 0 ? rbytes : -EIO;
	}

	dev_dbg(dev, "ART version: %x.%x\n",
		 eeprom.eeprom_version, eeprom.template_version);
	dev_dbg(dev, "mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
	       eeprom.mac_addr[0],
	       eeprom.mac_addr[1],
	       eeprom.mac_addr[2],
	       eeprom.mac_addr[3],
	       eeprom.mac_addr[4],
	       eeprom.mac_addr[5]);

	if (!is_valid_ether_addr(&eeprom.mac_addr[0])) {
		dev_err(dev, "bad MAC addr\n");
		return -EILSEQ;
	}

	return art_set_mac(dev, &eeprom);
}

static int art_probe(struct device_d *dev)
{
	char *path;
	int ret;

	dev_dbg(dev, "found ART partition\n");

	ret = of_find_path(dev->device_node, "device-path", &path, 0);
	if (ret) {
		dev_err(dev, "can't find path\n");
		return ret;
	}

	return art_read_mac(dev, path);
}

static struct of_device_id art_dt_ids[] = {
	{
		.compatible = "qca,art",
	}, {
		/* sentinel */
	}
};

static struct driver_d art_driver = {
	.name		= "qca-art",
	.probe		= art_probe,
	.of_compatible	= art_dt_ids,
};

static int art_of_driver_init(void)
{
	platform_driver_register(&art_driver);

	return 0;
}
late_initcall(art_of_driver_init);