Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * drivers/mtd/nand/ams-delta.c |
| 3 | * |
| 4 | * Copyright (C) 2006 Jonathan McDowell <noodles@earth.li> |
| 5 | * |
| 6 | * Derived from drivers/mtd/toto.c |
Janusz Krzysztofik | 7e95d1f | 2010-12-14 21:09:40 +0100 | [diff] [blame^] | 7 | * Converted to platform driver by Janusz Krzysztofik <jkrzyszt@tis.icnet.pl> |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * Overview: |
| 14 | * This is a device driver for the NAND flash device found on the |
| 15 | * Amstrad E3 (Delta). |
| 16 | */ |
| 17 | |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/mtd/mtd.h> |
| 23 | #include <linux/mtd/nand.h> |
| 24 | #include <linux/mtd/partitions.h> |
| 25 | #include <asm/io.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 26 | #include <mach/hardware.h> |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 27 | #include <asm/sizes.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 28 | #include <mach/gpio.h> |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 29 | #include <plat/board-ams-delta.h> |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * MTD structure for E3 (Delta) |
| 33 | */ |
| 34 | static struct mtd_info *ams_delta_mtd = NULL; |
| 35 | |
| 36 | #define NAND_MASK (AMS_DELTA_LATCH2_NAND_NRE | AMS_DELTA_LATCH2_NAND_NWE | AMS_DELTA_LATCH2_NAND_CLE | AMS_DELTA_LATCH2_NAND_ALE | AMS_DELTA_LATCH2_NAND_NCE | AMS_DELTA_LATCH2_NAND_NWP) |
| 37 | |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 38 | /* |
| 39 | * Define partitions for flash devices |
| 40 | */ |
| 41 | |
| 42 | static struct mtd_partition partition_info[] = { |
| 43 | { .name = "Kernel", |
| 44 | .offset = 0, |
| 45 | .size = 3 * SZ_1M + SZ_512K }, |
| 46 | { .name = "u-boot", |
| 47 | .offset = 3 * SZ_1M + SZ_512K, |
| 48 | .size = SZ_256K }, |
| 49 | { .name = "u-boot params", |
| 50 | .offset = 3 * SZ_1M + SZ_512K + SZ_256K, |
| 51 | .size = SZ_256K }, |
| 52 | { .name = "Amstrad LDR", |
| 53 | .offset = 4 * SZ_1M, |
| 54 | .size = SZ_256K }, |
| 55 | { .name = "File system", |
| 56 | .offset = 4 * SZ_1M + 1 * SZ_256K, |
| 57 | .size = 27 * SZ_1M }, |
| 58 | { .name = "PBL reserved", |
| 59 | .offset = 32 * SZ_1M - 3 * SZ_256K, |
| 60 | .size = 3 * SZ_256K }, |
| 61 | }; |
| 62 | |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 63 | static void ams_delta_write_byte(struct mtd_info *mtd, u_char byte) |
| 64 | { |
| 65 | struct nand_chip *this = mtd->priv; |
| 66 | |
Tony Lindgren | 6175556 | 2009-08-28 10:50:34 -0700 | [diff] [blame] | 67 | omap_writew(0, (OMAP1_MPUIO_BASE + OMAP_MPUIO_IO_CNTL)); |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 68 | omap_writew(byte, this->IO_ADDR_W); |
| 69 | ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NWE, 0); |
| 70 | ndelay(40); |
| 71 | ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NWE, |
| 72 | AMS_DELTA_LATCH2_NAND_NWE); |
| 73 | } |
| 74 | |
| 75 | static u_char ams_delta_read_byte(struct mtd_info *mtd) |
| 76 | { |
| 77 | u_char res; |
| 78 | struct nand_chip *this = mtd->priv; |
| 79 | |
| 80 | ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NRE, 0); |
| 81 | ndelay(40); |
Tony Lindgren | 6175556 | 2009-08-28 10:50:34 -0700 | [diff] [blame] | 82 | omap_writew(~0, (OMAP1_MPUIO_BASE + OMAP_MPUIO_IO_CNTL)); |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 83 | res = omap_readw(this->IO_ADDR_R); |
| 84 | ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NRE, |
| 85 | AMS_DELTA_LATCH2_NAND_NRE); |
| 86 | |
| 87 | return res; |
| 88 | } |
| 89 | |
| 90 | static void ams_delta_write_buf(struct mtd_info *mtd, const u_char *buf, |
| 91 | int len) |
| 92 | { |
| 93 | int i; |
| 94 | |
| 95 | for (i=0; i<len; i++) |
| 96 | ams_delta_write_byte(mtd, buf[i]); |
| 97 | } |
| 98 | |
| 99 | static void ams_delta_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 100 | { |
| 101 | int i; |
| 102 | |
| 103 | for (i=0; i<len; i++) |
| 104 | buf[i] = ams_delta_read_byte(mtd); |
| 105 | } |
| 106 | |
| 107 | static int ams_delta_verify_buf(struct mtd_info *mtd, const u_char *buf, |
| 108 | int len) |
| 109 | { |
| 110 | int i; |
| 111 | |
| 112 | for (i=0; i<len; i++) |
| 113 | if (buf[i] != ams_delta_read_byte(mtd)) |
| 114 | return -EFAULT; |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 119 | /* |
| 120 | * Command control function |
| 121 | * |
| 122 | * ctrl: |
| 123 | * NAND_NCE: bit 0 -> bit 2 |
| 124 | * NAND_CLE: bit 1 -> bit 7 |
| 125 | * NAND_ALE: bit 2 -> bit 6 |
| 126 | */ |
| 127 | static void ams_delta_hwcontrol(struct mtd_info *mtd, int cmd, |
| 128 | unsigned int ctrl) |
| 129 | { |
| 130 | |
| 131 | if (ctrl & NAND_CTRL_CHANGE) { |
| 132 | unsigned long bits; |
| 133 | |
Jonathan McDowell | fb8d81e | 2006-08-27 01:23:41 -0700 | [diff] [blame] | 134 | bits = (~ctrl & NAND_NCE) ? AMS_DELTA_LATCH2_NAND_NCE : 0; |
| 135 | bits |= (ctrl & NAND_CLE) ? AMS_DELTA_LATCH2_NAND_CLE : 0; |
| 136 | bits |= (ctrl & NAND_ALE) ? AMS_DELTA_LATCH2_NAND_ALE : 0; |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 137 | |
Jonathan McDowell | fb8d81e | 2006-08-27 01:23:41 -0700 | [diff] [blame] | 138 | ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_CLE | |
| 139 | AMS_DELTA_LATCH2_NAND_ALE | |
| 140 | AMS_DELTA_LATCH2_NAND_NCE, bits); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | if (cmd != NAND_CMD_NONE) |
| 144 | ams_delta_write_byte(mtd, cmd); |
| 145 | } |
| 146 | |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 147 | static int ams_delta_nand_ready(struct mtd_info *mtd) |
| 148 | { |
David Brownell | 93a22f8 | 2008-10-15 22:03:15 -0700 | [diff] [blame] | 149 | return gpio_get_value(AMS_DELTA_GPIO_PIN_NAND_RB); |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Main initialization routine |
| 154 | */ |
Janusz Krzysztofik | 7e95d1f | 2010-12-14 21:09:40 +0100 | [diff] [blame^] | 155 | static int __devinit ams_delta_init(struct platform_device *pdev) |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 156 | { |
| 157 | struct nand_chip *this; |
| 158 | int err = 0; |
| 159 | |
| 160 | /* Allocate memory for MTD device structure and private data */ |
| 161 | ams_delta_mtd = kmalloc(sizeof(struct mtd_info) + |
| 162 | sizeof(struct nand_chip), GFP_KERNEL); |
| 163 | if (!ams_delta_mtd) { |
| 164 | printk (KERN_WARNING "Unable to allocate E3 NAND MTD device structure.\n"); |
| 165 | err = -ENOMEM; |
| 166 | goto out; |
| 167 | } |
| 168 | |
| 169 | ams_delta_mtd->owner = THIS_MODULE; |
| 170 | |
| 171 | /* Get pointer to private data */ |
| 172 | this = (struct nand_chip *) (&ams_delta_mtd[1]); |
| 173 | |
| 174 | /* Initialize structures */ |
| 175 | memset(ams_delta_mtd, 0, sizeof(struct mtd_info)); |
| 176 | memset(this, 0, sizeof(struct nand_chip)); |
| 177 | |
| 178 | /* Link the private data with the MTD structure */ |
| 179 | ams_delta_mtd->priv = this; |
| 180 | |
| 181 | /* Set address of NAND IO lines */ |
Tony Lindgren | 6175556 | 2009-08-28 10:50:34 -0700 | [diff] [blame] | 182 | this->IO_ADDR_R = (OMAP1_MPUIO_BASE + OMAP_MPUIO_INPUT_LATCH); |
| 183 | this->IO_ADDR_W = (OMAP1_MPUIO_BASE + OMAP_MPUIO_OUTPUT); |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 184 | this->read_byte = ams_delta_read_byte; |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 185 | this->write_buf = ams_delta_write_buf; |
| 186 | this->read_buf = ams_delta_read_buf; |
| 187 | this->verify_buf = ams_delta_verify_buf; |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 188 | this->cmd_ctrl = ams_delta_hwcontrol; |
David Brownell | 93a22f8 | 2008-10-15 22:03:15 -0700 | [diff] [blame] | 189 | if (gpio_request(AMS_DELTA_GPIO_PIN_NAND_RB, "nand_rdy") == 0) { |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 190 | this->dev_ready = ams_delta_nand_ready; |
| 191 | } else { |
| 192 | this->dev_ready = NULL; |
| 193 | printk(KERN_NOTICE "Couldn't request gpio for Delta NAND ready.\n"); |
| 194 | } |
| 195 | /* 25 us command delay time */ |
| 196 | this->chip_delay = 30; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 197 | this->ecc.mode = NAND_ECC_SOFT; |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 198 | |
| 199 | /* Set chip enabled, but */ |
| 200 | ams_delta_latch2_write(NAND_MASK, AMS_DELTA_LATCH2_NAND_NRE | |
| 201 | AMS_DELTA_LATCH2_NAND_NWE | |
| 202 | AMS_DELTA_LATCH2_NAND_NCE | |
| 203 | AMS_DELTA_LATCH2_NAND_NWP); |
| 204 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 205 | /* Scan to find existance of the device */ |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 206 | if (nand_scan(ams_delta_mtd, 1)) { |
| 207 | err = -ENXIO; |
| 208 | goto out_mtd; |
| 209 | } |
| 210 | |
| 211 | /* Register the partitions */ |
| 212 | add_mtd_partitions(ams_delta_mtd, partition_info, |
| 213 | ARRAY_SIZE(partition_info)); |
| 214 | |
| 215 | goto out; |
| 216 | |
| 217 | out_mtd: |
| 218 | kfree(ams_delta_mtd); |
| 219 | out: |
| 220 | return err; |
| 221 | } |
| 222 | |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 223 | /* |
| 224 | * Clean up routine |
| 225 | */ |
Janusz Krzysztofik | 7e95d1f | 2010-12-14 21:09:40 +0100 | [diff] [blame^] | 226 | static int __devexit ams_delta_cleanup(struct platform_device *pdev) |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 227 | { |
| 228 | /* Release resources, unregister device */ |
| 229 | nand_release(ams_delta_mtd); |
| 230 | |
| 231 | /* Free the MTD device structure */ |
| 232 | kfree(ams_delta_mtd); |
Janusz Krzysztofik | 7e95d1f | 2010-12-14 21:09:40 +0100 | [diff] [blame^] | 233 | |
| 234 | return 0; |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 235 | } |
Janusz Krzysztofik | 7e95d1f | 2010-12-14 21:09:40 +0100 | [diff] [blame^] | 236 | |
| 237 | static struct platform_driver ams_delta_nand_driver = { |
| 238 | .probe = ams_delta_init, |
| 239 | .remove = __devexit_p(ams_delta_cleanup), |
| 240 | .driver = { |
| 241 | .name = "ams-delta-nand", |
| 242 | .owner = THIS_MODULE, |
| 243 | }, |
| 244 | }; |
| 245 | |
| 246 | static int __init ams_delta_nand_init(void) |
| 247 | { |
| 248 | return platform_driver_register(&ams_delta_nand_driver); |
| 249 | } |
| 250 | module_init(ams_delta_nand_init); |
| 251 | |
| 252 | static void __exit ams_delta_nand_exit(void) |
| 253 | { |
| 254 | platform_driver_unregister(&ams_delta_nand_driver); |
| 255 | } |
| 256 | module_exit(ams_delta_nand_exit); |
Jonathan McDowell | 3d12c0c | 2006-05-21 18:11:55 +0100 | [diff] [blame] | 257 | |
| 258 | MODULE_LICENSE("GPL"); |
| 259 | MODULE_AUTHOR("Jonathan McDowell <noodles@earth.li>"); |
| 260 | MODULE_DESCRIPTION("Glue layer for NAND flash on Amstrad E3 (Delta)"); |