1/*-
2 * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer,
10 *    without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 *    redistribution must be conditioned upon including a substantially
14 *    similar Disclaimer requirement for further binary redistribution.
15 *
16 * NO WARRANTY
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
28 *
29 * $FreeBSD$
30 */
31
32#ifndef _BHND_NVRAM_BHND_NVRAMVAR_H_
33#define _BHND_NVRAM_BHND_NVRAMVAR_H_
34
35/** NVRAM Primitive data types */
36typedef enum {
37	BHND_NVRAM_DT_UINT8	= 0,	/**< unsigned 8-bit integer */
38	BHND_NVRAM_DT_UINT16	= 1,	/**< unsigned 16-bit integer */
39	BHND_NVRAM_DT_UINT32	= 2,	/**< unsigned 32-bit integer */
40	BHND_NVRAM_DT_INT8	= 3,	/**< signed 8-bit integer */
41	BHND_NVRAM_DT_INT16	= 4,	/**< signed 16-bit integer */
42	BHND_NVRAM_DT_INT32	= 5,	/**< signed 32-bit integer */
43	BHND_NVRAM_DT_CHAR	= 6,	/**< ASCII char */
44} bhnd_nvram_dt;
45
46/** NVRAM data type string representations */
47typedef enum {
48	BHND_NVRAM_VFMT_HEX	= 1,	/**< hex format */
49	BHND_NVRAM_VFMT_DEC	= 2,	/**< decimal format */
50	BHND_NVRAM_VFMT_MACADDR	= 3,	/**< mac address (canonical form, hex octets,
51					     separated with ':') */
52	BHND_NVRAM_VFMT_LEDDC	= 4,	/**< LED PWM duty-cycle (2 bytes -- on/off) */
53	BHND_NVRAM_VFMT_CCODE	= 5	/**< count code format (2-3 ASCII chars, or hex string) */
54} bhnd_nvram_fmt;
55
56/** NVRAM variable flags */
57enum {
58	BHND_NVRAM_VF_ARRAY	= (1<<0),	/**< variable is an array */
59	BHND_NVRAM_VF_MFGINT	= (1<<1),	/**< mfg-internal variable; should not be externally visible */
60	BHND_NVRAM_VF_IGNALL1	= (1<<2)	/**< hide variable if its value has all bits set. */
61};
62
63#define	BHND_SPROMREV_MAX	UINT8_MAX	/**< maximum supported SPROM revision */
64
65/** SPROM revision compatibility declaration */
66struct bhnd_sprom_compat {
67	uint8_t		first;	/**< first compatible SPROM revision */
68	uint8_t		last;	/**< last compatible SPROM revision, or BHND_SPROMREV_MAX */
69};
70
71/** SPROM value descriptor */
72struct bhnd_sprom_offset {
73	uint16_t	offset;	/**< byte offset within SPROM */
74	bool		cont:1;	/**< value should be bitwise OR'd with the
75				  *  previous offset descriptor */
76	bhnd_nvram_dt	type:7;	/**< data type */
77	int8_t		shift;	/**< shift to be applied to the value */
78	uint32_t	mask;	/**< mask to be applied to the value(s) */
79};
80
81/** SPROM-specific variable definition */
82struct bhnd_sprom_var {
83	struct bhnd_sprom_compat	 compat;	/**< sprom compatibility declaration */
84	const struct bhnd_sprom_offset	*offsets;	/**< offset descriptors */
85	size_t				 num_offsets;	/**< number of offset descriptors */
86};
87
88/** NVRAM variable definition */
89struct bhnd_nvram_var {
90	const char			*name;	  	/**< variable name */
91	bhnd_nvram_dt			 type;	 	/**< base data type */
92	bhnd_nvram_fmt			 fmt;		/**< string format */
93	uint32_t			 flags;		/**< BHND_NVRAM_VF_* flags */
94
95	const struct bhnd_sprom_var	*sprom_descs;	/**< SPROM-specific variable descriptors */
96	size_t				 num_sp_descs;	/**< number of sprom descriptors */
97};
98
99size_t				 bhnd_nvram_type_width(bhnd_nvram_dt dt);
100const struct bhnd_nvram_var	*bhnd_nvram_var_defn(const char *varname);
101
102/** Initial bhnd_nvram_crc8 value */
103#define	BHND_NVRAM_CRC8_INITIAL	0xFF
104
105/** Valid CRC-8 checksum */
106#define	BHND_NVRAM_CRC8_VALID	0x9F
107
108extern const uint8_t bhnd_nvram_crc8_tab[];
109
110/**
111 * Calculate CRC-8 over @p buf.
112 *
113 * @param buf input buffer
114 * @param size buffer size
115 * @param crc last computed crc, or BHND_NVRAM_CRC8_INITIAL
116 */
117static inline uint8_t
118bhnd_nvram_crc8(const void *buf, size_t size, uint8_t crc)
119{
120	const uint8_t *p = (const uint8_t *)buf;
121	while (size--)
122		crc = bhnd_nvram_crc8_tab[(crc ^ *p++)];
123
124	return (crc);
125}
126
127
128#endif /* _BHND_NVRAM_BHND_NVRAMVAR_H_ */