imx-weim.c 5.2 KB

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