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