书到用时方恨少,事不经过不知难。
- 前因篇——为什么需要移植
最近在移植macronix 旺宏电子的nand flash 芯片。之前的思路是使用GagiDevice 的驱动修修改改,经过测试,证明是行不通的。 因为两者的nand flash 的architecture 不同,有die、plane 等的不一致,故而不能直接使用。
头疼几日后,依赖于互联网,登录到macron 的官网去看看。
https://www.macronix.com/en-us/support/technical-documentation/Pages/Serial-NAND-Flash.aspx

原厂有提供Linux、uboot 下的驱动源码 以及应用测试sample code;

===》 作为BSP 的集成而言,对每家的硬件当然不知道。硬件产商会自己提供相关的驱动和document ,只需登录官网去查找。
下载的资料中甚至直接将如何将驱动集成到Linux、uboot 的步骤和方法都总结了。
但是直接拿到macronix 的驱动源码并不一定可以直接使用,因为Linux 内核版本不同,接口的使用会发生改变。
区别如下:
1. macronix 的源码
/* * Copyright (c) 2003-2013 Broadcom Corporation * * Copyright (c) 2009-2010 Micron Technology, Inc. * * Copyright (c) 2015-2018 Macronix International Co. LTD. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #include <linux/module.h> #include <linux/version.h> #include <linux/delay.h> #include <linux/mtd/mtd.h> #include <linux/mtd/partitions.h> #include <linux/mtd/nand.h> #include <linux/spi/spi.h> #include <linux/spi/flash.h> #include "mx35_spinand.h" #define BUFSIZE (10 * 64 * 2048) #define CACHE_BUF 2112 /* * OOB area specification layout: Total 32 available free bytes. */ static inline struct spinand_state *mtd_to_state(struct mtd_info *mtd) { struct nand_chip *chip = (struct nand_chip *)mtd->priv; struct spinand_info *info = (struct spinand_info *)chip->priv; struct spinand_state *state = (struct spinand_state *)info->priv; return state; } #ifdef CONFIG_MTD_SPINAND_ONDIEECC static int enable_hw_ecc; static int enable_read_hw_ecc; static struct nand_ecclayout spinand_oob_64 = { .eccbytes = 48, .eccpos = { 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, .oobavail = 14, .oobfree = { {.offset = 2, .length = 2}, {.offset = 16, .length = 4}, {.offset = 32, .length = 4}, {.offset = 48, .length = 4}, } }; #endif /* * spinand_cmd - to process a command to send to the SPI Nand * Description: * Set up the command buffer to send to the SPI controller. * The command buffer has to initialized to 0. */ static int spinand_cmd(struct spi_device *spi, struct spinand_cmd *cmd) { struct spi_message message; struct spi_transfer x[4]; u8 dummy = 0xff; spi_message_init(&message); memset(x, 0, sizeof(x)); x[0].len = 1; x[0].tx_buf = &cmd->cmd; spi_message_add_tail(&x[0], &message); if (cmd->n_addr) { x[1].len = cmd->n_addr; x[1].tx_buf = cmd->addr; spi_message_add_tail(&x[1], &message); } if (cmd->n_dummy) { x[2].len = cmd->n_dummy; x[2].tx_buf = &dummy; spi_message_add_tail(&x[2], &message); } if (cmd->n_tx) { x[3].len = cmd->n_tx; x[3].tx_buf = cmd->tx_buf; spi_message_add_tail(&x[3], &message); } if (cmd->n_rx) { x[3].len = cmd->n_rx; x[3].rx_buf = cmd->rx_buf; spi_message_add_tail(&x[3], &message); } return spi_sync(spi, &message); } /* * spinand_read_id- Read SPI Nand ID * Description: * Read ID: read two ID bytes from the SPI Nand device */ static int spinand_read_id(struct spi_device *spi_nand, u8 *id) { int retval; u8 nand_id[3]; struct spinand_cmd cmd = {0}; cmd.cmd = CMD_READ_ID; cmd.n_dummy = 1; cmd.n_rx = 2; cmd.rx_buf = &nand_id[0]; retval = spinand_cmd(spi_nand, &cmd); if (retval < 0) { dev_err(&spi_nand->dev, "error %d reading id\n", retval); return retval; } id[0] = nand_id[0]; id[1] = nand_id[1]; return retval; } /* * spinand_read_status- send command 0xf to the SPI Nand status register * Description: * After read, write, or erase, the Nand device is expected to set the * busy status. * This function is to allow reading the status of the command: read, * write, and erase. * Once the status turns to be ready, the other status bits also are * valid status bits. */ static int spinand_read_status(struct spi_device *spi_nand, uint8_t *status) { struct spinand_cmd cmd = {0}; int ret; cmd.cmd = CMD_READ_REG; cmd.n_addr = 1; cmd.addr[0] = REG_STATUS; cmd.n_rx = 1; cmd.rx_buf = status; ret = spinand_cmd(spi_nand, &cmd); if (ret < 0) dev_err(&spi_nand->dev, "err: %d read status register\n", ret); return ret; } #define MAX_WAIT_JIFFIES (40 * HZ) static int wait_till_ready(struct spi_device *spi_nand) { unsigned long deadline; int retval; u8 stat = 0; deadline = jiffies + MAX_WAIT_JIFFIES; do { retval = spinand_read_status(spi_nand, &stat); if (retval < 0) return -1; else if (!(stat & 0x1)) break; cond_resched(); } while (!time_after_eq(jiffies, deadline)); if ((stat & 0x1) == 0) return 0; return -1; } / * spinand_get_otp- send command 0xf to read the SPI Nand OTP register * Description: * There is one bit( bit 0x10 ) to set or to clear the internal ECC. * Enable chip internal ECC, set the bit to 1 * Disable chip internal ECC, clear the bit to 0 */ static int spinand_get_otp(struct spi_device *spi_nand, u8 *otp) { struct spinand_cmd cmd = {0}; int retval; cmd.cmd = CMD_READ_REG; cmd.n_addr = 1; cmd.addr[0] = REG_OTP; cmd.n_rx = 1; cmd.rx_buf = otp; retval = spinand_cmd(spi_nand, &cmd); if (retval < 0) dev_err(&spi_nand->dev, "error %d get otp\n", retval); return retval; } / * spinand_set_otp- send command 0x1f to write the SPI Nand OTP register * Description: * There is one bit( bit 0x10 ) to set or to clear the internal ECC. * Enable chip internal ECC, set the bit to 1 * Disable chip internal ECC, clear the bit to 0 */ static int spinand_set_otp(struct spi_device *spi_nand, u8 *otp) { int retval; struct spinand_cmd cmd = {0}; cmd.cmd = CMD_WRITE_REG; cmd.n_addr = 1; cmd.addr[0] = REG_OTP; cmd.n_tx = 1; cmd.tx_buf = otp; retval = spinand_cmd(spi_nand, &cmd); if (retval < 0) dev_err(&spi_nand->dev, "error %d set otp\n", retval); return retval; } #ifdef CONFIG_MTD_SPINAND_QIO / * spinand_enable_qio- send command 0x1f to write the SPI Nand OTP register * Description: * There is one bit( bit 0x01 ) to set or to clear the QE bit. * Enable chip Quad Read/Write, set the bit to 1 * Disable chip Quad Read/Write, clear the bit to 0 */ static int spinand_enable_qio(struct spi_device *spi_nand) { int retval; u8 otp = 0; retval = spinand_get_otp(spi_nand, &otp); if (retval < 0) return retval; if ((otp & OTP_QIO_MASK) == OTP_QIO_MASK) return 0; otp |= OTP_QIO_MASK; retval = spinand_set_otp(spi_nand, &otp); if (retval < 0) return retval; spinand_get_otp(spi_nand, &otp); return otp; } #endif #ifdef CONFIG_MTD_SPINAND_ONDIEECC / * spinand_enable_ecc- send command 0x1f to write the SPI Nand OTP register * Description: * There is one bit( bit 0x10 ) to set or to clear the internal ECC. * Enable chip internal ECC, set the bit to 1 * Disable chip internal ECC, clear the bit to 0 */ static int spinand_enable_ecc(struct spi_device *spi_nand) { int retval; u8 otp = 0; retval = spinand_get_otp(spi_nand, &otp); if (retval < 0) return retval; if ((otp & OTP_ECC_MASK) == OTP_ECC_MASK) return 0; otp |= OTP_ECC_MASK; retval = spinand_set_otp(spi_nand, &otp); if (retval < 0) return retval; return spinand_get_otp(spi_nand, &otp); } #endif static int spinand_disable_ecc(struct spi_device *spi_nand) { int retval; u8 otp = 0; retval = spinand_get_otp(spi_nand, &otp); if (retval < 0) return retval; if ((otp & OTP_ECC_MASK) == OTP_ECC_MASK) { otp &= ~OTP_ECC_MASK; retval = spinand_set_otp(spi_nand, &otp); if (retval < 0) return retval; return spinand_get_otp(spi_nand, &otp); } return 0; } / * spinand_write_enable- send command 0x06 to enable write or erase the * Nand cells * Description: * Before write and erase the Nand cells, the write enable has to be set. * After the write or erase, the write enable bit is automatically * cleared (status register bit 2) * Set the bit 2 of the status register has the same effect */ static int spinand_write_enable(struct spi_device *spi_nand) { struct spinand_cmd cmd = {0}; cmd.cmd = CMD_WR_ENABLE; return spinand_cmd(spi_nand, &cmd); } static int spinand_read_page_to_cache(struct spi_device *spi_nand, int page_id) { struct spinand_cmd cmd = {0}; int row; row = page_id; cmd.cmd = CMD_READ; cmd.n_addr = 3; cmd.addr[0] = (u8)((row & 0xff0000) >> 16); cmd.addr[1] = (u8)((row & 0xff00) >> 8); cmd.addr[2] = (u8)(row & 0x00ff); return spinand_cmd(spi_nand, &cmd); } /* * spinand_read_from_cache- send command 0x03 to read out the data from the * cache register(2112 bytes max) * Description: * The read can specify 1 to 2112 bytes of data read at the corresponding * locations. * No tRd delay. */ static int spinand_read_from_cache(struct spi_device *spi_nand, u16 page_id, u16 byte_id, u16 len, u8 *rbuf) { struct spinand_cmd cmd = {0}; u16 column; column = byte_id; #ifdef CONFIG_MTD_SPINAND_QIO cmd.cmd = CMD_READ_RDM4X; #else cmd.cmd = CMD_READ_RDM; #endif cmd.n_addr = 3; cmd.addr[0] = (u8)((column & 0xff00) >> 8); cmd.addr[0] |= (u8)(((page_id >> 6) & 0x1) << 4); cmd.addr[1] = (u8)(column & 0x00ff); cmd.addr[2] = (u8)(0xff); cmd.n_dummy = 0; cmd.n_rx = len; cmd.rx_buf = rbuf; return spinand_cmd(spi_nand, &cmd); } /* * spinand_read_param_from_cache- to read out the parameter page from the * cache register, only support cmd = 0x03 * Description: * The parameter page data read at the corresponding locations. * No tRd delay. */ static int spinand_read_param_from_cache(struct spi_device *spi_nand, u16 page_id, u16 byte_id, u16 len, u8 *rbuf) { struct spinand_cmd cmd = {0}; u16 column; column = byte_id; cmd.cmd = CMD_READ_RDM; cmd.n_addr = 3; cmd.addr[0] = (u8)((column & 0xff00) >> 8); cmd.addr[0] |= (u8)(((page_id >> 6) & 0x1) << 4); cmd.addr[1] = (u8)(column & 0x00ff); cmd.addr[2] = (u8)(0xff); cmd.n_dummy = 0; cmd.n_rx = len; cmd.rx_buf = rbuf; return spinand_cmd(spi_nand, &cmd); } /* * spinand_read_page-to read a page with: * @page_id: the physical page number * @offset: the location from 0 to 2111 * @len: number of bytes to read * @rbuf: read buffer to hold @len bytes * * Description: * The read includes two commands to the Nand: 0x13 and 0x03 commands * Poll to read status to wait for tRD time. */ static int spinand_read_page(struct spi_device *spi_nand, int page_id, u16 offset, u16 len, u8 *rbuf) { int ret; u8 status = 0; #ifdef CONFIG_MTD_SPINAND_ONDIEECC if (enable_read_hw_ecc) { if (spinand_enable_ecc(spi_nand) < 0) dev_err(&spi_nand->dev, "enable HW ECC failed!"); } #endif ret = spinand_read_page_to_cache(spi_nand, page_id); if (ret < 0) return ret; if (wait_till_ready(spi_nand)) dev_err(&spi_nand->dev, "WAIT timedout!!!\n"); while (1) { ret = spinand_read_status(spi_nand, &status); if (ret < 0) { dev_err(&spi_nand->dev, "err %d read status register\n", ret); return ret; } if ((status & STATUS_OIP_MASK) == STATUS_READY) { if ((status & STATUS_ECC_MASK) == STATUS_ECC_ERROR) { dev_err(&spi_nand->dev, "ecc error, page=%d\n", page_id); return 0; } break; } } ret = spinand_read_from_cache(spi_nand, page_id, offset, len, rbuf); if (ret < 0) { dev_err(&spi_nand->dev, "read from cache failed!!\n"); return ret; } #ifdef CONFIG_MTD_SPINAND_ONDIEECC if (enable_read_hw_ecc) { ret = spinand_disable_ecc(spi_nand); if (ret < 0) { dev_err(&spi_nand->dev, "disable ecc failed!!\n"); return ret; } enable_read_hw_ecc = 0; } #endif return ret; } /* * spinand_read_param-to read a parameter page with: * @rbuf
讯享网

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/84224.html