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> |
| 23 | |
| 24 | #include <asm/io.h> |
| 25 | #include <asm/irq.h> |
| 26 | |
| 27 | #include <asm/arch/hardware.h> |
| 28 | #include <asm/arch/pxa-regs.h> |
| 29 | |
| 30 | #define GPIO_NAND_CS (11) |
| 31 | #define GPIO_NAND_RB (89) |
| 32 | |
| 33 | /* This macro needed to ensure in-order operation of GPIO and local |
| 34 | * bus. Without both asm command and dummy uncached read there're |
| 35 | * states when NAND access is broken. I've looked for such macro(s) in |
| 36 | * include/asm-arm but found nothing approptiate. |
| 37 | * dmac_clean_range is close, but is makes cache invalidation |
| 38 | * unnecessary here and it cannot be used in module |
| 39 | */ |
| 40 | #define DRAIN_WB() \ |
| 41 | do { \ |
| 42 | unsigned char dummy; \ |
| 43 | asm volatile ("mcr p15, 0, r0, c7, c10, 4":::"r0"); \ |
| 44 | dummy=*((unsigned char*)UNCACHED_ADDR); \ |
| 45 | } while(0) |
| 46 | |
| 47 | /* MTD structure for CM-X270 board */ |
| 48 | static struct mtd_info *cmx270_nand_mtd; |
| 49 | |
| 50 | /* remaped IO address of the device */ |
| 51 | static void __iomem *cmx270_nand_io; |
| 52 | |
| 53 | /* |
| 54 | * Define static partitions for flash device |
| 55 | */ |
| 56 | static struct mtd_partition partition_info[] = { |
| 57 | [0] = { |
| 58 | .name = "cmx270-0", |
| 59 | .offset = 0, |
| 60 | .size = MTDPART_SIZ_FULL |
| 61 | } |
| 62 | }; |
| 63 | #define NUM_PARTITIONS (ARRAY_SIZE(partition_info)) |
| 64 | |
| 65 | const char *part_probes[] = { "cmdlinepart", NULL }; |
| 66 | |
| 67 | static u_char cmx270_read_byte(struct mtd_info *mtd) |
| 68 | { |
| 69 | struct nand_chip *this = mtd->priv; |
| 70 | |
| 71 | return (readl(this->IO_ADDR_R) >> 16); |
| 72 | } |
| 73 | |
| 74 | static void cmx270_write_buf(struct mtd_info *mtd, const u_char *buf, int len) |
| 75 | { |
| 76 | int i; |
| 77 | struct nand_chip *this = mtd->priv; |
| 78 | |
| 79 | for (i=0; i<len; i++) |
| 80 | writel((*buf++ << 16), this->IO_ADDR_W); |
| 81 | } |
| 82 | |
| 83 | static void cmx270_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 84 | { |
| 85 | int i; |
| 86 | struct nand_chip *this = mtd->priv; |
| 87 | |
| 88 | for (i=0; i<len; i++) |
| 89 | *buf++ = readl(this->IO_ADDR_R) >> 16; |
| 90 | } |
| 91 | |
| 92 | static int cmx270_verify_buf(struct mtd_info *mtd, const u_char *buf, int len) |
| 93 | { |
| 94 | int i; |
| 95 | struct nand_chip *this = mtd->priv; |
| 96 | |
| 97 | for (i=0; i<len; i++) |
| 98 | if (buf[i] != (u_char)(readl(this->IO_ADDR_R) >> 16)) |
| 99 | return -EFAULT; |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static inline void nand_cs_on(void) |
| 105 | { |
| 106 | GPCR(GPIO_NAND_CS) = GPIO_bit(GPIO_NAND_CS); |
| 107 | } |
| 108 | |
| 109 | static void nand_cs_off(void) |
| 110 | { |
| 111 | DRAIN_WB(); |
| 112 | |
| 113 | GPSR(GPIO_NAND_CS) = GPIO_bit(GPIO_NAND_CS); |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * hardware specific access to control-lines |
| 118 | */ |
| 119 | static void cmx270_hwcontrol(struct mtd_info *mtd, int dat, |
| 120 | unsigned int ctrl) |
| 121 | { |
| 122 | struct nand_chip* this = mtd->priv; |
| 123 | unsigned int nandaddr = (unsigned int)this->IO_ADDR_W; |
| 124 | |
| 125 | DRAIN_WB(); |
| 126 | |
| 127 | if (ctrl & NAND_CTRL_CHANGE) { |
| 128 | if ( ctrl & NAND_ALE ) |
| 129 | nandaddr |= (1 << 3); |
| 130 | else |
| 131 | nandaddr &= ~(1 << 3); |
| 132 | if ( ctrl & NAND_CLE ) |
| 133 | nandaddr |= (1 << 2); |
| 134 | else |
| 135 | nandaddr &= ~(1 << 2); |
| 136 | if ( ctrl & NAND_NCE ) |
| 137 | nand_cs_on(); |
| 138 | else |
| 139 | nand_cs_off(); |
| 140 | } |
| 141 | |
| 142 | DRAIN_WB(); |
| 143 | this->IO_ADDR_W = (void __iomem*)nandaddr; |
| 144 | if (dat != NAND_CMD_NONE) |
| 145 | writel((dat << 16), this->IO_ADDR_W); |
| 146 | |
| 147 | DRAIN_WB(); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * read device ready pin |
| 152 | */ |
| 153 | static int cmx270_device_ready(struct mtd_info *mtd) |
| 154 | { |
| 155 | DRAIN_WB(); |
| 156 | |
| 157 | return (GPLR(GPIO_NAND_RB) & GPIO_bit(GPIO_NAND_RB)); |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Main initialization routine |
| 162 | */ |
| 163 | static int cmx270_init(void) |
| 164 | { |
| 165 | struct nand_chip *this; |
| 166 | const char *part_type; |
| 167 | struct mtd_partition *mtd_parts; |
| 168 | int mtd_parts_nb = 0; |
| 169 | int ret; |
| 170 | |
| 171 | /* Allocate memory for MTD device structure and private data */ |
| 172 | cmx270_nand_mtd = kzalloc(sizeof(struct mtd_info) + |
| 173 | sizeof(struct nand_chip), |
| 174 | GFP_KERNEL); |
| 175 | if (!cmx270_nand_mtd) { |
| 176 | printk("Unable to allocate CM-X270 NAND MTD device structure.\n"); |
| 177 | return -ENOMEM; |
| 178 | } |
| 179 | |
| 180 | cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12); |
| 181 | if (!cmx270_nand_io) { |
| 182 | printk("Unable to ioremap NAND device\n"); |
| 183 | ret = -EINVAL; |
| 184 | goto err1; |
| 185 | } |
| 186 | |
| 187 | /* Get pointer to private data */ |
| 188 | this = (struct nand_chip *)(&cmx270_nand_mtd[1]); |
| 189 | |
| 190 | /* Link the private data with the MTD structure */ |
| 191 | cmx270_nand_mtd->owner = THIS_MODULE; |
| 192 | cmx270_nand_mtd->priv = this; |
| 193 | |
| 194 | /* insert callbacks */ |
| 195 | this->IO_ADDR_R = cmx270_nand_io; |
| 196 | this->IO_ADDR_W = cmx270_nand_io; |
| 197 | this->cmd_ctrl = cmx270_hwcontrol; |
| 198 | this->dev_ready = cmx270_device_ready; |
| 199 | |
| 200 | /* 15 us command delay time */ |
| 201 | this->chip_delay = 20; |
| 202 | this->ecc.mode = NAND_ECC_SOFT; |
| 203 | |
| 204 | /* read/write functions */ |
| 205 | this->read_byte = cmx270_read_byte; |
| 206 | this->read_buf = cmx270_read_buf; |
| 207 | this->write_buf = cmx270_write_buf; |
| 208 | this->verify_buf = cmx270_verify_buf; |
| 209 | |
| 210 | /* Scan to find existence of the device */ |
| 211 | if (nand_scan (cmx270_nand_mtd, 1)) { |
| 212 | printk(KERN_NOTICE "No NAND device\n"); |
| 213 | ret = -ENXIO; |
| 214 | goto err2; |
| 215 | } |
| 216 | |
| 217 | #ifdef CONFIG_MTD_CMDLINE_PARTS |
| 218 | mtd_parts_nb = parse_mtd_partitions(cmx270_nand_mtd, part_probes, |
| 219 | &mtd_parts, 0); |
| 220 | if (mtd_parts_nb > 0) |
| 221 | part_type = "command line"; |
| 222 | else |
| 223 | mtd_parts_nb = 0; |
| 224 | #endif |
| 225 | if (!mtd_parts_nb) { |
| 226 | mtd_parts = partition_info; |
| 227 | mtd_parts_nb = NUM_PARTITIONS; |
| 228 | part_type = "static"; |
| 229 | } |
| 230 | |
| 231 | /* Register the partitions */ |
| 232 | printk(KERN_NOTICE "Using %s partition definition\n", part_type); |
| 233 | ret = add_mtd_partitions(cmx270_nand_mtd, mtd_parts, mtd_parts_nb); |
| 234 | if (ret) |
| 235 | goto err2; |
| 236 | |
| 237 | /* Return happy */ |
| 238 | return 0; |
| 239 | |
| 240 | err2: |
| 241 | iounmap(cmx270_nand_io); |
| 242 | err1: |
| 243 | kfree(cmx270_nand_mtd); |
| 244 | |
| 245 | return ret; |
| 246 | |
| 247 | } |
| 248 | module_init(cmx270_init); |
| 249 | |
| 250 | /* |
| 251 | * Clean up routine |
| 252 | */ |
| 253 | static void cmx270_cleanup(void) |
| 254 | { |
| 255 | /* Release resources, unregister device */ |
| 256 | nand_release(cmx270_nand_mtd); |
| 257 | |
| 258 | iounmap(cmx270_nand_io); |
| 259 | |
| 260 | /* Free the MTD device structure */ |
| 261 | kfree (cmx270_nand_mtd); |
| 262 | } |
| 263 | module_exit(cmx270_cleanup); |
| 264 | |
| 265 | MODULE_LICENSE("GPL"); |
| 266 | MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>"); |
| 267 | MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module"); |