1 /* $NetBSD: nandemulator.c,v 1.6 2012/11/03 12:45:28 ahoka 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.6 2012/11/03 12:45:28 ahoka Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/device.h> 39 #include <sys/conf.h> 40 #include <sys/kmem.h> 41 #include <sys/kernel.h> 42 43 #include "nandemulator.h" 44 45 #include <dev/nand/nand.h> 46 #include <dev/nand/onfi.h> 47 #include <dev/nand/nand_crc.h> 48 49 extern struct cfdriver nandemulator_cd; 50 void nandemulatorattach(int n); 51 52 static int nandemulator_match(device_t, cfdata_t, void *); 53 static void nandemulator_attach(device_t, device_t, void *); 54 static int nandemulator_detach(device_t, int); 55 56 static void nandemulator_device_reset(device_t); 57 static void nandemulator_command(device_t, uint8_t); 58 static void nandemulator_address(device_t, uint8_t); 59 static void nandemulator_busy(device_t); 60 static void nandemulator_read_1(device_t, uint8_t *); 61 static void nandemulator_write_1(device_t, uint8_t); 62 static void nandemulator_read_2(device_t, uint16_t *); 63 static void nandemulator_write_2(device_t, uint16_t); 64 static void nandemulator_read_buf_1(device_t, void *, size_t); 65 static void nandemulator_read_buf_2(device_t, void *, size_t); 66 static void nandemulator_write_buf_1(device_t, const void *, size_t); 67 static void nandemulator_write_buf_2(device_t, const void *, size_t); 68 69 static size_t nandemulator_address_to_page(device_t); 70 static size_t nandemulator_page_to_backend_offset(device_t, size_t); 71 static size_t nandemulator_column_address_to_subpage(device_t); 72 /* 73 #define NANDEMULATOR_DEBUG 1 74 75 #ifdef NANDEMULATOR_DEBUG 76 #warning debug enabled 77 #define DPRINTF(x) if (nandemulatordebug) printf x 78 #define DPRINTFN(n,x) if (nandemulatordebug>(n)) printf x 79 #else 80 #error no debug 81 #define DPRINTF(x) 82 #define DPRINTFN(n,x) 83 #endif 84 85 #ifdef NANDEMULATOR_DEBUG 86 int nandemulatordebug = NANDEMULATOR_DEBUG; 87 #endif 88 */ 89 90 extern int nanddebug; 91 92 enum { 93 NANDEMULATOR_8BIT, 94 NANDEMULATOR_16BIT 95 }; 96 97 struct nandemulator_softc { 98 device_t sc_dev; 99 device_t sc_nanddev; 100 101 int sc_buswidth; 102 103 struct nand_interface sc_nand_if; 104 105 uint8_t sc_command; 106 size_t sc_io_len; 107 uint8_t *sc_io_pointer; 108 uint64_t sc_address; 109 110 uint8_t *sc_backend; 111 size_t sc_backend_size; 112 size_t sc_device_size; 113 bool sc_register_writable; 114 115 uint8_t sc_status_register; 116 uint8_t sc_ids[2]; 117 uint8_t sc_onfi[4]; 118 119 size_t sc_page_size; 120 size_t sc_block_size; 121 size_t sc_spare_size; 122 size_t sc_lun_size; 123 uint8_t sc_row_cycles; 124 uint8_t sc_column_cycles; 125 uint64_t sc_row_mask; 126 127 int sc_address_counter; 128 129 struct onfi_parameter_page *sc_parameter_page; 130 }; 131 132 CFATTACH_DECL_NEW(nandemulator, sizeof(struct nandemulator_softc), 133 nandemulator_match, nandemulator_attach, nandemulator_detach, NULL); 134 135 void 136 nandemulatorattach(int n) 137 { 138 int i, err; 139 cfdata_t cf; 140 141 aprint_debug("nandemulator: requested %d units\n", n); 142 143 err = config_cfattach_attach(nandemulator_cd.cd_name, 144 &nandemulator_ca); 145 if (err) { 146 aprint_error("%s: couldn't register cfattach: %d\n", 147 nandemulator_cd.cd_name, err); 148 config_cfdriver_detach(&nandemulator_cd); 149 return; 150 } 151 for (i = 0; i < n; i++) { 152 cf = kmem_alloc(sizeof(struct cfdata), KM_NOSLEEP); 153 if (cf == NULL) { 154 aprint_error("%s: couldn't allocate cfdata\n", 155 nandemulator_cd.cd_name); 156 continue; 157 } 158 cf->cf_name = nandemulator_cd.cd_name; 159 cf->cf_atname = nandemulator_cd.cd_name; 160 cf->cf_unit = i; 161 cf->cf_fstate = FSTATE_STAR; 162 163 (void)config_attach_pseudo(cf); 164 } 165 } 166 167 /* ARGSUSED */ 168 static int 169 nandemulator_match(device_t parent, cfdata_t match, void *aux) 170 { 171 /* pseudo device, always attaches */ 172 return 1; 173 } 174 175 static void 176 nandemulator_attach(device_t parent, device_t self, void *aux) 177 { 178 struct nandemulator_softc *sc = device_private(self); 179 int i; 180 181 aprint_normal_dev(self, "NAND emulator\n"); 182 183 sc->sc_dev = self; 184 185 nand_init_interface(&sc->sc_nand_if); 186 187 sc->sc_nand_if.command = &nandemulator_command; 188 sc->sc_nand_if.address = &nandemulator_address; 189 sc->sc_nand_if.read_buf_1 = &nandemulator_read_buf_1; 190 sc->sc_nand_if.read_buf_2 = &nandemulator_read_buf_2; 191 sc->sc_nand_if.read_1 = &nandemulator_read_1; 192 sc->sc_nand_if.read_2 = &nandemulator_read_2; 193 sc->sc_nand_if.write_buf_1 = &nandemulator_write_buf_1; 194 sc->sc_nand_if.write_buf_2 = &nandemulator_write_buf_2; 195 sc->sc_nand_if.write_1 = &nandemulator_write_1; 196 sc->sc_nand_if.write_2 = &nandemulator_write_2; 197 sc->sc_nand_if.busy = &nandemulator_busy; 198 199 sc->sc_nand_if.ecc.necc_code_size = 3; 200 sc->sc_nand_if.ecc.necc_block_size = 256; 201 202 if (!pmf_device_register1(sc->sc_dev, NULL, NULL, NULL)) 203 aprint_error_dev(sc->sc_dev, 204 "couldn't establish power handler\n"); 205 206 sc->sc_buswidth = NANDEMULATOR_16BIT; /* 16bit for now */ 207 208 /* hardcode these now, make it configurable later */ 209 sc->sc_device_size = 32 * 1024 * 1024; /* 32MB */ 210 sc->sc_page_size = 2048; 211 sc->sc_block_size = 64; 212 sc->sc_lun_size = 213 sc->sc_device_size / (sc->sc_page_size * sc->sc_block_size); 214 KASSERT(sc->sc_device_size % 215 (sc->sc_page_size * sc->sc_block_size) == 0); 216 sc->sc_spare_size = 64; 217 218 sc->sc_column_cycles = 2; 219 sc->sc_row_cycles = 3; 220 221 /* init the emulator data structures */ 222 sc->sc_backend_size = 223 sc->sc_device_size + 224 sc->sc_device_size / sc->sc_page_size * sc->sc_spare_size; 225 226 sc->sc_backend = kmem_alloc(sc->sc_backend_size, KM_SLEEP); 227 memset(sc->sc_backend, 0xff, sc->sc_backend_size); 228 229 sc->sc_parameter_page = 230 kmem_zalloc(sizeof(struct onfi_parameter_page) * 4, KM_SLEEP); 231 232 struct onfi_parameter_page *opp; 233 uint8_t sig[4] = { 'O', 'N', 'F', 'I' }; 234 235 for (i = 0; i < 4; i++) { 236 opp = &sc->sc_parameter_page[i]; 237 238 opp->param_signature = htole32(*(uint32_t *)sig); 239 opp->param_pagesize = htole32(sc->sc_page_size); 240 opp->param_blocksize = htole32(sc->sc_block_size); 241 opp->param_sparesize = htole16(sc->sc_spare_size); 242 opp->param_lunsize = htole32(sc->sc_lun_size); 243 opp->param_numluns = 1; 244 245 opp->param_manufacturer_id = 0x00; 246 memcpy(opp->param_manufacturer, 247 "NETBSD", strlen("NETBSD")); 248 memcpy(opp->param_model, 249 "NANDEMULATOR", strlen("NANDEMULATOR")); 250 251 uint16_t features = ONFI_FEATURE_16BIT; 252 opp->param_features = htole16(features); 253 254 /* the lower 4 bits contain the row address cycles 255 * the upper 4 bits contain the column address cycles 256 */ 257 opp->param_addr_cycles = sc->sc_row_cycles; 258 opp->param_addr_cycles |= (sc->sc_column_cycles << 4); 259 260 opp->param_integrity_crc = nand_crc16((uint8_t *)opp, 254); 261 } 262 263 sc->sc_ids[0] = 0x00; 264 sc->sc_ids[1] = 0x00; 265 266 sc->sc_onfi[0] = 'O'; 267 sc->sc_onfi[1] = 'N'; 268 sc->sc_onfi[2] = 'F'; 269 sc->sc_onfi[3] = 'I'; 270 271 sc->sc_row_mask = 0x00; 272 for (i = 0; i < sc->sc_row_cycles; i++) { 273 sc->sc_row_mask <<= 8; 274 sc->sc_row_mask |= 0xff; 275 } 276 277 nandemulator_device_reset(self); 278 279 sc->sc_nanddev = nand_attach_mi(&sc->sc_nand_if, sc->sc_dev); 280 } 281 282 static int 283 nandemulator_detach(device_t self, int flags) 284 { 285 struct nandemulator_softc *sc = device_private(self); 286 int ret = 0; 287 288 aprint_normal_dev(sc->sc_dev, "detaching emulator\n"); 289 290 pmf_device_deregister(sc->sc_dev); 291 292 if (sc->sc_nanddev != NULL) 293 ret = config_detach(sc->sc_nanddev, flags); 294 295 kmem_free(sc->sc_backend, sc->sc_backend_size); 296 kmem_free(sc->sc_parameter_page, 297 sizeof(struct onfi_parameter_page) * 4); 298 299 return ret; 300 } 301 302 /** 303 * bring the emulated device to a known state 304 */ 305 static void 306 nandemulator_device_reset(device_t self) 307 { 308 struct nandemulator_softc *sc = device_private(self); 309 310 DPRINTF(("device reset\n")); 311 312 sc->sc_command = 0; 313 sc->sc_register_writable = false; 314 sc->sc_io_len = 0; 315 sc->sc_io_pointer = NULL; 316 sc->sc_address = 0; 317 sc->sc_address_counter = 0; 318 319 sc->sc_status_register = ONFI_STATUS_RDY | ONFI_STATUS_WP; 320 } 321 322 static void 323 nandemulator_address_chip(device_t self) 324 { 325 struct nandemulator_softc *sc = device_private(self); 326 size_t page, offset; 327 328 KASSERT(sc->sc_address_counter == 329 sc->sc_column_cycles + sc->sc_row_cycles); 330 331 if (sc->sc_address_counter != 332 sc->sc_column_cycles + sc->sc_row_cycles) { 333 aprint_error_dev(self, "incorrect number of address cycles\n"); 334 aprint_error_dev(self, "cc: %d, rc: %d, ac: %d\n", 335 sc->sc_column_cycles, sc->sc_row_cycles, 336 sc->sc_address_counter); 337 } 338 339 page = nandemulator_address_to_page(self); 340 offset = sc->sc_page_size * page; 341 342 DPRINTF(("READ/PROGRAM; page: 0x%jx (row addr: 0x%jx)\n", 343 (uintmax_t )page, 344 (uintmax_t )offset)); 345 346 KASSERT(offset < sc->sc_device_size); 347 348 if (offset >= sc->sc_device_size) { 349 aprint_error_dev(self, "address > device size!\n"); 350 sc->sc_io_len = 0; 351 } else { 352 size_t addr = 353 nandemulator_page_to_backend_offset(self, page); 354 size_t pageoff = 355 nandemulator_column_address_to_subpage(self); 356 357 DPRINTF(("subpage: 0x%jx\n", (uintmax_t )pageoff)); 358 359 KASSERT(pageoff < 360 sc->sc_page_size + sc->sc_spare_size); 361 KASSERT(addr < sc->sc_backend_size); 362 363 sc->sc_io_pointer = sc->sc_backend + addr + pageoff; 364 sc->sc_io_len = 365 sc->sc_page_size + sc->sc_spare_size - pageoff; 366 } 367 } 368 369 static void 370 nandemulator_command(device_t self, uint8_t command) 371 { 372 struct nandemulator_softc *sc = device_private(self); 373 size_t offset, page; 374 375 sc->sc_command = command; 376 sc->sc_register_writable = false; 377 378 DPRINTF(("nandemulator command: 0x%hhx\n", command)); 379 380 switch (command) { 381 case ONFI_READ_STATUS: 382 sc->sc_io_pointer = &sc->sc_status_register; 383 sc->sc_io_len = 1; 384 break; 385 case ONFI_RESET: 386 nandemulator_device_reset(self); 387 break; 388 case ONFI_PAGE_PROGRAM: 389 sc->sc_register_writable = true; 390 case ONFI_READ: 391 case ONFI_BLOCK_ERASE: 392 sc->sc_address_counter = 0; 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