Lines Matching full:nand

3  * Amlogic Meson Nand Flash Controller Driver
95 /* nand flash controller delay 3 ns */
113 struct nand_chip nand; member
221 static struct meson_nfc_nand_chip *to_meson_nand(struct nand_chip *nand) in to_meson_nand() argument
223 return container_of(nand, struct meson_nfc_nand_chip, nand); in to_meson_nand()
226 static void meson_nfc_select_chip(struct nand_chip *nand, int chip) in meson_nfc_select_chip() argument
228 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_select_chip()
229 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_select_chip()
269 static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir, in meson_nfc_cmd_access() argument
272 struct mtd_info *mtd = nand_to_mtd(nand); in meson_nfc_cmd_access()
274 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_cmd_access()
278 pagesize = nand->ecc.size; in meson_nfc_cmd_access()
287 pages = len / nand->ecc.size; in meson_nfc_cmd_access()
300 * The Nand flash controller is designed as two stages pipleline - in meson_nfc_drain_cmd()
303 * but the Nand flash controller still has two commands buffered, in meson_nfc_drain_cmd()
335 static u8 *meson_nfc_oob_ptr(struct nand_chip *nand, int i) in meson_nfc_oob_ptr() argument
337 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_oob_ptr()
340 len = nand->ecc.size * (i + 1) + (nand->ecc.bytes + 2) * i; in meson_nfc_oob_ptr()
345 static u8 *meson_nfc_data_ptr(struct nand_chip *nand, int i) in meson_nfc_data_ptr() argument
347 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_data_ptr()
350 temp = nand->ecc.size + nand->ecc.bytes; in meson_nfc_data_ptr()
356 static void meson_nfc_get_data_oob(struct nand_chip *nand, in meson_nfc_get_data_oob() argument
362 oob_len = nand->ecc.bytes + 2; in meson_nfc_get_data_oob()
363 for (i = 0; i < nand->ecc.steps; i++) { in meson_nfc_get_data_oob()
365 dsrc = meson_nfc_data_ptr(nand, i); in meson_nfc_get_data_oob()
366 memcpy(buf, dsrc, nand->ecc.size); in meson_nfc_get_data_oob()
367 buf += nand->ecc.size; in meson_nfc_get_data_oob()
369 osrc = meson_nfc_oob_ptr(nand, i); in meson_nfc_get_data_oob()
375 static void meson_nfc_set_data_oob(struct nand_chip *nand, in meson_nfc_set_data_oob() argument
381 oob_len = nand->ecc.bytes + 2; in meson_nfc_set_data_oob()
382 for (i = 0; i < nand->ecc.steps; i++) { in meson_nfc_set_data_oob()
384 dsrc = meson_nfc_data_ptr(nand, i); in meson_nfc_set_data_oob()
385 memcpy(dsrc, buf, nand->ecc.size); in meson_nfc_set_data_oob()
386 buf += nand->ecc.size; in meson_nfc_set_data_oob()
388 osrc = meson_nfc_oob_ptr(nand, i); in meson_nfc_set_data_oob()
422 static void meson_nfc_set_user_byte(struct nand_chip *nand, u8 *oob_buf) in meson_nfc_set_user_byte() argument
424 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_set_user_byte()
428 for (i = 0, count = 0; i < nand->ecc.steps; i++, count += 2) { in meson_nfc_set_user_byte()
435 static void meson_nfc_get_user_byte(struct nand_chip *nand, u8 *oob_buf) in meson_nfc_get_user_byte() argument
437 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_get_user_byte()
441 for (i = 0, count = 0; i < nand->ecc.steps; i++, count += 2) { in meson_nfc_get_user_byte()
448 static int meson_nfc_ecc_correct(struct nand_chip *nand, u32 *bitflips, in meson_nfc_ecc_correct() argument
451 struct mtd_info *mtd = nand_to_mtd(nand); in meson_nfc_ecc_correct()
452 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_ecc_correct()
456 for (i = 0; i < nand->ecc.steps; i++) { in meson_nfc_ecc_correct()
464 if ((nand->options & NAND_NEED_SCRAMBLING) && in meson_nfc_ecc_correct()
465 ECC_ZERO_CNT(*info) < nand->ecc.strength) { in meson_nfc_ecc_correct()
477 static int meson_nfc_dma_buffer_setup(struct nand_chip *nand, void *databuf, in meson_nfc_dma_buffer_setup() argument
481 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_dma_buffer_setup()
516 static void meson_nfc_dma_buffer_release(struct nand_chip *nand, in meson_nfc_dma_buffer_release() argument
520 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_dma_buffer_release()
527 static int meson_nfc_read_buf(struct nand_chip *nand, u8 *buf, int len) in meson_nfc_read_buf() argument
529 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_read_buf()
538 ret = meson_nfc_dma_buffer_setup(nand, buf, len, info, in meson_nfc_read_buf()
548 meson_nfc_dma_buffer_release(nand, len, PER_INFO_BYTE, DMA_FROM_DEVICE); in meson_nfc_read_buf()
556 static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len) in meson_nfc_write_buf() argument
558 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_write_buf()
562 ret = meson_nfc_dma_buffer_setup(nand, buf, len, NULL, in meson_nfc_write_buf()
572 meson_nfc_dma_buffer_release(nand, len, 0, DMA_TO_DEVICE); in meson_nfc_write_buf()
577 static int meson_nfc_rw_cmd_prepare_and_execute(struct nand_chip *nand, in meson_nfc_rw_cmd_prepare_and_execute() argument
581 nand_get_sdr_timings(nand_get_interface_config(nand)); in meson_nfc_rw_cmd_prepare_and_execute()
582 struct mtd_info *mtd = nand_to_mtd(nand); in meson_nfc_rw_cmd_prepare_and_execute()
583 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_rw_cmd_prepare_and_execute()
606 if (nand->options & NAND_ROW_ADDR_3) in meson_nfc_rw_cmd_prepare_and_execute()
630 static int meson_nfc_write_page_sub(struct nand_chip *nand, in meson_nfc_write_page_sub() argument
634 nand_get_sdr_timings(nand_get_interface_config(nand)); in meson_nfc_write_page_sub()
635 struct mtd_info *mtd = nand_to_mtd(nand); in meson_nfc_write_page_sub()
636 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_write_page_sub()
637 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_write_page_sub()
642 meson_nfc_select_chip(nand, nand->cur_cs); in meson_nfc_write_page_sub()
645 info_len = nand->ecc.steps * PER_INFO_BYTE; in meson_nfc_write_page_sub()
647 ret = meson_nfc_rw_cmd_prepare_and_execute(nand, page, DIRWRITE); in meson_nfc_write_page_sub()
651 ret = meson_nfc_dma_buffer_setup(nand, meson_chip->data_buf, in meson_nfc_write_page_sub()
657 if (nand->options & NAND_NEED_SCRAMBLING) { in meson_nfc_write_page_sub()
659 meson_nfc_cmd_access(nand, raw, DIRWRITE, in meson_nfc_write_page_sub()
662 meson_nfc_cmd_access(nand, raw, DIRWRITE, in meson_nfc_write_page_sub()
670 meson_nfc_dma_buffer_release(nand, data_len, info_len, DMA_TO_DEVICE); in meson_nfc_write_page_sub()
675 static int meson_nfc_write_page_raw(struct nand_chip *nand, const u8 *buf, in meson_nfc_write_page_raw() argument
678 u8 *oob_buf = nand->oob_poi; in meson_nfc_write_page_raw()
680 meson_nfc_set_data_oob(nand, buf, oob_buf); in meson_nfc_write_page_raw()
682 return meson_nfc_write_page_sub(nand, page, 1); in meson_nfc_write_page_raw()
685 static int meson_nfc_write_page_hwecc(struct nand_chip *nand, in meson_nfc_write_page_hwecc() argument
688 struct mtd_info *mtd = nand_to_mtd(nand); in meson_nfc_write_page_hwecc()
689 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_write_page_hwecc()
690 u8 *oob_buf = nand->oob_poi; in meson_nfc_write_page_hwecc()
693 memset(meson_chip->info_buf, 0, nand->ecc.steps * PER_INFO_BYTE); in meson_nfc_write_page_hwecc()
694 meson_nfc_set_user_byte(nand, oob_buf); in meson_nfc_write_page_hwecc()
696 return meson_nfc_write_page_sub(nand, page, 0); in meson_nfc_write_page_hwecc()
700 struct nand_chip *nand, int raw) in meson_nfc_check_ecc_pages_valid() argument
702 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_check_ecc_pages_valid()
707 neccpages = raw ? 1 : nand->ecc.steps; in meson_nfc_check_ecc_pages_valid()
717 static int meson_nfc_read_page_sub(struct nand_chip *nand, in meson_nfc_read_page_sub() argument
720 struct mtd_info *mtd = nand_to_mtd(nand); in meson_nfc_read_page_sub()
721 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_read_page_sub()
722 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_read_page_sub()
726 meson_nfc_select_chip(nand, nand->cur_cs); in meson_nfc_read_page_sub()
729 info_len = nand->ecc.steps * PER_INFO_BYTE; in meson_nfc_read_page_sub()
731 ret = meson_nfc_rw_cmd_prepare_and_execute(nand, page, DIRREAD); in meson_nfc_read_page_sub()
735 ret = meson_nfc_dma_buffer_setup(nand, meson_chip->data_buf, in meson_nfc_read_page_sub()
741 if (nand->options & NAND_NEED_SCRAMBLING) { in meson_nfc_read_page_sub()
743 meson_nfc_cmd_access(nand, raw, DIRREAD, in meson_nfc_read_page_sub()
746 meson_nfc_cmd_access(nand, raw, DIRREAD, in meson_nfc_read_page_sub()
751 meson_nfc_check_ecc_pages_valid(nfc, nand, raw); in meson_nfc_read_page_sub()
753 meson_nfc_dma_buffer_release(nand, data_len, info_len, DMA_FROM_DEVICE); in meson_nfc_read_page_sub()
758 static int meson_nfc_read_page_raw(struct nand_chip *nand, u8 *buf, in meson_nfc_read_page_raw() argument
761 u8 *oob_buf = nand->oob_poi; in meson_nfc_read_page_raw()
764 ret = meson_nfc_read_page_sub(nand, page, 1); in meson_nfc_read_page_raw()
768 meson_nfc_get_data_oob(nand, buf, oob_buf); in meson_nfc_read_page_raw()
773 static int meson_nfc_read_page_hwecc(struct nand_chip *nand, u8 *buf, in meson_nfc_read_page_hwecc() argument
776 struct mtd_info *mtd = nand_to_mtd(nand); in meson_nfc_read_page_hwecc()
777 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_read_page_hwecc()
778 struct nand_ecc_ctrl *ecc = &nand->ecc; in meson_nfc_read_page_hwecc()
781 u8 *oob_buf = nand->oob_poi; in meson_nfc_read_page_hwecc()
784 ret = meson_nfc_read_page_sub(nand, page, 0); in meson_nfc_read_page_hwecc()
788 meson_nfc_get_user_byte(nand, oob_buf); in meson_nfc_read_page_hwecc()
789 ret = meson_nfc_ecc_correct(nand, &bitflips, &correct_bitmap); in meson_nfc_read_page_hwecc()
795 if ((nand->options & NAND_NEED_SCRAMBLING) || !buf) { in meson_nfc_read_page_hwecc()
799 ret = meson_nfc_read_page_raw(nand, buf, 0, page); in meson_nfc_read_page_hwecc()
803 for (i = 0; i < nand->ecc.steps ; i++) { in meson_nfc_read_page_hwecc()
805 u8 *oob = nand->oob_poi + i * (ecc->bytes + 2); in meson_nfc_read_page_hwecc()
827 static int meson_nfc_read_oob_raw(struct nand_chip *nand, int page) in meson_nfc_read_oob_raw() argument
829 return meson_nfc_read_page_raw(nand, NULL, 1, page); in meson_nfc_read_oob_raw()
832 static int meson_nfc_read_oob(struct nand_chip *nand, int page) in meson_nfc_read_oob() argument
834 return meson_nfc_read_page_hwecc(nand, NULL, 1, page); in meson_nfc_read_oob()
896 static int meson_nfc_exec_op(struct nand_chip *nand, in meson_nfc_exec_op() argument
899 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_exec_op()
900 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nfc_exec_op()
909 meson_nfc_select_chip(nand, op->cs); in meson_nfc_exec_op()
936 meson_nfc_read_buf(nand, buf, instr->ctx.data.len); in meson_nfc_exec_op()
944 meson_nfc_write_buf(nand, buf, instr->ctx.data.len); in meson_nfc_exec_op()
962 struct nand_chip *nand = mtd_to_nand(mtd); in meson_ooblayout_ecc() local
964 if (section >= nand->ecc.steps) in meson_ooblayout_ecc()
967 oobregion->offset = 2 + (section * (2 + nand->ecc.bytes)); in meson_ooblayout_ecc()
968 oobregion->length = nand->ecc.bytes; in meson_ooblayout_ecc()
976 struct nand_chip *nand = mtd_to_nand(mtd); in meson_ooblayout_free() local
978 if (section >= nand->ecc.steps) in meson_ooblayout_free()
981 oobregion->offset = section * (2 + nand->ecc.bytes); in meson_ooblayout_free()
1074 static void meson_nfc_free_buffer(struct nand_chip *nand) in meson_nfc_free_buffer() argument
1076 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_free_buffer()
1082 static int meson_chip_buffer_init(struct nand_chip *nand) in meson_chip_buffer_init() argument
1084 struct mtd_info *mtd = nand_to_mtd(nand); in meson_chip_buffer_init()
1085 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_chip_buffer_init()
1088 nsectors = mtd->writesize / nand->ecc.size; in meson_chip_buffer_init()
1107 int meson_nfc_setup_interface(struct nand_chip *nand, int csline, in meson_nfc_setup_interface() argument
1110 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nfc_setup_interface()
1149 static int meson_nand_bch_mode(struct nand_chip *nand) in meson_nand_bch_mode() argument
1151 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nand_bch_mode()
1154 if (nand->ecc.strength > 60 || nand->ecc.strength < 8) in meson_nand_bch_mode()
1158 if (meson_ecc[i].strength == nand->ecc.strength) { in meson_nand_bch_mode()
1167 static void meson_nand_detach_chip(struct nand_chip *nand) in meson_nand_detach_chip() argument
1169 meson_nfc_free_buffer(nand); in meson_nand_detach_chip()
1172 static int meson_nand_attach_chip(struct nand_chip *nand) in meson_nand_attach_chip() argument
1174 struct meson_nfc *nfc = nand_get_controller_data(nand); in meson_nand_attach_chip()
1175 struct meson_nfc_nand_chip *meson_chip = to_meson_nand(nand); in meson_nand_attach_chip()
1176 struct mtd_info *mtd = nand_to_mtd(nand); in meson_nand_attach_chip()
1182 "%s:nand%d", in meson_nand_attach_chip()
1189 if (nand->bbt_options & NAND_BBT_USE_FLASH) in meson_nand_attach_chip()
1190 nand->bbt_options |= NAND_BBT_NO_OOB; in meson_nand_attach_chip()
1192 nand->options |= NAND_NO_SUBPAGE_WRITE; in meson_nand_attach_chip()
1194 ret = nand_ecc_choose_conf(nand, nfc->data->ecc_caps, in meson_nand_attach_chip()
1203 ret = meson_nand_bch_mode(nand); in meson_nand_attach_chip()
1207 nand->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST; in meson_nand_attach_chip()
1208 nand->ecc.write_page_raw = meson_nfc_write_page_raw; in meson_nand_attach_chip()
1209 nand->ecc.write_page = meson_nfc_write_page_hwecc; in meson_nand_attach_chip()
1210 nand->ecc.write_oob_raw = nand_write_oob_std; in meson_nand_attach_chip()
1211 nand->ecc.write_oob = nand_write_oob_std; in meson_nand_attach_chip()
1213 nand->ecc.read_page_raw = meson_nfc_read_page_raw; in meson_nand_attach_chip()
1214 nand->ecc.read_page = meson_nfc_read_page_hwecc; in meson_nand_attach_chip()
1215 nand->ecc.read_oob_raw = meson_nfc_read_oob_raw; in meson_nand_attach_chip()
1216 nand->ecc.read_oob = meson_nfc_read_oob; in meson_nand_attach_chip()
1218 if (nand->options & NAND_BUSWIDTH_16) { in meson_nand_attach_chip()
1222 ret = meson_chip_buffer_init(nand); in meson_nand_attach_chip()
1241 struct nand_chip *nand; in meson_nfc_nand_chip_init() local
1273 nand = &meson_chip->nand; in meson_nfc_nand_chip_init()
1274 nand->controller = &nfc->controller; in meson_nfc_nand_chip_init()
1275 nand->controller->ops = &meson_nand_controller_ops; in meson_nfc_nand_chip_init()
1276 nand_set_flash_node(nand, np); in meson_nfc_nand_chip_init()
1277 nand_set_controller_data(nand, nfc); in meson_nfc_nand_chip_init()
1279 nand->options |= NAND_USES_DMA; in meson_nfc_nand_chip_init()
1280 mtd = nand_to_mtd(nand); in meson_nfc_nand_chip_init()
1284 ret = nand_scan(nand, nsels); in meson_nfc_nand_chip_init()
1291 nand_cleanup(nand); in meson_nfc_nand_chip_init()
1308 mtd = nand_to_mtd(&meson_chip->nand); in meson_nfc_nand_chip_cleanup()
1311 nand_cleanup(&meson_chip->nand); in meson_nfc_nand_chip_cleanup()
1405 dev_err(dev, "failed to initialize NAND clock\n"); in meson_nfc_probe()
1427 dev_err(dev, "failed to init NAND chips\n"); in meson_nfc_probe()
1452 .name = "meson-nand",
1460 MODULE_DESCRIPTION("Amlogic's Meson NAND Flash Controller driver");