55eb009bac10992074b81ebaaa8d26527de989f7
[openwrt.git] / target / linux / oxnas / files / drivers / mtd / nand / oxnas_nand.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify it
3  *  under the terms of the GNU General Public License version 2 as published
4  *  by the Free Software Foundation.
5  */
6
7 #include <linux/module.h>
8 #include <linux/mtd/nand.h>
9 #include <linux/of_gpio.h>
10 #include <linux/of_platform.h>
11 #include <linux/clk.h>
12 #include <linux/reset.h>
13 #include <mach/utils.h>
14
15 /* nand commands */
16 #define NAND_CMD_ALE            BIT(18)
17 #define NAND_CMD_CLE            BIT(19)
18 #define NAND_CMD_CS             0
19 #define NAND_CMD_RESET          0xff
20 #define NAND_CMD                (NAND_CMD_CS | NAND_CMD_CLE)
21 #define NAND_ADDR               (NAND_CMD_CS | NAND_CMD_ALE)
22 #define NAND_DATA               (NAND_CMD_CS)
23
24 static void oxnas_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
25 {
26         struct nand_chip *this = mtd->priv;
27         unsigned long nandaddr = (unsigned long) this->IO_ADDR_W;
28
29         if (ctrl & NAND_CTRL_CHANGE) {
30                 nandaddr &= ~(NAND_CMD | NAND_ADDR);
31                 if (ctrl & NAND_CLE)
32                         nandaddr |= NAND_CMD;
33                 else if (ctrl & NAND_ALE)
34                         nandaddr |= NAND_ADDR;
35                 this->IO_ADDR_W = (void __iomem *) nandaddr;
36         }
37
38         if (cmd != NAND_CMD_NONE)
39                 writeb(cmd, (void __iomem *) nandaddr);
40 }
41
42 static int oxnas_nand_probe(struct platform_device *pdev)
43 {
44         /* enable clock and release static block reset */
45         struct clk *clk = of_clk_get(pdev->dev.of_node, 0);
46
47         if (IS_ERR(clk))
48                 return PTR_ERR(clk);
49
50         clk_prepare_enable(clk);
51         device_reset(&pdev->dev);
52
53         return 0;
54 }
55
56 /* allow users to override the partition in DT using the cmdline */
57 static const char * part_probes[] = { "cmdlinepart", "ofpart", NULL };
58
59 static struct platform_nand_data oxnas_nand_data = {
60         .chip = {
61                 .nr_chips               = 1,
62                 .chip_delay             = 30,
63                 .part_probe_types       = part_probes,
64         },
65         .ctrl = {
66                 .probe          = oxnas_nand_probe,
67                 .cmd_ctrl       = oxnas_cmd_ctrl,
68         }
69 };
70
71 /*
72  * Try to find the node inside the DT. If it is available attach out
73  * platform_nand_data
74  */
75 static int __init oxnas_register_nand(void)
76 {
77         struct device_node *node;
78         struct platform_device *pdev;
79
80         node = of_find_compatible_node(NULL, NULL, "plxtech,nand-nas782x");
81         if (!node)
82                 return -ENOENT;
83         pdev = of_find_device_by_node(node);
84         if (!pdev)
85                 return -EINVAL;
86         pdev->dev.platform_data = &oxnas_nand_data;
87         of_node_put(node);
88         return 0;
89 }
90
91 subsys_initcall(oxnas_register_nand);
92
93 static const struct of_device_id oxnas_nand_ids[] = {
94         { .compatible = "plxtech,nand-nas782x"},
95         { /* sentinel */ }
96 };
97 MODULE_DEVICE_TABLE(of, oxnas_nand_ids);
98
99 MODULE_LICENSE("GPL");
100 MODULE_AUTHOR("Ma Haijun");
101 MODULE_DESCRIPTION("NAND glue for Oxnas platforms");
102 MODULE_ALIAS("platform:oxnas_nand");