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