1 /* $NetBSD: nandemulator.c,v 1.9 2019/10/01 18:00:08 chs Exp $ */ 2 3 /*- 4 * Copyright (c) 2011 Department of Software Engineering, 5 * University of Szeged, Hungary 6 * Copyright (c) 2011 Adam Hoka <ahoka@NetBSD.org> 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by the Department of Software Engineering, University of Szeged, Hungary 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: nandemulator.c,v 1.9 2019/10/01 18:00:08 chs Exp $"); 36 37 /* XXX this code likely needs work */ 38 39 #include <sys/param.h> 40 #include <sys/device.h> 41 #include <sys/conf.h> 42 #include <sys/kmem.h> 43 #include <sys/kernel.h> 44 45 #include "nandemulator.h" 46 47 #include <dev/nand/nand.h> 48 #include <dev/nand/onfi.h> 49 #include <dev/nand/nand_crc.h> 50 51 #include "ioconf.h" 52 53 extern struct cfdriver nandemulator_cd; 54 55 static int nandemulator_match(device_t, cfdata_t, void *); 56 static void nandemulator_attach(device_t, device_t, void *); 57 static int nandemulator_detach(device_t, int); 58 59 static void nandemulator_device_reset(device_t); 60 static void nandemulator_command(device_t, uint8_t); 61 static void nandemulator_address(device_t, uint8_t); 62 static void nandemulator_busy(device_t); 63 static void nandemulator_read_1(device_t, uint8_t *); 64 static void nandemulator_write_1(device_t, uint8_t); 65 static void nandemulator_read_2(device_t, uint16_t *); 66 static void nandemulator_write_2(device_t, uint16_t); 67 static void nandemulator_read_buf_1(device_t, void *, size_t); 68 static void nandemulator_read_buf_2(device_t, void *, size_t); 69 static void nandemulator_write_buf_1(device_t, const void *, size_t); 70 static void nandemulator_write_buf_2(device_t, const void *, size_t); 71 72 static size_t nandemulator_address_to_page(device_t); 73 static size_t nandemulator_page_to_backend_offset(device_t, size_t); 74 static size_t nandemulator_column_address_to_subpage(device_t); 75 /* 76 #define NANDEMULATOR_DEBUG 1 77 78 #ifdef NANDEMULATOR_DEBUG 79 #warning debug enabled 80 #define DPRINTF(x) if (nandemulatordebug) printf x 81 #define DPRINTFN(n,x) if (nandemulatordebug>(n)) printf x 82 #else 83 #error no debug 84 #define DPRINTF(x) 85 #define DPRINTFN(n,x) 86 #endif 87 88 #ifdef NANDEMULATOR_DEBUG 89 int nandemulatordebug = NANDEMULATOR_DEBUG; 90 #endif 91 */ 92 93 extern int nanddebug; 94 95 enum { 96 NANDEMULATOR_8BIT, 97 NANDEMULATOR_16BIT 98 }; 99 100 struct nandemulator_softc { 101 device_t sc_dev; 102 device_t sc_nanddev; 103 104 int sc_buswidth; 105 106 struct nand_interface sc_nand_if; 107 108 uint8_t sc_command; 109 size_t sc_io_len; 110 uint8_t *sc_io_pointer; 111 uint64_t sc_address; 112 113 uint8_t *sc_backend; 114 size_t sc_backend_size; 115 size_t sc_device_size; 116 bool sc_register_writable; 117 118 uint8_t sc_status_register; 119 uint8_t sc_ids[2]; 120 uint8_t sc_onfi[4]; 121 122 size_t sc_page_size; 123 size_t sc_block_size; 124 size_t sc_spare_size; 125 size_t sc_lun_size; 126 uint8_t sc_row_cycles; 127 uint8_t sc_column_cycles; 128 uint64_t sc_row_mask; 129 130 int sc_address_counter; 131 132 struct onfi_parameter_page *sc_parameter_page; 133 }; 134 135 CFATTACH_DECL_NEW(nandemulator, sizeof(struct nandemulator_softc), 136 nandemulator_match, nandemulator_attach, nandemulator_detach, NULL); 137 138 void 139 nandemulatorattach(int n) 140 { 141 int i, err; 142 cfdata_t cf; 143 144 aprint_debug("nandemulator: requested %d units\n", n); 145 146 err = config_cfattach_attach(nandemulator_cd.cd_name, 147 &nandemulator_ca); 148 if (err) { 149 aprint_error("%s: couldn't register cfattach: %d\n", 150 nandemulator_cd.cd_name, err); 151 config_cfdriver_detach(&nandemulator_cd); 152 return; 153 } 154 for (i = 0; i < n; i++) { 155 cf = kmem_alloc(sizeof(struct cfdata), KM_SLEEP); 156 cf->cf_name = nandemulator_cd.cd_name; 157 cf->cf_atname = nandemulator_cd.cd_name; 158 cf->cf_unit = i; 159 cf->cf_fstate = FSTATE_STAR; 160 161 (void)config_attach_pseudo(cf); 162 } 163 } 164 165 /* ARGSUSED */ 166 static int 167 nandemulator_match(device_t parent, cfdata_t match, void *aux) 168 { 169 /* pseudo device, always attaches */ 170 return 1; 171 } 172 173 static void 174 nandemulator_attach(device_t parent, device_t self, void *aux) 175 { 176 struct nandemulator_softc *sc = device_private(self); 177 int i; 178 179 aprint_normal_dev(self, "NAND emulator\n"); 180 181 sc->sc_dev = self; 182 183 nand_init_interface(&sc->sc_nand_if); 184 185 sc->sc_nand_if.command = &nandemulator_command; 186 sc->sc_nand_if.address = &nandemulator_address; 187 sc->sc_nand_if.read_buf_1 = &nandemulator_read_buf_1; 188 sc->sc_nand_if.read_buf_2 = &nandemulator_read_buf_2; 189 sc->sc_nand_if.read_1 = &nandemulator_read_1; 190 sc->sc_nand_if.read_2 = &nandemulator_read_2; 191 sc->sc_nand_if.write_buf_1 = &nandemulator_write_buf_1; 192 sc->sc_nand_if.write_buf_2 = &nandemulator_write_buf_2; 193 sc->sc_nand_if.write_1 = &nandemulator_write_1; 194 sc->sc_nand_if.write_2 = &nandemulator_write_2; 195 sc->sc_nand_if.busy = &nandemulator_busy; 196 197 sc->sc_nand_if.ecc.necc_code_size = 3; 198 sc->sc_nand_if.ecc.necc_block_size = 256; 199 200 if (!pmf_device_register1(sc->sc_dev, NULL, NULL, NULL)) 201 aprint_error_dev(sc->sc_dev, 202 "couldn't establish power handler\n"); 203 204 sc->sc_buswidth = NANDEMULATOR_16BIT; /* 16bit for now */ 205 206 /* hardcode these now, make it configurable later */ 207 sc->sc_device_size = 32 * 1024 * 1024; /* 32MB */ 208 sc->sc_page_size = 2048; 209 sc->sc_block_size = 64; 210 sc->sc_lun_size = 211 sc->sc_device_size / (sc->sc_page_size * sc->sc_block_size); 212 KASSERT(sc->sc_device_size % 213 (sc->sc_page_size * sc->sc_block_size) == 0); 214 sc->sc_spare_size = 64; 215 216 sc->sc_column_cycles = 2; 217 sc->sc_row_cycles = 3; 218 219 /* init the emulator data structures */ 220 sc->sc_backend_size = 221 sc->sc_device_size + 222 sc->sc_device_size / sc->sc_page_size * sc->sc_spare_size; 223 224 sc->sc_backend = kmem_alloc(sc->sc_backend_size, KM_SLEEP); 225 memset(sc->sc_backend, 0xff, sc->sc_backend_size); 226 227 sc->sc_parameter_page = 228 kmem_zalloc(sizeof(struct onfi_parameter_page) * 4, KM_SLEEP); 229 230 struct onfi_parameter_page *opp; 231 uint8_t sig[4] = { 'O', 'N', 'F', 'I' }; 232 233 for (i = 0; i < 4; i++) { 234 opp = &sc->sc_parameter_page[i]; 235 236 opp->param_signature = htole32(*(uint32_t *)sig); 237 opp->param_pagesize = htole32(sc->sc_page_size); 238 opp->param_blocksize = htole32(sc->sc_block_size); 239 opp->param_sparesize = htole16(sc->sc_spare_size); 240 opp->param_lunsize = htole32(sc->sc_lun_size); 241 opp->param_numluns = 1; 242 243 opp->param_manufacturer_id = 0x00; 244 memcpy(opp->param_manufacturer, 245 "NETBSD", strlen("NETBSD")); 246 memcpy(opp->param_model, 247 "NANDEMULATOR", strlen("NANDEMULATOR")); 248 249 uint16_t features = ONFI_FEATURE_16BIT; 250 opp->param_features = htole16(features); 251 252 /* the lower 4 bits contain the row address cycles 253 * the upper 4 bits contain the column address cycles 254 */ 255 opp->param_addr_cycles = sc->sc_row_cycles; 256 opp->param_addr_cycles |= (sc->sc_column_cycles << 4); 257 258 opp->param_integrity_crc = nand_crc16((uint8_t *)opp, 254); 259 } 260 261 sc->sc_ids[0] = 0x00; 262 sc->sc_ids[1] = 0x00; 263 264 sc->sc_onfi[0] = 'O'; 265 sc->sc_onfi[1] = 'N'; 266 sc->sc_onfi[2] = 'F'; 267 sc->sc_onfi[3] = 'I'; 268 269 sc->sc_row_mask = 0x00; 270 for (i = 0; i < sc->sc_row_cycles; i++) { 271 sc->sc_row_mask <<= 8; 272 sc->sc_row_mask |= 0xff; 273 } 274 275 nandemulator_device_reset(self); 276 277 sc->sc_nanddev = nand_attach_mi(&sc->sc_nand_if, sc->sc_dev); 278 } 279 280 static int 281 nandemulator_detach(device_t self, int flags) 282 { 283 struct nandemulator_softc *sc = device_private(self); 284 int ret = 0; 285 286 aprint_normal_dev(sc->sc_dev, "detaching emulator\n"); 287 288 pmf_device_deregister(sc->sc_dev); 289 290 if (sc->sc_nanddev != NULL) 291 ret = config_detach(sc->sc_nanddev, flags); 292 293 kmem_free(sc->sc_backend, sc->sc_backend_size); 294 kmem_free(sc->sc_parameter_page, 295 sizeof(struct onfi_parameter_page) * 4); 296 297 return ret; 298 } 299 300 /** 301 * bring the emulated device to a known state 302 */ 303 static void 304 nandemulator_device_reset(device_t self) 305 { 306 struct nandemulator_softc *sc = device_private(self); 307 308 DPRINTF(("device reset\n")); 309 310 sc->sc_command = 0; 311 sc->sc_register_writable = false; 312 sc->sc_io_len = 0; 313 sc->sc_io_pointer = NULL; 314 sc->sc_address = 0; 315 sc->sc_address_counter = 0; 316 317 sc->sc_status_register = ONFI_STATUS_RDY | ONFI_STATUS_WP; 318 } 319 320 static void 321 nandemulator_address_chip(device_t self) 322 { 323 struct nandemulator_softc *sc = device_private(self); 324 size_t page, offset; 325 326 KASSERT(sc->sc_address_counter == 327 sc->sc_column_cycles + sc->sc_row_cycles); 328 329 if (sc->sc_address_counter != 330 sc->sc_column_cycles + sc->sc_row_cycles) { 331 aprint_error_dev(self, "incorrect number of address cycles\n"); 332 aprint_error_dev(self, "cc: %d, rc: %d, ac: %d\n", 333 sc->sc_column_cycles, sc->sc_row_cycles, 334 sc->sc_address_counter); 335 } 336 337 page = nandemulator_address_to_page(self); 338 offset = sc->sc_page_size * page; 339 340 DPRINTF(("READ/PROGRAM; page: 0x%jx (row addr: 0x%jx)\n", 341 (uintmax_t )page, 342 (uintmax_t )offset)); 343 344 KASSERT(offset < sc->sc_device_size); 345 346 if (offset >= sc->sc_device_size) { 347 aprint_error_dev(self, "address > device size!\n"); 348 sc->sc_io_len = 0; 349 } else { 350 size_t addr = 351 nandemulator_page_to_backend_offset(self, page); 352 size_t pageoff = 353 nandemulator_column_address_to_subpage(self); 354 355 DPRINTF(("subpage: 0x%jx\n", (uintmax_t )pageoff)); 356 357 KASSERT(pageoff < 358 sc->sc_page_size + sc->sc_spare_size); 359 KASSERT(addr < sc->sc_backend_size); 360 361 sc->sc_io_pointer = sc->sc_backend + addr + pageoff; 362 sc->sc_io_len = 363 sc->sc_page_size + sc->sc_spare_size - pageoff; 364 } 365 } 366 367 static void 368 nandemulator_command(device_t self, uint8_t command) 369 { 370 struct nandemulator_softc *sc = device_private(self); 371 size_t offset, page; 372 373 sc->sc_command = command; 374 sc->sc_register_writable = false; 375 376 DPRINTF(("nandemulator command: 0x%hhx\n", command)); 377 378 switch (command) { 379 case ONFI_READ_STATUS: 380 sc->sc_io_pointer = &sc->sc_status_register; 381 sc->sc_io_len = 1; 382 break; 383 case ONFI_RESET: 384 nandemulator_device_reset(self); 385 break; 386 case ONFI_PAGE_PROGRAM: 387 sc->sc_register_writable = true; 388 /* FALLTHROUGH */ 389 case ONFI_READ: 390 case ONFI_BLOCK_ERASE: 391 sc->sc_address_counter = 0; 392 /* FALLTHROUGH */ 393 case ONFI_READ_ID: 394 case ONFI_READ_PARAMETER_PAGE: 395 sc->sc_io_len = 0; 396 sc->sc_address = 0; 397 break; 398 case ONFI_PAGE_PROGRAM_START: 399 /* XXX the program should only happen here */ 400 break; 401 case ONFI_READ_START: 402 nandemulator_address_chip(self); 403 break; 404 case ONFI_BLOCK_ERASE_START: 405 page = nandemulator_address_to_page(self); 406 offset = sc->sc_page_size * page; 407 408 KASSERT(offset % 409 (sc->sc_block_size * sc->sc_page_size) == 0); 410 411 KASSERT(offset < sc->sc_device_size); 412 413 if (offset >= sc->sc_device_size) { 414 aprint_error_dev(self, "address > device size!\n"); 415 } else { 416 size_t addr = 417 nandemulator_page_to_backend_offset(self, page); 418 419 size_t blocklen = 420 sc->sc_block_size * 421 (sc->sc_page_size + sc->sc_spare_size); 422 423 KASSERT(addr < sc->sc_backend_size); 424 uint8_t *block = sc->sc_backend + addr; 425 426 DPRINTF(("erasing block at 0x%jx\n", 427 (uintmax_t )offset)); 428 429 memset(block, 0xff, blocklen); 430 } 431 sc->sc_io_len = 0; 432 break; 433 default: 434 aprint_error_dev(self, 435 "invalid nand command (0x%hhx)\n", command); 436 KASSERT(false); 437 sc->sc_io_len = 0; 438 } 439 }; 440 441 static void 442 nandemulator_address(device_t self, uint8_t address) 443 { 444 struct nandemulator_softc *sc = device_private(self); 445 446 DPRINTF(("nandemulator_address: %hhx\n", address)); 447 448 /** 449 * we have to handle read id/parameter page here, 450 * as we can read right after giving the address. 451 */ 452 switch (sc->sc_command) { 453 case ONFI_READ_ID: 454 if (address == 0x00) { 455 sc->sc_io_len = 2; 456 sc->sc_io_pointer = sc->sc_ids; 457 } else if (address == 0x20) { 458 sc->sc_io_len = 4; 459 sc->sc_io_pointer = sc->sc_onfi; 460 } else { 461 sc->sc_io_len = 0; 462 } 463 break; 464 case ONFI_READ_PARAMETER_PAGE: 465 if (address == 0x00) { 466 sc->sc_io_len = sizeof(struct onfi_parameter_page) * 4; 467 sc->sc_io_pointer = (uint8_t *)sc->sc_parameter_page; 468 } else { 469 sc->sc_io_len = 0; 470 } 471 break; 472 case ONFI_PAGE_PROGRAM: 473 sc->sc_address <<= 8; 474 sc->sc_address |= address; 475 sc->sc_address_counter++; 476 477 if (sc->sc_address_counter == 478 sc->sc_column_cycles + sc->sc_row_cycles) { 479 nandemulator_address_chip(self); 480 } 481 break; 482 default: 483 sc->sc_address <<= 8; 484 sc->sc_address |= address; 485 sc->sc_address_counter++; 486 } 487 }; 488 489 static void 490 nandemulator_busy(device_t self) 491 { 492 #ifdef NANDEMULATOR_DELAYS 493 struct nandemulator_softc *sc = device_private(self); 494 495 /* do some delay depending on command */ 496 switch (sc->sc_command) { 497 case ONFI_PAGE_PROGRAM_START: 498 case ONFI_BLOCK_ERASE_START: 499 DELAY(10); 500 break; 501 case ONFI_READ_START: 502 default: 503 DELAY(1); 504 } 505 #endif 506 } 507 508 static void 509 nandemulator_read_1(device_t self, uint8_t *data) 510 { 511 struct nandemulator_softc *sc = device_private(self); 512 513 KASSERT(sc->sc_io_len > 0); 514 515 if (sc->sc_io_len > 0) { 516 *data = *sc->sc_io_pointer; 517 518 sc->sc_io_pointer++; 519 sc->sc_io_len--; 520 } else { 521 aprint_error_dev(self, "reading byte from invalid location\n"); 522 *data = 0xff; 523 } 524 } 525 526 static void 527 nandemulator_write_1(device_t self, uint8_t data) 528 { 529 struct nandemulator_softc *sc = device_private(self); 530 531 KASSERT(sc->sc_register_writable); 532 533 if (!sc->sc_register_writable) { 534 aprint_error_dev(self, 535 "trying to write read only location without effect\n"); 536 return; 537 } 538 539 KASSERT(sc->sc_io_len > 0); 540 541 if (sc->sc_io_len > 0) { 542 *sc->sc_io_pointer = data; 543 544 sc->sc_io_pointer++; 545 sc->sc_io_len--; 546 } else { 547 aprint_error_dev(self, "write to invalid location\n"); 548 } 549 } 550 551 static void 552 nandemulator_read_2(device_t self, uint16_t *data) 553 { 554 struct nandemulator_softc *sc = device_private(self); 555 556 KASSERT(sc->sc_buswidth == NANDEMULATOR_16BIT); 557 558 if (sc->sc_buswidth != NANDEMULATOR_16BIT) { 559 aprint_error_dev(self, 560 "trying to read a word on an 8bit chip\n"); 561 return; 562 } 563 564 KASSERT(sc->sc_io_len > 1); 565 566 if (sc->sc_io_len > 1) { 567 *data = *(uint16_t *)sc->sc_io_pointer; 568 569 sc->sc_io_pointer += 2; 570 sc->sc_io_len -= 2; 571 } else { 572 aprint_error_dev(self, "reading word from invalid location\n"); 573 *data = 0xffff; 574 } 575 } 576 577 static void 578 nandemulator_write_2(device_t self, uint16_t data) 579 { 580 struct nandemulator_softc *sc = device_private(self); 581 582 KASSERT(sc->sc_register_writable); 583 584 if (!sc->sc_register_writable) { 585 aprint_error_dev(self, 586 "trying to write read only location without effect\n"); 587 return; 588 } 589 590 KASSERT(sc->sc_buswidth == NANDEMULATOR_16BIT); 591 592 if (sc->sc_buswidth != NANDEMULATOR_16BIT) { 593 aprint_error_dev(self, 594 "trying to write a word to an 8bit chip"); 595 return; 596 } 597 598 KASSERT(sc->sc_io_len > 1); 599 600 if (sc->sc_io_len > 1) { 601 *(uint16_t *)sc->sc_io_pointer = data; 602 603 sc->sc_io_pointer += 2; 604 sc->sc_io_len -= 2; 605 } else { 606 aprint_error_dev(self, "writing to invalid location"); 607 } 608 } 609 610 static void 611 nandemulator_read_buf_1(device_t self, void *buf, size_t len) 612 { 613 uint8_t *addr; 614 615 KASSERT(buf != NULL); 616 KASSERT(len >= 1); 617 618 addr = buf; 619 while (len > 0) { 620 nandemulator_read_1(self, addr); 621 addr++, len--; 622 } 623 } 624 625 static void 626 nandemulator_read_buf_2(device_t self, void *buf, size_t len) 627 { 628 uint16_t *addr; 629 630 KASSERT(buf != NULL); 631 KASSERT(len >= 2); 632 KASSERT(!(len & 0x01)); 633 634 addr = buf; 635 len /= 2; 636 while (len > 0) { 637 nandemulator_read_2(self, addr); 638 addr++, len--; 639 } 640 } 641 642 static void 643 nandemulator_write_buf_1(device_t self, const void *buf, size_t len) 644 { 645 const uint8_t *addr; 646 647 KASSERT(buf != NULL); 648 KASSERT(len >= 1); 649 650 addr = buf; 651 while (len > 0) { 652 nandemulator_write_1(self, *addr); 653 addr++, len--; 654 } 655 } 656 657 static void 658 nandemulator_write_buf_2(device_t self, const void *buf, size_t len) 659 { 660 const uint16_t *addr; 661 662 KASSERT(buf != NULL); 663 KASSERT(len >= 2); 664 KASSERT(!(len & 0x01)); 665 666 addr = buf; 667 len /= 2; 668 while (len > 0) { 669 nandemulator_write_2(self, *addr); 670 addr++, len--; 671 } 672 } 673 674 static size_t 675 nandemulator_address_to_page(device_t self) 676 { 677 struct nandemulator_softc *sc = device_private(self); 678 uint64_t address, offset; 679 int i; 680 681 address = htole64(sc->sc_address); 682 address &= sc->sc_row_mask; 683 684 offset = 0; 685 for (i = 0; i < sc->sc_row_cycles; i++) { 686 offset <<= 8; 687 offset |= (address & 0xff); 688 address >>= 8; 689 } 690 691 return le64toh(offset); 692 } 693 694 static size_t 695 nandemulator_column_address_to_subpage(device_t self) 696 { 697 struct nandemulator_softc *sc = device_private(self); 698 uint64_t address, offset; 699 int i; 700 701 address = htole64(sc->sc_address); 702 address >>= (8 * sc->sc_row_cycles); 703 704 offset = 0; 705 for (i = 0; i < sc->sc_column_cycles; i++) { 706 offset <<= 8; 707 offset |= (address & 0xff); 708 address >>= 8; 709 } 710 711 if (sc->sc_buswidth == NANDEMULATOR_16BIT) 712 return (size_t )le64toh(offset << 1); 713 else 714 return (size_t )le64toh(offset); 715 } 716 717 static size_t 718 nandemulator_page_to_backend_offset(device_t self, size_t page) 719 { 720 struct nandemulator_softc *sc = device_private(self); 721 722 return (sc->sc_page_size + sc->sc_spare_size) * page; 723 } 724 725 #ifdef _MODULE 726 727 MODULE(MODULE_CLASS_DRIVER, nandemulator, "nand"); 728 729 static const struct cfiattrdata nandbuscf_iattrdata = { 730 "nandbus", 0, { { NULL, NULL, 0 }, } 731 }; 732 static const struct cfiattrdata * const nandemulator_attrs[] = { 733 &nandbuscf_iattrdata, NULL 734 }; 735 736 CFDRIVER_DECL(nandemulator, DV_DULL, nandemulator_attrs); 737 extern struct cfattach nandemulator_ca; 738 static int nandemulatorloc[] = { -1, -1 }; 739 740 static struct cfdata nandemulator_cfdata[] = { 741 { 742 .cf_name = "nandemulator", 743 .cf_atname = "nandemulator", 744 .cf_unit = 0, 745 .cf_fstate = FSTATE_STAR, 746 .cf_loc = nandemulatorloc, 747 .cf_flags = 0, 748 .cf_pspec = NULL, 749 }, 750 { NULL, NULL, 0, 0, NULL, 0, NULL } 751 }; 752 753 static int 754 nandemulator_modcmd(modcmd_t cmd, void *arg) 755 { 756 int error; 757 758 switch (cmd) { 759 case MODULE_CMD_INIT: 760 error = config_cfdriver_attach(&nandemulator_cd); 761 if (error) { 762 return error; 763 } 764 765 error = config_cfattach_attach(nandemulator_cd.cd_name, 766 &nandemulator_ca); 767 if (error) { 768 config_cfdriver_detach(&nandemulator_cd); 769 aprint_error("%s: unable to register cfattach\n", 770 nandemulator_cd.cd_name); 771 772 return error; 773 } 774 775 error = config_cfdata_attach(nandemulator_cfdata, 1); 776 if (error) { 777 config_cfattach_detach(nandemulator_cd.cd_name, 778 &nandemulator_ca); 779 config_cfdriver_detach(&nandemulator_cd); 780 aprint_error("%s: unable to register cfdata\n", 781 nandemulator_cd.cd_name); 782 783 return error; 784 } 785 786 (void)config_attach_pseudo(nandemulator_cfdata); 787 788 return 0; 789 790 case MODULE_CMD_FINI: 791 error = config_cfdata_detach(nandemulator_cfdata); 792 if (error) { 793 return error; 794 } 795 796 config_cfattach_detach(nandemulator_cd.cd_name, 797 &nandemulator_ca); 798 config_cfdriver_detach(&nandemulator_cd); 799 800 return 0; 801 802 case MODULE_CMD_AUTOUNLOAD: 803 /* prevent auto-unload */ 804 return EBUSY; 805 806 default: 807 return ENOTTY; 808 } 809 } 810 811 #endif 812