2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2007, Juniper Networks, Inc.
5 * Copyright (c) 2012-2013, SRI International
8 * Portions of this software were developed by SRI International and the
9 * University of Cambridge Computer Laboratory under DARPA/AFRL contract
10 * (FA8750-10-C-0237) ("CTSRD"), as part of the DARPA CRASH research
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the author nor the names of any co-contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/cdefs.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
45 #include <sys/endian.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
51 #include <sys/sysctl.h>
53 #include <machine/bus.h>
55 #include <dev/cfi/cfi_reg.h>
56 #include <dev/cfi/cfi_var.h>
58 static void cfi_add_sysctls(struct cfi_softc *);
60 extern struct cdevsw cfi_cdevsw;
62 char cfi_driver_name[] = "cfi";
65 cfi_read_raw(struct cfi_softc *sc, u_int ofs)
69 ofs &= ~(sc->sc_width - 1);
70 switch (sc->sc_width) {
72 val = bus_space_read_1(sc->sc_tag, sc->sc_handle, ofs);
75 val = bus_space_read_2(sc->sc_tag, sc->sc_handle, ofs);
78 val = bus_space_read_4(sc->sc_tag, sc->sc_handle, ofs);
88 cfi_read(struct cfi_softc *sc, u_int ofs)
93 ofs &= ~(sc->sc_width - 1);
94 switch (sc->sc_width) {
96 val = bus_space_read_1(sc->sc_tag, sc->sc_handle, ofs);
99 sval = bus_space_read_2(sc->sc_tag, sc->sc_handle, ofs);
100 #ifdef CFI_HARDWAREBYTESWAP
107 val = bus_space_read_4(sc->sc_tag, sc->sc_handle, ofs);
108 #ifndef CFI_HARDWAREBYTESWAP
120 cfi_write(struct cfi_softc *sc, u_int ofs, u_int val)
123 ofs &= ~(sc->sc_width - 1);
124 switch (sc->sc_width) {
126 bus_space_write_1(sc->sc_tag, sc->sc_handle, ofs, val);
129 #ifdef CFI_HARDWAREBYTESWAP
130 bus_space_write_2(sc->sc_tag, sc->sc_handle, ofs, val);
132 bus_space_write_2(sc->sc_tag, sc->sc_handle, ofs, htole16(val));
137 #ifdef CFI_HARDWAREBYTESWAP
138 bus_space_write_4(sc->sc_tag, sc->sc_handle, ofs, val);
140 bus_space_write_4(sc->sc_tag, sc->sc_handle, ofs, htole32(val));
147 * This is same workaound as NetBSD sys/dev/nor/cfi.c cfi_reset_default()
150 cfi_reset_default(struct cfi_softc *sc)
153 cfi_write(sc, 0, CFI_BCS_READ_ARRAY2);
154 cfi_write(sc, 0, CFI_BCS_READ_ARRAY);
158 cfi_read_qry(struct cfi_softc *sc, u_int ofs)
162 cfi_write(sc, CFI_QRY_CMD_ADDR * sc->sc_width, CFI_QRY_CMD_DATA);
163 val = cfi_read(sc, ofs * sc->sc_width);
164 cfi_reset_default(sc);
169 cfi_amd_write(struct cfi_softc *sc, u_int ofs, u_int addr, u_int data)
172 cfi_write(sc, ofs + AMD_ADDR_START, CFI_AMD_UNLOCK);
173 cfi_write(sc, ofs + AMD_ADDR_ACK, CFI_AMD_UNLOCK_ACK);
174 cfi_write(sc, ofs + addr, data);
178 cfi_fmtsize(uint32_t sz)
181 static const char *sfx[] = { "", "K", "M", "G" };
185 while (sfxidx < 3 && sz > 1023) {
190 sprintf(buf, "%u%sB", sz, sfx[sfxidx]);
195 cfi_probe(device_t dev)
198 struct cfi_softc *sc;
201 uint16_t iface, vend;
203 sc = device_get_softc(dev);
207 sc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rid,
209 if (sc->sc_res == NULL)
212 sc->sc_tag = rman_get_bustag(sc->sc_res);
213 sc->sc_handle = rman_get_bushandle(sc->sc_res);
215 if (sc->sc_width == 0) {
217 while (sc->sc_width <= 4) {
218 if (cfi_read_qry(sc, CFI_QRY_IDENT) == 'Q')
222 } else if (cfi_read_qry(sc, CFI_QRY_IDENT) != 'Q') {
226 if (sc->sc_width > 4) {
231 /* We got a Q. Check if we also have the R and the Y. */
232 if (cfi_read_qry(sc, CFI_QRY_IDENT + 1) != 'R' ||
233 cfi_read_qry(sc, CFI_QRY_IDENT + 2) != 'Y') {
238 /* Get the vendor and command set. */
239 vend = cfi_read_qry(sc, CFI_QRY_VEND) |
240 (cfi_read_qry(sc, CFI_QRY_VEND + 1) << 8);
242 sc->sc_cmdset = vend;
245 case CFI_VEND_AMD_ECS:
246 case CFI_VEND_AMD_SCS:
247 vend_str = "AMD/Fujitsu";
249 case CFI_VEND_INTEL_ECS:
250 vend_str = "Intel/Sharp";
252 case CFI_VEND_INTEL_SCS:
255 case CFI_VEND_MITSUBISHI_ECS:
256 case CFI_VEND_MITSUBISHI_SCS:
257 vend_str = "Mitsubishi";
260 vend_str = "Unknown vendor";
264 /* Get the device size. */
265 sc->sc_size = 1U << cfi_read_qry(sc, CFI_QRY_SIZE);
267 /* Sanity-check the I/F */
268 iface = cfi_read_qry(sc, CFI_QRY_IFACE) |
269 (cfi_read_qry(sc, CFI_QRY_IFACE + 1) << 8);
272 * Adding 1 to iface will give us a bit-wise "switch"
273 * that allows us to test for the interface width by
274 * testing a single bit.
278 error = (iface & sc->sc_width) ? 0 : EINVAL;
282 snprintf(desc, sizeof(desc), "%s - %s", vend_str,
283 cfi_fmtsize(sc->sc_size));
284 device_set_desc_copy(dev, desc);
287 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rid, sc->sc_res);
292 cfi_attach(device_t dev)
294 struct cfi_softc *sc;
297 uint64_t mtoexp, ttoexp;
298 #ifdef CFI_SUPPORT_STRATAFLASH
300 char name[KENV_MNAMELEN], value[32];
303 sc = device_get_softc(dev);
307 sc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rid,
308 #ifndef ATSE_CFI_HACK
311 RF_ACTIVE | RF_SHAREABLE);
313 if (sc->sc_res == NULL)
316 sc->sc_tag = rman_get_bustag(sc->sc_res);
317 sc->sc_handle = rman_get_bushandle(sc->sc_res);
319 /* Get time-out values for erase, write, and buffer write. */
320 ttoexp = cfi_read_qry(sc, CFI_QRY_TTO_ERASE);
321 mtoexp = cfi_read_qry(sc, CFI_QRY_MTO_ERASE);
323 device_printf(dev, "erase timeout == 0, using 2^16ms\n");
327 device_printf(dev, "insane timeout: 2^%jdms\n", ttoexp);
331 device_printf(dev, "max erase timeout == 0, using 2^%jdms\n",
335 if (ttoexp + mtoexp > 41) {
336 device_printf(dev, "insane max erase timeout: 2^%jd\n",
340 sc->sc_typical_timeouts[CFI_TIMEOUT_ERASE] = SBT_1MS * (1ULL << ttoexp);
341 sc->sc_max_timeouts[CFI_TIMEOUT_ERASE] =
342 sc->sc_typical_timeouts[CFI_TIMEOUT_ERASE] * (1ULL << mtoexp);
344 ttoexp = cfi_read_qry(sc, CFI_QRY_TTO_WRITE);
345 mtoexp = cfi_read_qry(sc, CFI_QRY_MTO_WRITE);
347 device_printf(dev, "write timeout == 0, using 2^18ns\n");
351 device_printf(dev, "insane write timeout: 2^%jdus\n", ttoexp);
355 device_printf(dev, "max write timeout == 0, using 2^%jdms\n",
359 if (ttoexp + mtoexp > 51) {
360 device_printf(dev, "insane max write timeout: 2^%jdus\n",
364 sc->sc_typical_timeouts[CFI_TIMEOUT_WRITE] = SBT_1US * (1ULL << ttoexp);
365 sc->sc_max_timeouts[CFI_TIMEOUT_WRITE] =
366 sc->sc_typical_timeouts[CFI_TIMEOUT_WRITE] * (1ULL << mtoexp);
368 ttoexp = cfi_read_qry(sc, CFI_QRY_TTO_BUFWRITE);
369 mtoexp = cfi_read_qry(sc, CFI_QRY_MTO_BUFWRITE);
370 /* Don't check for 0, it means not-supported. */
372 device_printf(dev, "insane write timeout: 2^%jdus\n", ttoexp);
375 if (ttoexp + mtoexp > 51) {
376 device_printf(dev, "insane max write timeout: 2^%jdus\n",
380 sc->sc_typical_timeouts[CFI_TIMEOUT_BUFWRITE] =
381 SBT_1US * (1ULL << cfi_read_qry(sc, CFI_QRY_TTO_BUFWRITE));
382 sc->sc_max_timeouts[CFI_TIMEOUT_BUFWRITE] =
383 sc->sc_typical_timeouts[CFI_TIMEOUT_BUFWRITE] *
384 (1ULL << cfi_read_qry(sc, CFI_QRY_MTO_BUFWRITE));
386 /* Get the maximum size of a multibyte program */
387 if (sc->sc_typical_timeouts[CFI_TIMEOUT_BUFWRITE] != 0)
388 sc->sc_maxbuf = 1 << (cfi_read_qry(sc, CFI_QRY_MAXBUF) |
389 cfi_read_qry(sc, CFI_QRY_MAXBUF) << 8);
393 /* Get erase regions. */
394 sc->sc_regions = cfi_read_qry(sc, CFI_QRY_NREGIONS);
395 sc->sc_region = malloc(sc->sc_regions * sizeof(struct cfi_region),
396 M_TEMP, M_WAITOK | M_ZERO);
397 for (r = 0; r < sc->sc_regions; r++) {
398 blocks = cfi_read_qry(sc, CFI_QRY_REGION(r)) |
399 (cfi_read_qry(sc, CFI_QRY_REGION(r) + 1) << 8);
400 sc->sc_region[r].r_blocks = blocks + 1;
402 blksz = cfi_read_qry(sc, CFI_QRY_REGION(r) + 2) |
403 (cfi_read_qry(sc, CFI_QRY_REGION(r) + 3) << 8);
404 sc->sc_region[r].r_blksz = (blksz == 0) ? 128 :
408 /* Reset the device to a default state. */
409 cfi_write(sc, 0, CFI_BCS_CLEAR_STATUS);
412 device_printf(dev, "[");
413 for (r = 0; r < sc->sc_regions; r++) {
414 printf("%ux%s%s", sc->sc_region[r].r_blocks,
415 cfi_fmtsize(sc->sc_region[r].r_blksz),
416 (r == sc->sc_regions - 1) ? "]\n" : ",");
420 if (sc->sc_cmdset == CFI_VEND_AMD_ECS ||
421 sc->sc_cmdset == CFI_VEND_AMD_SCS) {
422 cfi_amd_write(sc, 0, AMD_ADDR_START, CFI_AMD_AUTO_SELECT);
423 sc->sc_manid = cfi_read(sc, 0);
424 sc->sc_devid = cfi_read(sc, 2);
425 device_printf(dev, "Manufacturer ID:%x Device ID:%x\n",
426 sc->sc_manid, sc->sc_devid);
427 cfi_write(sc, 0, CFI_BCS_READ_ARRAY2);
430 u = device_get_unit(dev);
431 sc->sc_nod = make_dev(&cfi_cdevsw, u, UID_ROOT, GID_WHEEL, 0600,
432 "%s%u", cfi_driver_name, u);
433 sc->sc_nod->si_drv1 = sc;
437 #ifdef CFI_SUPPORT_STRATAFLASH
439 * Store the Intel factory PPR in the environment. In some
440 * cases it is the most unique ID on a board.
442 if (cfi_intel_get_factory_pr(sc, &ppr) == 0) {
443 if (snprintf(name, sizeof(name), "%s.factory_ppr",
444 device_get_nameunit(dev)) < (sizeof(name) - 1) &&
445 snprintf(value, sizeof(value), "0x%016jx", ppr) <
447 (void) kern_setenv(name, value);
451 device_add_child(dev, "cfid", -1);
452 bus_generic_attach(dev);
458 cfi_add_sysctls(struct cfi_softc *sc)
460 struct sysctl_ctx_list *ctx;
461 struct sysctl_oid_list *children;
463 ctx = device_get_sysctl_ctx(sc->sc_dev);
464 children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->sc_dev));
466 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
467 "typical_erase_timout_count",
468 CTLFLAG_RD, &sc->sc_tto_counts[CFI_TIMEOUT_ERASE],
469 0, "Number of times the typical erase timeout was exceeded");
470 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
471 "max_erase_timout_count",
472 CTLFLAG_RD, &sc->sc_mto_counts[CFI_TIMEOUT_ERASE], 0,
473 "Number of times the maximum erase timeout was exceeded");
474 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
475 "typical_write_timout_count",
476 CTLFLAG_RD, &sc->sc_tto_counts[CFI_TIMEOUT_WRITE], 0,
477 "Number of times the typical write timeout was exceeded");
478 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
479 "max_write_timout_count",
480 CTLFLAG_RD, &sc->sc_mto_counts[CFI_TIMEOUT_WRITE], 0,
481 "Number of times the maximum write timeout was exceeded");
482 if (sc->sc_maxbuf > 0) {
483 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
484 "typical_bufwrite_timout_count",
485 CTLFLAG_RD, &sc->sc_tto_counts[CFI_TIMEOUT_BUFWRITE], 0,
486 "Number of times the typical buffered write timeout was "
488 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
489 "max_bufwrite_timout_count",
490 CTLFLAG_RD, &sc->sc_mto_counts[CFI_TIMEOUT_BUFWRITE], 0,
491 "Number of times the maximum buffered write timeout was "
497 cfi_detach(device_t dev)
499 struct cfi_softc *sc;
501 sc = device_get_softc(dev);
503 destroy_dev(sc->sc_nod);
504 free(sc->sc_region, M_TEMP);
505 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rid, sc->sc_res);
510 cfi_check_erase(struct cfi_softc *sc, u_int ofs, u_int sz)
517 for (i = 0; i < sz; i += sc->sc_width) {
518 val = cfi_read(sc, ofs + i);
519 switch (sc->sc_width) {
529 if (val != 0xffffffff)
541 cfi_wait_ready(struct cfi_softc *sc, u_int ofs, sbintime_t start,
542 enum cfi_wait_cmd cmd)
544 int done, error, tto_exceeded;
545 uint32_t st0 = 0, st = 0;
551 while (!done && !error) {
553 * Save time before we start so we always do one check
554 * after the timeout has expired.
558 switch (sc->sc_cmdset) {
559 case CFI_VEND_INTEL_ECS:
560 case CFI_VEND_INTEL_SCS:
561 st = cfi_read(sc, ofs);
562 done = (st & CFI_INTEL_STATUS_WSMS);
564 /* NB: bit 0 is reserved */
565 st &= ~(CFI_INTEL_XSTATUS_RSVD |
566 CFI_INTEL_STATUS_WSMS |
567 CFI_INTEL_STATUS_RSVD);
568 if (st & CFI_INTEL_STATUS_DPS)
570 else if (st & CFI_INTEL_STATUS_PSLBS)
572 else if (st & CFI_INTEL_STATUS_ECLBS)
578 case CFI_VEND_AMD_SCS:
579 case CFI_VEND_AMD_ECS:
580 st0 = cfi_read(sc, ofs);
581 st = cfi_read(sc, ofs);
582 done = ((st & 0x40) == (st0 & 0x40)) ? 1 : 0;
587 now > start + sc->sc_typical_timeouts[cmd]) {
590 sc->sc_tto_counts[cmd]++;
591 #ifdef CFI_DEBUG_TIMEOUT
592 device_printf(sc->sc_dev,
593 "typical timeout exceeded (cmd %d)", cmd);
596 if (now > start + sc->sc_max_timeouts[cmd]) {
597 sc->sc_mto_counts[cmd]++;
598 #ifdef CFI_DEBUG_TIMEOUT
599 device_printf(sc->sc_dev,
600 "max timeout exceeded (cmd %d)", cmd);
608 printf("\nerror=%d (st 0x%x st0 0x%x)\n", error, st, st0);
613 cfi_write_block(struct cfi_softc *sc)
621 int error, i, j, neederase = 0;
628 /* Intel flash must be unlocked before modification */
629 switch (sc->sc_cmdset) {
630 case CFI_VEND_INTEL_ECS:
631 case CFI_VEND_INTEL_SCS:
632 cfi_write(sc, sc->sc_wrofs, CFI_INTEL_LBS);
633 cfi_write(sc, sc->sc_wrofs, CFI_INTEL_UB);
634 cfi_write(sc, sc->sc_wrofs, CFI_BCS_READ_ARRAY);
638 /* Check if an erase is required. */
639 for (i = 0; i < sc->sc_wrbufsz; i++)
640 if ((sc->sc_wrbuf[i] & sc->sc_wrbufcpy[i]) != sc->sc_wrbuf[i]) {
646 intr = intr_disable();
647 start = sbinuptime();
648 /* Erase the block. */
649 switch (sc->sc_cmdset) {
650 case CFI_VEND_INTEL_ECS:
651 case CFI_VEND_INTEL_SCS:
652 cfi_write(sc, sc->sc_wrofs, CFI_BCS_BLOCK_ERASE);
653 cfi_write(sc, sc->sc_wrofs, CFI_BCS_CONFIRM);
655 case CFI_VEND_AMD_SCS:
656 case CFI_VEND_AMD_ECS:
657 /* find minimum sector size */
658 minsz = sc->sc_region[0].r_blksz;
659 for (i = 1; i < sc->sc_regions; i++) {
660 if (sc->sc_region[i].r_blksz < minsz)
661 minsz = sc->sc_region[i].r_blksz;
663 cfi_amd_write(sc, sc->sc_wrofs, AMD_ADDR_START,
664 CFI_AMD_ERASE_SECTOR);
665 cfi_amd_write(sc, sc->sc_wrofs,
666 sc->sc_wrofs >> (ffs(minsz) - 1),
667 CFI_AMD_BLOCK_ERASE);
668 for (i = 0; i < CFI_AMD_MAXCHK; ++i) {
669 if (cfi_check_erase(sc, sc->sc_wrofs,
674 if (i == CFI_AMD_MAXCHK) {
675 printf("\nCFI Sector Erase time out error\n");
680 /* Better safe than sorry... */
685 error = cfi_wait_ready(sc, sc->sc_wrofs, start,
692 /* Write the block using a multibyte write if supported. */
693 ptr.x8 = sc->sc_wrbuf;
694 cpyprt.x8 = sc->sc_wrbufcpy;
695 if (sc->sc_maxbuf > sc->sc_width) {
696 switch (sc->sc_cmdset) {
697 case CFI_VEND_INTEL_ECS:
698 case CFI_VEND_INTEL_SCS:
699 for (i = 0; i < sc->sc_wrbufsz; i += wlen) {
700 wlen = MIN(sc->sc_maxbuf, sc->sc_wrbufsz - i);
702 intr = intr_disable();
704 start = sbinuptime();
706 cfi_write(sc, sc->sc_wrofs + i,
707 CFI_BCS_BUF_PROG_SETUP);
708 if (sbinuptime() > start + sc->sc_max_timeouts[CFI_TIMEOUT_BUFWRITE]) {
712 st = cfi_read(sc, sc->sc_wrofs + i);
713 } while (! (st & CFI_INTEL_STATUS_WSMS));
715 cfi_write(sc, sc->sc_wrofs + i,
716 (wlen / sc->sc_width) - 1);
717 switch (sc->sc_width) {
719 bus_space_write_region_1(sc->sc_tag,
720 sc->sc_handle, sc->sc_wrofs + i,
724 bus_space_write_region_2(sc->sc_tag,
725 sc->sc_handle, sc->sc_wrofs + i,
726 ptr.x16 + i / 2, wlen / 2);
729 bus_space_write_region_4(sc->sc_tag,
730 sc->sc_handle, sc->sc_wrofs + i,
731 ptr.x32 + i / 4, wlen / 4);
735 cfi_write(sc, sc->sc_wrofs + i,
740 error = cfi_wait_ready(sc, sc->sc_wrofs + i,
741 start, CFI_TIMEOUT_BUFWRITE);
747 /* Fall through to single word case */
752 /* Write the block one byte/word at a time. */
753 for (i = 0; i < sc->sc_wrbufsz; i += sc->sc_width) {
754 /* Avoid writing unless we are actually changing bits */
756 switch (sc->sc_width) {
758 if(*(ptr.x8 + i) == *(cpyprt.x8 + i))
762 if(*(ptr.x16 + i / 2) == *(cpyprt.x16 + i / 2))
766 if(*(ptr.x32 + i / 4) == *(cpyprt.x32 + i / 4))
773 * Make sure the command to start a write and the
774 * actual write happens back-to-back without any
777 intr = intr_disable();
779 start = sbinuptime();
780 switch (sc->sc_cmdset) {
781 case CFI_VEND_INTEL_ECS:
782 case CFI_VEND_INTEL_SCS:
783 cfi_write(sc, sc->sc_wrofs + i, CFI_BCS_PROGRAM);
785 case CFI_VEND_AMD_SCS:
786 case CFI_VEND_AMD_ECS:
787 cfi_amd_write(sc, 0, AMD_ADDR_START, CFI_AMD_PROGRAM);
790 switch (sc->sc_width) {
792 bus_space_write_1(sc->sc_tag, sc->sc_handle,
793 sc->sc_wrofs + i, *(ptr.x8 + i));
796 bus_space_write_2(sc->sc_tag, sc->sc_handle,
797 sc->sc_wrofs + i, *(ptr.x16 + i / 2));
800 bus_space_write_4(sc->sc_tag, sc->sc_handle,
801 sc->sc_wrofs + i, *(ptr.x32 + i / 4));
807 if (sc->sc_cmdset == CFI_VEND_AMD_ECS ||
808 sc->sc_cmdset == CFI_VEND_AMD_SCS) {
809 for (j = 0; j < CFI_AMD_MAXCHK; ++j) {
810 switch (sc->sc_width) {
815 val = *(ptr.x16 + i / 2);
818 val = *(ptr.x32 + i / 4);
822 if (cfi_read(sc, sc->sc_wrofs + i) == val)
827 if (j == CFI_AMD_MAXCHK) {
828 printf("\nCFI Program Verify time out error\n");
833 error = cfi_wait_ready(sc, sc->sc_wrofs, start,
843 cfi_reset_default(sc);
845 /* Relock Intel flash */
846 switch (sc->sc_cmdset) {
847 case CFI_VEND_INTEL_ECS:
848 case CFI_VEND_INTEL_SCS:
849 cfi_write(sc, sc->sc_wrofs, CFI_INTEL_LBS);
850 cfi_write(sc, sc->sc_wrofs, CFI_INTEL_LB);
851 cfi_write(sc, sc->sc_wrofs, CFI_BCS_READ_ARRAY);
857 #ifdef CFI_SUPPORT_STRATAFLASH
859 * Intel StrataFlash Protection Register Support.
861 * The memory includes a 128-bit Protection Register that can be
862 * used for security. There are two 64-bit segments; one is programmed
863 * at the factory with a unique 64-bit number which is immutable.
864 * The other segment is left blank for User (OEM) programming.
865 * The User/OEM segment is One Time Programmable (OTP). It can also
866 * be locked to prevent any further writes by setting bit 0 of the
867 * Protection Lock Register (PLR). The PLR can written only once.
871 cfi_get16(struct cfi_softc *sc, int off)
873 uint16_t v = bus_space_read_2(sc->sc_tag, sc->sc_handle, off<<1);
877 #ifdef CFI_ARMEDANDDANGEROUS
879 cfi_put16(struct cfi_softc *sc, int off, uint16_t v)
881 bus_space_write_2(sc->sc_tag, sc->sc_handle, off<<1, v);
886 * Read the factory-defined 64-bit segment of the PR.
889 cfi_intel_get_factory_pr(struct cfi_softc *sc, uint64_t *id)
891 if (sc->sc_cmdset != CFI_VEND_INTEL_ECS)
893 KASSERT(sc->sc_width == 2, ("sc_width %d", sc->sc_width));
895 cfi_write(sc, 0, CFI_INTEL_READ_ID);
896 *id = ((uint64_t)cfi_get16(sc, CFI_INTEL_PR(0)))<<48 |
897 ((uint64_t)cfi_get16(sc, CFI_INTEL_PR(1)))<<32 |
898 ((uint64_t)cfi_get16(sc, CFI_INTEL_PR(2)))<<16 |
899 ((uint64_t)cfi_get16(sc, CFI_INTEL_PR(3)));
900 cfi_write(sc, 0, CFI_BCS_READ_ARRAY);
905 * Read the User/OEM 64-bit segment of the PR.
908 cfi_intel_get_oem_pr(struct cfi_softc *sc, uint64_t *id)
910 if (sc->sc_cmdset != CFI_VEND_INTEL_ECS)
912 KASSERT(sc->sc_width == 2, ("sc_width %d", sc->sc_width));
914 cfi_write(sc, 0, CFI_INTEL_READ_ID);
915 *id = ((uint64_t)cfi_get16(sc, CFI_INTEL_PR(4)))<<48 |
916 ((uint64_t)cfi_get16(sc, CFI_INTEL_PR(5)))<<32 |
917 ((uint64_t)cfi_get16(sc, CFI_INTEL_PR(6)))<<16 |
918 ((uint64_t)cfi_get16(sc, CFI_INTEL_PR(7)));
919 cfi_write(sc, 0, CFI_BCS_READ_ARRAY);
924 * Write the User/OEM 64-bit segment of the PR.
925 * XXX should allow writing individual words/bytes
928 cfi_intel_set_oem_pr(struct cfi_softc *sc, uint64_t id)
930 #ifdef CFI_ARMEDANDDANGEROUS
936 if (sc->sc_cmdset != CFI_VEND_INTEL_ECS)
938 KASSERT(sc->sc_width == 2, ("sc_width %d", sc->sc_width));
940 #ifdef CFI_ARMEDANDDANGEROUS
941 for (i = 7; i >= 4; i--, id >>= 16) {
942 intr = intr_disable();
943 start = sbinuptime();
944 cfi_write(sc, 0, CFI_INTEL_PP_SETUP);
945 cfi_put16(sc, CFI_INTEL_PR(i), id&0xffff);
947 error = cfi_wait_ready(sc, CFI_BCS_READ_STATUS, start,
952 cfi_write(sc, 0, CFI_BCS_READ_ARRAY);
955 device_printf(sc->sc_dev, "%s: OEM PR not set, "
956 "CFI_ARMEDANDDANGEROUS not configured\n", __func__);
962 * Read the contents of the Protection Lock Register.
965 cfi_intel_get_plr(struct cfi_softc *sc, uint32_t *plr)
967 if (sc->sc_cmdset != CFI_VEND_INTEL_ECS)
969 KASSERT(sc->sc_width == 2, ("sc_width %d", sc->sc_width));
971 cfi_write(sc, 0, CFI_INTEL_READ_ID);
972 *plr = cfi_get16(sc, CFI_INTEL_PLR);
973 cfi_write(sc, 0, CFI_BCS_READ_ARRAY);
978 * Write the Protection Lock Register to lock down the
979 * user-settable segment of the Protection Register.
980 * NOTE: this operation is not reversible.
983 cfi_intel_set_plr(struct cfi_softc *sc)
985 #ifdef CFI_ARMEDANDDANGEROUS
990 if (sc->sc_cmdset != CFI_VEND_INTEL_ECS)
992 KASSERT(sc->sc_width == 2, ("sc_width %d", sc->sc_width));
994 #ifdef CFI_ARMEDANDDANGEROUS
995 /* worthy of console msg */
996 device_printf(sc->sc_dev, "set PLR\n");
997 intr = intr_disable();
999 cfi_write(sc, 0, CFI_INTEL_PP_SETUP);
1000 cfi_put16(sc, CFI_INTEL_PLR, 0xFFFD);
1002 error = cfi_wait_ready(sc, CFI_BCS_READ_STATUS, start,
1004 cfi_write(sc, 0, CFI_BCS_READ_ARRAY);
1007 device_printf(sc->sc_dev, "%s: PLR not set, "
1008 "CFI_ARMEDANDDANGEROUS not configured\n", __func__);
1012 #endif /* CFI_SUPPORT_STRATAFLASH */