Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 2 | /* |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 3 | * Copyright (C) 2006 Compulab, Ltd. |
| 4 | * Mike Rapoport <mike@compulab.co.il> |
| 5 | * |
Boris Brezillon | 187c5448 | 2018-02-05 23:02:02 +0100 | [diff] [blame] | 6 | * Derived from drivers/mtd/nand/h1910.c (removed in v3.10) |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 7 | * Copyright (C) 2002 Marius Gröger (mag@sysgo.de) |
| 8 | * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de) |
| 9 | * |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 10 | * Overview: |
| 11 | * This is a device driver for the NAND flash device found on the |
| 12 | * CM-X270 board. |
| 13 | */ |
| 14 | |
Boris Brezillon | d4092d7 | 2017-08-04 17:29:10 +0200 | [diff] [blame] | 15 | #include <linux/mtd/rawnand.h> |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 16 | #include <linux/mtd/partitions.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 18 | #include <linux/gpio.h> |
Paul Gortmaker | a0e5cc5 | 2011-07-03 15:17:31 -0400 | [diff] [blame] | 19 | #include <linux/module.h> |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 20 | |
| 21 | #include <asm/io.h> |
| 22 | #include <asm/irq.h> |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 23 | #include <asm/mach-types.h> |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 24 | |
Eric Miao | b74d196 | 2009-01-20 10:31:55 +0800 | [diff] [blame] | 25 | #include <mach/pxa2xx-regs.h> |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 26 | |
| 27 | #define GPIO_NAND_CS (11) |
| 28 | #define GPIO_NAND_RB (89) |
| 29 | |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 30 | /* MTD structure for CM-X270 board */ |
| 31 | static struct mtd_info *cmx270_nand_mtd; |
| 32 | |
| 33 | /* remaped IO address of the device */ |
| 34 | static void __iomem *cmx270_nand_io; |
| 35 | |
| 36 | /* |
| 37 | * Define static partitions for flash device |
| 38 | */ |
Arvind Yadav | d490668 | 2017-08-28 13:54:57 +0530 | [diff] [blame] | 39 | static const struct mtd_partition partition_info[] = { |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 40 | [0] = { |
| 41 | .name = "cmx270-0", |
| 42 | .offset = 0, |
| 43 | .size = MTDPART_SIZ_FULL |
| 44 | } |
| 45 | }; |
| 46 | #define NUM_PARTITIONS (ARRAY_SIZE(partition_info)) |
| 47 | |
Boris Brezillon | 7e53432 | 2018-09-06 14:05:22 +0200 | [diff] [blame] | 48 | static u_char cmx270_read_byte(struct nand_chip *this) |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 49 | { |
Boris Brezillon | 82fc509 | 2018-09-07 00:38:34 +0200 | [diff] [blame] | 50 | return (readl(this->legacy.IO_ADDR_R) >> 16); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 51 | } |
| 52 | |
Boris Brezillon | c0739d8 | 2018-09-06 14:05:23 +0200 | [diff] [blame] | 53 | static void cmx270_write_buf(struct nand_chip *this, const u_char *buf, |
| 54 | int len) |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 55 | { |
| 56 | int i; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 57 | |
| 58 | for (i=0; i<len; i++) |
Boris Brezillon | 82fc509 | 2018-09-07 00:38:34 +0200 | [diff] [blame] | 59 | writel((*buf++ << 16), this->legacy.IO_ADDR_W); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 60 | } |
| 61 | |
Boris Brezillon | 7e53432 | 2018-09-06 14:05:22 +0200 | [diff] [blame] | 62 | static void cmx270_read_buf(struct nand_chip *this, u_char *buf, int len) |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 63 | { |
| 64 | int i; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 65 | |
| 66 | for (i=0; i<len; i++) |
Boris Brezillon | 82fc509 | 2018-09-07 00:38:34 +0200 | [diff] [blame] | 67 | *buf++ = readl(this->legacy.IO_ADDR_R) >> 16; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 68 | } |
| 69 | |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 70 | static inline void nand_cs_on(void) |
| 71 | { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 72 | gpio_set_value(GPIO_NAND_CS, 0); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static void nand_cs_off(void) |
| 76 | { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 77 | dsb(); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 78 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 79 | gpio_set_value(GPIO_NAND_CS, 1); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* |
| 83 | * hardware specific access to control-lines |
| 84 | */ |
Boris Brezillon | 0f808c1 | 2018-09-06 14:05:26 +0200 | [diff] [blame] | 85 | static void cmx270_hwcontrol(struct nand_chip *this, int dat, |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 86 | unsigned int ctrl) |
| 87 | { |
Boris Brezillon | 82fc509 | 2018-09-07 00:38:34 +0200 | [diff] [blame] | 88 | unsigned int nandaddr = (unsigned int)this->legacy.IO_ADDR_W; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 89 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 90 | dsb(); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 91 | |
| 92 | if (ctrl & NAND_CTRL_CHANGE) { |
| 93 | if ( ctrl & NAND_ALE ) |
| 94 | nandaddr |= (1 << 3); |
| 95 | else |
| 96 | nandaddr &= ~(1 << 3); |
| 97 | if ( ctrl & NAND_CLE ) |
| 98 | nandaddr |= (1 << 2); |
| 99 | else |
| 100 | nandaddr &= ~(1 << 2); |
| 101 | if ( ctrl & NAND_NCE ) |
| 102 | nand_cs_on(); |
| 103 | else |
| 104 | nand_cs_off(); |
| 105 | } |
| 106 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 107 | dsb(); |
Boris Brezillon | 82fc509 | 2018-09-07 00:38:34 +0200 | [diff] [blame] | 108 | this->legacy.IO_ADDR_W = (void __iomem*)nandaddr; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 109 | if (dat != NAND_CMD_NONE) |
Boris Brezillon | 82fc509 | 2018-09-07 00:38:34 +0200 | [diff] [blame] | 110 | writel((dat << 16), this->legacy.IO_ADDR_W); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 111 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 112 | dsb(); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* |
| 116 | * read device ready pin |
| 117 | */ |
Boris Brezillon | 50a487e | 2018-09-06 14:05:27 +0200 | [diff] [blame] | 118 | static int cmx270_device_ready(struct nand_chip *this) |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 119 | { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 120 | dsb(); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 121 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 122 | return (gpio_get_value(GPIO_NAND_RB)); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Main initialization routine |
| 127 | */ |
Peter Huewe | 627df23 | 2009-06-11 02:23:33 +0200 | [diff] [blame] | 128 | static int __init cmx270_init(void) |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 129 | { |
| 130 | struct nand_chip *this; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 131 | int ret; |
| 132 | |
Mike Rapoport | a7f3f03 | 2008-10-05 10:26:55 +0100 | [diff] [blame] | 133 | if (!(machine_is_armcore() && cpu_is_pxa27x())) |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 134 | return -ENODEV; |
| 135 | |
| 136 | ret = gpio_request(GPIO_NAND_CS, "NAND CS"); |
| 137 | if (ret) { |
Joe Perches | e8348dc | 2017-02-16 23:11:37 -0800 | [diff] [blame] | 138 | pr_warn("CM-X270: failed to request NAND CS gpio\n"); |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | gpio_direction_output(GPIO_NAND_CS, 1); |
| 143 | |
| 144 | ret = gpio_request(GPIO_NAND_RB, "NAND R/B"); |
| 145 | if (ret) { |
Joe Perches | e8348dc | 2017-02-16 23:11:37 -0800 | [diff] [blame] | 146 | pr_warn("CM-X270: failed to request NAND R/B gpio\n"); |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 147 | goto err_gpio_request; |
| 148 | } |
| 149 | |
| 150 | gpio_direction_input(GPIO_NAND_RB); |
| 151 | |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 152 | /* Allocate memory for MTD device structure and private data */ |
Boris BREZILLON | 2afd14f | 2015-12-10 08:59:56 +0100 | [diff] [blame] | 153 | this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL); |
| 154 | if (!this) { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 155 | ret = -ENOMEM; |
| 156 | goto err_kzalloc; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12); |
| 160 | if (!cmx270_nand_io) { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 161 | pr_debug("Unable to ioremap NAND device\n"); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 162 | ret = -EINVAL; |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 163 | goto err_ioremap; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 164 | } |
| 165 | |
Boris BREZILLON | 2afd14f | 2015-12-10 08:59:56 +0100 | [diff] [blame] | 166 | cmx270_nand_mtd = nand_to_mtd(this); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 167 | |
| 168 | /* Link the private data with the MTD structure */ |
| 169 | cmx270_nand_mtd->owner = THIS_MODULE; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 170 | |
| 171 | /* insert callbacks */ |
Boris Brezillon | 82fc509 | 2018-09-07 00:38:34 +0200 | [diff] [blame] | 172 | this->legacy.IO_ADDR_R = cmx270_nand_io; |
| 173 | this->legacy.IO_ADDR_W = cmx270_nand_io; |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 174 | this->legacy.cmd_ctrl = cmx270_hwcontrol; |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 175 | this->legacy.dev_ready = cmx270_device_ready; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 176 | |
| 177 | /* 15 us command delay time */ |
Boris Brezillon | 3cece3a | 2018-09-07 00:38:41 +0200 | [diff] [blame] | 178 | this->legacy.chip_delay = 20; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 179 | this->ecc.mode = NAND_ECC_SOFT; |
Rafał Miłecki | d9944e1 | 2016-04-13 14:06:59 +0200 | [diff] [blame] | 180 | this->ecc.algo = NAND_ECC_HAMMING; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 181 | |
| 182 | /* read/write functions */ |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 183 | this->legacy.read_byte = cmx270_read_byte; |
| 184 | this->legacy.read_buf = cmx270_read_buf; |
| 185 | this->legacy.write_buf = cmx270_write_buf; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 186 | |
| 187 | /* Scan to find existence of the device */ |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 188 | ret = nand_scan(this, 1); |
Masahiro Yamada | 546fe03 | 2016-11-04 19:42:50 +0900 | [diff] [blame] | 189 | if (ret) { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 190 | pr_notice("No NAND device\n"); |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 191 | goto err_scan; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 192 | } |
| 193 | |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 194 | /* Register the partitions */ |
Rafał Miłecki | 29597ca | 2018-07-13 11:27:31 +0200 | [diff] [blame] | 195 | ret = mtd_device_register(cmx270_nand_mtd, partition_info, |
| 196 | NUM_PARTITIONS); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 197 | if (ret) |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 198 | goto err_scan; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 199 | |
| 200 | /* Return happy */ |
| 201 | return 0; |
| 202 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 203 | err_scan: |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 204 | iounmap(cmx270_nand_io); |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 205 | err_ioremap: |
Boris BREZILLON | 2afd14f | 2015-12-10 08:59:56 +0100 | [diff] [blame] | 206 | kfree(this); |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 207 | err_kzalloc: |
| 208 | gpio_free(GPIO_NAND_RB); |
| 209 | err_gpio_request: |
| 210 | gpio_free(GPIO_NAND_CS); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 211 | |
| 212 | return ret; |
| 213 | |
| 214 | } |
| 215 | module_init(cmx270_init); |
| 216 | |
| 217 | /* |
| 218 | * Clean up routine |
| 219 | */ |
Peter Huewe | 627df23 | 2009-06-11 02:23:33 +0200 | [diff] [blame] | 220 | static void __exit cmx270_cleanup(void) |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 221 | { |
| 222 | /* Release resources, unregister device */ |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 223 | nand_release(mtd_to_nand(cmx270_nand_mtd)); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 224 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 225 | gpio_free(GPIO_NAND_RB); |
| 226 | gpio_free(GPIO_NAND_CS); |
| 227 | |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 228 | iounmap(cmx270_nand_io); |
| 229 | |
Boris BREZILLON | 2afd14f | 2015-12-10 08:59:56 +0100 | [diff] [blame] | 230 | kfree(mtd_to_nand(cmx270_nand_mtd)); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 231 | } |
| 232 | module_exit(cmx270_cleanup); |
| 233 | |
| 234 | MODULE_LICENSE("GPL"); |
| 235 | MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>"); |
| 236 | MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module"); |