Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
nuc900_nand.c
Go to the documentation of this file.
1 /*
2  * Copyright © 2009 Nuvoton technology corporation.
3  *
4  * Wan ZongShun <[email protected]>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation;version 2 of the License.
9  *
10  */
11 
12 #include <linux/slab.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/platform_device.h>
18 #include <linux/delay.h>
19 #include <linux/clk.h>
20 #include <linux/err.h>
21 
22 #include <linux/mtd/mtd.h>
23 #include <linux/mtd/nand.h>
24 #include <linux/mtd/partitions.h>
25 
26 #define REG_FMICSR 0x00
27 #define REG_SMCSR 0xa0
28 #define REG_SMISR 0xac
29 #define REG_SMCMD 0xb0
30 #define REG_SMADDR 0xb4
31 #define REG_SMDATA 0xb8
32 
33 #define RESET_FMI 0x01
34 #define NAND_EN 0x08
35 #define READYBUSY (0x01 << 18)
36 
37 #define SWRST 0x01
38 #define PSIZE (0x01 << 3)
39 #define DMARWEN (0x03 << 1)
40 #define BUSWID (0x01 << 4)
41 #define ECC4EN (0x01 << 5)
42 #define WP (0x01 << 24)
43 #define NANDCS (0x01 << 25)
44 #define ENDADDR (0x01 << 31)
45 
46 #define read_data_reg(dev) \
47  __raw_readl((dev)->reg + REG_SMDATA)
48 
49 #define write_data_reg(dev, val) \
50  __raw_writel((val), (dev)->reg + REG_SMDATA)
51 
52 #define write_cmd_reg(dev, val) \
53  __raw_writel((val), (dev)->reg + REG_SMCMD)
54 
55 #define write_addr_reg(dev, val) \
56  __raw_writel((val), (dev)->reg + REG_SMADDR)
57 
58 struct nuc900_nand {
59  struct mtd_info mtd;
60  struct nand_chip chip;
61  void __iomem *reg;
62  struct clk *clk;
64 };
65 
66 static const struct mtd_partition partitions[] = {
67  {
68  .name = "NAND FS 0",
69  .offset = 0,
70  .size = 8 * 1024 * 1024
71  },
72  {
73  .name = "NAND FS 1",
74  .offset = MTDPART_OFS_APPEND,
75  .size = MTDPART_SIZ_FULL
76  }
77 };
78 
79 static unsigned char nuc900_nand_read_byte(struct mtd_info *mtd)
80 {
81  unsigned char ret;
82  struct nuc900_nand *nand;
83 
84  nand = container_of(mtd, struct nuc900_nand, mtd);
85 
86  ret = (unsigned char)read_data_reg(nand);
87 
88  return ret;
89 }
90 
91 static void nuc900_nand_read_buf(struct mtd_info *mtd,
92  unsigned char *buf, int len)
93 {
94  int i;
95  struct nuc900_nand *nand;
96 
97  nand = container_of(mtd, struct nuc900_nand, mtd);
98 
99  for (i = 0; i < len; i++)
100  buf[i] = (unsigned char)read_data_reg(nand);
101 }
102 
103 static void nuc900_nand_write_buf(struct mtd_info *mtd,
104  const unsigned char *buf, int len)
105 {
106  int i;
107  struct nuc900_nand *nand;
108 
109  nand = container_of(mtd, struct nuc900_nand, mtd);
110 
111  for (i = 0; i < len; i++)
112  write_data_reg(nand, buf[i]);
113 }
114 
115 static int nuc900_check_rb(struct nuc900_nand *nand)
116 {
117  unsigned int val;
118  spin_lock(&nand->lock);
119  val = __raw_readl(REG_SMISR);
120  val &= READYBUSY;
121  spin_unlock(&nand->lock);
122 
123  return val;
124 }
125 
126 static int nuc900_nand_devready(struct mtd_info *mtd)
127 {
128  struct nuc900_nand *nand;
129  int ready;
130 
131  nand = container_of(mtd, struct nuc900_nand, mtd);
132 
133  ready = (nuc900_check_rb(nand)) ? 1 : 0;
134  return ready;
135 }
136 
137 static void nuc900_nand_command_lp(struct mtd_info *mtd, unsigned int command,
138  int column, int page_addr)
139 {
140  register struct nand_chip *chip = mtd->priv;
141  struct nuc900_nand *nand;
142 
143  nand = container_of(mtd, struct nuc900_nand, mtd);
144 
145  if (command == NAND_CMD_READOOB) {
146  column += mtd->writesize;
147  command = NAND_CMD_READ0;
148  }
149 
150  write_cmd_reg(nand, command & 0xff);
151 
152  if (column != -1 || page_addr != -1) {
153 
154  if (column != -1) {
155  if (chip->options & NAND_BUSWIDTH_16)
156  column >>= 1;
157  write_addr_reg(nand, column);
158  write_addr_reg(nand, column >> 8 | ENDADDR);
159  }
160  if (page_addr != -1) {
161  write_addr_reg(nand, page_addr);
162 
163  if (chip->chipsize > (128 << 20)) {
164  write_addr_reg(nand, page_addr >> 8);
165  write_addr_reg(nand, page_addr >> 16 | ENDADDR);
166  } else {
167  write_addr_reg(nand, page_addr >> 8 | ENDADDR);
168  }
169  }
170  }
171 
172  switch (command) {
173  case NAND_CMD_CACHEDPROG:
174  case NAND_CMD_PAGEPROG:
175  case NAND_CMD_ERASE1:
176  case NAND_CMD_ERASE2:
177  case NAND_CMD_SEQIN:
178  case NAND_CMD_RNDIN:
179  case NAND_CMD_STATUS:
180  case NAND_CMD_DEPLETE1:
181  return;
182 
188  udelay(chip->chip_delay);
189  return;
190 
191  case NAND_CMD_RESET:
192  if (chip->dev_ready)
193  break;
194  udelay(chip->chip_delay);
195 
197  write_cmd_reg(nand, command);
198 
199  while (!nuc900_check_rb(nand))
200  ;
201 
202  return;
203 
204  case NAND_CMD_RNDOUT:
206  return;
207 
208  case NAND_CMD_READ0:
209 
211  default:
212 
213  if (!chip->dev_ready) {
214  udelay(chip->chip_delay);
215  return;
216  }
217  }
218 
219  /* Apply this short delay always to ensure that we do wait tWB in
220  * any case on any machine. */
221  ndelay(100);
222 
223  while (!chip->dev_ready(mtd))
224  ;
225 }
226 
227 
228 static void nuc900_nand_enable(struct nuc900_nand *nand)
229 {
230  unsigned int val;
231  spin_lock(&nand->lock);
232  __raw_writel(RESET_FMI, (nand->reg + REG_FMICSR));
233 
234  val = __raw_readl(nand->reg + REG_FMICSR);
235 
236  if (!(val & NAND_EN))
237  __raw_writel(val | NAND_EN, REG_FMICSR);
238 
239  val = __raw_readl(nand->reg + REG_SMCSR);
240 
242  val |= WP;
243 
244  __raw_writel(val, nand->reg + REG_SMCSR);
245 
246  spin_unlock(&nand->lock);
247 }
248 
249 static int __devinit nuc900_nand_probe(struct platform_device *pdev)
250 {
251  struct nuc900_nand *nuc900_nand;
252  struct nand_chip *chip;
253  int retval;
254  struct resource *res;
255 
256  retval = 0;
257 
258  nuc900_nand = kzalloc(sizeof(struct nuc900_nand), GFP_KERNEL);
259  if (!nuc900_nand)
260  return -ENOMEM;
261  chip = &(nuc900_nand->chip);
262 
263  nuc900_nand->mtd.priv = chip;
264  nuc900_nand->mtd.owner = THIS_MODULE;
265  spin_lock_init(&nuc900_nand->lock);
266 
267  nuc900_nand->clk = clk_get(&pdev->dev, NULL);
268  if (IS_ERR(nuc900_nand->clk)) {
269  retval = -ENOENT;
270  goto fail1;
271  }
272  clk_enable(nuc900_nand->clk);
273 
274  chip->cmdfunc = nuc900_nand_command_lp;
275  chip->dev_ready = nuc900_nand_devready;
276  chip->read_byte = nuc900_nand_read_byte;
277  chip->write_buf = nuc900_nand_write_buf;
278  chip->read_buf = nuc900_nand_read_buf;
279  chip->chip_delay = 50;
280  chip->options = 0;
281  chip->ecc.mode = NAND_ECC_SOFT;
282 
283  res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
284  if (!res) {
285  retval = -ENXIO;
286  goto fail1;
287  }
288 
289  if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
290  retval = -EBUSY;
291  goto fail1;
292  }
293 
294  nuc900_nand->reg = ioremap(res->start, resource_size(res));
295  if (!nuc900_nand->reg) {
296  retval = -ENOMEM;
297  goto fail2;
298  }
299 
300  nuc900_nand_enable(nuc900_nand);
301 
302  if (nand_scan(&(nuc900_nand->mtd), 1)) {
303  retval = -ENXIO;
304  goto fail3;
305  }
306 
307  mtd_device_register(&(nuc900_nand->mtd), partitions,
308  ARRAY_SIZE(partitions));
309 
310  platform_set_drvdata(pdev, nuc900_nand);
311 
312  return retval;
313 
314 fail3: iounmap(nuc900_nand->reg);
315 fail2: release_mem_region(res->start, resource_size(res));
316 fail1: kfree(nuc900_nand);
317  return retval;
318 }
319 
320 static int __devexit nuc900_nand_remove(struct platform_device *pdev)
321 {
322  struct nuc900_nand *nuc900_nand = platform_get_drvdata(pdev);
323  struct resource *res;
324 
325  nand_release(&nuc900_nand->mtd);
326  iounmap(nuc900_nand->reg);
327 
328  res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
329  release_mem_region(res->start, resource_size(res));
330 
331  clk_disable(nuc900_nand->clk);
332  clk_put(nuc900_nand->clk);
333 
334  kfree(nuc900_nand);
335 
336  platform_set_drvdata(pdev, NULL);
337 
338  return 0;
339 }
340 
341 static struct platform_driver nuc900_nand_driver = {
342  .probe = nuc900_nand_probe,
343  .remove = __devexit_p(nuc900_nand_remove),
344  .driver = {
345  .name = "nuc900-fmi",
346  .owner = THIS_MODULE,
347  },
348 };
349 
350 module_platform_driver(nuc900_nand_driver);
351 
352 MODULE_AUTHOR("Wan ZongShun <[email protected]>");
353 MODULE_DESCRIPTION("w90p910/NUC9xx nand driver!");
354 MODULE_LICENSE("GPL");
355 MODULE_ALIAS("platform:nuc900-fmi");