blob: 628f795812955690ab6a523be80d9320122b2aa1 [file] [log] [blame]
Tudor Ambarusd63d7602019-11-13 15:42:54 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 Microchip Technology Inc. and its subsidiaries
4 *
5 * Author: Tudor Ambarus <tudor.ambarus@microchip.com>
6 */
7
Tudor Ambarusd63d7602019-11-13 15:42:54 +00008#include <dm.h>
9#include <env.h>
Simon Glass274e0b02020-05-10 11:39:56 -060010#include <net.h>
Tudor Ambarusd63d7602019-11-13 15:42:54 +000011#include <linux/mtd/spi-nor.h>
12#include <netdev.h>
13
14#define ETH_ADDR_SIZE 6
15
16#ifdef CONFIG_SPI_FLASH_SST
17#define SFDP_MICROCHIP_MANUF_ID 0xbf
18#define SFDP_MICROCHIP_MEM_TYPE 0x26
19#define SFDP_MICROCHIP_DEV_ID 0x43
20
21#define SFDP_MICROCHIP_EUI_OFFSET 0x60
22#define SFDP_MICROCHIP_EUI48 0x30
23
24struct sst26vf064beui {
25 u8 manufacturer_id;
26 u8 memory_type;
27 u8 device_id;
28 u8 reserved;
29};
30
31/**
32 * sst26vf064beui_check() - Check the validity of the EUI-48 information from
33 * the sst26vf064beui SPI NOR Microchip SFDP table.
34 * @manufacturer_sfdp: pointer to the Microchip manufacturer specific SFDP
35 * table.
36 *
37 * Return: 0 on success, -errno otherwise.
38 */
39static int sst26vf064beui_check(const u8 *manufacturer_sfdp)
40{
41 struct sst26vf064beui *sst26vf064beui =
42 (struct sst26vf064beui *)manufacturer_sfdp;
43
44 if (sst26vf064beui->manufacturer_id != SFDP_MICROCHIP_MANUF_ID)
45 return -EINVAL;
46
47 if (sst26vf064beui->memory_type != SFDP_MICROCHIP_MEM_TYPE)
48 return -EINVAL;
49
50 if (sst26vf064beui->device_id != SFDP_MICROCHIP_DEV_ID)
51 return -EINVAL;
52
53 /*
54 * Check if the EUI-48 MAC address is programmed in the next six address
55 * locations.
56 */
57 if (manufacturer_sfdp[SFDP_MICROCHIP_EUI_OFFSET] !=
58 SFDP_MICROCHIP_EUI48)
59 return -EINVAL;
60
61 return 0;
62}
63
64/**
65 * sst26vf064beui_get_ethaddr() - Get the ethernet address from the
66 * sst26vf064beui SPI NOR Microchip SFDP table.
67 * @manufacturer_sfdp: pointer to the Microchip manufacturer specific SFDP
68 * table.
69 * @ethaddr: pointer where to fill the ethernet address
70 * @size: size of the ethernet address.
71 *
72 * Return: 0 on success, -errno otherwise.
73 */
74static int sst26vf064beui_get_ethaddr(const u8 *manufacturer_sfdp,
75 u8 *ethaddr, size_t size)
76{
77 u64 eui_table[2];
78 u64 *p = (u64 *)&manufacturer_sfdp[SFDP_MICROCHIP_EUI_OFFSET];
79 int i, ret;
80
81 ret = sst26vf064beui_check(manufacturer_sfdp);
82 if (ret)
83 return ret;
84
85 for (i = 0; i < 2; i++)
86 eui_table[i] = le64_to_cpu(p[i]);
87
88 /* Ethaddr starts at offset one. */
89 memcpy(ethaddr, &((u8 *)eui_table)[1], size);
90
91 return 0;
92}
93#endif
94
95/**
96 * at91_spi_nor_set_ethaddr() - Retrieve and set the ethernet address from the
97 * SPI NOR manufacturer specific SFDP table.
98 */
99void at91_spi_nor_set_ethaddr(void)
100{
101 struct udevice *dev;
102 struct spi_nor *nor;
103 const char *ethaddr_name = "ethaddr";
104 u8 ethaddr[ETH_ADDR_SIZE] = {0};
105
106 if (env_get(ethaddr_name))
107 return;
108
109 if (uclass_first_device_err(UCLASS_SPI_FLASH, &dev))
110 return;
111
112 nor = dev_get_uclass_priv(dev);
113 if (!nor)
114 return;
115
116 if (!nor->manufacturer_sfdp)
117 return;
118
119#ifdef CONFIG_SPI_FLASH_SST
120 if (sst26vf064beui_get_ethaddr(nor->manufacturer_sfdp, ethaddr,
121 ETH_ADDR_SIZE))
122 return;
123#endif
124
125 if (is_valid_ethaddr(ethaddr))
126 eth_env_set_enetaddr(ethaddr_name, ethaddr);
127}