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