22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/slab.h>
27 #include <linux/module.h>
37 static uint8_t scan_ff_pattern[] = { 0xff };
42 .pattern = scan_ff_pattern
49 .oobfree = { {.offset = 0, .length = 0} }
96 unsigned int metadata_size;
97 unsigned int status_size;
98 unsigned int block_mark_bit_offset;
120 pr_err(
"wrong ECC strength.\n");
139 if (!this->swap_block_mark)
188 block_mark_bit_offset = mtd->
writesize * 8 -
199 int chipnr = this->current_chip;
201 return this->dma_chans[chipnr];
210 this->direct_dma_map_ok =
true;
213 sg_init_one(sgl, this->upper_buf, this->upper_len);
220 memcpy(this->data_buffer_dma, this->upper_buf,
227 this->direct_dma_map_ok =
false;
232 static void dma_irq_callback(
void *
param)
246 if (this->direct_dma_map_ok ==
false)
247 memcpy(this->upper_buf, this->data_buffer_dma,
261 pr_err(
"in wrong DMA operation.\n");
271 init_completion(dma_c);
275 dmaengine_submit(desc);
281 pr_err(
"DMA timeout, last DMA :%d\n", this->last_dma_type);
302 init_completion(bch_c);
310 pr_err(
"BCH timeout, last DMA :%d\n", this->last_dma_type);
318 acquire_register_block(
struct gpmi_nand_data *
this,
const char *res_name)
327 pr_err(
"Can't get resource for %s\n", res_name);
333 pr_err(
"Can't remap %s\n", res_name);
342 pr_err(
"unknown resource name : %s\n", res_name);
369 pr_err(
"Can't get resource for %s\n", res_name);
375 pr_err(
"Can't own %s\n", res_name);
393 static bool gpmi_dma_filter(
struct dma_chan *
chan,
void *param)
408 if (dma_channel == chan->
chan_id) {
436 dn = pdev->
dev.of_node;
437 ret = of_property_read_u32(dn,
"fsl,gpmi-dma-channel", &dma_channel);
439 pr_err(
"unable to get DMA channel from dt.\n");
442 this->
private = (
void *)dma_channel;
448 pr_err(
"Can't get resource for DMA\n");
451 this->dma_data.chan_irq = r_dma->
start;
459 pr_err(
"dma_request_channel failed.\n");
463 this->dma_chans[0] = dma_chan;
467 release_dma_channels(
this);
487 "gpmi_apb",
"gpmi_bch",
"gpmi_bch_apb",
"per1_bch",
493 char **extra_clks =
NULL;
499 if (IS_ERR(r->
clock[0]))
504 extra_clks = extra_clks_for_mx6q;
509 if (extra_clks[i - 1] ==
NULL)
531 dev_dbg(this->
dev,
"failed in finding the clocks.\n");
549 ret = acquire_bch_irq(
this, bch_irq);
553 ret = acquire_dma_channels(
this);
555 goto exit_dma_channels;
557 pinctrl = devm_pinctrl_get_select_default(&this->pdev->
dev);
558 if (IS_ERR(pinctrl)) {
559 ret = PTR_ERR(pinctrl);
563 ret = gpmi_get_clks(
this);
570 release_dma_channels(
this);
572 release_bch_irq(
this);
574 release_register_block(
this);
581 release_register_block(
this);
582 release_bch_irq(
this);
583 release_dma_channels(
this);
597 .data_hold_in_ns = 60,
598 .address_setup_in_ns = 25,
599 .gpmi_sample_delay_in_ns = 6,
610 this->
timing = safe_timing;
615 void *destination,
unsigned length,
616 void *alt_virt,
dma_addr_t alt_phys,
unsigned alt_size,
627 if (alt_size < length) {
628 pr_err(
"Alternate buffer is too small\n");
633 *use_virt = destination;
634 *use_phys = dest_phys;
635 this->direct_dma_map_ok =
true;
640 *use_virt = alt_virt;
641 *use_phys = alt_phys;
642 this->direct_dma_map_ok =
false;
647 void *destination,
unsigned length,
648 void *alt_virt,
dma_addr_t alt_phys,
unsigned alt_size,
651 if (this->direct_dma_map_ok)
655 static inline void read_page_swap_end(
struct gpmi_nand_data *
this,
656 void *destination,
unsigned length,
657 void *alt_virt,
dma_addr_t alt_phys,
unsigned alt_size,
660 if (!this->direct_dma_map_ok)
661 memcpy(destination, alt_virt, length);
665 const void *
source,
unsigned length,
666 void *alt_virt,
dma_addr_t alt_phys,
unsigned alt_size,
677 if (alt_size < length) {
678 pr_err(
"Alternate buffer is too small\n");
684 *use_phys = source_phys;
692 memcpy(alt_virt, source, length);
694 *use_virt = alt_virt;
695 *use_phys = alt_phys;
700 const void *source,
unsigned length,
701 void *alt_virt,
dma_addr_t alt_phys,
unsigned alt_size,
705 if (used_virt == source)
715 this->page_buffer_virt,
716 this->page_buffer_phys);
717 kfree(this->cmd_buffer);
718 kfree(this->data_buffer_dma);
720 this->cmd_buffer =
NULL;
721 this->data_buffer_dma =
NULL;
722 this->page_buffer_virt =
NULL;
723 this->page_buffer_size = 0;
734 if (this->cmd_buffer ==
NULL)
739 if (this->data_buffer_dma ==
NULL)
752 &this->page_buffer_phys,
GFP_DMA);
753 if (!this->page_buffer_virt)
758 this->payload_virt = this->page_buffer_virt;
759 this->payload_phys = this->page_buffer_phys;
760 this->auxiliary_virt = this->payload_virt + geo->
payload_size;
761 this->auxiliary_phys = this->payload_phys + geo->
payload_size;
765 gpmi_free_dma_buffer(
this);
766 pr_err(
"allocate DMA buffer ret!!\n");
770 static void gpmi_cmd_ctrl(
struct mtd_info *mtd,
int data,
unsigned int ctrl)
803 static int gpmi_dev_ready(
struct mtd_info *mtd)
811 static void gpmi_select_chip(
struct mtd_info *mtd,
int chipnr)
854 gpmi_read_buf(mtd, buf, 1);
871 unsigned char from_data;
872 unsigned char from_oob;
874 if (!this->swap_block_mark)
891 from_data = (p[0] >>
bit) | (p[1] << (8 - bit));
899 mask = (0x1 <<
bit) - 1;
900 p[0] = (p[0] &
mask) | (from_oob << bit);
903 p[1] = (p[1] &
mask) | (from_oob >> (8 - bit));
913 void *auxiliary_virt;
918 unsigned int corrected;
921 pr_debug(
"page number is : %d\n", page);
922 ret = read_page_prepare(
this, buf, mtd->
writesize,
923 this->payload_virt, this->payload_phys,
925 &payload_virt, &payload_phys);
927 pr_err(
"Inadequate DMA buffer\n");
931 auxiliary_virt = this->auxiliary_virt;
932 auxiliary_phys = this->auxiliary_phys;
937 this->payload_virt, this->payload_phys,
939 payload_virt, payload_phys);
941 pr_err(
"Error in ECC-based read: %d\n", ret);
946 block_mark_swapping(
this, payload_virt, auxiliary_virt);
968 if (failed || corrected >= (nfc_geo->
ecc_strength - 1)) {
988 read_page_swap_end(
this, buf, mtd->
writesize,
989 this->payload_virt, this->payload_phys,
991 payload_virt, payload_phys);
997 const uint8_t *buf,
int oob_required)
1001 const void *payload_virt;
1003 const void *auxiliary_virt;
1008 if (this->swap_block_mark) {
1015 payload_virt = this->payload_virt;
1016 payload_phys = this->payload_phys;
1020 auxiliary_virt = this->auxiliary_virt;
1021 auxiliary_phys = this->auxiliary_phys;
1024 block_mark_swapping(
this,
1025 (
void *) payload_virt, (
void *) auxiliary_virt);
1031 ret = send_page_prepare(
this,
1033 this->payload_virt, this->payload_phys,
1035 &payload_virt, &payload_phys);
1037 pr_err(
"Inadequate payload DMA buffer\n");
1041 ret = send_page_prepare(
this,
1043 this->auxiliary_virt, this->auxiliary_phys,
1045 &auxiliary_virt, &auxiliary_phys);
1047 pr_err(
"Inadequate auxiliary DMA buffer\n");
1048 goto exit_auxiliary;
1055 pr_err(
"Error in ECC-based write: %d\n", ret);
1057 if (!this->swap_block_mark) {
1059 this->auxiliary_virt, this->auxiliary_phys,
1061 auxiliary_virt, auxiliary_phys);
1063 send_page_end(
this, buf, mtd->
writesize,
1064 this->payload_virt, this->payload_phys,
1066 payload_virt, payload_phys);
1145 pr_debug(
"page number is %d\n", page);
1180 static int gpmi_block_markbad(
struct mtd_info *mtd, loff_t ofs)
1191 chip->
bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1213 status = chip->
waitfunc(mtd, chip);
1251 static const char *fingerprint =
"STMP";
1252 static int mx23_check_transcription_stamp(
struct gpmi_nand_data *
this)
1258 unsigned int search_area_size_in_strides;
1259 unsigned int stride;
1262 int saved_chip_number;
1263 int found_an_ncb_fingerprint =
false;
1268 saved_chip_number = this->current_chip;
1274 dev_dbg(dev,
"Scanning for an NCB fingerprint...\n");
1276 for (stride = 0; stride < search_area_size_in_strides; stride++) {
1280 dev_dbg(dev,
"Looking for a fingerprint in page 0x%x\n", page);
1290 if (!
memcmp(buffer, fingerprint,
strlen(fingerprint))) {
1291 found_an_ncb_fingerprint =
true;
1299 if (found_an_ncb_fingerprint)
1300 dev_dbg(dev,
"\tFound a fingerprint\n");
1302 dev_dbg(dev,
"\tNo fingerprint found\n");
1303 return found_an_ncb_fingerprint;
1307 static int mx23_write_transcription_stamp(
struct gpmi_nand_data *
this)
1313 unsigned int block_size_in_pages;
1314 unsigned int search_area_size_in_strides;
1315 unsigned int search_area_size_in_pages;
1316 unsigned int search_area_size_in_blocks;
1318 unsigned int stride;
1321 int saved_chip_number;
1327 search_area_size_in_pages = search_area_size_in_strides *
1329 search_area_size_in_blocks =
1330 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1331 block_size_in_pages;
1333 dev_dbg(dev,
"Search Area Geometry :\n");
1334 dev_dbg(dev,
"\tin Blocks : %u\n", search_area_size_in_blocks);
1335 dev_dbg(dev,
"\tin Strides: %u\n", search_area_size_in_strides);
1336 dev_dbg(dev,
"\tin Pages : %u\n", search_area_size_in_pages);
1339 saved_chip_number = this->current_chip;
1343 dev_dbg(dev,
"Erasing the search area...\n");
1345 for (block = 0; block < search_area_size_in_blocks; block++) {
1347 page = block * block_size_in_pages;
1350 dev_dbg(dev,
"\tErasing block 0x%x\n", block);
1355 status = chip->
waitfunc(mtd, chip);
1356 if (status & NAND_STATUS_FAIL)
1357 dev_err(dev,
"[%s] Erase failed.\n", __func__);
1366 dev_dbg(dev,
"Writing NCB fingerprints...\n");
1367 for (stride = 0; stride < search_area_size_in_strides; stride++) {
1372 dev_dbg(dev,
"Writing an NCB fingerprint in page 0x%x\n", page);
1374 chip->
ecc.write_page_raw(mtd, chip, buffer, 0);
1378 status = chip->
waitfunc(mtd, chip);
1379 if (status & NAND_STATUS_FAIL)
1380 dev_err(dev,
"[%s] Write failed.\n", __func__);
1407 if (mx23_check_transcription_stamp(
this))
1414 dev_dbg(dev,
"Transcribing bad block marks...\n");
1443 if (block_mark != 0xff) {
1444 dev_dbg(dev,
"Transcribing mark in block %u\n", block);
1447 dev_err(dev,
"Failed to mark block bad with "
1453 mx23_write_transcription_stamp(
this);
1459 nand_boot_set_geometry(
this);
1463 return mx23_boot_init(
this);
1472 gpmi_free_dma_buffer(
this);
1477 pr_err(
"set geometry ret : %d\n", ret);
1482 return gpmi_alloc_dma_buffer(
this);
1491 this->swap_block_mark =
false;
1493 this->swap_block_mark =
true;
1496 ret = gpmi_set_geometry(
this);
1506 return nand_boot_init(
this);
1509 static int gpmi_scan_bbt(
struct mtd_info *mtd)
1516 ret = gpmi_pre_bbt_scan(
this);
1535 gpmi_free_dma_buffer(
this);
1546 this->current_chip = -1;
1550 mtd->
name =
"gpmi-nand";
1561 chip->
ecc.read_page = gpmi_ecc_read_page;
1562 chip->
ecc.write_page = gpmi_ecc_write_page;
1563 chip->
ecc.read_oob = gpmi_ecc_read_oob;
1564 chip->
ecc.write_oob = gpmi_ecc_write_oob;
1571 chip->
ecc.strength = 8;
1572 chip->
ecc.layout = &gpmi_hw_ecclayout;
1579 ret = gpmi_alloc_dma_buffer(
this);
1585 pr_err(
"Chip scan failed\n");
1596 gpmi_nfc_exit(
this);
1601 { .name =
"imx23-gpmi-nand", .driver_data =
IS_MX23, },
1602 { .name =
"imx28-gpmi-nand", .driver_data =
IS_MX28, },
1603 { .name =
"imx6q-gpmi-nand", .driver_data =
IS_MX6Q, },
1607 static const struct of_device_id gpmi_nand_id_table[] = {
1609 .compatible =
"fsl,imx23-gpmi-nand",
1610 .data = (
void *)&gpmi_ids[
IS_MX23]
1612 .compatible =
"fsl,imx28-gpmi-nand",
1613 .data = (
void *)&gpmi_ids[
IS_MX28]
1615 .compatible =
"fsl,imx6q-gpmi-nand",
1616 .data = (
void *)&gpmi_ids[
IS_MX6Q]
1631 pr_err(
"Failed to find the right device id.\n");
1637 pr_err(
"Failed to allocate per-device memory\n");
1641 platform_set_drvdata(pdev,
this);
1643 this->dev = &pdev->
dev;
1645 ret = acquire_resources(
this);
1647 goto exit_acquire_resources;
1649 ret = init_hardware(
this);
1653 ret = gpmi_nfc_init(
this);
1657 dev_info(this->dev,
"driver registered.\n");
1662 release_resources(
this);
1663 exit_acquire_resources:
1664 platform_set_drvdata(pdev,
NULL);
1666 dev_err(this->dev,
"driver registration failed: %d\n", ret);
1675 gpmi_nfc_exit(
this);
1676 release_resources(
this);
1677 platform_set_drvdata(pdev,
NULL);
1684 .name =
"gpmi-nand",
1685 .of_match_table = gpmi_nand_id_table,
1687 .probe = gpmi_nand_probe,
1689 .id_table = gpmi_ids,