blob: babb02c4b2204ed0c5881a3577084c2ea826d5df [file] [log] [blame]
Dinh Nguyen30f9f2f2012-09-27 10:58:06 -06001/*
2 * NAND Flash Controller Device Driver for DT
3 *
4 * Copyright © 2011, Picochip.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 */
15#include <linux/clk.h>
16#include <linux/err.h>
17#include <linux/io.h>
18#include <linux/ioport.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/platform_device.h>
22#include <linux/of.h>
23#include <linux/of_device.h>
24#include <linux/slab.h>
25
26#include "denali.h"
27
28struct denali_dt {
29 struct denali_nand_info denali;
30 struct clk *clk;
31};
32
33static void __iomem *request_and_map(struct device *dev,
34 const struct resource *res)
35{
36 void __iomem *ptr;
37
38 if (!devm_request_mem_region(dev, res->start, resource_size(res),
39 "denali-dt")) {
40 dev_err(dev, "unable to request %s\n", res->name);
41 return NULL;
42 }
43
44 ptr = devm_ioremap_nocache(dev, res->start, resource_size(res));
Sachin Kamat6c670502013-03-18 15:11:11 +053045 if (!ptr)
Dinh Nguyen30f9f2f2012-09-27 10:58:06 -060046 dev_err(dev, "ioremap_nocache of %s failed!", res->name);
47
48 return ptr;
49}
50
51static const struct of_device_id denali_nand_dt_ids[] = {
52 { .compatible = "denali,denali-nand-dt" },
53 { /* sentinel */ }
54 };
55
56MODULE_DEVICE_TABLE(of, denali_nand_dt_ids);
57
58static u64 denali_dma_mask;
59
Bill Pemberton06f25512012-11-19 13:23:07 -050060static int denali_dt_probe(struct platform_device *ofdev)
Dinh Nguyen30f9f2f2012-09-27 10:58:06 -060061{
62 struct resource *denali_reg, *nand_data;
63 struct denali_dt *dt;
64 struct denali_nand_info *denali;
65 int ret;
66 const struct of_device_id *of_id;
67
68 of_id = of_match_device(denali_nand_dt_ids, &ofdev->dev);
69 if (of_id) {
70 ofdev->id_entry = of_id->data;
71 } else {
72 pr_err("Failed to find the right device id.\n");
73 return -ENOMEM;
74 }
75
76 dt = devm_kzalloc(&ofdev->dev, sizeof(*dt), GFP_KERNEL);
77 if (!dt)
78 return -ENOMEM;
79 denali = &dt->denali;
80
81 denali_reg = platform_get_resource_byname(ofdev, IORESOURCE_MEM, "denali_reg");
82 nand_data = platform_get_resource_byname(ofdev, IORESOURCE_MEM, "nand_data");
83 if (!denali_reg || !nand_data) {
84 dev_err(&ofdev->dev, "resources not completely defined\n");
85 return -EINVAL;
86 }
87
88 denali->platform = DT;
89 denali->dev = &ofdev->dev;
90 denali->irq = platform_get_irq(ofdev, 0);
91 if (denali->irq < 0) {
92 dev_err(&ofdev->dev, "no irq defined\n");
Sachin Kamat2f2ff142013-03-18 15:11:13 +053093 return denali->irq;
Dinh Nguyen30f9f2f2012-09-27 10:58:06 -060094 }
95
96 denali->flash_reg = request_and_map(&ofdev->dev, denali_reg);
97 if (!denali->flash_reg)
98 return -ENOMEM;
99
100 denali->flash_mem = request_and_map(&ofdev->dev, nand_data);
101 if (!denali->flash_mem)
102 return -ENOMEM;
103
104 if (!of_property_read_u32(ofdev->dev.of_node,
105 "dma-mask", (u32 *)&denali_dma_mask)) {
106 denali->dev->dma_mask = &denali_dma_mask;
107 } else {
108 denali->dev->dma_mask = NULL;
109 }
110
Jingoo Han2281f7b62013-12-26 10:43:11 +0900111 dt->clk = devm_clk_get(&ofdev->dev, NULL);
Dinh Nguyen30f9f2f2012-09-27 10:58:06 -0600112 if (IS_ERR(dt->clk)) {
113 dev_err(&ofdev->dev, "no clk available\n");
114 return PTR_ERR(dt->clk);
115 }
116 clk_prepare_enable(dt->clk);
117
118 ret = denali_init(denali);
119 if (ret)
120 goto out_disable_clk;
121
122 platform_set_drvdata(ofdev, dt);
123 return 0;
124
125out_disable_clk:
126 clk_disable_unprepare(dt->clk);
Dinh Nguyen30f9f2f2012-09-27 10:58:06 -0600127
128 return ret;
129}
130
Bill Pemberton810b7e02012-11-19 13:26:04 -0500131static int denali_dt_remove(struct platform_device *ofdev)
Dinh Nguyen30f9f2f2012-09-27 10:58:06 -0600132{
133 struct denali_dt *dt = platform_get_drvdata(ofdev);
134
135 denali_remove(&dt->denali);
136 clk_disable(dt->clk);
Dinh Nguyen30f9f2f2012-09-27 10:58:06 -0600137
138 return 0;
139}
140
141static struct platform_driver denali_dt_driver = {
142 .probe = denali_dt_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500143 .remove = denali_dt_remove,
Dinh Nguyen30f9f2f2012-09-27 10:58:06 -0600144 .driver = {
145 .name = "denali-nand-dt",
146 .owner = THIS_MODULE,
Sachin Kamatef54f872013-03-18 15:11:14 +0530147 .of_match_table = denali_nand_dt_ids,
Dinh Nguyen30f9f2f2012-09-27 10:58:06 -0600148 },
149};
150
Sachin Kamat82fc8122013-03-18 15:11:12 +0530151module_platform_driver(denali_dt_driver);
Dinh Nguyen30f9f2f2012-09-27 10:58:06 -0600152
153MODULE_LICENSE("GPL");
154MODULE_AUTHOR("Jamie Iles");
155MODULE_DESCRIPTION("DT driver for Denali NAND controller");