18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * (C) 2005, 2006 Red Hat Inc.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author: David Woodhouse <dwmw2@infradead.org>
68c2ecf20Sopenharmony_ci *	   Tom Sylla <tom.sylla@amd.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci *  Overview:
98c2ecf20Sopenharmony_ci *   This is a device driver for the NAND flash controller found on
108c2ecf20Sopenharmony_ci *   the AMD CS5535/CS5536 companion chipsets for the Geode processor.
118c2ecf20Sopenharmony_ci *   mtd-id for command line partitioning is cs553x_nand_cs[0-3]
128c2ecf20Sopenharmony_ci *   where 0-3 reflects the chip select for NAND.
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/kernel.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include <linux/init.h>
188c2ecf20Sopenharmony_ci#include <linux/module.h>
198c2ecf20Sopenharmony_ci#include <linux/delay.h>
208c2ecf20Sopenharmony_ci#include <linux/mtd/mtd.h>
218c2ecf20Sopenharmony_ci#include <linux/mtd/rawnand.h>
228c2ecf20Sopenharmony_ci#include <linux/mtd/nand_ecc.h>
238c2ecf20Sopenharmony_ci#include <linux/mtd/partitions.h>
248c2ecf20Sopenharmony_ci#include <linux/iopoll.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <asm/msr.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define NR_CS553X_CONTROLLERS	4
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define MSR_DIVIL_GLD_CAP	0x51400000	/* DIVIL capabilitiies */
318c2ecf20Sopenharmony_ci#define CAP_CS5535		0x2df000ULL
328c2ecf20Sopenharmony_ci#define CAP_CS5536		0x5df500ULL
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/* NAND Timing MSRs */
358c2ecf20Sopenharmony_ci#define MSR_NANDF_DATA		0x5140001b	/* NAND Flash Data Timing MSR */
368c2ecf20Sopenharmony_ci#define MSR_NANDF_CTL		0x5140001c	/* NAND Flash Control Timing */
378c2ecf20Sopenharmony_ci#define MSR_NANDF_RSVD		0x5140001d	/* Reserved */
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/* NAND BAR MSRs */
408c2ecf20Sopenharmony_ci#define MSR_DIVIL_LBAR_FLSH0	0x51400010	/* Flash Chip Select 0 */
418c2ecf20Sopenharmony_ci#define MSR_DIVIL_LBAR_FLSH1	0x51400011	/* Flash Chip Select 1 */
428c2ecf20Sopenharmony_ci#define MSR_DIVIL_LBAR_FLSH2	0x51400012	/* Flash Chip Select 2 */
438c2ecf20Sopenharmony_ci#define MSR_DIVIL_LBAR_FLSH3	0x51400013	/* Flash Chip Select 3 */
448c2ecf20Sopenharmony_ci	/* Each made up of... */
458c2ecf20Sopenharmony_ci#define FLSH_LBAR_EN		(1ULL<<32)
468c2ecf20Sopenharmony_ci#define FLSH_NOR_NAND		(1ULL<<33)	/* 1 for NAND */
478c2ecf20Sopenharmony_ci#define FLSH_MEM_IO		(1ULL<<34)	/* 1 for MMIO */
488c2ecf20Sopenharmony_ci	/* I/O BARs have BASE_ADDR in bits 15:4, IO_MASK in 47:36 */
498c2ecf20Sopenharmony_ci	/* MMIO BARs have BASE_ADDR in bits 31:12, MEM_MASK in 63:44 */
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/* Pin function selection MSR (IDE vs. flash on the IDE pins) */
528c2ecf20Sopenharmony_ci#define MSR_DIVIL_BALL_OPTS	0x51400015
538c2ecf20Sopenharmony_ci#define PIN_OPT_IDE		(1<<0)	/* 0 for flash, 1 for IDE */
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/* Registers within the NAND flash controller BAR -- memory mapped */
568c2ecf20Sopenharmony_ci#define MM_NAND_DATA		0x00	/* 0 to 0x7ff, in fact */
578c2ecf20Sopenharmony_ci#define MM_NAND_CTL		0x800	/* Any even address 0x800-0x80e */
588c2ecf20Sopenharmony_ci#define MM_NAND_IO		0x801	/* Any odd address 0x801-0x80f */
598c2ecf20Sopenharmony_ci#define MM_NAND_STS		0x810
608c2ecf20Sopenharmony_ci#define MM_NAND_ECC_LSB		0x811
618c2ecf20Sopenharmony_ci#define MM_NAND_ECC_MSB		0x812
628c2ecf20Sopenharmony_ci#define MM_NAND_ECC_COL		0x813
638c2ecf20Sopenharmony_ci#define MM_NAND_LAC		0x814
648c2ecf20Sopenharmony_ci#define MM_NAND_ECC_CTL		0x815
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/* Registers within the NAND flash controller BAR -- I/O mapped */
678c2ecf20Sopenharmony_ci#define IO_NAND_DATA		0x00	/* 0 to 3, in fact */
688c2ecf20Sopenharmony_ci#define IO_NAND_CTL		0x04
698c2ecf20Sopenharmony_ci#define IO_NAND_IO		0x05
708c2ecf20Sopenharmony_ci#define IO_NAND_STS		0x06
718c2ecf20Sopenharmony_ci#define IO_NAND_ECC_CTL		0x08
728c2ecf20Sopenharmony_ci#define IO_NAND_ECC_LSB		0x09
738c2ecf20Sopenharmony_ci#define IO_NAND_ECC_MSB		0x0a
748c2ecf20Sopenharmony_ci#define IO_NAND_ECC_COL		0x0b
758c2ecf20Sopenharmony_ci#define IO_NAND_LAC		0x0c
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define CS_NAND_CTL_DIST_EN	(1<<4)	/* Enable NAND Distract interrupt */
788c2ecf20Sopenharmony_ci#define CS_NAND_CTL_RDY_INT_MASK	(1<<3)	/* Enable RDY/BUSY# interrupt */
798c2ecf20Sopenharmony_ci#define CS_NAND_CTL_ALE		(1<<2)
808c2ecf20Sopenharmony_ci#define CS_NAND_CTL_CLE		(1<<1)
818c2ecf20Sopenharmony_ci#define CS_NAND_CTL_CE		(1<<0)	/* Keep low; 1 to reset */
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define CS_NAND_STS_FLASH_RDY	(1<<3)
848c2ecf20Sopenharmony_ci#define CS_NAND_CTLR_BUSY	(1<<2)
858c2ecf20Sopenharmony_ci#define CS_NAND_CMD_COMP	(1<<1)
868c2ecf20Sopenharmony_ci#define CS_NAND_DIST_ST		(1<<0)
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#define CS_NAND_ECC_PARITY	(1<<2)
898c2ecf20Sopenharmony_ci#define CS_NAND_ECC_CLRECC	(1<<1)
908c2ecf20Sopenharmony_ci#define CS_NAND_ECC_ENECC	(1<<0)
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistruct cs553x_nand_controller {
938c2ecf20Sopenharmony_ci	struct nand_controller base;
948c2ecf20Sopenharmony_ci	struct nand_chip chip;
958c2ecf20Sopenharmony_ci	void __iomem *mmio;
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic struct cs553x_nand_controller *
998c2ecf20Sopenharmony_cito_cs553x(struct nand_controller *controller)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	return container_of(controller, struct cs553x_nand_controller, base);
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic int cs553x_write_ctrl_byte(struct cs553x_nand_controller *cs553x,
1058c2ecf20Sopenharmony_ci				  u32 ctl, u8 data)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	u8 status;
1088c2ecf20Sopenharmony_ci	int ret;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	writeb(ctl, cs553x->mmio + MM_NAND_CTL);
1118c2ecf20Sopenharmony_ci	writeb(data, cs553x->mmio + MM_NAND_IO);
1128c2ecf20Sopenharmony_ci	ret = readb_poll_timeout_atomic(cs553x->mmio + MM_NAND_STS, status,
1138c2ecf20Sopenharmony_ci					!(status & CS_NAND_CTLR_BUSY), 1,
1148c2ecf20Sopenharmony_ci					100000);
1158c2ecf20Sopenharmony_ci	if (ret)
1168c2ecf20Sopenharmony_ci		return ret;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	return 0;
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic void cs553x_data_in(struct cs553x_nand_controller *cs553x, void *buf,
1228c2ecf20Sopenharmony_ci			   unsigned int len)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	writeb(0, cs553x->mmio + MM_NAND_CTL);
1258c2ecf20Sopenharmony_ci	while (unlikely(len > 0x800)) {
1268c2ecf20Sopenharmony_ci		memcpy_fromio(buf, cs553x->mmio, 0x800);
1278c2ecf20Sopenharmony_ci		buf += 0x800;
1288c2ecf20Sopenharmony_ci		len -= 0x800;
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci	memcpy_fromio(buf, cs553x->mmio, len);
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic void cs553x_data_out(struct cs553x_nand_controller *cs553x,
1348c2ecf20Sopenharmony_ci			    const void *buf, unsigned int len)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	writeb(0, cs553x->mmio + MM_NAND_CTL);
1378c2ecf20Sopenharmony_ci	while (unlikely(len > 0x800)) {
1388c2ecf20Sopenharmony_ci		memcpy_toio(cs553x->mmio, buf, 0x800);
1398c2ecf20Sopenharmony_ci		buf += 0x800;
1408c2ecf20Sopenharmony_ci		len -= 0x800;
1418c2ecf20Sopenharmony_ci	}
1428c2ecf20Sopenharmony_ci	memcpy_toio(cs553x->mmio, buf, len);
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistatic int cs553x_wait_ready(struct cs553x_nand_controller *cs553x,
1468c2ecf20Sopenharmony_ci			     unsigned int timeout_ms)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	u8 mask = CS_NAND_CTLR_BUSY | CS_NAND_STS_FLASH_RDY;
1498c2ecf20Sopenharmony_ci	u8 status;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	return readb_poll_timeout(cs553x->mmio + MM_NAND_STS, status,
1528c2ecf20Sopenharmony_ci				  (status & mask) == CS_NAND_STS_FLASH_RDY, 100,
1538c2ecf20Sopenharmony_ci				  timeout_ms * 1000);
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic int cs553x_exec_instr(struct cs553x_nand_controller *cs553x,
1578c2ecf20Sopenharmony_ci			     const struct nand_op_instr *instr)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	unsigned int i;
1608c2ecf20Sopenharmony_ci	int ret = 0;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	switch (instr->type) {
1638c2ecf20Sopenharmony_ci	case NAND_OP_CMD_INSTR:
1648c2ecf20Sopenharmony_ci		ret = cs553x_write_ctrl_byte(cs553x, CS_NAND_CTL_CLE,
1658c2ecf20Sopenharmony_ci					     instr->ctx.cmd.opcode);
1668c2ecf20Sopenharmony_ci		break;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	case NAND_OP_ADDR_INSTR:
1698c2ecf20Sopenharmony_ci		for (i = 0; i < instr->ctx.addr.naddrs; i++) {
1708c2ecf20Sopenharmony_ci			ret = cs553x_write_ctrl_byte(cs553x, CS_NAND_CTL_ALE,
1718c2ecf20Sopenharmony_ci						     instr->ctx.addr.addrs[i]);
1728c2ecf20Sopenharmony_ci			if (ret)
1738c2ecf20Sopenharmony_ci				break;
1748c2ecf20Sopenharmony_ci		}
1758c2ecf20Sopenharmony_ci		break;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	case NAND_OP_DATA_IN_INSTR:
1788c2ecf20Sopenharmony_ci		cs553x_data_in(cs553x, instr->ctx.data.buf.in,
1798c2ecf20Sopenharmony_ci			       instr->ctx.data.len);
1808c2ecf20Sopenharmony_ci		break;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	case NAND_OP_DATA_OUT_INSTR:
1838c2ecf20Sopenharmony_ci		cs553x_data_out(cs553x, instr->ctx.data.buf.out,
1848c2ecf20Sopenharmony_ci				instr->ctx.data.len);
1858c2ecf20Sopenharmony_ci		break;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	case NAND_OP_WAITRDY_INSTR:
1888c2ecf20Sopenharmony_ci		ret = cs553x_wait_ready(cs553x, instr->ctx.waitrdy.timeout_ms);
1898c2ecf20Sopenharmony_ci		break;
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	if (instr->delay_ns)
1938c2ecf20Sopenharmony_ci		ndelay(instr->delay_ns);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	return ret;
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic int cs553x_exec_op(struct nand_chip *this,
1998c2ecf20Sopenharmony_ci			  const struct nand_operation *op,
2008c2ecf20Sopenharmony_ci			  bool check_only)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	struct cs553x_nand_controller *cs553x = to_cs553x(this->controller);
2038c2ecf20Sopenharmony_ci	unsigned int i;
2048c2ecf20Sopenharmony_ci	int ret;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	if (check_only)
2078c2ecf20Sopenharmony_ci		return true;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	/* De-assert the CE pin */
2108c2ecf20Sopenharmony_ci	writeb(0, cs553x->mmio + MM_NAND_CTL);
2118c2ecf20Sopenharmony_ci	for (i = 0; i < op->ninstrs; i++) {
2128c2ecf20Sopenharmony_ci		ret = cs553x_exec_instr(cs553x, &op->instrs[i]);
2138c2ecf20Sopenharmony_ci		if (ret)
2148c2ecf20Sopenharmony_ci			break;
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	/* Re-assert the CE pin. */
2188c2ecf20Sopenharmony_ci	writeb(CS_NAND_CTL_CE, cs553x->mmio + MM_NAND_CTL);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	return ret;
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic void cs_enable_hwecc(struct nand_chip *this, int mode)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	struct cs553x_nand_controller *cs553x = to_cs553x(this->controller);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	writeb(0x07, cs553x->mmio + MM_NAND_ECC_CTL);
2288c2ecf20Sopenharmony_ci}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cistatic int cs_calculate_ecc(struct nand_chip *this, const u_char *dat,
2318c2ecf20Sopenharmony_ci			    u_char *ecc_code)
2328c2ecf20Sopenharmony_ci{
2338c2ecf20Sopenharmony_ci	struct cs553x_nand_controller *cs553x = to_cs553x(this->controller);
2348c2ecf20Sopenharmony_ci	uint32_t ecc;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	ecc = readl(cs553x->mmio + MM_NAND_STS);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	ecc_code[1] = ecc >> 8;
2398c2ecf20Sopenharmony_ci	ecc_code[0] = ecc >> 16;
2408c2ecf20Sopenharmony_ci	ecc_code[2] = ecc >> 24;
2418c2ecf20Sopenharmony_ci	return 0;
2428c2ecf20Sopenharmony_ci}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic struct cs553x_nand_controller *controllers[4];
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic int cs553x_attach_chip(struct nand_chip *chip)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
2498c2ecf20Sopenharmony_ci		return 0;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	chip->ecc.size = 256;
2528c2ecf20Sopenharmony_ci	chip->ecc.bytes = 3;
2538c2ecf20Sopenharmony_ci	chip->ecc.hwctl  = cs_enable_hwecc;
2548c2ecf20Sopenharmony_ci	chip->ecc.calculate = cs_calculate_ecc;
2558c2ecf20Sopenharmony_ci	chip->ecc.correct  = nand_correct_data;
2568c2ecf20Sopenharmony_ci	chip->ecc.strength = 1;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	return 0;
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic const struct nand_controller_ops cs553x_nand_controller_ops = {
2628c2ecf20Sopenharmony_ci	.exec_op = cs553x_exec_op,
2638c2ecf20Sopenharmony_ci	.attach_chip = cs553x_attach_chip,
2648c2ecf20Sopenharmony_ci};
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic int __init cs553x_init_one(int cs, int mmio, unsigned long adr)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	struct cs553x_nand_controller *controller;
2698c2ecf20Sopenharmony_ci	int err = 0;
2708c2ecf20Sopenharmony_ci	struct nand_chip *this;
2718c2ecf20Sopenharmony_ci	struct mtd_info *new_mtd;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	pr_notice("Probing CS553x NAND controller CS#%d at %sIO 0x%08lx\n",
2748c2ecf20Sopenharmony_ci		  cs, mmio ? "MM" : "P", adr);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	if (!mmio) {
2778c2ecf20Sopenharmony_ci		pr_notice("PIO mode not yet implemented for CS553X NAND controller\n");
2788c2ecf20Sopenharmony_ci		return -ENXIO;
2798c2ecf20Sopenharmony_ci	}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	/* Allocate memory for MTD device structure and private data */
2828c2ecf20Sopenharmony_ci	controller = kzalloc(sizeof(*controller), GFP_KERNEL);
2838c2ecf20Sopenharmony_ci	if (!controller) {
2848c2ecf20Sopenharmony_ci		err = -ENOMEM;
2858c2ecf20Sopenharmony_ci		goto out;
2868c2ecf20Sopenharmony_ci	}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	this = &controller->chip;
2898c2ecf20Sopenharmony_ci	nand_controller_init(&controller->base);
2908c2ecf20Sopenharmony_ci	controller->base.ops = &cs553x_nand_controller_ops;
2918c2ecf20Sopenharmony_ci	this->controller = &controller->base;
2928c2ecf20Sopenharmony_ci	new_mtd = nand_to_mtd(this);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	/* Link the private data with the MTD structure */
2958c2ecf20Sopenharmony_ci	new_mtd->owner = THIS_MODULE;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	/* map physical address */
2988c2ecf20Sopenharmony_ci	controller->mmio = ioremap(adr, 4096);
2998c2ecf20Sopenharmony_ci	if (!controller->mmio) {
3008c2ecf20Sopenharmony_ci		pr_warn("ioremap cs553x NAND @0x%08lx failed\n", adr);
3018c2ecf20Sopenharmony_ci		err = -EIO;
3028c2ecf20Sopenharmony_ci		goto out_mtd;
3038c2ecf20Sopenharmony_ci	}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	/* Enable the following for a flash based bad block table */
3068c2ecf20Sopenharmony_ci	this->bbt_options = NAND_BBT_USE_FLASH;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	new_mtd->name = kasprintf(GFP_KERNEL, "cs553x_nand_cs%d", cs);
3098c2ecf20Sopenharmony_ci	if (!new_mtd->name) {
3108c2ecf20Sopenharmony_ci		err = -ENOMEM;
3118c2ecf20Sopenharmony_ci		goto out_ior;
3128c2ecf20Sopenharmony_ci	}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	/* Scan to find existence of the device */
3158c2ecf20Sopenharmony_ci	err = nand_scan(this, 1);
3168c2ecf20Sopenharmony_ci	if (err)
3178c2ecf20Sopenharmony_ci		goto out_free;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	controllers[cs] = controller;
3208c2ecf20Sopenharmony_ci	goto out;
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ciout_free:
3238c2ecf20Sopenharmony_ci	kfree(new_mtd->name);
3248c2ecf20Sopenharmony_ciout_ior:
3258c2ecf20Sopenharmony_ci	iounmap(controller->mmio);
3268c2ecf20Sopenharmony_ciout_mtd:
3278c2ecf20Sopenharmony_ci	kfree(controller);
3288c2ecf20Sopenharmony_ciout:
3298c2ecf20Sopenharmony_ci	return err;
3308c2ecf20Sopenharmony_ci}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_cistatic int is_geode(void)
3338c2ecf20Sopenharmony_ci{
3348c2ecf20Sopenharmony_ci	/* These are the CPUs which will have a CS553[56] companion chip */
3358c2ecf20Sopenharmony_ci	if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
3368c2ecf20Sopenharmony_ci	    boot_cpu_data.x86 == 5 &&
3378c2ecf20Sopenharmony_ci	    boot_cpu_data.x86_model == 10)
3388c2ecf20Sopenharmony_ci		return 1; /* Geode LX */
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	if ((boot_cpu_data.x86_vendor == X86_VENDOR_NSC ||
3418c2ecf20Sopenharmony_ci	     boot_cpu_data.x86_vendor == X86_VENDOR_CYRIX) &&
3428c2ecf20Sopenharmony_ci	    boot_cpu_data.x86 == 5 &&
3438c2ecf20Sopenharmony_ci	    boot_cpu_data.x86_model == 5)
3448c2ecf20Sopenharmony_ci		return 1; /* Geode GX (née GX2) */
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	return 0;
3478c2ecf20Sopenharmony_ci}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_cistatic int __init cs553x_init(void)
3508c2ecf20Sopenharmony_ci{
3518c2ecf20Sopenharmony_ci	int err = -ENXIO;
3528c2ecf20Sopenharmony_ci	int i;
3538c2ecf20Sopenharmony_ci	uint64_t val;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	/* If the CPU isn't a Geode GX or LX, abort */
3568c2ecf20Sopenharmony_ci	if (!is_geode())
3578c2ecf20Sopenharmony_ci		return -ENXIO;
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	/* If it doesn't have the CS553[56], abort */
3608c2ecf20Sopenharmony_ci	rdmsrl(MSR_DIVIL_GLD_CAP, val);
3618c2ecf20Sopenharmony_ci	val &= ~0xFFULL;
3628c2ecf20Sopenharmony_ci	if (val != CAP_CS5535 && val != CAP_CS5536)
3638c2ecf20Sopenharmony_ci		return -ENXIO;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	/* If it doesn't have the NAND controller enabled, abort */
3668c2ecf20Sopenharmony_ci	rdmsrl(MSR_DIVIL_BALL_OPTS, val);
3678c2ecf20Sopenharmony_ci	if (val & PIN_OPT_IDE) {
3688c2ecf20Sopenharmony_ci		pr_info("CS553x NAND controller: Flash I/O not enabled in MSR_DIVIL_BALL_OPTS.\n");
3698c2ecf20Sopenharmony_ci		return -ENXIO;
3708c2ecf20Sopenharmony_ci	}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	for (i = 0; i < NR_CS553X_CONTROLLERS; i++) {
3738c2ecf20Sopenharmony_ci		rdmsrl(MSR_DIVIL_LBAR_FLSH0 + i, val);
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci		if ((val & (FLSH_LBAR_EN|FLSH_NOR_NAND)) == (FLSH_LBAR_EN|FLSH_NOR_NAND))
3768c2ecf20Sopenharmony_ci			err = cs553x_init_one(i, !!(val & FLSH_MEM_IO), val & 0xFFFFFFFF);
3778c2ecf20Sopenharmony_ci	}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	/* Register all devices together here. This means we can easily hack it to
3808c2ecf20Sopenharmony_ci	   do mtdconcat etc. if we want to. */
3818c2ecf20Sopenharmony_ci	for (i = 0; i < NR_CS553X_CONTROLLERS; i++) {
3828c2ecf20Sopenharmony_ci		if (controllers[i]) {
3838c2ecf20Sopenharmony_ci			/* If any devices registered, return success. Else the last error. */
3848c2ecf20Sopenharmony_ci			mtd_device_register(nand_to_mtd(&controllers[i]->chip),
3858c2ecf20Sopenharmony_ci					    NULL, 0);
3868c2ecf20Sopenharmony_ci			err = 0;
3878c2ecf20Sopenharmony_ci		}
3888c2ecf20Sopenharmony_ci	}
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	return err;
3918c2ecf20Sopenharmony_ci}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_cimodule_init(cs553x_init);
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_cistatic void __exit cs553x_cleanup(void)
3968c2ecf20Sopenharmony_ci{
3978c2ecf20Sopenharmony_ci	int i;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	for (i = 0; i < NR_CS553X_CONTROLLERS; i++) {
4008c2ecf20Sopenharmony_ci		struct cs553x_nand_controller *controller = controllers[i];
4018c2ecf20Sopenharmony_ci		struct nand_chip *this = &controller->chip;
4028c2ecf20Sopenharmony_ci		struct mtd_info *mtd = nand_to_mtd(this);
4038c2ecf20Sopenharmony_ci		int ret;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci		if (!mtd)
4068c2ecf20Sopenharmony_ci			continue;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci		/* Release resources, unregister device */
4098c2ecf20Sopenharmony_ci		ret = mtd_device_unregister(mtd);
4108c2ecf20Sopenharmony_ci		WARN_ON(ret);
4118c2ecf20Sopenharmony_ci		nand_cleanup(this);
4128c2ecf20Sopenharmony_ci		kfree(mtd->name);
4138c2ecf20Sopenharmony_ci		controllers[i] = NULL;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci		/* unmap physical address */
4168c2ecf20Sopenharmony_ci		iounmap(controller->mmio);
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci		/* Free the MTD device structure */
4198c2ecf20Sopenharmony_ci		kfree(controller);
4208c2ecf20Sopenharmony_ci	}
4218c2ecf20Sopenharmony_ci}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_cimodule_exit(cs553x_cleanup);
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
4268c2ecf20Sopenharmony_ciMODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
4278c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("NAND controller driver for AMD CS5535/CS5536 companion chip");
428