blob: 04769a49a7cbd1c6d60980ad651c241416f16d15 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/mtd/autcpu12.c
3 *
4 * Copyright (c) 2002 Thomas Gleixner <tgxl@linutronix.de>
5 *
6 * Derived from drivers/mtd/spia.c
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02007 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000015 * autronix autcpu12 board, which is a SmartMediaCard. It supports
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * 16MiB, 32MiB and 64MiB cards.
17 *
18 *
19 * 02-12-2002 TG Cleanup of module params
20 *
Joe Perches8e87d782008-02-03 17:22:34 +020021 * 02-20-2002 TG adjusted for different rd/wr address support
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * added support for read device ready/busy line
23 * added page_cache
24 *
25 * 10-06-2002 TG 128K card support added
26 */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/slab.h>
29#include <linux/init.h>
30#include <linux/module.h>
31#include <linux/mtd/mtd.h>
32#include <linux/mtd/nand.h>
33#include <linux/mtd/partitions.h>
34#include <asm/io.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010035#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/sizes.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010037#include <mach/autcpu12.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
40 * MTD structure for AUTCPU12 board
41 */
42static struct mtd_info *autcpu12_mtd = NULL;
David Woodhousee0c7d762006-05-13 18:07:53 +010043static void __iomem *autcpu12_fio_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
46 * Define partitions for flash devices
47 */
48static struct mtd_partition partition_info16k[] = {
49 { .name = "AUTCPU12 flash partition 1",
50 .offset = 0,
51 .size = 8 * SZ_1M },
52 { .name = "AUTCPU12 flash partition 2",
53 .offset = 8 * SZ_1M,
54 .size = 8 * SZ_1M },
55};
56
57static struct mtd_partition partition_info32k[] = {
58 { .name = "AUTCPU12 flash partition 1",
59 .offset = 0,
60 .size = 8 * SZ_1M },
61 { .name = "AUTCPU12 flash partition 2",
62 .offset = 8 * SZ_1M,
63 .size = 24 * SZ_1M },
64};
65
66static struct mtd_partition partition_info64k[] = {
67 { .name = "AUTCPU12 flash partition 1",
68 .offset = 0,
69 .size = 16 * SZ_1M },
70 { .name = "AUTCPU12 flash partition 2",
71 .offset = 16 * SZ_1M,
72 .size = 48 * SZ_1M },
73};
74
75static struct mtd_partition partition_info128k[] = {
76 { .name = "AUTCPU12 flash partition 1",
77 .offset = 0,
78 .size = 16 * SZ_1M },
79 { .name = "AUTCPU12 flash partition 2",
80 .offset = 16 * SZ_1M,
81 .size = 112 * SZ_1M },
82};
83
84#define NUM_PARTITIONS16K 2
85#define NUM_PARTITIONS32K 2
86#define NUM_PARTITIONS64K 2
87#define NUM_PARTITIONS128K 2
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000088/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * hardware specific access to control-lines
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020090 *
91 * ALE bit 4 autcpu12_pedr
92 * CLE bit 5 autcpu12_pedr
93 * NCE bit 0 fio_ctrl
94 *
95 */
96static void autcpu12_hwcontrol(struct mtd_info *mtd, int cmd,
97 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020099 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200101 if (ctrl & NAND_CTRL_CHANGE) {
Yoann Padioleau632155e2007-06-01 00:46:35 -0700102 void __iomem *addr;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200103 unsigned char bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Alexander Shiyan304b2c62012-05-06 09:21:57 +0400105 bits = clps_readb(AUTCPU12_SMC_PORT_OFFSET) & ~0x30;
106 bits |= (ctrl & NAND_CLE) << 4;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200107 bits |= (ctrl & NAND_ALE) << 2;
Alexander Shiyan304b2c62012-05-06 09:21:57 +0400108 clps_writeb(bits, AUTCPU12_SMC_PORT_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200110 addr = autcpu12_fio_base + AUTCPU12_SMC_SELECT_OFFSET;
111 writeb((readb(addr) & ~0x1) | (ctrl & NAND_NCE), addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200113
114 if (cmd != NAND_CMD_NONE)
115 writeb(cmd, chip->IO_ADDR_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118/*
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200119 * read device ready pin
120 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121int autcpu12_device_ready(struct mtd_info *mtd)
122{
Alexander Shiyan304b2c62012-05-06 09:21:57 +0400123 return clps_readb(AUTCPU12_SMC_PORT_OFFSET) & AUTCPU12_SMC_RDY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126/*
127 * Main initialization routine
128 */
David Woodhousecead4db2006-05-16 13:54:50 +0100129static int __init autcpu12_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct nand_chip *this;
132 int err = 0;
133
134 /* Allocate memory for MTD device structure and private data */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200135 autcpu12_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip),
136 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (!autcpu12_mtd) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100138 printk("Unable to allocate AUTCPU12 NAND MTD device structure.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 err = -ENOMEM;
140 goto out;
141 }
142
Joe Perches8e87d782008-02-03 17:22:34 +0200143 /* map physical address */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200144 autcpu12_fio_base = ioremap(AUTCPU12_PHYS_SMC, SZ_1K);
David Woodhousee0c7d762006-05-13 18:07:53 +0100145 if (!autcpu12_fio_base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 printk("Ioremap autcpu12 SmartMedia Card failed\n");
147 err = -EIO;
148 goto out_mtd;
149 }
150
151 /* Get pointer to private data */
David Woodhousee0c7d762006-05-13 18:07:53 +0100152 this = (struct nand_chip *)(&autcpu12_mtd[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 /* Initialize structures */
David Woodhousee0c7d762006-05-13 18:07:53 +0100155 memset(autcpu12_mtd, 0, sizeof(struct mtd_info));
156 memset(this, 0, sizeof(struct nand_chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 /* Link the private data with the MTD structure */
159 autcpu12_mtd->priv = this;
David Woodhouse552d9202006-05-14 01:20:46 +0100160 autcpu12_mtd->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /* Set address of NAND IO lines */
163 this->IO_ADDR_R = autcpu12_fio_base;
164 this->IO_ADDR_W = autcpu12_fio_base;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200165 this->cmd_ctrl = autcpu12_hwcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 this->dev_ready = autcpu12_device_ready;
167 /* 20 us command delay time */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000168 this->chip_delay = 20;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200169 this->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 /* Enable the following for a flash based bad block table */
172 /*
Brian Norrisbb9ebd42011-05-31 16:31:23 -0700173 this->bbt_options = NAND_BBT_USE_FLASH;
David Woodhousee0c7d762006-05-13 18:07:53 +0100174 */
Brian Norrisbb9ebd42011-05-31 16:31:23 -0700175 this->bbt_options = NAND_BBT_USE_FLASH;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000176
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300177 /* Scan to find existence of the device */
David Woodhousee0c7d762006-05-13 18:07:53 +0100178 if (nand_scan(autcpu12_mtd, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 err = -ENXIO;
180 goto out_ior;
181 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 /* Register the partitions */
David Woodhousee0c7d762006-05-13 18:07:53 +0100184 switch (autcpu12_mtd->size) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200185 case SZ_16M:
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100186 mtd_device_register(autcpu12_mtd, partition_info16k,
187 NUM_PARTITIONS16K);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200188 break;
189 case SZ_32M:
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100190 mtd_device_register(autcpu12_mtd, partition_info32k,
191 NUM_PARTITIONS32K);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200192 break;
193 case SZ_64M:
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100194 mtd_device_register(autcpu12_mtd, partition_info64k,
195 NUM_PARTITIONS64K);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200196 break;
197 case SZ_128M:
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100198 mtd_device_register(autcpu12_mtd, partition_info128k,
199 NUM_PARTITIONS128K);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200200 break;
David Woodhousee0c7d762006-05-13 18:07:53 +0100201 default:
202 printk("Unsupported SmartMedia device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 err = -ENXIO;
204 goto out_ior;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206 goto out;
207
David Woodhousee0c7d762006-05-13 18:07:53 +0100208 out_ior:
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200209 iounmap(autcpu12_fio_base);
David Woodhousee0c7d762006-05-13 18:07:53 +0100210 out_mtd:
211 kfree(autcpu12_mtd);
212 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return err;
214}
215
216module_init(autcpu12_init);
217
218/*
219 * Clean up routine
220 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100221static void __exit autcpu12_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 /* Release resources, unregister device */
David Woodhousee0c7d762006-05-13 18:07:53 +0100224 nand_release(autcpu12_mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Joe Perches8e87d782008-02-03 17:22:34 +0200226 /* unmap physical address */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200227 iounmap(autcpu12_fio_base);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* Free the MTD device structure */
David Woodhousee0c7d762006-05-13 18:07:53 +0100230 kfree(autcpu12_mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
David Woodhousee0c7d762006-05-13 18:07:53 +0100232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233module_exit(autcpu12_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235MODULE_LICENSE("GPL");
236MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
237MODULE_DESCRIPTION("Glue layer for SmartMediaCard on autronix autcpu12");