imx-weim.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * EIM driver for Freescale's i.MX chips
  3. *
  4. * Copyright (C) 2013 Freescale Semiconductor, Inc.
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/clk.h>
  12. #include <linux/io.h>
  13. #include <linux/of_device.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
  16. #include <linux/regmap.h>
  17. struct imx_weim_devtype {
  18. unsigned int cs_count;
  19. unsigned int cs_regs_count;
  20. unsigned int cs_stride;
  21. };
  22. static const struct imx_weim_devtype imx1_weim_devtype = {
  23. .cs_count = 6,
  24. .cs_regs_count = 2,
  25. .cs_stride = 0x08,
  26. };
  27. static const struct imx_weim_devtype imx27_weim_devtype = {
  28. .cs_count = 6,
  29. .cs_regs_count = 3,
  30. .cs_stride = 0x10,
  31. };
  32. static const struct imx_weim_devtype imx50_weim_devtype = {
  33. .cs_count = 4,
  34. .cs_regs_count = 6,
  35. .cs_stride = 0x18,
  36. };
  37. static const struct imx_weim_devtype imx51_weim_devtype = {
  38. .cs_count = 6,
  39. .cs_regs_count = 6,
  40. .cs_stride = 0x18,
  41. };
  42. static const struct of_device_id weim_id_table[] = {
  43. /* i.MX1/21 */
  44. { .compatible = "fsl,imx1-weim", .data = &imx1_weim_devtype, },
  45. /* i.MX25/27/31/35 */
  46. { .compatible = "fsl,imx27-weim", .data = &imx27_weim_devtype, },
  47. /* i.MX50/53/6Q */
  48. { .compatible = "fsl,imx50-weim", .data = &imx50_weim_devtype, },
  49. { .compatible = "fsl,imx6q-weim", .data = &imx50_weim_devtype, },
  50. /* i.MX51 */
  51. { .compatible = "fsl,imx51-weim", .data = &imx51_weim_devtype, },
  52. { }
  53. };
  54. MODULE_DEVICE_TABLE(of, weim_id_table);
  55. static int __init imx_weim_gpr_setup(struct platform_device *pdev)
  56. {
  57. struct device_node *np = pdev->dev.of_node;
  58. struct property *prop;
  59. const __be32 *p;
  60. struct regmap *gpr;
  61. u32 gprvals[4] = {
  62. 05, /* CS0(128M) CS1(0M) CS2(0M) CS3(0M) */
  63. 033, /* CS0(64M) CS1(64M) CS2(0M) CS3(0M) */
  64. 0113, /* CS0(64M) CS1(32M) CS2(32M) CS3(0M) */
  65. 01111, /* CS0(32M) CS1(32M) CS2(32M) CS3(32M) */
  66. };
  67. u32 gprval = 0;
  68. u32 val;
  69. int cs = 0;
  70. int i = 0;
  71. gpr = syscon_regmap_lookup_by_phandle(np, "fsl,weim-cs-gpr");
  72. if (IS_ERR(gpr)) {
  73. dev_dbg(&pdev->dev, "failed to find weim-cs-gpr\n");
  74. return 0;
  75. }
  76. of_property_for_each_u32(np, "ranges", prop, p, val) {
  77. if (i % 4 == 0) {
  78. cs = val;
  79. } else if (i % 4 == 3 && val) {
  80. val = (val / SZ_32M) | 1;
  81. gprval |= val << cs * 3;
  82. }
  83. i++;
  84. }
  85. if (i == 0 || i % 4)
  86. goto err;
  87. for (i = 0; i < ARRAY_SIZE(gprvals); i++) {
  88. if (gprval == gprvals[i]) {
  89. /* Found it. Set up IOMUXC_GPR1[11:0] with it. */
  90. regmap_update_bits(gpr, IOMUXC_GPR1, 0xfff, gprval);
  91. return 0;
  92. }
  93. }
  94. err:
  95. dev_err(&pdev->dev, "Invalid 'ranges' configuration\n");
  96. return -EINVAL;
  97. }
  98. /* Parse and set the timing for this device. */
  99. static int __init weim_timing_setup(struct device_node *np, void __iomem *base,
  100. const struct imx_weim_devtype *devtype)
  101. {
  102. u32 cs_idx, value[devtype->cs_regs_count];
  103. int i, ret;
  104. /* get the CS index from this child node's "reg" property. */
  105. ret = of_property_read_u32(np, "reg", &cs_idx);
  106. if (ret)
  107. return ret;
  108. if (cs_idx >= devtype->cs_count)
  109. return -EINVAL;
  110. ret = of_property_read_u32_array(np, "fsl,weim-cs-timing",
  111. value, devtype->cs_regs_count);
  112. if (ret)
  113. return ret;
  114. /* set the timing for WEIM */
  115. for (i = 0; i < devtype->cs_regs_count; i++)
  116. writel(value[i], base + cs_idx * devtype->cs_stride + i * 4);
  117. return 0;
  118. }
  119. static int __init weim_parse_dt(struct platform_device *pdev,
  120. void __iomem *base)
  121. {
  122. const struct of_device_id *of_id = of_match_device(weim_id_table,
  123. &pdev->dev);
  124. const struct imx_weim_devtype *devtype = of_id->data;
  125. struct device_node *child;
  126. int ret, have_child = 0;
  127. if (devtype == &imx50_weim_devtype) {
  128. ret = imx_weim_gpr_setup(pdev);
  129. if (ret)
  130. return ret;
  131. }
  132. for_each_available_child_of_node(pdev->dev.of_node, child) {
  133. if (!child->name)
  134. continue;
  135. ret = weim_timing_setup(child, base, devtype);
  136. if (ret)
  137. dev_warn(&pdev->dev, "%s set timing failed.\n",
  138. child->full_name);
  139. else
  140. have_child = 1;
  141. }
  142. if (have_child)
  143. ret = of_platform_default_populate(pdev->dev.of_node,
  144. NULL, &pdev->dev);
  145. if (ret)
  146. dev_err(&pdev->dev, "%s fail to create devices.\n",
  147. pdev->dev.of_node->full_name);
  148. return ret;
  149. }
  150. static int __init weim_probe(struct platform_device *pdev)
  151. {
  152. struct resource *res;
  153. struct clk *clk;
  154. void __iomem *base;
  155. int ret;
  156. /* get the resource */
  157. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  158. base = devm_ioremap_resource(&pdev->dev, res);
  159. if (IS_ERR(base))
  160. return PTR_ERR(base);
  161. /* get the clock */
  162. clk = devm_clk_get(&pdev->dev, NULL);
  163. if (IS_ERR(clk))
  164. return PTR_ERR(clk);
  165. ret = clk_prepare_enable(clk);
  166. if (ret)
  167. return ret;
  168. /* parse the device node */
  169. ret = weim_parse_dt(pdev, base);
  170. if (ret)
  171. clk_disable_unprepare(clk);
  172. else
  173. dev_info(&pdev->dev, "Driver registered.\n");
  174. return ret;
  175. }
  176. static struct platform_driver weim_driver = {
  177. .driver = {
  178. .name = "imx-weim",
  179. .of_match_table = weim_id_table,
  180. },
  181. };
  182. module_platform_driver_probe(weim_driver, weim_probe);
  183. MODULE_AUTHOR("Freescale Semiconductor Inc.");
  184. MODULE_DESCRIPTION("i.MX EIM Controller Driver");
  185. MODULE_LICENSE("GPL");