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