Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 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> |
Shawn Guo | 8d9ee21 | 2014-02-11 09:52:09 +0800 | [diff] [blame] | 14 | #include <linux/mfd/syscon.h> |
| 15 | #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h> |
| 16 | #include <linux/regmap.h> |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 17 | |
Alexander Shiyan | 3f98b6b | 2013-06-29 08:27:54 +0400 | [diff] [blame] | 18 | struct imx_weim_devtype { |
| 19 | unsigned int cs_count; |
| 20 | unsigned int cs_regs_count; |
| 21 | unsigned int cs_stride; |
| 22 | }; |
| 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 | |
| 30 | static const struct imx_weim_devtype imx27_weim_devtype = { |
| 31 | .cs_count = 6, |
| 32 | .cs_regs_count = 3, |
| 33 | .cs_stride = 0x10, |
| 34 | }; |
| 35 | |
| 36 | static const struct imx_weim_devtype imx50_weim_devtype = { |
| 37 | .cs_count = 4, |
| 38 | .cs_regs_count = 6, |
| 39 | .cs_stride = 0x18, |
| 40 | }; |
| 41 | |
| 42 | static const struct imx_weim_devtype imx51_weim_devtype = { |
| 43 | .cs_count = 6, |
| 44 | .cs_regs_count = 6, |
| 45 | .cs_stride = 0x18, |
| 46 | }; |
| 47 | |
Kees Cook | d8dfa59 | 2018-06-28 17:04:21 -0700 | [diff] [blame] | 48 | #define MAX_CS_REGS_COUNT 6 |
Sven Van Asbroeck | 8b8cb52 | 2018-12-17 10:47:59 -0500 | [diff] [blame^] | 49 | #define OF_REG_SIZE 3 |
Kees Cook | d8dfa59 | 2018-06-28 17:04:21 -0700 | [diff] [blame] | 50 | |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 51 | static const struct of_device_id weim_id_table[] = { |
Alexander Shiyan | 3f98b6b | 2013-06-29 08:27:54 +0400 | [diff] [blame] | 52 | /* i.MX1/21 */ |
| 53 | { .compatible = "fsl,imx1-weim", .data = &imx1_weim_devtype, }, |
| 54 | /* i.MX25/27/31/35 */ |
| 55 | { .compatible = "fsl,imx27-weim", .data = &imx27_weim_devtype, }, |
| 56 | /* i.MX50/53/6Q */ |
| 57 | { .compatible = "fsl,imx50-weim", .data = &imx50_weim_devtype, }, |
| 58 | { .compatible = "fsl,imx6q-weim", .data = &imx50_weim_devtype, }, |
| 59 | /* i.MX51 */ |
| 60 | { .compatible = "fsl,imx51-weim", .data = &imx51_weim_devtype, }, |
| 61 | { } |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 62 | }; |
| 63 | MODULE_DEVICE_TABLE(of, weim_id_table); |
| 64 | |
Shawn Guo | 8d9ee21 | 2014-02-11 09:52:09 +0800 | [diff] [blame] | 65 | static int __init imx_weim_gpr_setup(struct platform_device *pdev) |
| 66 | { |
| 67 | struct device_node *np = pdev->dev.of_node; |
| 68 | struct property *prop; |
| 69 | const __be32 *p; |
| 70 | struct regmap *gpr; |
| 71 | u32 gprvals[4] = { |
| 72 | 05, /* CS0(128M) CS1(0M) CS2(0M) CS3(0M) */ |
| 73 | 033, /* CS0(64M) CS1(64M) CS2(0M) CS3(0M) */ |
| 74 | 0113, /* CS0(64M) CS1(32M) CS2(32M) CS3(0M) */ |
| 75 | 01111, /* CS0(32M) CS1(32M) CS2(32M) CS3(32M) */ |
| 76 | }; |
| 77 | u32 gprval = 0; |
| 78 | u32 val; |
| 79 | int cs = 0; |
| 80 | int i = 0; |
| 81 | |
| 82 | gpr = syscon_regmap_lookup_by_phandle(np, "fsl,weim-cs-gpr"); |
| 83 | if (IS_ERR(gpr)) { |
| 84 | dev_dbg(&pdev->dev, "failed to find weim-cs-gpr\n"); |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | of_property_for_each_u32(np, "ranges", prop, p, val) { |
| 89 | if (i % 4 == 0) { |
| 90 | cs = val; |
| 91 | } else if (i % 4 == 3 && val) { |
| 92 | val = (val / SZ_32M) | 1; |
| 93 | gprval |= val << cs * 3; |
| 94 | } |
| 95 | i++; |
| 96 | } |
| 97 | |
| 98 | if (i == 0 || i % 4) |
| 99 | goto err; |
| 100 | |
| 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 | |
| 109 | err: |
| 110 | dev_err(&pdev->dev, "Invalid 'ranges' configuration\n"); |
| 111 | return -EINVAL; |
| 112 | } |
| 113 | |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 114 | /* Parse and set the timing for this device. */ |
Alexander Shiyan | 3f98b6b | 2013-06-29 08:27:54 +0400 | [diff] [blame] | 115 | static int __init weim_timing_setup(struct device_node *np, void __iomem *base, |
| 116 | const struct imx_weim_devtype *devtype) |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 117 | { |
Kees Cook | d8dfa59 | 2018-06-28 17:04:21 -0700 | [diff] [blame] | 118 | u32 cs_idx, value[MAX_CS_REGS_COUNT]; |
Alexander Shiyan | 3f98b6b | 2013-06-29 08:27:54 +0400 | [diff] [blame] | 119 | int i, ret; |
Sven Van Asbroeck | 8b8cb52 | 2018-12-17 10:47:59 -0500 | [diff] [blame^] | 120 | int reg_idx, num_regs; |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 121 | |
Kees Cook | d8dfa59 | 2018-06-28 17:04:21 -0700 | [diff] [blame] | 122 | if (WARN_ON(devtype->cs_regs_count > MAX_CS_REGS_COUNT)) |
| 123 | return -EINVAL; |
| 124 | |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 125 | ret = of_property_read_u32_array(np, "fsl,weim-cs-timing", |
Alexander Shiyan | 3f98b6b | 2013-06-29 08:27:54 +0400 | [diff] [blame] | 126 | value, devtype->cs_regs_count); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 127 | if (ret) |
| 128 | return ret; |
| 129 | |
Sven Van Asbroeck | 8b8cb52 | 2018-12-17 10:47:59 -0500 | [diff] [blame^] | 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 | |
| 146 | if (cs_idx >= devtype->cs_count) |
| 147 | return -EINVAL; |
| 148 | |
| 149 | /* set the timing for WEIM */ |
| 150 | for (i = 0; i < devtype->cs_regs_count; i++) |
| 151 | writel(value[i], |
| 152 | base + cs_idx * devtype->cs_stride + i * 4); |
| 153 | } |
Alexander Shiyan | 3f98b6b | 2013-06-29 08:27:54 +0400 | [diff] [blame] | 154 | |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
Alexander Shiyan | 29e5497 | 2013-06-29 08:27:52 +0400 | [diff] [blame] | 158 | static int __init weim_parse_dt(struct platform_device *pdev, |
| 159 | void __iomem *base) |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 160 | { |
Alexander Shiyan | 3f98b6b | 2013-06-29 08:27:54 +0400 | [diff] [blame] | 161 | const struct of_device_id *of_id = of_match_device(weim_id_table, |
| 162 | &pdev->dev); |
| 163 | const struct imx_weim_devtype *devtype = of_id->data; |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 164 | struct device_node *child; |
Alison Chaiken | 52c47b6 | 2015-02-18 23:24:10 -0800 | [diff] [blame] | 165 | int ret, have_child = 0; |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 166 | |
Shawn Guo | 8d9ee21 | 2014-02-11 09:52:09 +0800 | [diff] [blame] | 167 | if (devtype == &imx50_weim_devtype) { |
| 168 | ret = imx_weim_gpr_setup(pdev); |
| 169 | if (ret) |
| 170 | return ret; |
| 171 | } |
| 172 | |
Fabio Estevam | 33b96d2 | 2016-02-22 09:01:53 -0300 | [diff] [blame] | 173 | for_each_available_child_of_node(pdev->dev.of_node, child) { |
Alexander Shiyan | 3f98b6b | 2013-06-29 08:27:54 +0400 | [diff] [blame] | 174 | ret = weim_timing_setup(child, base, devtype); |
Alison Chaiken | 52c47b6 | 2015-02-18 23:24:10 -0800 | [diff] [blame] | 175 | if (ret) |
Rob Herring | 9c0982d | 2017-07-18 16:42:51 -0500 | [diff] [blame] | 176 | dev_warn(&pdev->dev, "%pOF set timing failed.\n", |
| 177 | child); |
Alison Chaiken | 52c47b6 | 2015-02-18 23:24:10 -0800 | [diff] [blame] | 178 | else |
| 179 | have_child = 1; |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 180 | } |
| 181 | |
Alison Chaiken | 52c47b6 | 2015-02-18 23:24:10 -0800 | [diff] [blame] | 182 | if (have_child) |
Kefeng Wang | 39ec8d3 | 2016-06-01 14:53:07 +0800 | [diff] [blame] | 183 | ret = of_platform_default_populate(pdev->dev.of_node, |
| 184 | NULL, &pdev->dev); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 185 | if (ret) |
Rob Herring | 9c0982d | 2017-07-18 16:42:51 -0500 | [diff] [blame] | 186 | dev_err(&pdev->dev, "%pOF fail to create devices.\n", |
| 187 | pdev->dev.of_node); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 188 | return ret; |
| 189 | } |
| 190 | |
Alexander Shiyan | 29e5497 | 2013-06-29 08:27:52 +0400 | [diff] [blame] | 191 | static int __init weim_probe(struct platform_device *pdev) |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 192 | { |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 193 | struct resource *res; |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 194 | struct clk *clk; |
| 195 | void __iomem *base; |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 196 | int ret; |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 197 | |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 198 | /* get the resource */ |
| 199 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 200 | base = devm_ioremap_resource(&pdev->dev, res); |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 201 | if (IS_ERR(base)) |
| 202 | return PTR_ERR(base); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 203 | |
| 204 | /* get the clock */ |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 205 | clk = devm_clk_get(&pdev->dev, NULL); |
| 206 | if (IS_ERR(clk)) |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 207 | return PTR_ERR(clk); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 208 | |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 209 | ret = clk_prepare_enable(clk); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 210 | if (ret) |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 211 | return ret; |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 212 | |
| 213 | /* parse the device node */ |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 214 | ret = weim_parse_dt(pdev, base); |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 215 | if (ret) |
Alexander Shiyan | 70ac98d | 2013-06-29 08:27:50 +0400 | [diff] [blame] | 216 | clk_disable_unprepare(clk); |
Alexander Shiyan | b2d1fb7 | 2013-06-29 08:27:51 +0400 | [diff] [blame] | 217 | else |
| 218 | dev_info(&pdev->dev, "Driver registered.\n"); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 219 | |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | static struct platform_driver weim_driver = { |
| 224 | .driver = { |
Alexander Shiyan | fc608c7 | 2013-06-29 08:27:53 +0400 | [diff] [blame] | 225 | .name = "imx-weim", |
Alexander Shiyan | fc608c7 | 2013-06-29 08:27:53 +0400 | [diff] [blame] | 226 | .of_match_table = weim_id_table, |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 227 | }, |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 228 | }; |
Alexander Shiyan | 29e5497 | 2013-06-29 08:27:52 +0400 | [diff] [blame] | 229 | module_platform_driver_probe(weim_driver, weim_probe); |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 230 | |
Huang Shijie | 85bf6d4 | 2013-05-28 14:20:07 +0800 | [diff] [blame] | 231 | MODULE_AUTHOR("Freescale Semiconductor Inc."); |
| 232 | MODULE_DESCRIPTION("i.MX EIM Controller Driver"); |
| 233 | MODULE_LICENSE("GPL"); |