From patchwork Tue Mar 25 08:19:59 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 26985 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ig0-f198.google.com (mail-ig0-f198.google.com [209.85.213.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 0C5ED20062 for ; Tue, 25 Mar 2014 08:24:12 +0000 (UTC) Received: by mail-ig0-f198.google.com with SMTP id uq10sf1824520igb.1 for ; Tue, 25 Mar 2014 01:24:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=l8mwgiO9licYuZ+sjupEjWaTTcKNjFjy5FXVasLHBHU=; b=EtGy54CTjdMfvzk3hWB+hAD+4m9zp7RPqpHtD5mAQIXqCRGTgW6nJ+m6/snFEyXaeW NjfqdNWyfrzGDzguKc6P5xhpCrKf//GKWxwjSjH+R3dClhfLeLgE5Qaj+Xfd49fiq/IJ WQdIUGGEVrIUWgHvZPwAvJ8C1FMM4UYfWz6ZNlfPTpxm+BxEEIbcRisdnNXfxwqZoNXQ 6eYZBZqRdpxV2gCuQvHSe2WUDnEkrCt3Xa6vedvto1NFssF+1Y+3kFaaciLHTdkdnheR OnejQDfQXv7xIQp9MsvI+PHg/dWzkQfUtLn2SyoUb62e/mW+9fNot73Y+c53aD26X31k zRJw== X-Gm-Message-State: ALoCoQmfxUmvoEMEnpMkiHl7Duvb1w1lxCLimholEFON/FNhX8yuzrTbBhdLePkJWrc86s1sAqmr X-Received: by 10.43.163.199 with SMTP id mp7mr24259859icc.6.1395735852541; Tue, 25 Mar 2014 01:24:12 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.20.22 with SMTP id 22ls118409qgi.63.gmail; Tue, 25 Mar 2014 01:24:12 -0700 (PDT) X-Received: by 10.52.72.48 with SMTP id a16mr4910803vdv.19.1395735852435; Tue, 25 Mar 2014 01:24:12 -0700 (PDT) Received: from mail-ve0-f179.google.com (mail-ve0-f179.google.com [209.85.128.179]) by mx.google.com with ESMTPS id i7si3586000vei.85.2014.03.25.01.24.12 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 25 Mar 2014 01:24:12 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.128.179 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.179; Received: by mail-ve0-f179.google.com with SMTP id db12so126344veb.38 for ; Tue, 25 Mar 2014 01:24:12 -0700 (PDT) X-Received: by 10.221.40.10 with SMTP id to10mr5379216vcb.22.1395735852312; Tue, 25 Mar 2014 01:24:12 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.220.78.9 with SMTP id i9csp285452vck; Tue, 25 Mar 2014 01:24:11 -0700 (PDT) X-Received: by 10.66.11.66 with SMTP id o2mr9217019pab.142.1395735850271; Tue, 25 Mar 2014 01:24:10 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id my2si10890091pbc.326.2014.03.25.01.24.09; Tue, 25 Mar 2014 01:24:09 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753332AbaCYIVo (ORCPT + 26 others); Tue, 25 Mar 2014 04:21:44 -0400 Received: from mail-wg0-f45.google.com ([74.125.82.45]:56649 "EHLO mail-wg0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753274AbaCYIVk (ORCPT ); Tue, 25 Mar 2014 04:21:40 -0400 Received: by mail-wg0-f45.google.com with SMTP id l18so78746wgh.4 for ; Tue, 25 Mar 2014 01:21:38 -0700 (PDT) X-Received: by 10.180.189.139 with SMTP id gi11mr21195737wic.53.1395735698812; Tue, 25 Mar 2014 01:21:38 -0700 (PDT) Received: from lee--X1.home (host109-148-113-193.range109-148.btcentralplus.com. [109.148.113.193]) by mx.google.com with ESMTPSA id k4sm5567676wib.19.2014.03.25.01.21.36 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 25 Mar 2014 01:21:37 -0700 (PDT) From: Lee Jones To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: lee.jones@linaro.org, kernel@stlinux.com, computersforpeace@gmail.com, linux-mtd@lists.infradead.org, dwmw2@infradead.org, angus.clark@st.com, pekon@ti.com Subject: [RFC 42/47] mtd: nand: stm_nand_bch: add read and write OOB (BCH) Date: Tue, 25 Mar 2014 08:19:59 +0000 Message-Id: <1395735604-26706-43-git-send-email-lee.jones@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1395735604-26706-1-git-send-email-lee.jones@linaro.org> References: <1395735604-26706-1-git-send-email-lee.jones@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: lee.jones@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.128.179 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Provide functions to read and write to the Out-Of-Bounds (OOB) area. Signed-off-by: Lee Jones --- drivers/mtd/nand/stm_nand_bch.c | 107 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/drivers/mtd/nand/stm_nand_bch.c b/drivers/mtd/nand/stm_nand_bch.c index 38658b8..389ccee 100644 --- a/drivers/mtd/nand/stm_nand_bch.c +++ b/drivers/mtd/nand/stm_nand_bch.c @@ -1218,6 +1218,111 @@ static int flex_do_write_ops(struct nandi_controller *nandi, return 0; } +static char *mtd_oob_mode_strs[] = {"PLACE", "AUTO", "RAW"}; + +static int bch_mtd_read_and_write_oob(struct mtd_info *mtd, loff_t tofrom, + struct mtd_oob_ops *ops, + bool read) +{ + struct nand_chip *chip = mtd->priv; + struct nandi_controller *nandi = chip->priv; + uint32_t page_mask = mtd->writesize - 1; + int ret; + + dev_dbg(nandi->dev, "%s: 0x%012llx [page = %u, oob = %u mode = %s]\n", + __func__, tofrom, + (ops->datbuf ? ops->len : 0), + (ops->oobbuf ? ops->ooblen : 0), + mtd_oob_mode_strs[ops->mode]); + + if (!ops->oobbuf && ops->mode != MTD_OPS_RAW) { + if (read) + return mtd_read(mtd, tofrom, ops->len, + &ops->retlen, ops->datbuf); + else + return mtd_write(mtd, tofrom, ops->len, + &ops->retlen, ops->datbuf); + } + + ops->oobretlen = 0; + ops->retlen = 0; + + /* + * We report OOB as unavailable (i.e. oobavail = 0), therefore nothing + * should call this + */ + if (ops->oobbuf && ops->mode == MTD_OPS_AUTO_OOB) + return -ENOTSUPP; + + /* Not currently supported by MTD */ + if (ops->datbuf && ops->oobbuf && ops->mode == MTD_OPS_PLACE_OOB) + return -ENOTSUPP; + + /* Do not allow oob reads with ooboffs */ + if (ops->oobbuf && ops->ooboffs) + return -ENOTSUPP; + + /* Do not allow reads past end of device */ + if (ops->datbuf && (tofrom + ops->len) > mtd->size) { + dev_err(nandi->dev, "attempt %s beyond end of device\n", + read ? "read" : "write"); + return -EINVAL; + } + + if (ops->oobbuf && + (tofrom + mtd->writesize * (ops->ooblen / mtd->oobsize)) + > mtd->size) { + dev_err(nandi->dev, "attempt %s beyond end of device\n", + read ? "read" : "write"); + return -EINVAL; + } + + /* Do not allow non-aligned reads/writes */ + if ((tofrom & page_mask) || + (ops->datbuf && (ops->len & page_mask)) || + (ops->oobbuf && (ops->ooblen % mtd->oobsize))) { + dev_err(nandi->dev, "attempt to %s non-aligned data\n", + read ? "read" : "write"); + return -EINVAL; + } + + /* Do not allow inconsistent data and oob lengths */ + if (ops->datbuf && ops->oobbuf && + (ops->len / mtd->writesize != ops->ooblen / mtd->oobsize)) { + dev_err(nandi->dev, + "data length inconsistent with oob length\n"); + return -EINVAL; + } + + nand_get_device(mtd, read ? FL_READING : FL_WRITING); + + if (!read && flex_check_wp(nandi)) { + dev_dbg(nandi->dev, "device is write-protected\n"); + return -EIO; + } + + if (read) + ret = flex_do_read_ops(nandi, tofrom, ops); + else + ret = flex_do_write_ops(nandi, tofrom, ops); + + nand_release_device(mtd); + + return ret; +} + +static int bch_mtd_read_oob(struct mtd_info *mtd, loff_t from, + struct mtd_oob_ops *ops) +{ + return bch_mtd_read_and_write_oob(mtd, from, ops, true); +} + +static int bch_mtd_write_oob(struct mtd_info *mtd, loff_t to, + struct mtd_oob_ops *ops) +{ + return bch_mtd_read_and_write_oob(mtd, to, ops, false); +} + static int bch_mtd_block_isbad(struct mtd_info *mtd, loff_t offs) { struct nand_chip *chip = mtd->priv; @@ -1454,6 +1559,8 @@ static void nandi_set_mtd_defaults(struct nandi_controller *nandi, mtd->subpage_sft = 0; mtd->_erase = bch_mtd_erase; + mtd->_read_oob = bch_mtd_read_oob; + mtd->_write_oob = bch_mtd_write_oob; mtd->_block_isbad = bch_mtd_block_isbad; mtd->_block_markbad = bch_mtd_block_markbad;