Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/mtd/nand/sharpsl.c |
| 3 | * |
| 4 | * Copyright (C) 2004 Richard Purdie |
Dmitry Baryshkov | c176d0c | 2008-10-16 18:22:28 +0400 | [diff] [blame] | 5 | * Copyright (C) 2008 Dmitry Baryshkov |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * Based on Sharp's NAND driver sharp_sl.c |
| 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 | */ |
| 14 | |
| 15 | #include <linux/genhd.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/mtd/mtd.h> |
| 20 | #include <linux/mtd/nand.h> |
| 21 | #include <linux/mtd/nand_ecc.h> |
| 22 | #include <linux/mtd/partitions.h> |
Dmitry Baryshkov | a20c7ab | 2008-10-16 18:43:48 +0400 | [diff] [blame] | 23 | #include <linux/mtd/sharpsl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/interrupt.h> |
Dmitry Baryshkov | 2661524 | 2008-10-16 12:08:14 +0400 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <asm/io.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 28 | #include <mach/hardware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <asm/mach-types.h> |
| 30 | |
Dmitry Baryshkov | 2206ef1 | 2008-10-16 17:49:06 +0400 | [diff] [blame] | 31 | struct sharpsl_nand { |
| 32 | struct mtd_info mtd; |
| 33 | struct nand_chip chip; |
Dmitry Baryshkov | a4e4f29 | 2008-10-16 17:58:18 +0400 | [diff] [blame] | 34 | |
| 35 | void __iomem *io; |
Dmitry Baryshkov | 2206ef1 | 2008-10-16 17:49:06 +0400 | [diff] [blame] | 36 | }; |
| 37 | |
Dmitry Baryshkov | a4e4f29 | 2008-10-16 17:58:18 +0400 | [diff] [blame] | 38 | #define mtd_to_sharpsl(_mtd) container_of(_mtd, struct sharpsl_nand, mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | /* register offset */ |
Dmitry Baryshkov | a4e4f29 | 2008-10-16 17:58:18 +0400 | [diff] [blame] | 41 | #define ECCLPLB 0x00 /* line parity 7 - 0 bit */ |
| 42 | #define ECCLPUB 0x04 /* line parity 15 - 8 bit */ |
| 43 | #define ECCCP 0x08 /* column parity 5 - 0 bit */ |
| 44 | #define ECCCNTR 0x0C /* ECC byte counter */ |
| 45 | #define ECCCLRR 0x10 /* cleare ECC */ |
| 46 | #define FLASHIO 0x14 /* Flash I/O */ |
| 47 | #define FLASHCTL 0x18 /* Flash Control */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | /* Flash control bit */ |
| 50 | #define FLRYBY (1 << 5) |
| 51 | #define FLCE1 (1 << 4) |
| 52 | #define FLWP (1 << 3) |
| 53 | #define FLALE (1 << 2) |
| 54 | #define FLCLE (1 << 1) |
| 55 | #define FLCE0 (1 << 0) |
| 56 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 57 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | * hardware specific access to control-lines |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 59 | * ctrl: |
Richard Purdie | 6a5a297c | 2006-07-15 13:05:24 +0100 | [diff] [blame] | 60 | * NAND_CNE: bit 0 -> ! bit 0 & 4 |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 61 | * NAND_CLE: bit 1 -> bit 1 |
| 62 | * NAND_ALE: bit 2 -> bit 2 |
| 63 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 65 | static void sharpsl_nand_hwcontrol(struct mtd_info *mtd, int cmd, |
| 66 | unsigned int ctrl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
Dmitry Baryshkov | a4e4f29 | 2008-10-16 17:58:18 +0400 | [diff] [blame] | 68 | struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 69 | struct nand_chip *chip = mtd->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 71 | if (ctrl & NAND_CTRL_CHANGE) { |
| 72 | unsigned char bits = ctrl & 0x07; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 74 | bits |= (ctrl & 0x01) << 4; |
Richard Purdie | 6a5a297c | 2006-07-15 13:05:24 +0100 | [diff] [blame] | 75 | |
| 76 | bits ^= 0x11; |
| 77 | |
Dmitry Baryshkov | a4e4f29 | 2008-10-16 17:58:18 +0400 | [diff] [blame] | 78 | writeb((readb(sharpsl->io + FLASHCTL) & ~0x17) | bits, sharpsl->io + FLASHCTL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 80 | |
| 81 | if (cmd != NAND_CMD_NONE) |
| 82 | writeb(cmd, chip->IO_ADDR_W); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 85 | static int sharpsl_nand_dev_ready(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
Dmitry Baryshkov | a4e4f29 | 2008-10-16 17:58:18 +0400 | [diff] [blame] | 87 | struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd); |
| 88 | return !((readb(sharpsl->io + FLASHCTL) & FLRYBY) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 91 | static void sharpsl_nand_enable_hwecc(struct mtd_info *mtd, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
Dmitry Baryshkov | a4e4f29 | 2008-10-16 17:58:18 +0400 | [diff] [blame] | 93 | struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd); |
| 94 | writeb(0, sharpsl->io + ECCCLRR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 97 | static int sharpsl_nand_calculate_ecc(struct mtd_info *mtd, const u_char * dat, u_char * ecc_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
Dmitry Baryshkov | a4e4f29 | 2008-10-16 17:58:18 +0400 | [diff] [blame] | 99 | struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd); |
| 100 | ecc_code[0] = ~readb(sharpsl->io + ECCLPUB); |
| 101 | ecc_code[1] = ~readb(sharpsl->io + ECCLPLB); |
| 102 | ecc_code[2] = (~readb(sharpsl->io + ECCCP) << 2) | 0x03; |
| 103 | return readb(sharpsl->io + ECCCNTR) != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | /* |
| 107 | * Main initialization routine |
| 108 | */ |
Dmitry Baryshkov | 2661524 | 2008-10-16 12:08:14 +0400 | [diff] [blame] | 109 | static int __devinit sharpsl_nand_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
| 111 | struct nand_chip *this; |
Dmitry Baryshkov | 2661524 | 2008-10-16 12:08:14 +0400 | [diff] [blame] | 112 | struct resource *r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | int err = 0; |
Dmitry Baryshkov | 2206ef1 | 2008-10-16 17:49:06 +0400 | [diff] [blame] | 114 | struct sharpsl_nand *sharpsl; |
Dmitry Baryshkov | a20c7ab | 2008-10-16 18:43:48 +0400 | [diff] [blame] | 115 | struct sharpsl_nand_platform_data *data = pdev->dev.platform_data; |
| 116 | |
| 117 | if (!data) { |
| 118 | dev_err(&pdev->dev, "no platform data!\n"); |
| 119 | return -EINVAL; |
| 120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | /* Allocate memory for MTD device structure and private data */ |
Dmitry Baryshkov | 2206ef1 | 2008-10-16 17:49:06 +0400 | [diff] [blame] | 123 | sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL); |
| 124 | if (!sharpsl) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 125 | printk("Unable to allocate SharpSL NAND MTD device structure.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | return -ENOMEM; |
| 127 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 128 | |
Dmitry Baryshkov | 2661524 | 2008-10-16 12:08:14 +0400 | [diff] [blame] | 129 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 130 | if (!r) { |
| 131 | dev_err(&pdev->dev, "no io memory resource defined!\n"); |
| 132 | err = -ENODEV; |
| 133 | goto err_get_res; |
| 134 | } |
| 135 | |
Joe Perches | 8e87d78 | 2008-02-03 17:22:34 +0200 | [diff] [blame] | 136 | /* map physical address */ |
Dmitry Baryshkov | a4e4f29 | 2008-10-16 17:58:18 +0400 | [diff] [blame] | 137 | sharpsl->io = ioremap(r->start, resource_size(r)); |
| 138 | if (!sharpsl->io) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | printk("ioremap to access Sharp SL NAND chip failed\n"); |
Dmitry Baryshkov | 2206ef1 | 2008-10-16 17:49:06 +0400 | [diff] [blame] | 140 | err = -EIO; |
| 141 | goto err_ioremap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | /* Get pointer to private data */ |
Dmitry Baryshkov | 2206ef1 | 2008-10-16 17:49:06 +0400 | [diff] [blame] | 145 | this = (struct nand_chip *)(&sharpsl->chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | /* Link the private data with the MTD structure */ |
Dmitry Baryshkov | 2206ef1 | 2008-10-16 17:49:06 +0400 | [diff] [blame] | 148 | sharpsl->mtd.priv = this; |
| 149 | sharpsl->mtd.owner = THIS_MODULE; |
| 150 | |
| 151 | platform_set_drvdata(pdev, sharpsl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * PXA initialize |
| 155 | */ |
Dmitry Baryshkov | a4e4f29 | 2008-10-16 17:58:18 +0400 | [diff] [blame] | 156 | writeb(readb(sharpsl->io + FLASHCTL) | FLWP, sharpsl->io + FLASHCTL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | /* Set address of NAND IO lines */ |
Dmitry Baryshkov | a4e4f29 | 2008-10-16 17:58:18 +0400 | [diff] [blame] | 159 | this->IO_ADDR_R = sharpsl->io + FLASHIO; |
| 160 | this->IO_ADDR_W = sharpsl->io + FLASHIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | /* Set address of hardware control function */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 162 | this->cmd_ctrl = sharpsl_nand_hwcontrol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | this->dev_ready = sharpsl_nand_dev_ready; |
| 164 | /* 15 us command delay time */ |
| 165 | this->chip_delay = 15; |
| 166 | /* set eccmode using hardware ECC */ |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 167 | this->ecc.mode = NAND_ECC_HW; |
| 168 | this->ecc.size = 256; |
| 169 | this->ecc.bytes = 3; |
Dmitry Baryshkov | a20c7ab | 2008-10-16 18:43:48 +0400 | [diff] [blame] | 170 | this->badblock_pattern = data->badblock_pattern; |
| 171 | this->ecc.layout = data->ecc_layout; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 172 | this->ecc.hwctl = sharpsl_nand_enable_hwecc; |
| 173 | this->ecc.calculate = sharpsl_nand_calculate_ecc; |
| 174 | this->ecc.correct = nand_correct_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | /* Scan to find existence of the device */ |
Dmitry Baryshkov | 2206ef1 | 2008-10-16 17:49:06 +0400 | [diff] [blame] | 177 | err = nand_scan(&sharpsl->mtd, 1); |
Dmitry Baryshkov | a4e4f29 | 2008-10-16 17:58:18 +0400 | [diff] [blame] | 178 | if (err) |
| 179 | goto err_scan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | /* Register the partitions */ |
Dmitry Baryshkov | 2206ef1 | 2008-10-16 17:49:06 +0400 | [diff] [blame] | 182 | sharpsl->mtd.name = "sharpsl-nand"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Dmitry Eremin-Solenikov | 3af55a8 | 2011-06-02 18:01:03 +0400 | [diff] [blame] | 184 | err = mtd_device_parse_register(&sharpsl->mtd, NULL, 0, |
| 185 | data->partitions, data->nr_partitions); |
Dmitry Baryshkov | c176d0c | 2008-10-16 18:22:28 +0400 | [diff] [blame] | 186 | if (err) |
| 187 | goto err_add; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | /* Return happy */ |
| 190 | return 0; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 191 | |
Dmitry Baryshkov | c176d0c | 2008-10-16 18:22:28 +0400 | [diff] [blame] | 192 | err_add: |
| 193 | nand_release(&sharpsl->mtd); |
| 194 | |
Dmitry Baryshkov | a4e4f29 | 2008-10-16 17:58:18 +0400 | [diff] [blame] | 195 | err_scan: |
| 196 | platform_set_drvdata(pdev, NULL); |
| 197 | iounmap(sharpsl->io); |
Dmitry Baryshkov | 2206ef1 | 2008-10-16 17:49:06 +0400 | [diff] [blame] | 198 | err_ioremap: |
Dmitry Baryshkov | 2661524 | 2008-10-16 12:08:14 +0400 | [diff] [blame] | 199 | err_get_res: |
Dmitry Baryshkov | 2206ef1 | 2008-10-16 17:49:06 +0400 | [diff] [blame] | 200 | kfree(sharpsl); |
Dmitry Baryshkov | 2661524 | 2008-10-16 12:08:14 +0400 | [diff] [blame] | 201 | return err; |
| 202 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * Clean up routine |
| 206 | */ |
Dmitry Baryshkov | 2661524 | 2008-10-16 12:08:14 +0400 | [diff] [blame] | 207 | static int __devexit sharpsl_nand_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
Dmitry Baryshkov | 2206ef1 | 2008-10-16 17:49:06 +0400 | [diff] [blame] | 209 | struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev); |
| 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | /* Release resources, unregister device */ |
Dmitry Baryshkov | 2206ef1 | 2008-10-16 17:49:06 +0400 | [diff] [blame] | 212 | nand_release(&sharpsl->mtd); |
| 213 | |
| 214 | platform_set_drvdata(pdev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Dmitry Baryshkov | a4e4f29 | 2008-10-16 17:58:18 +0400 | [diff] [blame] | 216 | iounmap(sharpsl->io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
| 218 | /* Free the MTD device structure */ |
Dmitry Baryshkov | 2206ef1 | 2008-10-16 17:49:06 +0400 | [diff] [blame] | 219 | kfree(sharpsl); |
Dmitry Baryshkov | 2661524 | 2008-10-16 12:08:14 +0400 | [diff] [blame] | 220 | |
| 221 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 223 | |
Dmitry Baryshkov | 2661524 | 2008-10-16 12:08:14 +0400 | [diff] [blame] | 224 | static struct platform_driver sharpsl_nand_driver = { |
| 225 | .driver = { |
| 226 | .name = "sharpsl-nand", |
| 227 | .owner = THIS_MODULE, |
| 228 | }, |
| 229 | .probe = sharpsl_nand_probe, |
| 230 | .remove = __devexit_p(sharpsl_nand_remove), |
| 231 | }; |
| 232 | |
Dmitry Baryshkov | 2661524 | 2008-10-16 12:08:14 +0400 | [diff] [blame] | 233 | static int __init sharpsl_nand_init(void) |
| 234 | { |
Dmitry Baryshkov | 2661524 | 2008-10-16 12:08:14 +0400 | [diff] [blame] | 235 | return platform_driver_register(&sharpsl_nand_driver); |
| 236 | } |
| 237 | module_init(sharpsl_nand_init); |
| 238 | |
| 239 | static void __exit sharpsl_nand_exit(void) |
| 240 | { |
| 241 | platform_driver_unregister(&sharpsl_nand_driver); |
Dmitry Baryshkov | 2661524 | 2008-10-16 12:08:14 +0400 | [diff] [blame] | 242 | } |
| 243 | module_exit(sharpsl_nand_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | MODULE_LICENSE("GPL"); |
| 246 | MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>"); |
| 247 | MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series"); |