18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2005, Intec Automation Inc.
48c2ecf20Sopenharmony_ci * Copyright (C) 2014, Freescale Semiconductor, Inc.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/mtd/spi-nor.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include "core.h"
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cistatic const struct flash_info xilinx_parts[] = {
128c2ecf20Sopenharmony_ci	/* Xilinx S3AN Internal Flash */
138c2ecf20Sopenharmony_ci	{ "3S50AN", S3AN_INFO(0x1f2200, 64, 264) },
148c2ecf20Sopenharmony_ci	{ "3S200AN", S3AN_INFO(0x1f2400, 256, 264) },
158c2ecf20Sopenharmony_ci	{ "3S400AN", S3AN_INFO(0x1f2400, 256, 264) },
168c2ecf20Sopenharmony_ci	{ "3S700AN", S3AN_INFO(0x1f2500, 512, 264) },
178c2ecf20Sopenharmony_ci	{ "3S1400AN", S3AN_INFO(0x1f2600, 512, 528) },
188c2ecf20Sopenharmony_ci};
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*
218c2ecf20Sopenharmony_ci * This code converts an address to the Default Address Mode, that has non
228c2ecf20Sopenharmony_ci * power of two page sizes. We must support this mode because it is the default
238c2ecf20Sopenharmony_ci * mode supported by Xilinx tools, it can access the whole flash area and
248c2ecf20Sopenharmony_ci * changing over to the Power-of-two mode is irreversible and corrupts the
258c2ecf20Sopenharmony_ci * original data.
268c2ecf20Sopenharmony_ci * Addr can safely be unsigned int, the biggest S3AN device is smaller than
278c2ecf20Sopenharmony_ci * 4 MiB.
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_cistatic u32 s3an_convert_addr(struct spi_nor *nor, u32 addr)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	u32 offset, page;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	offset = addr % nor->page_size;
348c2ecf20Sopenharmony_ci	page = addr / nor->page_size;
358c2ecf20Sopenharmony_ci	page <<= (nor->page_size > 512) ? 10 : 9;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	return page | offset;
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic int xilinx_nor_setup(struct spi_nor *nor,
418c2ecf20Sopenharmony_ci			    const struct spi_nor_hwcaps *hwcaps)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	int ret;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	ret = spi_nor_xread_sr(nor, nor->bouncebuf);
468c2ecf20Sopenharmony_ci	if (ret)
478c2ecf20Sopenharmony_ci		return ret;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	nor->erase_opcode = SPINOR_OP_XSE;
508c2ecf20Sopenharmony_ci	nor->program_opcode = SPINOR_OP_XPP;
518c2ecf20Sopenharmony_ci	nor->read_opcode = SPINOR_OP_READ;
528c2ecf20Sopenharmony_ci	nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	/*
558c2ecf20Sopenharmony_ci	 * This flashes have a page size of 264 or 528 bytes (known as
568c2ecf20Sopenharmony_ci	 * Default addressing mode). It can be changed to a more standard
578c2ecf20Sopenharmony_ci	 * Power of two mode where the page size is 256/512. This comes
588c2ecf20Sopenharmony_ci	 * with a price: there is 3% less of space, the data is corrupted
598c2ecf20Sopenharmony_ci	 * and the page size cannot be changed back to default addressing
608c2ecf20Sopenharmony_ci	 * mode.
618c2ecf20Sopenharmony_ci	 *
628c2ecf20Sopenharmony_ci	 * The current addressing mode can be read from the XRDSR register
638c2ecf20Sopenharmony_ci	 * and should not be changed, because is a destructive operation.
648c2ecf20Sopenharmony_ci	 */
658c2ecf20Sopenharmony_ci	if (nor->bouncebuf[0] & XSR_PAGESIZE) {
668c2ecf20Sopenharmony_ci		/* Flash in Power of 2 mode */
678c2ecf20Sopenharmony_ci		nor->page_size = (nor->page_size == 264) ? 256 : 512;
688c2ecf20Sopenharmony_ci		nor->mtd.writebufsize = nor->page_size;
698c2ecf20Sopenharmony_ci		nor->mtd.size = 8 * nor->page_size * nor->info->n_sectors;
708c2ecf20Sopenharmony_ci		nor->mtd.erasesize = 8 * nor->page_size;
718c2ecf20Sopenharmony_ci	} else {
728c2ecf20Sopenharmony_ci		/* Flash in Default addressing mode */
738c2ecf20Sopenharmony_ci		nor->params->convert_addr = s3an_convert_addr;
748c2ecf20Sopenharmony_ci		nor->mtd.erasesize = nor->info->sector_size;
758c2ecf20Sopenharmony_ci	}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	return 0;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic void xilinx_post_sfdp_fixups(struct spi_nor *nor)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	nor->params->setup = xilinx_nor_setup;
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic const struct spi_nor_fixups xilinx_fixups = {
868c2ecf20Sopenharmony_ci	.post_sfdp = xilinx_post_sfdp_fixups,
878c2ecf20Sopenharmony_ci};
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ciconst struct spi_nor_manufacturer spi_nor_xilinx = {
908c2ecf20Sopenharmony_ci	.name = "xilinx",
918c2ecf20Sopenharmony_ci	.parts = xilinx_parts,
928c2ecf20Sopenharmony_ci	.nparts = ARRAY_SIZE(xilinx_parts),
938c2ecf20Sopenharmony_ci	.fixups = &xilinx_fixups,
948c2ecf20Sopenharmony_ci};
95