1 /*
2  * atmel_df_pow2.c - convert Atmel Dataflashes to Power of 2 mode
3  *
4  * Copyright 2009 Analog Devices Inc.
5  *
6  * Licensed under the 2-clause BSD.
7  */
8 
9 #include <common.h>
10 #include <exports.h>
11 #include <spi.h>
12 #include <linux/delay.h>
13 
14 #define CMD_ID    0x9f
15 #define CMD_STAT  0xd7
16 #define CMD_CFG   0x3d
17 
flash_cmd(struct spi_slave * slave,uchar cmd,uchar * buf,int len)18 static int flash_cmd(struct spi_slave *slave, uchar cmd, uchar *buf, int len)
19 {
20 	buf[0] = cmd;
21 	return spi_xfer(slave, 8 * len, buf, buf, SPI_XFER_BEGIN | SPI_XFER_END);
22 }
23 
flash_status(struct spi_slave * slave)24 static int flash_status(struct spi_slave *slave)
25 {
26 	uchar buf[2];
27 	if (flash_cmd(slave, CMD_STAT, buf, sizeof(buf)))
28 		return -1;
29 	return buf[1];
30 }
31 
flash_set_pow2(struct spi_slave * slave)32 static int flash_set_pow2(struct spi_slave *slave)
33 {
34 	int ret;
35 	uchar buf[4];
36 
37 	buf[1] = 0x2a;
38 	buf[2] = 0x80;
39 	buf[3] = 0xa6;
40 
41 	ret = flash_cmd(slave, CMD_CFG, buf, sizeof(buf));
42 	if (ret)
43 		return ret;
44 
45 	/* wait Tp, or 6 msec */
46 	udelay(6000);
47 
48 	ret = flash_status(slave);
49 	if (ret == -1)
50 		return 1;
51 
52 	return ret & 0x1 ? 0 : 1;
53 }
54 
flash_check(struct spi_slave * slave)55 static int flash_check(struct spi_slave *slave)
56 {
57 	int ret;
58 	uchar buf[4];
59 
60 	ret = flash_cmd(slave, CMD_ID, buf, sizeof(buf));
61 	if (ret)
62 		return ret;
63 
64 	if (buf[1] != 0x1F) {
65 		printf("atmel flash not found (id[0] = %#x)\n", buf[1]);
66 		return 1;
67 	}
68 
69 	if ((buf[2] >> 5) != 0x1) {
70 		printf("AT45 flash not found (id[0] = %#x)\n", buf[2]);
71 		return 2;
72 	}
73 
74 	return 0;
75 }
76 
getline(void)77 static char *getline(void)
78 {
79 	static char buffer[100];
80 	char c;
81 	size_t i;
82 
83 	i = 0;
84 	while (1) {
85 		buffer[i] = '\0';
86 
87 		c = getc();
88 
89 		switch (c) {
90 		case '\r':	/* Enter/Return key */
91 		case '\n':
92 			puts("\n");
93 			return buffer;
94 
95 		case 0x03:	/* ^C - break */
96 			return NULL;
97 
98 		case 0x5F:
99 		case 0x08:	/* ^H  - backspace */
100 		case 0x7F:	/* DEL - backspace */
101 			if (i) {
102 				puts("\b \b");
103 				i--;
104 			}
105 			break;
106 
107 		default:
108 			/* Ignore control characters */
109 			if (c < 0x20)
110 				break;
111 			/* Queue up all other characters */
112 			buffer[i++] = c;
113 			printf("%c", c);
114 			break;
115 		}
116 	}
117 }
118 
atmel_df_pow2(int argc,char * const argv[])119 int atmel_df_pow2(int argc, char *const argv[])
120 {
121 	/* Print the ABI version */
122 	app_startup(argv);
123 	if (XF_VERSION != get_version()) {
124 		printf("Expects ABI version %d\n", XF_VERSION);
125 		printf("Actual U-Boot ABI version %lu\n", get_version());
126 		printf("Can't run\n\n");
127 		return 1;
128 	}
129 
130 	while (1) {
131 		struct spi_slave *slave;
132 		char *line, *p;
133 		int bus, cs, status;
134 
135 		puts("\nenter the [BUS:]CS of the SPI flash: ");
136 		line = getline();
137 
138 		/* CTRL+C */
139 		if (!line)
140 			return 0;
141 		if (line[0] == '\0')
142 			continue;
143 
144 		bus = cs = simple_strtoul(line, &p, 10);
145 		if (*p) {
146 			if (*p == ':') {
147 				++p;
148 				cs = simple_strtoul(p, &p, 10);
149 			}
150 			if (*p) {
151 				puts("invalid format, please try again\n");
152 				continue;
153 			}
154 		} else
155 			bus = 0;
156 
157 		printf("\ngoing to work with dataflash at %i:%i\n", bus, cs);
158 
159 		/* use a low speed -- it'll work with all devices, and
160 		 * speed here doesn't really matter.
161 		 */
162 		slave = spi_setup_slave(bus, cs, 1000, SPI_MODE_3);
163 		if (!slave) {
164 			puts("unable to setup slave\n");
165 			continue;
166 		}
167 
168 		if (spi_claim_bus(slave)) {
169 			spi_free_slave(slave);
170 			continue;
171 		}
172 
173 		if (flash_check(slave)) {
174 			puts("no flash found\n");
175 			goto done;
176 		}
177 
178 		status = flash_status(slave);
179 		if (status == -1) {
180 			puts("unable to read status register\n");
181 			goto done;
182 		}
183 		if (status & 0x1) {
184 			puts("flash is already in power-of-2 mode!\n");
185 			goto done;
186 		}
187 
188 		puts("are you sure you wish to set power-of-2 mode?\n");
189 		puts("this operation is permanent and irreversible\n");
190 		printf("enter YES to continue: ");
191 		line = getline();
192 		if (!line || strcmp(line, "YES"))
193 			goto done;
194 
195 		if (flash_set_pow2(slave)) {
196 			puts("setting pow2 mode failed\n");
197 			goto done;
198 		}
199 
200 		puts(
201 			"Configuration should be updated now.  You will have to\n"
202 			"power cycle the part in order to finish the conversion.\n"
203 		);
204 
205  done:
206 		spi_release_bus(slave);
207 		spi_free_slave(slave);
208 	}
209 }
210