blob: b15468218bb15b8c7c1fd2d63c084c8a55ccedbf [file] [log] [blame]
Norman James6a58a272015-10-07 14:34:16 -05001#include <stdint.h>
2#include <stdbool.h>
3#include <stdlib.h>
4#include <errno.h>
5#include <stdio.h>
6#include <string.h>
7
8#include <libflash/libflash.h>
9#include <libflash/libflash-priv.h>
10
11#include "ast.h"
12
13#ifndef __unused
14#define __unused __attribute__((unused))
15#endif
16
17#define CALIBRATE_BUF_SIZE 16384
18
19struct ast_sf_ctrl {
20 /* We have 2 controllers, one for the BMC flash, one for the PNOR */
21 uint8_t type;
22
23 /* Address and previous value of the ctrl register */
24 uint32_t ctl_reg;
25
26 /* Control register value for normal commands */
27 uint32_t ctl_val;
28
29 /* Control register value for (fast) reads */
30 uint32_t ctl_read_val;
31
32 /* Flash read timing register */
33 uint32_t fread_timing_reg;
34 uint32_t fread_timing_val;
35
36 /* Address of the flash mapping */
37 uint32_t flash;
38
39 /* Current 4b mode */
40 bool mode_4b;
41
42 /* Callbacks */
43 struct spi_flash_ctrl ops;
44};
45
46static uint32_t ast_ahb_freq;
47
48static const uint32_t ast_ct_hclk_divs[] = {
49 0xf, /* HCLK */
50 0x7, /* HCLK/2 */
51 0xe, /* HCLK/3 */
52 0x6, /* HCLK/4 */
53 0xd, /* HCLK/5 */
54};
55
56static int ast_sf_start_cmd(struct ast_sf_ctrl *ct, uint8_t cmd)
57{
58 /* Switch to user mode, CE# dropped */
59 ast_ahb_writel(ct->ctl_val | 7, ct->ctl_reg);
60
61 /* user mode, CE# active */
62 ast_ahb_writel(ct->ctl_val | 3, ct->ctl_reg);
63
64 /* write cmd */
65 return ast_copy_to_ahb(ct->flash, &cmd, 1);
66}
67
68static void ast_sf_end_cmd(struct ast_sf_ctrl *ct)
69{
70 /* clear CE# */
71 ast_ahb_writel(ct->ctl_val | 7, ct->ctl_reg);
72
73 /* Switch back to read mode */
74 ast_ahb_writel(ct->ctl_read_val, ct->ctl_reg);
75}
76
77static int ast_sf_send_addr(struct ast_sf_ctrl *ct, uint32_t addr)
78{
79 const void *ap;
80
81 /* Layout address MSB first in memory */
82 addr = cpu_to_be32(addr);
83
84 /* Send the right amount of bytes */
85 ap = (char *)&addr;
86
87 if (ct->mode_4b)
88 return ast_copy_to_ahb(ct->flash, ap, 4);
89 else
90 return ast_copy_to_ahb(ct->flash, ap + 1, 3);
91}
92
93static int ast_sf_cmd_rd(struct spi_flash_ctrl *ctrl, uint8_t cmd,
94 bool has_addr, uint32_t addr, void *buffer,
95 uint32_t size)
96{
97 struct ast_sf_ctrl *ct = container_of(ctrl, struct ast_sf_ctrl, ops);
98 int rc;
99
100 rc = ast_sf_start_cmd(ct, cmd);
101 if (rc)
102 goto bail;
103 if (has_addr) {
104 rc = ast_sf_send_addr(ct, addr);
105 if (rc)
106 goto bail;
107 }
108 if (buffer && size)
109 rc = ast_copy_from_ahb(buffer, ct->flash, size);
110 bail:
111 ast_sf_end_cmd(ct);
112 return rc;
113}
114
115static int ast_sf_cmd_wr(struct spi_flash_ctrl *ctrl, uint8_t cmd,
116 bool has_addr, uint32_t addr, const void *buffer,
117 uint32_t size)
118{
119 struct ast_sf_ctrl *ct = container_of(ctrl, struct ast_sf_ctrl, ops);
120 int rc;
121
122 rc = ast_sf_start_cmd(ct, cmd);
123 if (rc)
124 goto bail;
125 if (has_addr) {
126 rc = ast_sf_send_addr(ct, addr);
127 if (rc)
128 goto bail;
129 }
130 if (buffer && size)
131 rc = ast_copy_to_ahb(ct->flash, buffer, size);
132 bail:
133 ast_sf_end_cmd(ct);
134 return rc;
135}
136
137static int ast_sf_set_4b(struct spi_flash_ctrl *ctrl, bool enable)
138{
139 struct ast_sf_ctrl *ct = container_of(ctrl, struct ast_sf_ctrl, ops);
140
141 if (ct->type != AST_SF_TYPE_PNOR)
142 return enable ? FLASH_ERR_4B_NOT_SUPPORTED : 0;
143
144 /*
145 * We update the "old" value as well since when quitting
146 * we don't restore the mode of the flash itself so we need
147 * to leave the controller in a compatible setup
148 */
149 if (enable) {
150 ct->ctl_val |= 0x2000;
151 ct->ctl_read_val |= 0x2000;
152 } else {
153 ct->ctl_val &= ~0x2000;
154 ct->ctl_read_val &= ~0x2000;
155 }
156 ct->mode_4b = enable;
157
158 /* Update read mode */
159 ast_ahb_writel(ct->ctl_read_val, ct->ctl_reg);
160
161 return 0;
162}
163
164static int ast_sf_read(struct spi_flash_ctrl *ctrl, uint32_t pos,
165 void *buf, uint32_t len)
166{
167 struct ast_sf_ctrl *ct = container_of(ctrl, struct ast_sf_ctrl, ops);
168
169 /*
170 * We are in read mode by default. We don't yet support fancy
171 * things like fast read or X2 mode
172 */
173 return ast_copy_from_ahb(buf, ct->flash + pos, len);
174}
175
176static void ast_get_ahb_freq(void)
177{
178 static const uint32_t cpu_freqs_24_48[] = {
179 384000000,
180 360000000,
181 336000000,
182 408000000
183 };
184 static const uint32_t cpu_freqs_25[] = {
185 400000000,
186 375000000,
187 350000000,
188 425000000
189 };
190 static const uint32_t ahb_div[] = { 1, 2, 4, 3 };
191 uint32_t strap, cpu_clk, div;
192
193 if (ast_ahb_freq)
194 return;
195
196 /* HW strapping gives us the CPU freq and AHB divisor */
197 strap = ast_ahb_readl(SCU_HW_STRAPPING);
198 if (strap & 0x00800000) {
199 FL_DBG("AST: CLKIN 25Mhz\n");
200 cpu_clk = cpu_freqs_25[(strap >> 8) & 3];
201 } else {
202 FL_DBG("AST: CLKIN 24/48Mhz\n");
203 cpu_clk = cpu_freqs_24_48[(strap >> 8) & 3];
204 }
205 FL_DBG("AST: CPU frequency: %d Mhz\n", cpu_clk / 1000000);
206 div = ahb_div[(strap >> 10) & 3];
207 ast_ahb_freq = cpu_clk / div;
208 FL_DBG("AST: AHB frequency: %d Mhz\n", ast_ahb_freq / 1000000);
209}
210
211static int ast_sf_check_reads(struct ast_sf_ctrl *ct,
212 const uint8_t *golden_buf, uint8_t *test_buf)
213{
214 int i, rc;
215
216 for (i = 0; i < 10; i++) {
217 rc = ast_copy_from_ahb(test_buf, ct->flash, CALIBRATE_BUF_SIZE);
218 if (rc)
219 return rc;
220 if (memcmp(test_buf, golden_buf, CALIBRATE_BUF_SIZE) != 0)
221 return FLASH_ERR_VERIFY_FAILURE;
222 }
223 return 0;
224}
225
226static int ast_sf_calibrate_reads(struct ast_sf_ctrl *ct, uint32_t hdiv,
227 const uint8_t *golden_buf, uint8_t *test_buf)
228{
229 int i, rc;
230 int good_pass = -1, pass_count = 0;
231 uint32_t shift = (hdiv - 1) << 2;
232 uint32_t mask = ~(0xfu << shift);
233
234#define FREAD_TPASS(i) (((i) / 2) | (((i) & 1) ? 0 : 8))
235
236 /* Try HCLK delay 0..5, each one with/without delay and look for a
237 * good pair.
238 */
239 for (i = 0; i < 12; i++) {
240 bool pass;
241
242 ct->fread_timing_val &= mask;
243 ct->fread_timing_val |= FREAD_TPASS(i) << shift;
244 ast_ahb_writel(ct->fread_timing_val, ct->fread_timing_reg);
245 rc = ast_sf_check_reads(ct, golden_buf, test_buf);
246 if (rc && rc != FLASH_ERR_VERIFY_FAILURE)
247 return rc;
248 pass = (rc == 0);
249 FL_DBG(" * [%08x] %d HCLK delay, %dns DI delay : %s\n",
250 ct->fread_timing_val, i/2, (i & 1) ? 0 : 4, pass ? "PASS" : "FAIL");
251 if (pass) {
252 pass_count++;
253 if (pass_count == 3) {
254 good_pass = i - 1;
255 break;
256 }
257 } else
258 pass_count = 0;
259 }
260
261 /* No good setting for this frequency */
262 if (good_pass < 0)
263 return FLASH_ERR_VERIFY_FAILURE;
264
265 /* We have at least one pass of margin, let's use first pass */
266 ct->fread_timing_val &= mask;
267 ct->fread_timing_val |= FREAD_TPASS(good_pass) << shift;
268 ast_ahb_writel(ct->fread_timing_val, ct->fread_timing_reg);
269 FL_DBG("AST: * -> good is pass %d [0x%08x]\n",
270 good_pass, ct->fread_timing_val);
271 return 0;
272}
273
274static bool ast_calib_data_usable(const uint8_t *test_buf, uint32_t size)
275{
276 const uint32_t *tb32 = (const uint32_t *)test_buf;
277 uint32_t i, cnt = 0;
278
279 /* We check if we have enough words that are neither all 0
280 * nor all 1's so the calibration can be considered valid.
281 *
282 * I use an arbitrary threshold for now of 64
283 */
284 size >>= 2;
285 for (i = 0; i < size; i++) {
286 if (tb32[i] != 0 && tb32[i] != 0xffffffff)
287 cnt++;
288 }
289 return cnt >= 64;
290}
291
292static int ast_sf_optimize_reads(struct ast_sf_ctrl *ct, struct flash_info *info,
293 uint32_t max_freq)
294{
295 uint8_t *golden_buf, *test_buf;
296 int i, rc, best_div = -1;
297 uint32_t save_read_val = ct->ctl_read_val;
298
299 test_buf = malloc(CALIBRATE_BUF_SIZE * 2);
300 golden_buf = test_buf + CALIBRATE_BUF_SIZE;
301
302 /* We start with the dumbest setting and read some data */
303 ct->ctl_read_val = (ct->ctl_read_val & 0x2000) |
304 (0x00 << 28) | /* Single bit */
305 (0x00 << 24) | /* CE# max */
306 (0x03 << 16) | /* use normal reads */
307 (0x00 << 8) | /* HCLK/16 */
308 (0x00 << 6) | /* no dummy cycle */
309 (0x00); /* normal read */
310 ast_ahb_writel(ct->ctl_read_val, ct->ctl_reg);
311
312 rc = ast_copy_from_ahb(golden_buf, ct->flash, CALIBRATE_BUF_SIZE);
313 if (rc) {
314 free(test_buf);
315 return rc;
316 }
317
318 /* Establish our read mode with freq field set to 0 */
319 ct->ctl_read_val = save_read_val & 0xfffff0ff;
320
321 /* Check if calibration data is suitable */
322 if (!ast_calib_data_usable(golden_buf, CALIBRATE_BUF_SIZE)) {
323 FL_INF("AST: Calibration area too uniform, "
324 "using low speed\n");
325 ast_ahb_writel(ct->ctl_read_val, ct->ctl_reg);
326 free(test_buf);
327 return 0;
328 }
329
330 /* Now we iterate the HCLK dividers until we find our breaking point */
331 for (i = 5; i > 0; i--) {
332 uint32_t tv, freq;
333
334 /* Compare timing to max */
335 freq = ast_ahb_freq / i;
336 if (freq >= max_freq)
337 continue;
338
339 /* Set the timing */
340 tv = ct->ctl_read_val | (ast_ct_hclk_divs[i - 1] << 8);
341 ast_ahb_writel(tv, ct->ctl_reg);
342 FL_DBG("AST: Trying HCLK/%d...\n", i);
343 rc = ast_sf_calibrate_reads(ct, i, golden_buf, test_buf);
344
345 /* Some other error occurred, bail out */
346 if (rc && rc != FLASH_ERR_VERIFY_FAILURE) {
347 free(test_buf);
348 return rc;
349 }
350 if (rc == 0)
351 best_div = i;
352 }
353 free(test_buf);
354
355 /* Nothing found ? */
356 if (best_div < 0)
357 FL_ERR("AST: No good frequency, using dumb slow\n");
358 else {
359 FL_DBG("AST: Found good read timings at HCLK/%d\n", best_div);
360 ct->ctl_read_val |= (ast_ct_hclk_divs[best_div - 1] << 8);
361 }
362 ast_ahb_writel(ct->ctl_read_val, ct->ctl_reg);
363
364 return 0;
365}
366
367static int ast_sf_get_hclk(uint32_t *ctl_val, uint32_t max_freq)
368{
369 int i;
370
371 /* It appears that running commands at HCLK/2 on some micron
372 * chips results in occasionally reads of bogus status (that
373 * or unrelated chip hangs).
374 *
375 * Since we cannot calibrate properly the reads for commands,
376 * instead, let's limit our SPI frequency to HCLK/4 to stay
377 * on the safe side of things
378 */
379#define MIN_CMD_FREQ 4
380 for (i = MIN_CMD_FREQ; i <= 5; i++) {
381 uint32_t freq = ast_ahb_freq / i;
382 if (freq >= max_freq)
383 continue;
384 *ctl_val |= (ast_ct_hclk_divs[i - 1] << 8);
385 return i;
386 }
387 return 0;
388}
389
390static int ast_sf_setup_macronix(struct ast_sf_ctrl *ct, struct flash_info *info)
391{
392 int rc, div;
393 uint8_t srcr[2];
394
395 /*
396 * Those Macronix chips support dual reads at 104Mhz
397 * and dual IO at 84Mhz with 4 dummies.
398 *
399 * Our calibration algo should give us something along
400 * the lines of HCLK/3 (HCLK/2 seems to work sometimes
401 * but appears to be fairly unreliable) which is 64Mhz
402 *
403 * So we chose dual IO mode.
404 *
405 * The CE# inactive width for reads must be 7ns, we set it
406 * to 3T which is about 15ns at the fastest speed we support
407 * HCLK/2) as I've had issue with smaller values.
408 *
409 * For write and program it's 30ns so let's set the value
410 * for normal ops to 6T.
411 *
412 * Preserve the current 4b mode.
413 */
414 FL_DBG("AST: Setting up Macronix...\n");
415
416 /*
417 * Read the status and config registers
418 */
419 rc = ast_sf_cmd_rd(&ct->ops, CMD_RDSR, false, 0, &srcr[0], 1);
420 if (rc != 0) {
421 FL_ERR("AST: Failed to read status\n");
422 return rc;
423 }
424 rc = ast_sf_cmd_rd(&ct->ops, CMD_RDCR, false, 0, &srcr[1], 1);
425 if (rc != 0) {
426 FL_ERR("AST: Failed to read configuration\n");
427 return rc;
428 }
429
430 FL_DBG("AST: Macronix SR:CR: 0x%02x:%02x\n", srcr[0], srcr[1]);
431
432 /* Switch to 8 dummy cycles to enable 104Mhz operations */
433 srcr[1] = (srcr[1] & 0x3f) | 0x80;
434
435 rc = fl_wren(&ct->ops);
436 if (rc) {
437 FL_ERR("AST: Failed to WREN for Macronix config\n");
438 return rc;
439 }
440
441 rc = ast_sf_cmd_wr(&ct->ops, CMD_WRSR, false, 0, srcr, 2);
442 if (rc != 0) {
443 FL_ERR("AST: Failed to write Macronix config\n");
444 return rc;
445 }
446 rc = fl_sync_wait_idle(&ct->ops);;
447 if (rc != 0) {
448 FL_ERR("AST: Failed waiting for config write\n");
449 return rc;
450 }
451
452 FL_DBG("AST: Macronix SR:CR: 0x%02x:%02x\n", srcr[0], srcr[1]);
453
454 /* Use 2READ */
455 ct->ctl_read_val = (ct->ctl_read_val & 0x2000) |
456 (0x03 << 28) | /* Dual IO */
457 (0x0d << 24) | /* CE# width 3T */
458 (0xbb << 16) | /* 2READ command */
459 (0x00 << 8) | /* HCLK/16 (optimize later) */
460 (0x02 << 6) | /* 2 bytes dummy cycle (8 clocks) */
461 (0x01); /* fast read */
462
463 /* Configure SPI flash read timing */
464 rc = ast_sf_optimize_reads(ct, info, 104000000);
465 if (rc) {
466 FL_ERR("AST: Failed to setup proper read timings, rc=%d\n", rc);
467 return rc;
468 }
469
470 /*
471 * For other commands and writes also increase the SPI clock
472 * to HCLK/2 since the chip supports up to 133Mhz and set
473 * CE# inactive to 6T. We request a timing that is 20% below
474 * the limit of the chip, so about 106Mhz which should fit.
475 */
476 ct->ctl_val = (ct->ctl_val & 0x2000) |
477 (0x00 << 28) | /* Single bit */
478 (0x0a << 24) | /* CE# width 6T (b1010) */
479 (0x00 << 16) | /* no command */
480 (0x00 << 8) | /* HCLK/16 (done later) */
481 (0x00 << 6) | /* no dummy cycle */
482 (0x00); /* normal read */
483
484 div = ast_sf_get_hclk(&ct->ctl_val, 106000000);
485 FL_DBG("AST: Command timing set to HCLK/%d\n", div);
486
487 /* Update chip with current read config */
488 ast_ahb_writel(ct->ctl_read_val, ct->ctl_reg);
489 return 0;
490}
491
492static int ast_sf_setup_winbond(struct ast_sf_ctrl *ct, struct flash_info *info)
493{
494 int rc, div;
495
496 FL_DBG("AST: Setting up Windbond...\n");
497
498 /*
499 * This Windbond chip support dual reads at 104Mhz
500 * with 8 dummy cycles.
501 *
502 * The CE# inactive width for reads must be 10ns, we set it
503 * to 3T which is about 15.6ns.
504 */
505 ct->ctl_read_val = (ct->ctl_read_val & 0x2000) |
506 (0x02 << 28) | /* Dual bit data only */
507 (0x0e << 24) | /* CE# width 2T (b1110) */
508 (0x3b << 16) | /* DREAD command */
509 (0x00 << 8) | /* HCLK/16 */
510 (0x01 << 6) | /* 1-byte dummy cycle */
511 (0x01); /* fast read */
512
513 /* Configure SPI flash read timing */
514 rc = ast_sf_optimize_reads(ct, info, 104000000);
515 if (rc) {
516 FL_ERR("AST: Failed to setup proper read timings, rc=%d\n", rc);
517 return rc;
518 }
519
520 /*
521 * For other commands and writes also increase the SPI clock
522 * to HCLK/2 since the chip supports up to 133Mhz. CE# inactive
523 * for write and erase is 50ns so let's set it to 10T.
524 */
525 ct->ctl_val = (ct->ctl_read_val & 0x2000) |
526 (0x00 << 28) | /* Single bit */
527 (0x06 << 24) | /* CE# width 10T (b0110) */
528 (0x00 << 16) | /* no command */
529 (0x00 << 8) | /* HCLK/16 */
530 (0x00 << 6) | /* no dummy cycle */
531 (0x01); /* fast read */
532
533 div = ast_sf_get_hclk(&ct->ctl_val, 106000000);
534 FL_DBG("AST: Command timing set to HCLK/%d\n", div);
535
536 /* Update chip with current read config */
537 ast_ahb_writel(ct->ctl_read_val, ct->ctl_reg);
538 return 0;
539}
540
541static int ast_sf_setup_micron(struct ast_sf_ctrl *ct, struct flash_info *info)
542{
543 uint8_t vconf, ext_id[6];
544 int rc, div;
545
546 FL_DBG("AST: Setting up Micron...\n");
547
548 /*
549 * Read the extended chip ID to try to detect old vs. new
550 * flashes since old Micron flashes have a lot of issues
551 */
552 rc = ast_sf_cmd_rd(&ct->ops, CMD_RDID, false, 0, ext_id, 6);
553 if (rc != 0) {
554 FL_ERR("AST: Failed to read Micron ext ID, sticking to dumb speed\n");
555 return 0;
556 }
557 /* Check ID matches expectations */
558 if (ext_id[0] != ((info->id >> 16) & 0xff) ||
559 ext_id[1] != ((info->id >> 8) & 0xff) ||
560 ext_id[2] != ((info->id ) & 0xff)) {
561 FL_ERR("AST: Micron ext ID mismatch, sticking to dumb speed\n");
562 return 0;
563 }
564 FL_DBG("AST: Micron ext ID byte: 0x%02x\n", ext_id[4]);
565
566 /* Check for old (<45nm) chips, don't try to be fancy on those */
567 if (!(ext_id[4] & 0x40)) {
568 FL_DBG("AST: Old chip, using dumb timings\n");
569 goto dumb;
570 }
571
572 /*
573 * Read the micron specific volatile configuration reg
574 */
575 rc = ast_sf_cmd_rd(&ct->ops, CMD_MIC_RDVCONF, false, 0, &vconf, 1);
576 if (rc != 0) {
577 FL_ERR("AST: Failed to read Micron vconf, sticking to dumb speed\n");
578 goto dumb;
579 }
580 FL_DBG("AST: Micron VCONF: 0x%02x\n", vconf);
581
582 /* Switch to 8 dummy cycles (we might be able to operate with 4
583 * but let's keep some margin
584 */
585 vconf = (vconf & 0x0f) | 0x80;
586
587 rc = ast_sf_cmd_wr(&ct->ops, CMD_MIC_WRVCONF, false, 0, &vconf, 1);
588 if (rc != 0) {
589 FL_ERR("AST: Failed to write Micron vconf, "
590 " sticking to dumb speed\n");
591 goto dumb;
592 }
593 rc = fl_sync_wait_idle(&ct->ops);;
594 if (rc != 0) {
595 FL_ERR("AST: Failed waiting for config write\n");
596 return rc;
597 }
598 FL_DBG("AST: Updated to : 0x%02x\n", vconf);
599
600 /*
601 * Try to do full dual IO, with 8 dummy cycles it supports 133Mhz
602 *
603 * The CE# inactive width for reads must be 20ns, we set it
604 * to 4T which is about 20.8ns.
605 */
606 ct->ctl_read_val = (ct->ctl_read_val & 0x2000) |
607 (0x03 << 28) | /* Single bit */
608 (0x0c << 24) | /* CE# 4T */
609 (0xbb << 16) | /* 2READ command */
610 (0x00 << 8) | /* HCLK/16 (optimize later) */
611 (0x02 << 6) | /* 8 dummy cycles (2 bytes) */
612 (0x01); /* fast read */
613
614 /* Configure SPI flash read timing */
615 rc = ast_sf_optimize_reads(ct, info, 133000000);
616 if (rc) {
617 FL_ERR("AST: Failed to setup proper read timings, rc=%d\n", rc);
618 return rc;
619 }
620
621 /*
622 * For other commands and writes also increase the SPI clock
623 * to HCLK/2 since the chip supports up to 133Mhz. CE# inactive
624 * for write and erase is 50ns so let's set it to 10T.
625 */
626 ct->ctl_val = (ct->ctl_read_val & 0x2000) |
627 (0x00 << 28) | /* Single bit */
628 (0x06 << 24) | /* CE# width 10T (b0110) */
629 (0x00 << 16) | /* no command */
630 (0x00 << 8) | /* HCLK/16 */
631 (0x00 << 6) | /* no dummy cycle */
632 (0x00); /* norm read */
633
634 div = ast_sf_get_hclk(&ct->ctl_val, 133000000);
635 FL_DBG("AST: Command timing set to HCLK/%d\n", div);
636
637 /* Update chip with current read config */
638 ast_ahb_writel(ct->ctl_read_val, ct->ctl_reg);
639
640 return 0;
641
642 dumb:
643 ct->ctl_val = ct->ctl_read_val = (ct->ctl_read_val & 0x2000) |
644 (0x00 << 28) | /* Single bit */
645 (0x00 << 24) | /* CE# max */
646 (0x03 << 16) | /* use normal reads */
647 (0x06 << 8) | /* HCLK/4 */
648 (0x00 << 6) | /* no dummy cycle */
649 (0x00); /* normal read */
650
651 /* Update chip with current read config */
652 ast_ahb_writel(ct->ctl_read_val, ct->ctl_reg);
653
654 return 0;
655}
656
657static int ast_sf_setup(struct spi_flash_ctrl *ctrl, uint32_t *tsize)
658{
659 struct ast_sf_ctrl *ct = container_of(ctrl, struct ast_sf_ctrl, ops);
660 struct flash_info *info = ctrl->finfo;
661
662 (void)tsize;
663
664 /*
665 * Configure better timings and read mode for known
666 * flash chips
667 */
668 switch(info->id) {
669 case 0xc22019: /* MX25L25635F */
670 case 0xc2201a: /* MX66L51235F */
671 return ast_sf_setup_macronix(ct, info);
672 case 0xef4018: /* W25Q128BV */
673 return ast_sf_setup_winbond(ct, info);
674 case 0x20ba20: /* MT25Qx512xx */
675 return ast_sf_setup_micron(ct, info);
676 }
677 /* No special tuning */
678 return 0;
679}
680
681static bool ast_sf_init_pnor(struct ast_sf_ctrl *ct)
682{
683 uint32_t reg;
684
685 ct->ctl_reg = PNOR_SPI_FCTL_CTRL;
686 ct->fread_timing_reg = PNOR_SPI_FREAD_TIMING;
687 ct->flash = PNOR_FLASH_BASE;
688
689 /* Enable writing to the controller */
690 reg = ast_ahb_readl(PNOR_SPI_FCTL_CONF);
691 if (reg == 0xffffffff) {
692 FL_ERR("AST_SF: Failed read from controller config\n");
693 return false;
694 }
695 ast_ahb_writel(reg | 1, PNOR_SPI_FCTL_CONF);
696
697 /*
698 * Snapshot control reg and sanitize it for our
699 * use, switching to 1-bit mode, clearing user
700 * mode if set, etc...
701 *
702 * Also configure SPI clock to something safe
703 * like HCLK/8 (24Mhz)
704 */
705 ct->ctl_val = ast_ahb_readl(ct->ctl_reg);
706 if (ct->ctl_val == 0xffffffff) {
707 FL_ERR("AST_SF: Failed read from controller control\n");
708 return false;
709 }
710
711 ct->ctl_val = (ct->ctl_val & 0x2000) |
712 (0x00 << 28) | /* Single bit */
713 (0x00 << 24) | /* CE# width 16T */
714 (0x00 << 16) | /* no command */
715 (0x04 << 8) | /* HCLK/8 */
716 (0x00 << 6) | /* no dummy cycle */
717 (0x00); /* normal read */
718
719 /* Initial read mode is default */
720 ct->ctl_read_val = ct->ctl_val;
721
722 /* Initial read timings all 0 */
723 ct->fread_timing_val = 0;
724
725 /* Configure for read */
726 ast_ahb_writel(ct->ctl_read_val, ct->ctl_reg);
727 ast_ahb_writel(ct->fread_timing_val, ct->fread_timing_reg);
728
729 if (ct->ctl_val & 0x2000)
730 ct->mode_4b = true;
731 else
732 ct->mode_4b = false;
733
734 return true;
735}
736
737static bool ast_sf_init_bmc(struct ast_sf_ctrl *ct)
738{
739 ct->ctl_reg = BMC_SPI_FCTL_CTRL;
740 ct->fread_timing_reg = BMC_SPI_FREAD_TIMING;
741 ct->flash = BMC_FLASH_BASE;
742
743 /*
744 * Snapshot control reg and sanitize it for our
745 * use, switching to 1-bit mode, clearing user
746 * mode if set, etc...
747 *
748 * Also configure SPI clock to something safe
749 * like HCLK/8 (24Mhz)
750 */
751 ct->ctl_val =
752 (0x00 << 28) | /* Single bit */
753 (0x00 << 24) | /* CE# width 16T */
754 (0x00 << 16) | /* no command */
755 (0x04 << 8) | /* HCLK/8 */
756 (0x00 << 6) | /* no dummy cycle */
757 (0x00); /* normal read */
758
759 /* Initial read mode is default */
760 ct->ctl_read_val = ct->ctl_val;
761
762 /* Initial read timings all 0 */
763 ct->fread_timing_val = 0;
764
765 /* Configure for read */
766 ast_ahb_writel(ct->ctl_read_val, ct->ctl_reg);
767 ast_ahb_writel(ct->fread_timing_val, ct->fread_timing_reg);
768
769 ct->mode_4b = false;
770
771 return true;
772}
773
774int ast_sf_open(uint8_t type, struct spi_flash_ctrl **ctrl)
775{
776 struct ast_sf_ctrl *ct;
777
778 if (type != AST_SF_TYPE_PNOR && type != AST_SF_TYPE_BMC)
779 return -EINVAL;
780
781 *ctrl = NULL;
782 ct = malloc(sizeof(*ct));
783 if (!ct) {
784 FL_ERR("AST_SF: Failed to allocate\n");
785 return -ENOMEM;
786 }
787 memset(ct, 0, sizeof(*ct));
788 ct->type = type;
789 ct->ops.cmd_wr = ast_sf_cmd_wr;
790 ct->ops.cmd_rd = ast_sf_cmd_rd;
791 ct->ops.set_4b = ast_sf_set_4b;
792 ct->ops.read = ast_sf_read;
793 ct->ops.setup = ast_sf_setup;
794
795 ast_get_ahb_freq();
796
797 if (type == AST_SF_TYPE_PNOR) {
798 if (!ast_sf_init_pnor(ct))
799 goto fail;
800 } else {
801 if (!ast_sf_init_bmc(ct))
802 goto fail;
803 }
804
805 *ctrl = &ct->ops;
806
807 return 0;
808 fail:
809 free(ct);
810 return -EIO;
811}
812
813void ast_sf_close(struct spi_flash_ctrl *ctrl)
814{
815 struct ast_sf_ctrl *ct = container_of(ctrl, struct ast_sf_ctrl, ops);
816
817 /* Restore control reg to read */
818 ast_ahb_writel(ct->ctl_read_val, ct->ctl_reg);
819
820 /* Additional cleanup */
821 if (ct->type == AST_SF_TYPE_PNOR) {
822 uint32_t reg = ast_ahb_readl(PNOR_SPI_FCTL_CONF);
823 if (reg != 0xffffffff)
824 ast_ahb_writel(reg & ~1, PNOR_SPI_FCTL_CONF);
825 }
826
827 /* Free the whole lot */
828 free(ct);
829}
830