blob: 9c03fbb1f47dc41ca901304e501f9830a895b9ec [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02002/*
3 * Copyright (C) 2017 Free Electrons
4 * Copyright (C) 2017 NextThing Co
5 *
6 * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02007 */
8
Boris Brezillon348d56a2018-09-07 00:38:48 +02009#include "internals.h"
Boris Brezillon9b2d61f2016-06-08 10:34:57 +020010
KOBAYASHI Yoshitakef2237132018-08-04 14:25:52 +090011/* Bit for detecting BENAND */
12#define TOSHIBA_NAND_ID4_IS_BENAND BIT(7)
13
14/* Recommended to rewrite for BENAND */
15#define TOSHIBA_NAND_STATUS_REWRITE_RECOMMENDED BIT(3)
16
Miquel Raynale9836762018-09-24 11:35:18 +020017static int toshiba_nand_benand_eccstatus(struct nand_chip *chip)
KOBAYASHI Yoshitakef2237132018-08-04 14:25:52 +090018{
Miquel Raynale9836762018-09-24 11:35:18 +020019 struct mtd_info *mtd = nand_to_mtd(chip);
KOBAYASHI Yoshitakef2237132018-08-04 14:25:52 +090020 int ret;
21 unsigned int max_bitflips = 0;
22 u8 status;
23
24 /* Check Status */
25 ret = nand_status_op(chip, &status);
26 if (ret)
27 return ret;
28
29 if (status & NAND_STATUS_FAIL) {
30 /* uncorrected */
31 mtd->ecc_stats.failed++;
32 } else if (status & TOSHIBA_NAND_STATUS_REWRITE_RECOMMENDED) {
33 /* corrected */
34 max_bitflips = mtd->bitflip_threshold;
35 mtd->ecc_stats.corrected += max_bitflips;
36 }
37
38 return max_bitflips;
39}
40
41static int
Boris Brezillonb9761682018-09-06 14:05:20 +020042toshiba_nand_read_page_benand(struct nand_chip *chip, uint8_t *buf,
KOBAYASHI Yoshitakef2237132018-08-04 14:25:52 +090043 int oob_required, int page)
44{
45 int ret;
46
Boris Brezillonb9761682018-09-06 14:05:20 +020047 ret = nand_read_page_raw(chip, buf, oob_required, page);
KOBAYASHI Yoshitakef2237132018-08-04 14:25:52 +090048 if (ret)
49 return ret;
50
Miquel Raynale9836762018-09-24 11:35:18 +020051 return toshiba_nand_benand_eccstatus(chip);
KOBAYASHI Yoshitakef2237132018-08-04 14:25:52 +090052}
53
54static int
Boris Brezillonb9761682018-09-06 14:05:20 +020055toshiba_nand_read_subpage_benand(struct nand_chip *chip, uint32_t data_offs,
KOBAYASHI Yoshitakef2237132018-08-04 14:25:52 +090056 uint32_t readlen, uint8_t *bufpoi, int page)
57{
58 int ret;
59
60 ret = nand_read_page_op(chip, page, data_offs,
61 bufpoi + data_offs, readlen);
62 if (ret)
63 return ret;
64
Miquel Raynale9836762018-09-24 11:35:18 +020065 return toshiba_nand_benand_eccstatus(chip);
KOBAYASHI Yoshitakef2237132018-08-04 14:25:52 +090066}
67
68static void toshiba_nand_benand_init(struct nand_chip *chip)
69{
70 struct mtd_info *mtd = nand_to_mtd(chip);
71
72 /*
73 * On BENAND, the entire OOB region can be used by the MTD user.
74 * The calculated ECC bytes are stored into other isolated
75 * area which is not accessible to users.
76 * This is why chip->ecc.bytes = 0.
77 */
78 chip->ecc.bytes = 0;
79 chip->ecc.size = 512;
80 chip->ecc.strength = 8;
81 chip->ecc.read_page = toshiba_nand_read_page_benand;
82 chip->ecc.read_subpage = toshiba_nand_read_subpage_benand;
83 chip->ecc.write_page = nand_write_page_raw;
84 chip->ecc.read_page_raw = nand_read_page_raw_notsupp;
85 chip->ecc.write_page_raw = nand_write_page_raw_notsupp;
86
87 chip->options |= NAND_SUBPAGE_READ;
88
89 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
90}
91
Boris Brezillon9b2d61f2016-06-08 10:34:57 +020092static void toshiba_nand_decode_id(struct nand_chip *chip)
93{
94 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon629a4422018-10-25 17:10:37 +020095 struct nand_memory_organization *memorg;
96
97 memorg = nanddev_get_memorg(&chip->base);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +020098
99 nand_decode_ext_id(chip);
100
101 /*
102 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
103 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
104 * follows:
105 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
106 * 110b -> 24nm
107 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
108 */
109 if (chip->id.len >= 6 && nand_is_slc(chip) &&
110 (chip->id.data[5] & 0x7) == 0x6 /* 24nm */ &&
Boris Brezillon629a4422018-10-25 17:10:37 +0200111 !(chip->id.data[4] & 0x80) /* !BENAND */) {
112 memorg->oobsize = 32 * memorg->pagesize >> 9;
113 mtd->oobsize = memorg->oobsize;
114 }
KOBAYASHI Yoshitakefb3bff52018-02-15 00:35:06 +0900115
116 /*
117 * Extract ECC requirements from 6th id byte.
118 * For Toshiba SLC, ecc requrements are as follows:
119 * - 43nm: 1 bit ECC for each 512Byte is required.
120 * - 32nm: 4 bit ECC for each 512Byte is required.
121 * - 24nm: 8 bit ECC for each 512Byte is required.
122 */
123 if (chip->id.len >= 6 && nand_is_slc(chip)) {
Boris Brezillon6a1b66d2018-11-04 16:09:42 +0100124 chip->base.eccreq.step_size = 512;
KOBAYASHI Yoshitakefb3bff52018-02-15 00:35:06 +0900125 switch (chip->id.data[5] & 0x7) {
126 case 0x4:
Boris Brezillon6a1b66d2018-11-04 16:09:42 +0100127 chip->base.eccreq.strength = 1;
KOBAYASHI Yoshitakefb3bff52018-02-15 00:35:06 +0900128 break;
129 case 0x5:
Boris Brezillon6a1b66d2018-11-04 16:09:42 +0100130 chip->base.eccreq.strength = 4;
KOBAYASHI Yoshitakefb3bff52018-02-15 00:35:06 +0900131 break;
132 case 0x6:
Boris Brezillon6a1b66d2018-11-04 16:09:42 +0100133 chip->base.eccreq.strength = 8;
KOBAYASHI Yoshitakefb3bff52018-02-15 00:35:06 +0900134 break;
135 default:
136 WARN(1, "Could not get ECC info");
Boris Brezillon6a1b66d2018-11-04 16:09:42 +0100137 chip->base.eccreq.step_size = 0;
KOBAYASHI Yoshitakefb3bff52018-02-15 00:35:06 +0900138 break;
139 }
140 }
Boris Brezillon9b2d61f2016-06-08 10:34:57 +0200141}
142
143static int toshiba_nand_init(struct nand_chip *chip)
144{
145 if (nand_is_slc(chip))
Frieder Schrempfbb592542019-04-17 12:36:36 +0000146 chip->options |= NAND_BBM_FIRSTPAGE | NAND_BBM_SECONDPAGE;
Boris Brezillon9b2d61f2016-06-08 10:34:57 +0200147
KOBAYASHI Yoshitakef2237132018-08-04 14:25:52 +0900148 /* Check that chip is BENAND and ECC mode is on-die */
149 if (nand_is_slc(chip) && chip->ecc.mode == NAND_ECC_ON_DIE &&
150 chip->id.data[4] & TOSHIBA_NAND_ID4_IS_BENAND)
151 toshiba_nand_benand_init(chip);
152
Boris Brezillon9b2d61f2016-06-08 10:34:57 +0200153 return 0;
154}
155
156const struct nand_manufacturer_ops toshiba_nand_manuf_ops = {
157 .detect = toshiba_nand_decode_id,
158 .init = toshiba_nand_init,
159};