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