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> |
Paul Gortmaker | a0e5cc5 | 2011-07-03 15:17:31 -0400 | [diff] [blame] | 25 | #include <linux/module.h> |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 26 | |
| 27 | #include <asm/io.h> |
| 28 | #include <asm/irq.h> |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 29 | #include <asm/mach-types.h> |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 30 | |
Eric Miao | b74d196 | 2009-01-20 10:31:55 +0800 | [diff] [blame] | 31 | #include <mach/pxa2xx-regs.h> |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 32 | |
| 33 | #define GPIO_NAND_CS (11) |
| 34 | #define GPIO_NAND_RB (89) |
| 35 | |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 36 | /* MTD structure for CM-X270 board */ |
| 37 | static struct mtd_info *cmx270_nand_mtd; |
| 38 | |
| 39 | /* remaped IO address of the device */ |
| 40 | static void __iomem *cmx270_nand_io; |
| 41 | |
| 42 | /* |
| 43 | * Define static partitions for flash device |
| 44 | */ |
| 45 | static struct mtd_partition partition_info[] = { |
| 46 | [0] = { |
| 47 | .name = "cmx270-0", |
| 48 | .offset = 0, |
| 49 | .size = MTDPART_SIZ_FULL |
| 50 | } |
| 51 | }; |
| 52 | #define NUM_PARTITIONS (ARRAY_SIZE(partition_info)) |
| 53 | |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 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 | |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 79 | static inline void nand_cs_on(void) |
| 80 | { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 81 | gpio_set_value(GPIO_NAND_CS, 0); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static void nand_cs_off(void) |
| 85 | { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 86 | dsb(); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 87 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 88 | gpio_set_value(GPIO_NAND_CS, 1); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
| 92 | * hardware specific access to control-lines |
| 93 | */ |
| 94 | static void cmx270_hwcontrol(struct mtd_info *mtd, int dat, |
| 95 | unsigned int ctrl) |
| 96 | { |
| 97 | struct nand_chip* this = mtd->priv; |
| 98 | unsigned int nandaddr = (unsigned int)this->IO_ADDR_W; |
| 99 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 100 | dsb(); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 101 | |
| 102 | if (ctrl & NAND_CTRL_CHANGE) { |
| 103 | if ( ctrl & NAND_ALE ) |
| 104 | nandaddr |= (1 << 3); |
| 105 | else |
| 106 | nandaddr &= ~(1 << 3); |
| 107 | if ( ctrl & NAND_CLE ) |
| 108 | nandaddr |= (1 << 2); |
| 109 | else |
| 110 | nandaddr &= ~(1 << 2); |
| 111 | if ( ctrl & NAND_NCE ) |
| 112 | nand_cs_on(); |
| 113 | else |
| 114 | nand_cs_off(); |
| 115 | } |
| 116 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 117 | dsb(); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 118 | this->IO_ADDR_W = (void __iomem*)nandaddr; |
| 119 | if (dat != NAND_CMD_NONE) |
| 120 | writel((dat << 16), this->IO_ADDR_W); |
| 121 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 122 | dsb(); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* |
| 126 | * read device ready pin |
| 127 | */ |
| 128 | static int cmx270_device_ready(struct mtd_info *mtd) |
| 129 | { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 130 | dsb(); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 131 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 132 | return (gpio_get_value(GPIO_NAND_RB)); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Main initialization routine |
| 137 | */ |
Peter Huewe | 627df23 | 2009-06-11 02:23:33 +0200 | [diff] [blame] | 138 | static int __init cmx270_init(void) |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 139 | { |
| 140 | struct nand_chip *this; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 141 | int ret; |
| 142 | |
Mike Rapoport | a7f3f03 | 2008-10-05 10:26:55 +0100 | [diff] [blame] | 143 | if (!(machine_is_armcore() && cpu_is_pxa27x())) |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 144 | return -ENODEV; |
| 145 | |
| 146 | ret = gpio_request(GPIO_NAND_CS, "NAND CS"); |
| 147 | if (ret) { |
| 148 | pr_warning("CM-X270: failed to request NAND CS gpio\n"); |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | gpio_direction_output(GPIO_NAND_CS, 1); |
| 153 | |
| 154 | ret = gpio_request(GPIO_NAND_RB, "NAND R/B"); |
| 155 | if (ret) { |
| 156 | pr_warning("CM-X270: failed to request NAND R/B gpio\n"); |
| 157 | goto err_gpio_request; |
| 158 | } |
| 159 | |
| 160 | gpio_direction_input(GPIO_NAND_RB); |
| 161 | |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 162 | /* Allocate memory for MTD device structure and private data */ |
| 163 | cmx270_nand_mtd = kzalloc(sizeof(struct mtd_info) + |
| 164 | sizeof(struct nand_chip), |
| 165 | GFP_KERNEL); |
| 166 | if (!cmx270_nand_mtd) { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 167 | pr_debug("Unable to allocate CM-X270 NAND MTD device structure.\n"); |
| 168 | ret = -ENOMEM; |
| 169 | goto err_kzalloc; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12); |
| 173 | if (!cmx270_nand_io) { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 174 | pr_debug("Unable to ioremap NAND device\n"); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 175 | ret = -EINVAL; |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 176 | goto err_ioremap; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* Get pointer to private data */ |
| 180 | this = (struct nand_chip *)(&cmx270_nand_mtd[1]); |
| 181 | |
| 182 | /* Link the private data with the MTD structure */ |
| 183 | cmx270_nand_mtd->owner = THIS_MODULE; |
| 184 | cmx270_nand_mtd->priv = this; |
| 185 | |
| 186 | /* insert callbacks */ |
| 187 | this->IO_ADDR_R = cmx270_nand_io; |
| 188 | this->IO_ADDR_W = cmx270_nand_io; |
| 189 | this->cmd_ctrl = cmx270_hwcontrol; |
| 190 | this->dev_ready = cmx270_device_ready; |
| 191 | |
| 192 | /* 15 us command delay time */ |
| 193 | this->chip_delay = 20; |
| 194 | this->ecc.mode = NAND_ECC_SOFT; |
| 195 | |
| 196 | /* read/write functions */ |
| 197 | this->read_byte = cmx270_read_byte; |
| 198 | this->read_buf = cmx270_read_buf; |
| 199 | this->write_buf = cmx270_write_buf; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 200 | |
| 201 | /* Scan to find existence of the device */ |
| 202 | if (nand_scan (cmx270_nand_mtd, 1)) { |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 203 | pr_notice("No NAND device\n"); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 204 | ret = -ENXIO; |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 205 | goto err_scan; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 206 | } |
| 207 | |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 208 | /* Register the partitions */ |
Artem Bityutskiy | 42d7fbe | 2012-03-09 19:24:26 +0200 | [diff] [blame] | 209 | ret = mtd_device_parse_register(cmx270_nand_mtd, NULL, NULL, |
Dmitry Eremin-Solenikov | 0b118f0 | 2011-06-02 18:00:30 +0400 | [diff] [blame] | 210 | partition_info, NUM_PARTITIONS); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 211 | if (ret) |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 212 | goto err_scan; |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 213 | |
| 214 | /* Return happy */ |
| 215 | return 0; |
| 216 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 217 | err_scan: |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 218 | iounmap(cmx270_nand_io); |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 219 | err_ioremap: |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 220 | kfree(cmx270_nand_mtd); |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 221 | err_kzalloc: |
| 222 | gpio_free(GPIO_NAND_RB); |
| 223 | err_gpio_request: |
| 224 | gpio_free(GPIO_NAND_CS); |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 225 | |
| 226 | return ret; |
| 227 | |
| 228 | } |
| 229 | module_init(cmx270_init); |
| 230 | |
| 231 | /* |
| 232 | * Clean up routine |
| 233 | */ |
Peter Huewe | 627df23 | 2009-06-11 02:23:33 +0200 | [diff] [blame] | 234 | static void __exit cmx270_cleanup(void) |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 235 | { |
| 236 | /* Release resources, unregister device */ |
| 237 | nand_release(cmx270_nand_mtd); |
| 238 | |
Mike Rapoport | 70eb33d | 2008-06-17 09:48:46 +0100 | [diff] [blame] | 239 | gpio_free(GPIO_NAND_RB); |
| 240 | gpio_free(GPIO_NAND_CS); |
| 241 | |
Mike Rapoport | 54d33c4 | 2007-04-22 08:53:21 +0300 | [diff] [blame] | 242 | iounmap(cmx270_nand_io); |
| 243 | |
| 244 | /* Free the MTD device structure */ |
| 245 | kfree (cmx270_nand_mtd); |
| 246 | } |
| 247 | module_exit(cmx270_cleanup); |
| 248 | |
| 249 | MODULE_LICENSE("GPL"); |
| 250 | MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>"); |
| 251 | MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module"); |