imx-weim.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. unsigned int wcr_offset;
  22. unsigned int wcr_bcm;
  23. };
  24. static const struct imx_weim_devtype imx1_weim_devtype = {
  25. .cs_count = 6,
  26. .cs_regs_count = 2,
  27. .cs_stride = 0x08,
  28. };
  29. static const struct imx_weim_devtype imx27_weim_devtype = {
  30. .cs_count = 6,
  31. .cs_regs_count = 3,
  32. .cs_stride = 0x10,
  33. };
  34. static const struct imx_weim_devtype imx50_weim_devtype = {
  35. .cs_count = 4,
  36. .cs_regs_count = 6,
  37. .cs_stride = 0x18,
  38. .wcr_offset = 0x90,
  39. .wcr_bcm = BIT(0),
  40. };
  41. static const struct imx_weim_devtype imx51_weim_devtype = {
  42. .cs_count = 6,
  43. .cs_regs_count = 6,
  44. .cs_stride = 0x18,
  45. };
  46. #define MAX_CS_REGS_COUNT 6
  47. #define MAX_CS_COUNT 6
  48. #define OF_REG_SIZE 3
  49. struct cs_timing {
  50. bool is_applied;
  51. u32 regs[MAX_CS_REGS_COUNT];
  52. };
  53. struct cs_timing_state {
  54. struct cs_timing cs[MAX_CS_COUNT];
  55. };
  56. static const struct of_device_id weim_id_table[] = {
  57. /* i.MX1/21 */
  58. { .compatible = "fsl,imx1-weim", .data = &imx1_weim_devtype, },
  59. /* i.MX25/27/31/35 */
  60. { .compatible = "fsl,imx27-weim", .data = &imx27_weim_devtype, },
  61. /* i.MX50/53/6Q */
  62. { .compatible = "fsl,imx50-weim", .data = &imx50_weim_devtype, },
  63. { .compatible = "fsl,imx6q-weim", .data = &imx50_weim_devtype, },
  64. /* i.MX51 */
  65. { .compatible = "fsl,imx51-weim", .data = &imx51_weim_devtype, },
  66. { }
  67. };
  68. MODULE_DEVICE_TABLE(of, weim_id_table);
  69. static int imx_weim_gpr_setup(struct platform_device *pdev)
  70. {
  71. struct device_node *np = pdev->dev.of_node;
  72. struct property *prop;
  73. const __be32 *p;
  74. struct regmap *gpr;
  75. u32 gprvals[4] = {
  76. 05, /* CS0(128M) CS1(0M) CS2(0M) CS3(0M) */
  77. 033, /* CS0(64M) CS1(64M) CS2(0M) CS3(0M) */
  78. 0113, /* CS0(64M) CS1(32M) CS2(32M) CS3(0M) */
  79. 01111, /* CS0(32M) CS1(32M) CS2(32M) CS3(32M) */
  80. };
  81. u32 gprval = 0;
  82. u32 val;
  83. int cs = 0;
  84. int i = 0;
  85. gpr = syscon_regmap_lookup_by_phandle(np, "fsl,weim-cs-gpr");
  86. if (IS_ERR(gpr)) {
  87. dev_dbg(&pdev->dev, "failed to find weim-cs-gpr\n");
  88. return 0;
  89. }
  90. of_property_for_each_u32(np, "ranges", prop, p, val) {
  91. if (i % 4 == 0) {
  92. cs = val;
  93. } else if (i % 4 == 3 && val) {
  94. val = (val / SZ_32M) | 1;
  95. gprval |= val << cs * 3;
  96. }
  97. i++;
  98. }
  99. if (i == 0 || i % 4)
  100. goto err;
  101. for (i = 0; i < ARRAY_SIZE(gprvals); i++) {
  102. if (gprval == gprvals[i]) {
  103. /* Found it. Set up IOMUXC_GPR1[11:0] with it. */
  104. regmap_update_bits(gpr, IOMUXC_GPR1, 0xfff, gprval);
  105. return 0;
  106. }
  107. }
  108. err:
  109. dev_err(&pdev->dev, "Invalid 'ranges' configuration\n");
  110. return -EINVAL;
  111. }
  112. /* Parse and set the timing for this device. */
  113. static int weim_timing_setup(struct device *dev,
  114. struct device_node *np, void __iomem *base,
  115. const struct imx_weim_devtype *devtype,
  116. struct cs_timing_state *ts)
  117. {
  118. u32 cs_idx, value[MAX_CS_REGS_COUNT];
  119. int i, ret;
  120. int reg_idx, num_regs;
  121. struct cs_timing *cst;
  122. if (WARN_ON(devtype->cs_regs_count > MAX_CS_REGS_COUNT))
  123. return -EINVAL;
  124. if (WARN_ON(devtype->cs_count > MAX_CS_COUNT))
  125. return -EINVAL;
  126. ret = of_property_read_u32_array(np, "fsl,weim-cs-timing",
  127. value, devtype->cs_regs_count);
  128. if (ret)
  129. return ret;
  130. /*
  131. * the child node's "reg" property may contain multiple address ranges,
  132. * extract the chip select for each.
  133. */
  134. num_regs = of_property_count_elems_of_size(np, "reg", OF_REG_SIZE);
  135. if (num_regs < 0)
  136. return num_regs;
  137. if (!num_regs)
  138. return -EINVAL;
  139. for (reg_idx = 0; reg_idx < num_regs; reg_idx++) {
  140. /* get the CS index from this child node's "reg" property. */
  141. ret = of_property_read_u32_index(np, "reg",
  142. reg_idx * OF_REG_SIZE, &cs_idx);
  143. if (ret)
  144. break;
  145. if (cs_idx >= devtype->cs_count)
  146. return -EINVAL;
  147. /* prevent re-configuring a CS that's already been configured */
  148. cst = &ts->cs[cs_idx];
  149. if (cst->is_applied && memcmp(value, cst->regs,
  150. devtype->cs_regs_count * sizeof(u32))) {
  151. dev_err(dev, "fsl,weim-cs-timing conflict on %pOF", np);
  152. return -EINVAL;
  153. }
  154. /* set the timing for WEIM */
  155. for (i = 0; i < devtype->cs_regs_count; i++)
  156. writel(value[i],
  157. base + cs_idx * devtype->cs_stride + i * 4);
  158. if (!cst->is_applied) {
  159. cst->is_applied = true;
  160. memcpy(cst->regs, value,
  161. devtype->cs_regs_count * sizeof(u32));
  162. }
  163. }
  164. return 0;
  165. }
  166. static int weim_parse_dt(struct platform_device *pdev, void __iomem *base)
  167. {
  168. const struct of_device_id *of_id = of_match_device(weim_id_table,
  169. &pdev->dev);
  170. const struct imx_weim_devtype *devtype = of_id->data;
  171. struct device_node *child;
  172. int ret, have_child = 0;
  173. struct cs_timing_state ts = {};
  174. u32 reg;
  175. if (devtype == &imx50_weim_devtype) {
  176. ret = imx_weim_gpr_setup(pdev);
  177. if (ret)
  178. return ret;
  179. }
  180. if (of_property_read_bool(pdev->dev.of_node, "fsl,burst-clk-enable")) {
  181. if (devtype->wcr_bcm) {
  182. reg = readl(base + devtype->wcr_offset);
  183. writel(reg | devtype->wcr_bcm,
  184. base + devtype->wcr_offset);
  185. } else {
  186. dev_err(&pdev->dev, "burst clk mode not supported.\n");
  187. return -EINVAL;
  188. }
  189. }
  190. for_each_available_child_of_node(pdev->dev.of_node, child) {
  191. ret = weim_timing_setup(&pdev->dev, child, base, devtype, &ts);
  192. if (ret)
  193. dev_warn(&pdev->dev, "%pOF set timing failed.\n",
  194. child);
  195. else
  196. have_child = 1;
  197. }
  198. if (have_child)
  199. ret = of_platform_default_populate(pdev->dev.of_node,
  200. NULL, &pdev->dev);
  201. if (ret)
  202. dev_err(&pdev->dev, "%pOF fail to create devices.\n",
  203. pdev->dev.of_node);
  204. return ret;
  205. }
  206. static int weim_probe(struct platform_device *pdev)
  207. {
  208. struct resource *res;
  209. struct clk *clk;
  210. void __iomem *base;
  211. int ret;
  212. /* get the resource */
  213. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  214. base = devm_ioremap_resource(&pdev->dev, res);
  215. if (IS_ERR(base))
  216. return PTR_ERR(base);
  217. /* get the clock */
  218. clk = devm_clk_get(&pdev->dev, NULL);
  219. if (IS_ERR(clk))
  220. return PTR_ERR(clk);
  221. ret = clk_prepare_enable(clk);
  222. if (ret)
  223. return ret;
  224. /* parse the device node */
  225. ret = weim_parse_dt(pdev, base);
  226. if (ret)
  227. clk_disable_unprepare(clk);
  228. else
  229. dev_info(&pdev->dev, "Driver registered.\n");
  230. return ret;
  231. }
  232. static struct platform_driver weim_driver = {
  233. .driver = {
  234. .name = "imx-weim",
  235. .of_match_table = weim_id_table,
  236. },
  237. .probe = weim_probe,
  238. };
  239. module_platform_driver(weim_driver);
  240. MODULE_AUTHOR("Freescale Semiconductor Inc.");
  241. MODULE_DESCRIPTION("i.MX EIM Controller Driver");
  242. MODULE_LICENSE("GPL");