Dinh Nguyen | 30f9f2f | 2012-09-27 10:58:06 -0600 | [diff] [blame] | 1 | /* |
| 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 | |
| 28 | struct denali_dt { |
| 29 | struct denali_nand_info denali; |
| 30 | struct clk *clk; |
| 31 | }; |
| 32 | |
Dinh Nguyen | 30f9f2f | 2012-09-27 10:58:06 -0600 | [diff] [blame] | 33 | static const struct of_device_id denali_nand_dt_ids[] = { |
| 34 | { .compatible = "denali,denali-nand-dt" }, |
| 35 | { /* sentinel */ } |
| 36 | }; |
| 37 | |
| 38 | MODULE_DEVICE_TABLE(of, denali_nand_dt_ids); |
| 39 | |
| 40 | static u64 denali_dma_mask; |
| 41 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 42 | static int denali_dt_probe(struct platform_device *ofdev) |
Dinh Nguyen | 30f9f2f | 2012-09-27 10:58:06 -0600 | [diff] [blame] | 43 | { |
| 44 | struct resource *denali_reg, *nand_data; |
| 45 | struct denali_dt *dt; |
| 46 | struct denali_nand_info *denali; |
| 47 | int ret; |
| 48 | const struct of_device_id *of_id; |
| 49 | |
| 50 | of_id = of_match_device(denali_nand_dt_ids, &ofdev->dev); |
| 51 | if (of_id) { |
| 52 | ofdev->id_entry = of_id->data; |
| 53 | } else { |
| 54 | pr_err("Failed to find the right device id.\n"); |
| 55 | return -ENOMEM; |
| 56 | } |
| 57 | |
| 58 | dt = devm_kzalloc(&ofdev->dev, sizeof(*dt), GFP_KERNEL); |
| 59 | if (!dt) |
| 60 | return -ENOMEM; |
| 61 | denali = &dt->denali; |
| 62 | |
Dinh Nguyen | 30f9f2f | 2012-09-27 10:58:06 -0600 | [diff] [blame] | 63 | denali->platform = DT; |
| 64 | denali->dev = &ofdev->dev; |
| 65 | denali->irq = platform_get_irq(ofdev, 0); |
| 66 | if (denali->irq < 0) { |
| 67 | dev_err(&ofdev->dev, "no irq defined\n"); |
Sachin Kamat | 2f2ff14 | 2013-03-18 15:11:13 +0530 | [diff] [blame] | 68 | return denali->irq; |
Dinh Nguyen | 30f9f2f | 2012-09-27 10:58:06 -0600 | [diff] [blame] | 69 | } |
| 70 | |
Jingoo Han | 7995204 | 2014-02-12 11:29:42 +0900 | [diff] [blame] | 71 | denali_reg = platform_get_resource_byname(ofdev, IORESOURCE_MEM, "denali_reg"); |
| 72 | denali->flash_reg = devm_ioremap_resource(&ofdev->dev, denali_reg); |
| 73 | if (IS_ERR(denali->flash_reg)) |
| 74 | return PTR_ERR(denali->flash_reg); |
Dinh Nguyen | 30f9f2f | 2012-09-27 10:58:06 -0600 | [diff] [blame] | 75 | |
Jingoo Han | 7995204 | 2014-02-12 11:29:42 +0900 | [diff] [blame] | 76 | nand_data = platform_get_resource_byname(ofdev, IORESOURCE_MEM, "nand_data"); |
| 77 | denali->flash_mem = devm_ioremap_resource(&ofdev->dev, nand_data); |
| 78 | if (IS_ERR(denali->flash_mem)) |
| 79 | return PTR_ERR(denali->flash_mem); |
Dinh Nguyen | 30f9f2f | 2012-09-27 10:58:06 -0600 | [diff] [blame] | 80 | |
| 81 | if (!of_property_read_u32(ofdev->dev.of_node, |
| 82 | "dma-mask", (u32 *)&denali_dma_mask)) { |
| 83 | denali->dev->dma_mask = &denali_dma_mask; |
| 84 | } else { |
| 85 | denali->dev->dma_mask = NULL; |
| 86 | } |
| 87 | |
Jingoo Han | 2281f7b6 | 2013-12-26 10:43:11 +0900 | [diff] [blame] | 88 | dt->clk = devm_clk_get(&ofdev->dev, NULL); |
Dinh Nguyen | 30f9f2f | 2012-09-27 10:58:06 -0600 | [diff] [blame] | 89 | if (IS_ERR(dt->clk)) { |
| 90 | dev_err(&ofdev->dev, "no clk available\n"); |
| 91 | return PTR_ERR(dt->clk); |
| 92 | } |
| 93 | clk_prepare_enable(dt->clk); |
| 94 | |
| 95 | ret = denali_init(denali); |
| 96 | if (ret) |
| 97 | goto out_disable_clk; |
| 98 | |
| 99 | platform_set_drvdata(ofdev, dt); |
| 100 | return 0; |
| 101 | |
| 102 | out_disable_clk: |
| 103 | clk_disable_unprepare(dt->clk); |
Dinh Nguyen | 30f9f2f | 2012-09-27 10:58:06 -0600 | [diff] [blame] | 104 | |
| 105 | return ret; |
| 106 | } |
| 107 | |
Bill Pemberton | 810b7e0 | 2012-11-19 13:26:04 -0500 | [diff] [blame] | 108 | static int denali_dt_remove(struct platform_device *ofdev) |
Dinh Nguyen | 30f9f2f | 2012-09-27 10:58:06 -0600 | [diff] [blame] | 109 | { |
| 110 | struct denali_dt *dt = platform_get_drvdata(ofdev); |
| 111 | |
| 112 | denali_remove(&dt->denali); |
| 113 | clk_disable(dt->clk); |
Dinh Nguyen | 30f9f2f | 2012-09-27 10:58:06 -0600 | [diff] [blame] | 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static struct platform_driver denali_dt_driver = { |
| 119 | .probe = denali_dt_probe, |
Bill Pemberton | 5153b88 | 2012-11-19 13:21:24 -0500 | [diff] [blame] | 120 | .remove = denali_dt_remove, |
Dinh Nguyen | 30f9f2f | 2012-09-27 10:58:06 -0600 | [diff] [blame] | 121 | .driver = { |
| 122 | .name = "denali-nand-dt", |
Sachin Kamat | ef54f87 | 2013-03-18 15:11:14 +0530 | [diff] [blame] | 123 | .of_match_table = denali_nand_dt_ids, |
Dinh Nguyen | 30f9f2f | 2012-09-27 10:58:06 -0600 | [diff] [blame] | 124 | }, |
| 125 | }; |
| 126 | |
Sachin Kamat | 82fc812 | 2013-03-18 15:11:12 +0530 | [diff] [blame] | 127 | module_platform_driver(denali_dt_driver); |
Dinh Nguyen | 30f9f2f | 2012-09-27 10:58:06 -0600 | [diff] [blame] | 128 | |
| 129 | MODULE_LICENSE("GPL"); |
| 130 | MODULE_AUTHOR("Jamie Iles"); |
| 131 | MODULE_DESCRIPTION("DT driver for Denali NAND controller"); |