xref: /openbmc/u-boot/drivers/net/e1000_spi.c (revision a31a4158)
1 #include <common.h>
2 #include "e1000.h"
3 #include <linux/compiler.h>
4 
5 /*-----------------------------------------------------------------------
6  * SPI transfer
7  *
8  * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
9  * "bitlen" bits in the SPI MISO port.  That's just the way SPI works.
10  *
11  * The source of the outgoing bits is the "dout" parameter and the
12  * destination of the input bits is the "din" parameter.  Note that "dout"
13  * and "din" can point to the same memory location, in which case the
14  * input data overwrites the output data (since both are buffered by
15  * temporary variables, this is OK).
16  *
17  * This may be interrupted with Ctrl-C if "intr" is true, otherwise it will
18  * never return an error.
19  */
20 static int e1000_spi_xfer(struct e1000_hw *hw, unsigned int bitlen,
21 		const void *dout_mem, void *din_mem, bool intr)
22 {
23 	const uint8_t *dout = dout_mem;
24 	uint8_t *din = din_mem;
25 
26 	uint8_t mask = 0;
27 	uint32_t eecd;
28 	unsigned long i;
29 
30 	/* Pre-read the control register */
31 	eecd = E1000_READ_REG(hw, EECD);
32 
33 	/* Iterate over each bit */
34 	for (i = 0, mask = 0x80; i < bitlen; i++, mask = (mask >> 1)?:0x80) {
35 		/* Check for interrupt */
36 		if (intr && ctrlc())
37 			return -1;
38 
39 		/* Determine the output bit */
40 		if (dout && dout[i >> 3] & mask)
41 			eecd |=  E1000_EECD_DI;
42 		else
43 			eecd &= ~E1000_EECD_DI;
44 
45 		/* Write the output bit and wait 50us */
46 		E1000_WRITE_REG(hw, EECD, eecd);
47 		E1000_WRITE_FLUSH(hw);
48 		udelay(50);
49 
50 		/* Poke the clock (waits 50us) */
51 		e1000_raise_ee_clk(hw, &eecd);
52 
53 		/* Now read the input bit */
54 		eecd = E1000_READ_REG(hw, EECD);
55 		if (din) {
56 			if (eecd & E1000_EECD_DO)
57 				din[i >> 3] |=  mask;
58 			else
59 				din[i >> 3] &= ~mask;
60 		}
61 
62 		/* Poke the clock again (waits 50us) */
63 		e1000_lower_ee_clk(hw, &eecd);
64 	}
65 
66 	/* Now clear any remaining bits of the input */
67 	if (din && (i & 7))
68 		din[i >> 3] &= ~((mask << 1) - 1);
69 
70 	return 0;
71 }
72 
73 #ifdef CONFIG_E1000_SPI_GENERIC
74 static inline struct e1000_hw *e1000_hw_from_spi(struct spi_slave *spi)
75 {
76 	return container_of(spi, struct e1000_hw, spi);
77 }
78 
79 /* Not sure why all of these are necessary */
80 void spi_init_r(void) { /* Nothing to do */ }
81 void spi_init_f(void) { /* Nothing to do */ }
82 void spi_init(void)   { /* Nothing to do */ }
83 
84 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
85 		unsigned int max_hz, unsigned int mode)
86 {
87 	/* Find the right PCI device */
88 	struct e1000_hw *hw = e1000_find_card(bus);
89 	if (!hw) {
90 		printf("ERROR: No such e1000 device: e1000#%u\n", bus);
91 		return NULL;
92 	}
93 
94 	/* Make sure it has an SPI chip */
95 	if (hw->eeprom.type != e1000_eeprom_spi) {
96 		E1000_ERR(hw->nic, "No attached SPI EEPROM found!\n");
97 		return NULL;
98 	}
99 
100 	/* Argument sanity checks */
101 	if (cs != 0) {
102 		E1000_ERR(hw->nic, "No such SPI chip: %u\n", cs);
103 		return NULL;
104 	}
105 	if (mode != SPI_MODE_0) {
106 		E1000_ERR(hw->nic, "Only SPI MODE-0 is supported!\n");
107 		return NULL;
108 	}
109 
110 	/* TODO: Use max_hz somehow */
111 	E1000_DBG(hw->nic, "EEPROM SPI access requested\n");
112 	return &hw->spi;
113 }
114 
115 void spi_free_slave(struct spi_slave *spi)
116 {
117 	__maybe_unused struct e1000_hw *hw = e1000_hw_from_spi(spi);
118 	E1000_DBG(hw->nic, "EEPROM SPI access released\n");
119 }
120 
121 int spi_claim_bus(struct spi_slave *spi)
122 {
123 	struct e1000_hw *hw = e1000_hw_from_spi(spi);
124 
125 	if (e1000_acquire_eeprom(hw)) {
126 		E1000_ERR(hw->nic, "EEPROM SPI cannot be acquired!\n");
127 		return -1;
128 	}
129 
130 	return 0;
131 }
132 
133 void spi_release_bus(struct spi_slave *spi)
134 {
135 	struct e1000_hw *hw = e1000_hw_from_spi(spi);
136 	e1000_release_eeprom(hw);
137 }
138 
139 /* Skinny wrapper around e1000_spi_xfer */
140 int spi_xfer(struct spi_slave *spi, unsigned int bitlen,
141 		const void *dout_mem, void *din_mem, unsigned long flags)
142 {
143 	struct e1000_hw *hw = e1000_hw_from_spi(spi);
144 	int ret;
145 
146 	if (flags & SPI_XFER_BEGIN)
147 		e1000_standby_eeprom(hw);
148 
149 	ret = e1000_spi_xfer(hw, bitlen, dout_mem, din_mem, true);
150 
151 	if (flags & SPI_XFER_END)
152 		e1000_standby_eeprom(hw);
153 
154 	return ret;
155 }
156 
157 #endif /* not CONFIG_E1000_SPI_GENERIC */
158 
159 #ifdef CONFIG_CMD_E1000
160 
161 /* The EEPROM opcodes */
162 #define SPI_EEPROM_ENABLE_WR	0x06
163 #define SPI_EEPROM_DISABLE_WR	0x04
164 #define SPI_EEPROM_WRITE_STATUS	0x01
165 #define SPI_EEPROM_READ_STATUS	0x05
166 #define SPI_EEPROM_WRITE_PAGE	0x02
167 #define SPI_EEPROM_READ_PAGE	0x03
168 
169 /* The EEPROM status bits */
170 #define SPI_EEPROM_STATUS_BUSY	0x01
171 #define SPI_EEPROM_STATUS_WREN	0x02
172 
173 static int e1000_spi_eeprom_enable_wr(struct e1000_hw *hw, bool intr)
174 {
175 	u8 op[] = { SPI_EEPROM_ENABLE_WR };
176 	e1000_standby_eeprom(hw);
177 	return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
178 }
179 
180 /*
181  * These have been tested to perform correctly, but they are not used by any
182  * of the EEPROM commands at this time.
183  */
184 #if 0
185 static int e1000_spi_eeprom_disable_wr(struct e1000_hw *hw, bool intr)
186 {
187 	u8 op[] = { SPI_EEPROM_DISABLE_WR };
188 	e1000_standby_eeprom(hw);
189 	return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
190 }
191 
192 static int e1000_spi_eeprom_write_status(struct e1000_hw *hw,
193 		u8 status, bool intr)
194 {
195 	u8 op[] = { SPI_EEPROM_WRITE_STATUS, status };
196 	e1000_standby_eeprom(hw);
197 	return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr);
198 }
199 #endif
200 
201 static int e1000_spi_eeprom_read_status(struct e1000_hw *hw, bool intr)
202 {
203 	u8 op[] = { SPI_EEPROM_READ_STATUS, 0 };
204 	e1000_standby_eeprom(hw);
205 	if (e1000_spi_xfer(hw, 8*sizeof(op), op, op, intr))
206 		return -1;
207 	return op[1];
208 }
209 
210 static int e1000_spi_eeprom_write_page(struct e1000_hw *hw,
211 		const void *data, u16 off, u16 len, bool intr)
212 {
213 	u8 op[] = {
214 		SPI_EEPROM_WRITE_PAGE,
215 		(off >> (hw->eeprom.address_bits - 8)) & 0xff, off & 0xff
216 	};
217 
218 	e1000_standby_eeprom(hw);
219 
220 	if (e1000_spi_xfer(hw, 8 + hw->eeprom.address_bits, op, NULL, intr))
221 		return -1;
222 	if (e1000_spi_xfer(hw, len << 3, data, NULL, intr))
223 		return -1;
224 
225 	return 0;
226 }
227 
228 static int e1000_spi_eeprom_read_page(struct e1000_hw *hw,
229 		void *data, u16 off, u16 len, bool intr)
230 {
231 	u8 op[] = {
232 		SPI_EEPROM_READ_PAGE,
233 		(off >> (hw->eeprom.address_bits - 8)) & 0xff, off & 0xff
234 	};
235 
236 	e1000_standby_eeprom(hw);
237 
238 	if (e1000_spi_xfer(hw, 8 + hw->eeprom.address_bits, op, NULL, intr))
239 		return -1;
240 	if (e1000_spi_xfer(hw, len << 3, NULL, data, intr))
241 		return -1;
242 
243 	return 0;
244 }
245 
246 static int e1000_spi_eeprom_poll_ready(struct e1000_hw *hw, bool intr)
247 {
248 	int status;
249 	while ((status = e1000_spi_eeprom_read_status(hw, intr)) >= 0) {
250 		if (!(status & SPI_EEPROM_STATUS_BUSY))
251 			return 0;
252 	}
253 	return -1;
254 }
255 
256 static int e1000_spi_eeprom_dump(struct e1000_hw *hw,
257 		void *data, u16 off, unsigned int len, bool intr)
258 {
259 	/* Interruptibly wait for the EEPROM to be ready */
260 	if (e1000_spi_eeprom_poll_ready(hw, intr))
261 		return -1;
262 
263 	/* Dump each page in sequence */
264 	while (len) {
265 		/* Calculate the data bytes on this page */
266 		u16 pg_off = off & (hw->eeprom.page_size - 1);
267 		u16 pg_len = hw->eeprom.page_size - pg_off;
268 		if (pg_len > len)
269 			pg_len = len;
270 
271 		/* Now dump the page */
272 		if (e1000_spi_eeprom_read_page(hw, data, off, pg_len, intr))
273 			return -1;
274 
275 		/* Otherwise go on to the next page */
276 		len  -= pg_len;
277 		off  += pg_len;
278 		data += pg_len;
279 	}
280 
281 	/* We're done! */
282 	return 0;
283 }
284 
285 static int e1000_spi_eeprom_program(struct e1000_hw *hw,
286 		const void *data, u16 off, u16 len, bool intr)
287 {
288 	/* Program each page in sequence */
289 	while (len) {
290 		/* Calculate the data bytes on this page */
291 		u16 pg_off = off & (hw->eeprom.page_size - 1);
292 		u16 pg_len = hw->eeprom.page_size - pg_off;
293 		if (pg_len > len)
294 			pg_len = len;
295 
296 		/* Interruptibly wait for the EEPROM to be ready */
297 		if (e1000_spi_eeprom_poll_ready(hw, intr))
298 			return -1;
299 
300 		/* Enable write access */
301 		if (e1000_spi_eeprom_enable_wr(hw, intr))
302 			return -1;
303 
304 		/* Now program the page */
305 		if (e1000_spi_eeprom_write_page(hw, data, off, pg_len, intr))
306 			return -1;
307 
308 		/* Otherwise go on to the next page */
309 		len  -= pg_len;
310 		off  += pg_len;
311 		data += pg_len;
312 	}
313 
314 	/* Wait for the last write to complete */
315 	if (e1000_spi_eeprom_poll_ready(hw, intr))
316 		return -1;
317 
318 	/* We're done! */
319 	return 0;
320 }
321 
322 static int do_e1000_spi_show(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
323 		int argc, char * const argv[])
324 {
325 	unsigned int length = 0;
326 	u16 i, offset = 0;
327 	u8 *buffer;
328 	int err;
329 
330 	if (argc > 2) {
331 		cmd_usage(cmdtp);
332 		return 1;
333 	}
334 
335 	/* Parse the offset and length */
336 	if (argc >= 1)
337 		offset = simple_strtoul(argv[0], NULL, 0);
338 	if (argc == 2)
339 		length = simple_strtoul(argv[1], NULL, 0);
340 	else if (offset < (hw->eeprom.word_size << 1))
341 		length = (hw->eeprom.word_size << 1) - offset;
342 
343 	/* Extra sanity checks */
344 	if (!length) {
345 		E1000_ERR(hw->nic, "Requested zero-sized dump!\n");
346 		return 1;
347 	}
348 	if ((0x10000 < length) || (0x10000 - length < offset)) {
349 		E1000_ERR(hw->nic, "Can't dump past 0xFFFF!\n");
350 		return 1;
351 	}
352 
353 	/* Allocate a buffer to hold stuff */
354 	buffer = malloc(length);
355 	if (!buffer) {
356 		E1000_ERR(hw->nic, "Out of Memory!\n");
357 		return 1;
358 	}
359 
360 	/* Acquire the EEPROM and perform the dump */
361 	if (e1000_acquire_eeprom(hw)) {
362 		E1000_ERR(hw->nic, "EEPROM SPI cannot be acquired!\n");
363 		free(buffer);
364 		return 1;
365 	}
366 	err = e1000_spi_eeprom_dump(hw, buffer, offset, length, true);
367 	e1000_release_eeprom(hw);
368 	if (err) {
369 		E1000_ERR(hw->nic, "Interrupted!\n");
370 		free(buffer);
371 		return 1;
372 	}
373 
374 	/* Now hexdump the result */
375 	printf("%s: ===== Intel e1000 EEPROM (0x%04hX - 0x%04hX) =====",
376 			hw->nic->name, offset, offset + length - 1);
377 	for (i = 0; i < length; i++) {
378 		if ((i & 0xF) == 0)
379 			printf("\n%s: %04hX: ", hw->nic->name, offset + i);
380 		else if ((i & 0xF) == 0x8)
381 			printf(" ");
382 		printf(" %02hx", buffer[i]);
383 	}
384 	printf("\n");
385 
386 	/* Success! */
387 	free(buffer);
388 	return 0;
389 }
390 
391 static int do_e1000_spi_dump(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
392 		int argc, char * const argv[])
393 {
394 	unsigned int length;
395 	u16 offset;
396 	void *dest;
397 
398 	if (argc != 3) {
399 		cmd_usage(cmdtp);
400 		return 1;
401 	}
402 
403 	/* Parse the arguments */
404 	dest = (void *)simple_strtoul(argv[0], NULL, 16);
405 	offset = simple_strtoul(argv[1], NULL, 0);
406 	length = simple_strtoul(argv[2], NULL, 0);
407 
408 	/* Extra sanity checks */
409 	if (!length) {
410 		E1000_ERR(hw->nic, "Requested zero-sized dump!\n");
411 		return 1;
412 	}
413 	if ((0x10000 < length) || (0x10000 - length < offset)) {
414 		E1000_ERR(hw->nic, "Can't dump past 0xFFFF!\n");
415 		return 1;
416 	}
417 
418 	/* Acquire the EEPROM */
419 	if (e1000_acquire_eeprom(hw)) {
420 		E1000_ERR(hw->nic, "EEPROM SPI cannot be acquired!\n");
421 		return 1;
422 	}
423 
424 	/* Perform the programming operation */
425 	if (e1000_spi_eeprom_dump(hw, dest, offset, length, true) < 0) {
426 		E1000_ERR(hw->nic, "Interrupted!\n");
427 		e1000_release_eeprom(hw);
428 		return 1;
429 	}
430 
431 	e1000_release_eeprom(hw);
432 	printf("%s: ===== EEPROM DUMP COMPLETE =====\n", hw->nic->name);
433 	return 0;
434 }
435 
436 static int do_e1000_spi_program(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
437 		int argc, char * const argv[])
438 {
439 	unsigned int length;
440 	const void *source;
441 	u16 offset;
442 
443 	if (argc != 3) {
444 		cmd_usage(cmdtp);
445 		return 1;
446 	}
447 
448 	/* Parse the arguments */
449 	source = (const void *)simple_strtoul(argv[0], NULL, 16);
450 	offset = simple_strtoul(argv[1], NULL, 0);
451 	length = simple_strtoul(argv[2], NULL, 0);
452 
453 	/* Acquire the EEPROM */
454 	if (e1000_acquire_eeprom(hw)) {
455 		E1000_ERR(hw->nic, "EEPROM SPI cannot be acquired!\n");
456 		return 1;
457 	}
458 
459 	/* Perform the programming operation */
460 	if (e1000_spi_eeprom_program(hw, source, offset, length, true) < 0) {
461 		E1000_ERR(hw->nic, "Interrupted!\n");
462 		e1000_release_eeprom(hw);
463 		return 1;
464 	}
465 
466 	e1000_release_eeprom(hw);
467 	printf("%s: ===== EEPROM PROGRAMMED =====\n", hw->nic->name);
468 	return 0;
469 }
470 
471 static int do_e1000_spi_checksum(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
472 		int argc, char * const argv[])
473 {
474 	uint16_t i, length, checksum = 0, checksum_reg;
475 	uint16_t *buffer;
476 	bool upd;
477 
478 	if (argc == 0)
479 		upd = 0;
480 	else if ((argc == 1) && !strcmp(argv[0], "update"))
481 		upd = 1;
482 	else {
483 		cmd_usage(cmdtp);
484 		return 1;
485 	}
486 
487 	/* Allocate a temporary buffer */
488 	length = sizeof(uint16_t) * (EEPROM_CHECKSUM_REG + 1);
489 	buffer = malloc(length);
490 	if (!buffer) {
491 		E1000_ERR(hw->nic, "Unable to allocate EEPROM buffer!\n");
492 		return 1;
493 	}
494 
495 	/* Acquire the EEPROM */
496 	if (e1000_acquire_eeprom(hw)) {
497 		E1000_ERR(hw->nic, "EEPROM SPI cannot be acquired!\n");
498 		return 1;
499 	}
500 
501 	/* Read the EEPROM */
502 	if (e1000_spi_eeprom_dump(hw, buffer, 0, length, true) < 0) {
503 		E1000_ERR(hw->nic, "Interrupted!\n");
504 		e1000_release_eeprom(hw);
505 		return 1;
506 	}
507 
508 	/* Compute the checksum and read the expected value */
509 	for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
510 		checksum += le16_to_cpu(buffer[i]);
511 	checksum = ((uint16_t)EEPROM_SUM) - checksum;
512 	checksum_reg = le16_to_cpu(buffer[i]);
513 
514 	/* Verify it! */
515 	if (checksum_reg == checksum) {
516 		printf("%s: INFO: EEPROM checksum is correct! (0x%04hx)\n",
517 				hw->nic->name, checksum);
518 		e1000_release_eeprom(hw);
519 		return 0;
520 	}
521 
522 	/* Hrm, verification failed, print an error */
523 	E1000_ERR(hw->nic, "EEPROM checksum is incorrect!\n");
524 	E1000_ERR(hw->nic, "  ...register was 0x%04hx, calculated 0x%04hx\n",
525 			checksum_reg, checksum);
526 
527 	/* If they didn't ask us to update it, just return an error */
528 	if (!upd) {
529 		e1000_release_eeprom(hw);
530 		return 1;
531 	}
532 
533 	/* Ok, correct it! */
534 	printf("%s: Reprogramming the EEPROM checksum...\n", hw->nic->name);
535 	buffer[i] = cpu_to_le16(checksum);
536 	if (e1000_spi_eeprom_program(hw, &buffer[i], i * sizeof(uint16_t),
537 			sizeof(uint16_t), true)) {
538 		E1000_ERR(hw->nic, "Interrupted!\n");
539 		e1000_release_eeprom(hw);
540 		return 1;
541 	}
542 
543 	e1000_release_eeprom(hw);
544 	return 0;
545 }
546 
547 int do_e1000_spi(cmd_tbl_t *cmdtp, struct e1000_hw *hw,
548 		int argc, char * const argv[])
549 {
550 	if (argc < 1) {
551 		cmd_usage(cmdtp);
552 		return 1;
553 	}
554 
555 	/* Make sure it has an SPI chip */
556 	if (hw->eeprom.type != e1000_eeprom_spi) {
557 		E1000_ERR(hw->nic, "No attached SPI EEPROM found!\n");
558 		return 1;
559 	}
560 
561 	/* Check the eeprom sub-sub-command arguments */
562 	if (!strcmp(argv[0], "show"))
563 		return do_e1000_spi_show(cmdtp, hw, argc - 1, argv + 1);
564 
565 	if (!strcmp(argv[0], "dump"))
566 		return do_e1000_spi_dump(cmdtp, hw, argc - 1, argv + 1);
567 
568 	if (!strcmp(argv[0], "program"))
569 		return do_e1000_spi_program(cmdtp, hw, argc - 1, argv + 1);
570 
571 	if (!strcmp(argv[0], "checksum"))
572 		return do_e1000_spi_checksum(cmdtp, hw, argc - 1, argv + 1);
573 
574 	cmd_usage(cmdtp);
575 	return 1;
576 }
577 
578 #endif /* not CONFIG_CMD_E1000 */
579