denali_dt.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #include "denali.h"
  26. struct denali_dt {
  27. struct denali_nand_info denali;
  28. struct clk *clk;
  29. };
  30. static void __iomem *request_and_map(struct device *dev,
  31. const struct resource *res)
  32. {
  33. void __iomem *ptr;
  34. if (!devm_request_mem_region(dev, res->start, resource_size(res),
  35. "denali-dt")) {
  36. dev_err(dev, "unable to request %s\n", res->name);
  37. return NULL;
  38. }
  39. ptr = devm_ioremap_nocache(dev, res->start, resource_size(res));
  40. if (!res)
  41. dev_err(dev, "ioremap_nocache of %s failed!", res->name);
  42. return ptr;
  43. }
  44. static const struct of_device_id denali_nand_dt_ids[] = {
  45. { .compatible = "denali,denali-nand-dt" },
  46. { /* sentinel */ }
  47. };
  48. MODULE_DEVICE_TABLE(of, denali_nand_dt_ids);
  49. static u64 denali_dma_mask;
  50. static int denali_dt_probe(struct platform_device *ofdev)
  51. {
  52. struct resource *denali_reg, *nand_data;
  53. struct denali_dt *dt;
  54. struct denali_nand_info *denali;
  55. int ret;
  56. const struct of_device_id *of_id;
  57. of_id = of_match_device(denali_nand_dt_ids, &ofdev->dev);
  58. if (of_id) {
  59. ofdev->id_entry = of_id->data;
  60. } else {
  61. pr_err("Failed to find the right device id.\n");
  62. return -ENOMEM;
  63. }
  64. dt = devm_kzalloc(&ofdev->dev, sizeof(*dt), GFP_KERNEL);
  65. if (!dt)
  66. return -ENOMEM;
  67. denali = &dt->denali;
  68. denali_reg = platform_get_resource_byname(ofdev, IORESOURCE_MEM, "denali_reg");
  69. nand_data = platform_get_resource_byname(ofdev, IORESOURCE_MEM, "nand_data");
  70. if (!denali_reg || !nand_data) {
  71. dev_err(&ofdev->dev, "resources not completely defined\n");
  72. return -EINVAL;
  73. }
  74. denali->platform = DT;
  75. denali->dev = &ofdev->dev;
  76. denali->irq = platform_get_irq(ofdev, 0);
  77. if (denali->irq < 0) {
  78. dev_err(&ofdev->dev, "no irq defined\n");
  79. return -ENXIO;
  80. }
  81. denali->flash_reg = request_and_map(&ofdev->dev, denali_reg);
  82. if (!denali->flash_reg)
  83. return -ENOMEM;
  84. denali->flash_mem = request_and_map(&ofdev->dev, nand_data);
  85. if (!denali->flash_mem)
  86. return -ENOMEM;
  87. if (!of_property_read_u32(ofdev->dev.of_node,
  88. "dma-mask", (u32 *)&denali_dma_mask)) {
  89. denali->dev->dma_mask = &denali_dma_mask;
  90. } else {
  91. denali->dev->dma_mask = NULL;
  92. }
  93. dt->clk = clk_get(&ofdev->dev, NULL);
  94. if (IS_ERR(dt->clk)) {
  95. dev_err(&ofdev->dev, "no clk available\n");
  96. return PTR_ERR(dt->clk);
  97. }
  98. clk_prepare_enable(dt->clk);
  99. ret = denali_init(denali);
  100. if (ret)
  101. goto out_disable_clk;
  102. platform_set_drvdata(ofdev, dt);
  103. return 0;
  104. out_disable_clk:
  105. clk_disable_unprepare(dt->clk);
  106. clk_put(dt->clk);
  107. return ret;
  108. }
  109. static int denali_dt_remove(struct platform_device *ofdev)
  110. {
  111. struct denali_dt *dt = platform_get_drvdata(ofdev);
  112. denali_remove(&dt->denali);
  113. clk_disable(dt->clk);
  114. clk_put(dt->clk);
  115. return 0;
  116. }
  117. static struct platform_driver denali_dt_driver = {
  118. .probe = denali_dt_probe,
  119. .remove = denali_dt_remove,
  120. .driver = {
  121. .name = "denali-nand-dt",
  122. .owner = THIS_MODULE,
  123. .of_match_table = of_match_ptr(denali_nand_dt_ids),
  124. },
  125. };
  126. static int __init denali_init_dt(void)
  127. {
  128. return platform_driver_register(&denali_dt_driver);
  129. }
  130. module_init(denali_init_dt);
  131. static void __exit denali_exit_dt(void)
  132. {
  133. platform_driver_unregister(&denali_dt_driver);
  134. }
  135. module_exit(denali_exit_dt);
  136. MODULE_LICENSE("GPL");
  137. MODULE_AUTHOR("Jamie Iles");
  138. MODULE_DESCRIPTION("DT driver for Denali NAND controller");