xref: /openbmc/u-boot/cmd/mmc_spi.c (revision c916075f)
1 /*
2  * Command for mmc_spi setup.
3  *
4  * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
5  * Licensed under the GPL-2 or later.
6  */
7 
8 #include <common.h>
9 #include <mmc.h>
10 #include <spi.h>
11 
12 #ifndef CONFIG_MMC_SPI_BUS
13 # define CONFIG_MMC_SPI_BUS 0
14 #endif
15 #ifndef CONFIG_MMC_SPI_CS
16 # define CONFIG_MMC_SPI_CS 1
17 #endif
18 /* in SPI mode, MMC speed limit is 20MHz, while SD speed limit is 25MHz */
19 #ifndef CONFIG_MMC_SPI_SPEED
20 # define CONFIG_MMC_SPI_SPEED 25000000
21 #endif
22 /* MMC and SD specs only seem to care that sampling is on the
23  * rising edge ... meaning SPI modes 0 or 3.  So either SPI mode
24  * should be legit.  We'll use mode 0 since the steady state is 0,
25  * which is appropriate for hotplugging, unless the platform data
26  * specify mode 3 (if hardware is not compatible to mode 0).
27  */
28 #ifndef CONFIG_MMC_SPI_MODE
29 # define CONFIG_MMC_SPI_MODE SPI_MODE_0
30 #endif
31 
32 static int do_mmc_spi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
33 {
34 	uint bus = CONFIG_MMC_SPI_BUS;
35 	uint cs = CONFIG_MMC_SPI_CS;
36 	uint speed = CONFIG_MMC_SPI_SPEED;
37 	uint mode = CONFIG_MMC_SPI_MODE;
38 	char *endp;
39 	struct mmc *mmc;
40 
41 	if (argc < 2)
42 		goto usage;
43 
44 	cs = simple_strtoul(argv[1], &endp, 0);
45 	if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
46 		goto usage;
47 	if (*endp == ':') {
48 		if (endp[1] == 0)
49 			goto usage;
50 		bus = cs;
51 		cs = simple_strtoul(endp + 1, &endp, 0);
52 		if (*endp != 0)
53 			goto usage;
54 	}
55 	if (argc >= 3) {
56 		speed = simple_strtoul(argv[2], &endp, 0);
57 		if (*argv[2] == 0 || *endp != 0)
58 			goto usage;
59 	}
60 	if (argc >= 4) {
61 		mode = simple_strtoul(argv[3], &endp, 16);
62 		if (*argv[3] == 0 || *endp != 0)
63 			goto usage;
64 	}
65 	if (!spi_cs_is_valid(bus, cs)) {
66 		printf("Invalid SPI bus %u cs %u\n", bus, cs);
67 		return 1;
68 	}
69 
70 	mmc = mmc_spi_init(bus, cs, speed, mode);
71 	if (!mmc) {
72 		printf("Failed to create MMC Device\n");
73 		return 1;
74 	}
75 	printf("%s: %d at %u:%u hz %u mode %u\n", mmc->cfg->name,
76 	       mmc->block_dev.devnum, bus, cs, speed, mode);
77 	mmc_init(mmc);
78 	return 0;
79 
80 usage:
81 	return CMD_RET_USAGE;
82 }
83 
84 U_BOOT_CMD(
85 	mmc_spi,	4,	0,	do_mmc_spi,
86 	"mmc_spi setup",
87 	"[bus:]cs [hz] [mode]	- setup mmc_spi device"
88 );
89