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> |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 23 | #include <linux/gpio.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 | */ |
| 43 | static struct mtd_partition partition_info[] = { |
| 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 | |
| 52 | const char *part_probes[] = { "cmdlinepart", NULL }; |
| 53 | |
| 54 | static u_char cmx270_read_byte(struct mtd_info *mtd) |
| 55 | { |
| 56 | struct nand_chip *this = mtd->priv; |
| 57 | |
| 58 | return (readl(this->IO_ADDR_R) >> 16); |
| 59 | } |
| 60 | |
| 61 | static void cmx270_write_buf(struct mtd_info *mtd, const u_char *buf, int len) |
| 62 | { |
| 63 | int i; |
| 64 | struct nand_chip *this = mtd->priv; |
| 65 | |
| 66 | for (i=0; i<len; i++) |
| 67 | writel((*buf++ << 16), this->IO_ADDR_W); |
| 68 | } |
| 69 | |
| 70 | static void cmx270_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 71 | { |
| 72 | int i; |
| 73 | struct nand_chip *this = mtd->priv; |
| 74 | |
| 75 | for (i=0; i<len; i++) |
| 76 | *buf++ = readl(this->IO_ADDR_R) >> 16; |
| 77 | } |
| 78 | |
| 79 | static int cmx270_verify_buf(struct mtd_info *mtd, const u_char *buf, int len) |
| 80 | { |
| 81 | int i; |
| 82 | struct nand_chip *this = mtd->priv; |
| 83 | |
| 84 | for (i=0; i<len; i++) |
| 85 | if (buf[i] != (u_char)(readl(this->IO_ADDR_R) >> 16)) |
| 86 | return -EFAULT; |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static inline void nand_cs_on(void) |
| 92 | { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 93 | gpio_set_value(GPIO_NAND_CS, 0); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static void nand_cs_off(void) |
| 97 | { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 98 | dsb(); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 99 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 100 | gpio_set_value(GPIO_NAND_CS, 1); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
| 104 | * hardware specific access to control-lines |
| 105 | */ |
| 106 | static void cmx270_hwcontrol(struct mtd_info *mtd, int dat, |
| 107 | unsigned int ctrl) |
| 108 | { |
| 109 | struct nand_chip* this = mtd->priv; |
| 110 | unsigned int nandaddr = (unsigned int)this->IO_ADDR_W; |
| 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 | if (ctrl & NAND_CTRL_CHANGE) { |
| 115 | if ( ctrl & NAND_ALE ) |
| 116 | nandaddr |= (1 << 3); |
| 117 | else |
| 118 | nandaddr &= ~(1 << 3); |
| 119 | if ( ctrl & NAND_CLE ) |
| 120 | nandaddr |= (1 << 2); |
| 121 | else |
| 122 | nandaddr &= ~(1 << 2); |
| 123 | if ( ctrl & NAND_NCE ) |
| 124 | nand_cs_on(); |
| 125 | else |
| 126 | nand_cs_off(); |
| 127 | } |
| 128 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 129 | dsb(); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 130 | this->IO_ADDR_W = (void __iomem*)nandaddr; |
| 131 | if (dat != NAND_CMD_NONE) |
| 132 | writel((dat << 16), this->IO_ADDR_W); |
| 133 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 134 | dsb(); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* |
| 138 | * read device ready pin |
| 139 | */ |
| 140 | static int cmx270_device_ready(struct mtd_info *mtd) |
| 141 | { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 142 | dsb(); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 143 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 144 | return (gpio_get_value(GPIO_NAND_RB)); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Main initialization routine |
| 149 | */ |
Peter Huewe | 627df23 | 2009-06-11 02:23:33 +0200 | [diff] [blame^] | 150 | static int __init cmx270_init(void) |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 151 | { |
| 152 | struct nand_chip *this; |
| 153 | const char *part_type; |
| 154 | struct mtd_partition *mtd_parts; |
| 155 | int mtd_parts_nb = 0; |
| 156 | int ret; |
| 157 | |
Mike Rapoport | a7f3f03 | 2008-10-05 10:26:55 +0100 | [diff] [blame] | 158 | if (!(machine_is_armcore() && cpu_is_pxa27x())) |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 159 | return -ENODEV; |
| 160 | |
| 161 | ret = gpio_request(GPIO_NAND_CS, "NAND CS"); |
| 162 | if (ret) { |
| 163 | pr_warning("CM-X270: failed to request NAND CS gpio\n"); |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | gpio_direction_output(GPIO_NAND_CS, 1); |
| 168 | |
| 169 | ret = gpio_request(GPIO_NAND_RB, "NAND R/B"); |
| 170 | if (ret) { |
| 171 | pr_warning("CM-X270: failed to request NAND R/B gpio\n"); |
| 172 | goto err_gpio_request; |
| 173 | } |
| 174 | |
| 175 | gpio_direction_input(GPIO_NAND_RB); |
| 176 | |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 177 | /* Allocate memory for MTD device structure and private data */ |
| 178 | cmx270_nand_mtd = kzalloc(sizeof(struct mtd_info) + |
| 179 | sizeof(struct nand_chip), |
| 180 | GFP_KERNEL); |
| 181 | if (!cmx270_nand_mtd) { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 182 | pr_debug("Unable to allocate CM-X270 NAND MTD device structure.\n"); |
| 183 | ret = -ENOMEM; |
| 184 | goto err_kzalloc; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12); |
| 188 | if (!cmx270_nand_io) { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 189 | pr_debug("Unable to ioremap NAND device\n"); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 190 | ret = -EINVAL; |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 191 | goto err_ioremap; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* Get pointer to private data */ |
| 195 | this = (struct nand_chip *)(&cmx270_nand_mtd[1]); |
| 196 | |
| 197 | /* Link the private data with the MTD structure */ |
| 198 | cmx270_nand_mtd->owner = THIS_MODULE; |
| 199 | cmx270_nand_mtd->priv = this; |
| 200 | |
| 201 | /* insert callbacks */ |
| 202 | this->IO_ADDR_R = cmx270_nand_io; |
| 203 | this->IO_ADDR_W = cmx270_nand_io; |
| 204 | this->cmd_ctrl = cmx270_hwcontrol; |
| 205 | this->dev_ready = cmx270_device_ready; |
| 206 | |
| 207 | /* 15 us command delay time */ |
| 208 | this->chip_delay = 20; |
| 209 | this->ecc.mode = NAND_ECC_SOFT; |
| 210 | |
| 211 | /* read/write functions */ |
| 212 | this->read_byte = cmx270_read_byte; |
| 213 | this->read_buf = cmx270_read_buf; |
| 214 | this->write_buf = cmx270_write_buf; |
| 215 | this->verify_buf = cmx270_verify_buf; |
| 216 | |
| 217 | /* Scan to find existence of the device */ |
| 218 | if (nand_scan (cmx270_nand_mtd, 1)) { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 219 | pr_notice("No NAND device\n"); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 220 | ret = -ENXIO; |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 221 | goto err_scan; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | #ifdef CONFIG_MTD_CMDLINE_PARTS |
| 225 | mtd_parts_nb = parse_mtd_partitions(cmx270_nand_mtd, part_probes, |
| 226 | &mtd_parts, 0); |
| 227 | if (mtd_parts_nb > 0) |
| 228 | part_type = "command line"; |
| 229 | else |
| 230 | mtd_parts_nb = 0; |
| 231 | #endif |
| 232 | if (!mtd_parts_nb) { |
| 233 | mtd_parts = partition_info; |
| 234 | mtd_parts_nb = NUM_PARTITIONS; |
| 235 | part_type = "static"; |
| 236 | } |
| 237 | |
| 238 | /* Register the partitions */ |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 239 | pr_notice("Using %s partition definition\n", part_type); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 240 | ret = add_mtd_partitions(cmx270_nand_mtd, mtd_parts, mtd_parts_nb); |
| 241 | if (ret) |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 242 | goto err_scan; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 243 | |
| 244 | /* Return happy */ |
| 245 | return 0; |
| 246 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 247 | err_scan: |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 248 | iounmap(cmx270_nand_io); |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 249 | err_ioremap: |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 250 | kfree(cmx270_nand_mtd); |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 251 | err_kzalloc: |
| 252 | gpio_free(GPIO_NAND_RB); |
| 253 | err_gpio_request: |
| 254 | gpio_free(GPIO_NAND_CS); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 255 | |
| 256 | return ret; |
| 257 | |
| 258 | } |
| 259 | module_init(cmx270_init); |
| 260 | |
| 261 | /* |
| 262 | * Clean up routine |
| 263 | */ |
Peter Huewe | 627df23 | 2009-06-11 02:23:33 +0200 | [diff] [blame^] | 264 | static void __exit cmx270_cleanup(void) |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 265 | { |
| 266 | /* Release resources, unregister device */ |
| 267 | nand_release(cmx270_nand_mtd); |
| 268 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 269 | gpio_free(GPIO_NAND_RB); |
| 270 | gpio_free(GPIO_NAND_CS); |
| 271 | |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 272 | iounmap(cmx270_nand_io); |
| 273 | |
| 274 | /* Free the MTD device structure */ |
| 275 | kfree (cmx270_nand_mtd); |
| 276 | } |
| 277 | module_exit(cmx270_cleanup); |
| 278 | |
| 279 | MODULE_LICENSE("GPL"); |
| 280 | MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>"); |
| 281 | MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module"); |