Kaynağa Gözat

Basic non-volatile storage support

tags/v0.9.3
Michael Brown 18 yıl önce
ebeveyn
işleme
4cd56820ea
3 değiştirilmiş dosya ile 243 ekleme ve 11 silme
  1. 106
    0
      src/core/nvs.c
  2. 107
    11
      src/drivers/net/etherfabric.c
  3. 30
    0
      src/include/gpxe/nvs.h

+ 106
- 0
src/core/nvs.c Dosyayı Görüntüle

@@ -0,0 +1,106 @@
1
+/*
2
+ * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+#include <stdint.h>
20
+#include <string.h>
21
+#include <gpxe/dhcp.h>
22
+#include <gpxe/nvs.h>
23
+
24
+/** @file
25
+ *
26
+ * Non-volatile storage
27
+ *
28
+ */
29
+
30
+static size_t nvs_options_len ( struct nvs_device *nvs ) {
31
+	struct dhcp_option *option;
32
+	uint8_t sum;
33
+	unsigned int i;
34
+	size_t len;
35
+
36
+	for ( sum = 0, i = 0 ; i < nvs->len ; i++ ) {
37
+		sum += * ( ( uint8_t * ) ( nvs->options->data + i ) );
38
+	}
39
+	if ( sum != 0 ) {
40
+		DBG ( "NVS %p has bad checksum %02x; assuming empty\n",
41
+		      nvs, sum );
42
+		return 0;
43
+	}
44
+
45
+	option = nvs->options->data;
46
+	if ( option->tag == DHCP_PAD ) {
47
+		DBG ( "NVS %p has bad start; assuming empty\n", nvs );
48
+		return 0;
49
+	}
50
+	
51
+	option = find_dhcp_option ( nvs->options, DHCP_END );
52
+	if ( ! option ) {
53
+		DBG ( "NVS %p has no end tag; assuming empty\n", nvs );
54
+		return 0;
55
+	}
56
+
57
+	len = ( ( void * ) option - nvs->options->data + 1 );
58
+	DBG ( "NVS %p contains %zd bytes of options (maximum %zd)\n",
59
+	      nvs, len, nvs->len );
60
+
61
+	return len;
62
+}
63
+
64
+int nvs_register ( struct nvs_device *nvs ) {
65
+	struct dhcp_option *option;
66
+	int rc;
67
+
68
+	nvs->options = alloc_dhcp_options ( nvs->len );
69
+	if ( ! nvs->options ) {
70
+		DBG ( "NVS %p could not allocate %zd bytes\n", nvs, nvs->len );
71
+		rc = -ENOMEM;
72
+		goto err;
73
+	}
74
+
75
+	if ( ( rc = nvs->op->read ( nvs, 0, nvs->options->data,
76
+				    nvs->len ) ) != 0 ) {
77
+		DBG ( "NVS %p could not read [0,%zd)\n", nvs, nvs->len );
78
+		goto err;
79
+	}
80
+
81
+	nvs->options->len = nvs->options->max_len;
82
+	nvs->options->len = nvs_options_len ( nvs );
83
+	if ( ! nvs->options->len ) {
84
+		option = nvs->options->data;
85
+		option->tag = DHCP_END;
86
+		nvs->options->len = 1;
87
+	}
88
+
89
+	register_dhcp_options ( nvs->options );
90
+
91
+	return 0;
92
+	
93
+ err:
94
+	
95
+	free_dhcp_options ( nvs->options );
96
+	nvs->options = NULL;
97
+	return rc;
98
+}
99
+
100
+void nvs_unregister ( struct nvs_device *nvs ) {
101
+	if ( nvs->options ) {
102
+		unregister_dhcp_options ( nvs->options );
103
+		free_dhcp_options ( nvs->options );
104
+		nvs->options = NULL;
105
+	}
106
+}

+ 107
- 11
src/drivers/net/etherfabric.c Dosyayı Görüntüle

@@ -18,9 +18,11 @@
18 18
 
19 19
 #include "etherboot.h"
20 20
 #include "nic.h"
21
+#include <errno.h>
21 22
 #include <gpxe/pci.h>
22 23
 #include <gpxe/bitbash.h>
23 24
 #include <gpxe/i2c.h>
25
+#include <gpxe/nvs.h>
24 26
 #include "timer.h"
25 27
 #define dma_addr_t unsigned long
26 28
 #include "etherfabric.h"
@@ -210,6 +212,9 @@ struct efab_nic {
210 212
 	struct i2c_bit_basher ef1002_i2c;
211 213
 	unsigned long ef1002_i2c_outputs;
212 214
 	struct i2c_device ef1002_eeprom;
215
+
216
+	/** NVS access */
217
+	struct nvs_device nvs;
213 218
 };
214 219
 
215 220
 /**************************************************************************
@@ -2206,10 +2211,13 @@ struct efab_spi_device {
2206 2211
 	unsigned int device_id;
2207 2212
 	/** Address length (in bytes) */
2208 2213
 	unsigned int addr_len;
2209
-	/** Read command */
2210
-	unsigned int read_command;
2214
+	/** Device size */
2215
+	unsigned int len;
2211 2216
 };
2212 2217
 
2218
+#define SPI_WRITE_CMD 0x02
2219
+#define SPI_READ_CMD 0x03
2220
+
2213 2221
 /**
2214 2222
  * Wait for SPI command completion
2215 2223
  *
@@ -2234,8 +2242,8 @@ static int falcon_spi_wait ( struct efab_nic *efab ) {
2234 2242
  *
2235 2243
  */
2236 2244
 static int falcon_spi_read ( struct efab_nic *efab,
2237
-			     struct efab_spi_device *spi,
2238
-			     int address, void *data, unsigned int len ) {
2245
+			     struct efab_spi_device *spi, int address,
2246
+			     void *data, unsigned int len ) {
2239 2247
 	efab_oword_t reg;
2240 2248
 
2241 2249
 	/* Program address register */
@@ -2250,7 +2258,7 @@ static int falcon_spi_read ( struct efab_nic *efab,
2250 2258
 				FCN_EE_SPI_HCMD_READ, FCN_EE_SPI_READ,
2251 2259
 				FCN_EE_SPI_HCMD_DUBCNT, 0,
2252 2260
 				FCN_EE_SPI_HCMD_ADBCNT, spi->addr_len,
2253
-				FCN_EE_SPI_HCMD_ENC, spi->read_command );
2261
+				FCN_EE_SPI_HCMD_ENC, SPI_READ_CMD );
2254 2262
 	falcon_write ( efab, &reg, FCN_EE_SPI_HCMD_REG_KER );
2255 2263
 	
2256 2264
 	/* Wait for read to complete */
@@ -2264,24 +2272,61 @@ static int falcon_spi_read ( struct efab_nic *efab,
2264 2272
 	return 1;
2265 2273
 }
2266 2274
 
2267
-#define SPI_READ_CMD 0x03
2275
+/**
2276
+ * Perform SPI write
2277
+ *
2278
+ */
2279
+static int falcon_spi_write ( struct efab_nic *efab,
2280
+			      struct efab_spi_device *spi, int address,
2281
+			      const void *data, unsigned int len ) {
2282
+	efab_oword_t reg;
2283
+
2284
+	/* Program address register */
2285
+	EFAB_POPULATE_OWORD_1 ( reg, FCN_EE_SPI_HADR_ADR, address );
2286
+	falcon_write ( efab, &reg, FCN_EE_SPI_HADR_REG_KER );
2287
+	
2288
+	/* Program data register */
2289
+	memcpy ( &reg, data, len );
2290
+	falcon_write ( efab, &reg, FCN_EE_SPI_HDATA_REG_KER );
2291
+
2292
+	/* Issue write command */
2293
+	EFAB_POPULATE_OWORD_7 ( reg,
2294
+				FCN_EE_SPI_HCMD_CMD_EN, 1, 
2295
+				FCN_EE_SPI_HCMD_SF_SEL, spi->device_id,
2296
+				FCN_EE_SPI_HCMD_DABCNT, len,
2297
+				FCN_EE_SPI_HCMD_READ, FCN_EE_SPI_WRITE,
2298
+				FCN_EE_SPI_HCMD_DUBCNT, 0,
2299
+				FCN_EE_SPI_HCMD_ADBCNT, spi->addr_len,
2300
+				FCN_EE_SPI_HCMD_ENC, SPI_WRITE_CMD );
2301
+	falcon_write ( efab, &reg, FCN_EE_SPI_HCMD_REG_KER );
2302
+	
2303
+	/* Wait for read to complete */
2304
+	if ( ! falcon_spi_wait ( efab ) )
2305
+		return 0;
2306
+	
2307
+	return 1;
2308
+}
2309
+
2268 2310
 #define AT25F1024_ADDR_LEN 3
2269
-#define AT25F1024_READ_CMD SPI_READ_CMD
2311
+#define AT25040_ADDR_LEN 1
2270 2312
 #define MC25XX640_ADDR_LEN 2
2271
-#define MC25XX640_READ_CMD SPI_READ_CMD
2272 2313
 
2273 2314
 /** Falcon Flash SPI device */
2274 2315
 static struct efab_spi_device falcon_spi_flash = {
2275 2316
 	.device_id	= FCN_EE_SPI_FLASH,
2276 2317
 	.addr_len	= AT25F1024_ADDR_LEN,
2277
-	.read_command	= AT25F1024_READ_CMD,
2278 2318
 };
2279 2319
 
2280 2320
 /** Falcon EEPROM SPI device */
2281 2321
 static struct efab_spi_device falcon_spi_large_eeprom = {
2282 2322
 	.device_id	= FCN_EE_SPI_EEPROM,
2283 2323
 	.addr_len	= MC25XX640_ADDR_LEN,
2284
-	.read_command	= MC25XX640_READ_CMD,
2324
+};
2325
+
2326
+/** Falcon EEPROM SPI device */
2327
+static struct efab_spi_device falcon_spi_small_eeprom = {
2328
+	.device_id	= FCN_EE_SPI_EEPROM,
2329
+	.addr_len	= AT25040_ADDR_LEN,
2285 2330
 };
2286 2331
 
2287 2332
 /** Offset of MAC address within EEPROM or Flash */
@@ -2301,6 +2346,49 @@ static int falcon_read_eeprom ( struct efab_nic *efab ) {
2301 2346
 				 efab->mac_addr, sizeof ( efab->mac_addr ) );
2302 2347
 }
2303 2348
 
2349
+#define FALCON_NVS_OFFSET 0x000
2350
+
2351
+static int falcon_read_nvs ( struct nvs_device *nvs, unsigned int offset,
2352
+			     void *data, size_t len ) {
2353
+	struct efab_nic *efab = container_of ( nvs, struct efab_nic, nvs );
2354
+	struct efab_spi_device *spi = &falcon_spi_small_eeprom;
2355
+
2356
+	while ( len ) {
2357
+		if ( ! falcon_spi_read ( efab, spi,
2358
+					 ( offset + FALCON_NVS_OFFSET ),
2359
+					 data, 16 ) ) {
2360
+			return -EIO;
2361
+		}
2362
+		data += 16;
2363
+		offset += 16;
2364
+		len -=16;
2365
+	}
2366
+	return 0;
2367
+}
2368
+
2369
+static int falcon_write_nvs ( struct nvs_device *nvs, unsigned int offset,
2370
+			      const void *data, size_t len ) {
2371
+	struct efab_nic *efab = container_of ( nvs, struct efab_nic, nvs );
2372
+	struct efab_spi_device *spi = &falcon_spi_large_eeprom;
2373
+
2374
+	while ( len ) {
2375
+		if ( ! falcon_spi_write ( efab, spi,
2376
+					 ( offset + FALCON_NVS_OFFSET ),
2377
+					 data, 16 ) ) {
2378
+			return -EIO;
2379
+		}
2380
+		data += 16;
2381
+		offset += 16;
2382
+		len -=16;
2383
+	}
2384
+	return 0;
2385
+}
2386
+
2387
+static struct nvs_operations falcon_nvs_operations = {
2388
+	.read = falcon_read_nvs,
2389
+	.write = falcon_write_nvs,
2390
+};
2391
+
2304 2392
 /** RX descriptor */
2305 2393
 typedef efab_qword_t falcon_rx_desc_t;
2306 2394
 
@@ -2952,6 +3040,14 @@ static int falcon_init_nic ( struct efab_nic *efab ) {
2952 3040
 	falcon_write ( efab, &reg, FCN_INT_ADR_REG_KER );
2953 3041
 	udelay ( 1000 );
2954 3042
 
3043
+	/* Register non-volatile storage */
3044
+	if ( efab->has_eeprom ) {
3045
+		efab->nvs.op = &falcon_nvs_operations;
3046
+		efab->nvs.len = 0x100;
3047
+		if ( nvs_register ( &efab->nvs ) != 0 )
3048
+			return 0;
3049
+	}
3050
+
2955 3051
 	return 1;
2956 3052
 }
2957 3053
 
@@ -3303,7 +3399,7 @@ PROBE - Look for an adapter, this routine's visible to the outside
3303 3399
 ***************************************************************************/
3304 3400
 static int etherfabric_probe ( struct nic *nic, struct pci_device *pci ) {
3305 3401
 	static struct efab_nic efab;
3306
-	static int nic_port = 1;
3402
+	static int nic_port = 0;
3307 3403
 	struct efab_buffers *buffers;
3308 3404
 	int i;
3309 3405
 

+ 30
- 0
src/include/gpxe/nvs.h Dosyayı Görüntüle

@@ -0,0 +1,30 @@
1
+#ifndef _GPXE_NVS_H
2
+#define _GPXE_NVS_H
3
+
4
+/** @file
5
+ *
6
+ * Non-volatile storage
7
+ *
8
+ */
9
+
10
+#include <stdint.h>
11
+
12
+struct nvs_operations;
13
+
14
+struct nvs_device {
15
+	struct dhcp_option_block *options;
16
+	size_t len;
17
+	struct nvs_operations *op;
18
+};
19
+
20
+struct nvs_operations {
21
+	int ( * read ) ( struct nvs_device *nvs, unsigned int offset,
22
+			 void *data, size_t len );
23
+	int ( * write ) ( struct nvs_device *nvs, unsigned int offset,
24
+			  const void *data, size_t len );
25
+};
26
+
27
+extern int nvs_register ( struct nvs_device *nvs );
28
+extern void nvs_unregister ( struct nvs_device *nvs );
29
+
30
+#endif /* _GPXE_NVS_H */

Loading…
İptal
Kaydet