1 // SPDX-License-Identifier: GPL-2.0-only
3 * JZ4780 NAND/external memory controller (NEMC)
5 * Copyright (c) 2015 Imagination Technologies
6 * Author: Alex Smith <alex@alex-smith.me.uk>
10 #include <linux/init.h>
11 #include <linux/math64.h>
13 #include <linux/of_address.h>
14 #include <linux/of_device.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
20 #include <linux/jz4780-nemc.h>
22 #define NEMC_SMCRn(n) (0x14 + (((n) - 1) * 4))
23 #define NEMC_NFCSR 0x50
25 #define NEMC_SMCR_SMT BIT(0)
26 #define NEMC_SMCR_BW_SHIFT 6
27 #define NEMC_SMCR_BW_MASK (0x3 << NEMC_SMCR_BW_SHIFT)
28 #define NEMC_SMCR_BW_8 (0 << 6)
29 #define NEMC_SMCR_TAS_SHIFT 8
30 #define NEMC_SMCR_TAS_MASK (0xf << NEMC_SMCR_TAS_SHIFT)
31 #define NEMC_SMCR_TAH_SHIFT 12
32 #define NEMC_SMCR_TAH_MASK (0xf << NEMC_SMCR_TAH_SHIFT)
33 #define NEMC_SMCR_TBP_SHIFT 16
34 #define NEMC_SMCR_TBP_MASK (0xf << NEMC_SMCR_TBP_SHIFT)
35 #define NEMC_SMCR_TAW_SHIFT 20
36 #define NEMC_SMCR_TAW_MASK (0xf << NEMC_SMCR_TAW_SHIFT)
37 #define NEMC_SMCR_TSTRV_SHIFT 24
38 #define NEMC_SMCR_TSTRV_MASK (0x3f << NEMC_SMCR_TSTRV_SHIFT)
40 #define NEMC_NFCSR_NFEn(n) BIT(((n) - 1) << 1)
41 #define NEMC_NFCSR_NFCEn(n) BIT((((n) - 1) << 1) + 1)
42 #define NEMC_NFCSR_TNFEn(n) BIT(16 + (n) - 1)
45 u8 tas_tah_cycles_max
;
51 const struct jz_soc_info
*soc_info
;
55 unsigned long banks_present
;
59 * jz4780_nemc_num_banks() - count the number of banks referenced by a device
60 * @dev: device to count banks for, must be a child of the NEMC.
62 * Return: The number of unique NEMC banks referred to by the specified NEMC
63 * child device. Unique here means that a device that references the same bank
64 * multiple times in its "reg" property will only count once.
66 unsigned int jz4780_nemc_num_banks(struct device
*dev
)
69 unsigned int bank
, count
= 0;
70 unsigned long referenced
= 0;
73 while ((prop
= of_get_address(dev
->of_node
, i
++, NULL
, NULL
))) {
74 bank
= of_read_number(prop
, 1);
75 if (!(referenced
& BIT(bank
))) {
76 referenced
|= BIT(bank
);
83 EXPORT_SYMBOL(jz4780_nemc_num_banks
);
86 * jz4780_nemc_set_type() - set the type of device connected to a bank
87 * @dev: child device of the NEMC.
88 * @bank: bank number to configure.
89 * @type: type of device connected to the bank.
91 void jz4780_nemc_set_type(struct device
*dev
, unsigned int bank
,
92 enum jz4780_nemc_bank_type type
)
94 struct jz4780_nemc
*nemc
= dev_get_drvdata(dev
->parent
);
97 nfcsr
= readl(nemc
->base
+ NEMC_NFCSR
);
99 /* TODO: Support toggle NAND devices. */
101 case JZ4780_NEMC_BANK_SRAM
:
102 nfcsr
&= ~(NEMC_NFCSR_TNFEn(bank
) | NEMC_NFCSR_NFEn(bank
));
104 case JZ4780_NEMC_BANK_NAND
:
105 nfcsr
&= ~NEMC_NFCSR_TNFEn(bank
);
106 nfcsr
|= NEMC_NFCSR_NFEn(bank
);
110 writel(nfcsr
, nemc
->base
+ NEMC_NFCSR
);
112 EXPORT_SYMBOL(jz4780_nemc_set_type
);
115 * jz4780_nemc_assert() - (de-)assert a NAND device's chip enable pin
116 * @dev: child device of the NEMC.
117 * @bank: bank number of device.
118 * @assert: whether the chip enable pin should be asserted.
120 * (De-)asserts the chip enable pin for the NAND device connected to the
123 void jz4780_nemc_assert(struct device
*dev
, unsigned int bank
, bool assert)
125 struct jz4780_nemc
*nemc
= dev_get_drvdata(dev
->parent
);
128 nfcsr
= readl(nemc
->base
+ NEMC_NFCSR
);
131 nfcsr
|= NEMC_NFCSR_NFCEn(bank
);
133 nfcsr
&= ~NEMC_NFCSR_NFCEn(bank
);
135 writel(nfcsr
, nemc
->base
+ NEMC_NFCSR
);
137 EXPORT_SYMBOL(jz4780_nemc_assert
);
139 static uint32_t jz4780_nemc_clk_period(struct jz4780_nemc
*nemc
)
143 rate
= clk_get_rate(nemc
->clk
);
147 /* Return in picoseconds. */
148 return div64_ul(1000000000000ull, rate
);
151 static uint32_t jz4780_nemc_ns_to_cycles(struct jz4780_nemc
*nemc
, uint32_t ns
)
153 return ((ns
* 1000) + nemc
->clk_period
- 1) / nemc
->clk_period
;
156 static bool jz4780_nemc_configure_bank(struct jz4780_nemc
*nemc
,
158 struct device_node
*node
)
160 uint32_t smcr
, val
, cycles
;
163 * Conversion of tBP and tAW cycle counts to values supported by the
164 * hardware (round up to the next supported value).
166 static const u8 convert_tBP_tAW
[] = {
167 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
169 /* 11 - 12 -> 12 cycles */
172 /* 13 - 15 -> 15 cycles */
175 /* 16 - 20 -> 20 cycles */
178 /* 21 - 25 -> 25 cycles */
181 /* 26 - 31 -> 31 cycles */
182 15, 15, 15, 15, 15, 15
185 smcr
= readl(nemc
->base
+ NEMC_SMCRn(bank
));
186 smcr
&= ~NEMC_SMCR_SMT
;
188 if (!of_property_read_u32(node
, "ingenic,nemc-bus-width", &val
)) {
189 smcr
&= ~NEMC_SMCR_BW_MASK
;
192 smcr
|= NEMC_SMCR_BW_8
;
196 * Earlier SoCs support a 16 bit bus width (the 4780
197 * does not), until those are properly supported, error.
199 dev_err(nemc
->dev
, "unsupported bus width: %u\n", val
);
204 if (of_property_read_u32(node
, "ingenic,nemc-tAS", &val
) == 0) {
205 smcr
&= ~NEMC_SMCR_TAS_MASK
;
206 cycles
= jz4780_nemc_ns_to_cycles(nemc
, val
);
207 if (cycles
> nemc
->soc_info
->tas_tah_cycles_max
) {
208 dev_err(nemc
->dev
, "tAS %u is too high (%u cycles)\n",
213 smcr
|= cycles
<< NEMC_SMCR_TAS_SHIFT
;
216 if (of_property_read_u32(node
, "ingenic,nemc-tAH", &val
) == 0) {
217 smcr
&= ~NEMC_SMCR_TAH_MASK
;
218 cycles
= jz4780_nemc_ns_to_cycles(nemc
, val
);
219 if (cycles
> nemc
->soc_info
->tas_tah_cycles_max
) {
220 dev_err(nemc
->dev
, "tAH %u is too high (%u cycles)\n",
225 smcr
|= cycles
<< NEMC_SMCR_TAH_SHIFT
;
228 if (of_property_read_u32(node
, "ingenic,nemc-tBP", &val
) == 0) {
229 smcr
&= ~NEMC_SMCR_TBP_MASK
;
230 cycles
= jz4780_nemc_ns_to_cycles(nemc
, val
);
232 dev_err(nemc
->dev
, "tBP %u is too high (%u cycles)\n",
237 smcr
|= convert_tBP_tAW
[cycles
] << NEMC_SMCR_TBP_SHIFT
;
240 if (of_property_read_u32(node
, "ingenic,nemc-tAW", &val
) == 0) {
241 smcr
&= ~NEMC_SMCR_TAW_MASK
;
242 cycles
= jz4780_nemc_ns_to_cycles(nemc
, val
);
244 dev_err(nemc
->dev
, "tAW %u is too high (%u cycles)\n",
249 smcr
|= convert_tBP_tAW
[cycles
] << NEMC_SMCR_TAW_SHIFT
;
252 if (of_property_read_u32(node
, "ingenic,nemc-tSTRV", &val
) == 0) {
253 smcr
&= ~NEMC_SMCR_TSTRV_MASK
;
254 cycles
= jz4780_nemc_ns_to_cycles(nemc
, val
);
256 dev_err(nemc
->dev
, "tSTRV %u is too high (%u cycles)\n",
261 smcr
|= cycles
<< NEMC_SMCR_TSTRV_SHIFT
;
264 writel(smcr
, nemc
->base
+ NEMC_SMCRn(bank
));
268 static int jz4780_nemc_probe(struct platform_device
*pdev
)
270 struct device
*dev
= &pdev
->dev
;
271 struct jz4780_nemc
*nemc
;
272 struct resource
*res
;
273 struct device_node
*child
;
276 unsigned long referenced
;
279 nemc
= devm_kzalloc(dev
, sizeof(*nemc
), GFP_KERNEL
);
283 nemc
->soc_info
= device_get_match_data(dev
);
287 spin_lock_init(&nemc
->lock
);
290 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
291 nemc
->base
= devm_ioremap_resource(dev
, res
);
292 if (IS_ERR(nemc
->base
)) {
293 dev_err(dev
, "failed to get I/O memory\n");
294 return PTR_ERR(nemc
->base
);
297 writel(0, nemc
->base
+ NEMC_NFCSR
);
299 nemc
->clk
= devm_clk_get(dev
, NULL
);
300 if (IS_ERR(nemc
->clk
)) {
301 dev_err(dev
, "failed to get clock\n");
302 return PTR_ERR(nemc
->clk
);
305 ret
= clk_prepare_enable(nemc
->clk
);
307 dev_err(dev
, "failed to enable clock: %d\n", ret
);
311 nemc
->clk_period
= jz4780_nemc_clk_period(nemc
);
312 if (!nemc
->clk_period
) {
313 dev_err(dev
, "failed to calculate clock period\n");
314 clk_disable_unprepare(nemc
->clk
);
319 * Iterate over child devices, check that they do not conflict with
320 * each other, and register child devices for them. If a child device
321 * has invalid properties, it is ignored and no platform device is
324 for_each_child_of_node(nemc
->dev
->of_node
, child
) {
327 while ((prop
= of_get_address(child
, i
++, NULL
, NULL
))) {
328 bank
= of_read_number(prop
, 1);
329 if (bank
< 1 || bank
>= JZ4780_NEMC_NUM_BANKS
) {
331 "%pOF requests invalid bank %u\n",
334 /* Will continue the outer loop below. */
339 referenced
|= BIT(bank
);
343 dev_err(nemc
->dev
, "%pOF has no addresses\n",
346 } else if (nemc
->banks_present
& referenced
) {
347 dev_err(nemc
->dev
, "%pOF conflicts with another node\n",
352 /* Configure bank parameters. */
353 for_each_set_bit(bank
, &referenced
, JZ4780_NEMC_NUM_BANKS
) {
354 if (!jz4780_nemc_configure_bank(nemc
, bank
, child
)) {
361 if (of_platform_device_create(child
, NULL
, nemc
->dev
))
362 nemc
->banks_present
|= referenced
;
366 platform_set_drvdata(pdev
, nemc
);
367 dev_info(dev
, "JZ4780 NEMC initialised\n");
371 static int jz4780_nemc_remove(struct platform_device
*pdev
)
373 struct jz4780_nemc
*nemc
= platform_get_drvdata(pdev
);
375 clk_disable_unprepare(nemc
->clk
);
379 static const struct jz_soc_info jz4740_soc_info
= {
380 .tas_tah_cycles_max
= 7,
383 static const struct jz_soc_info jz4780_soc_info
= {
384 .tas_tah_cycles_max
= 15,
387 static const struct of_device_id jz4780_nemc_dt_match
[] = {
388 { .compatible
= "ingenic,jz4740-nemc", .data
= &jz4740_soc_info
, },
389 { .compatible
= "ingenic,jz4780-nemc", .data
= &jz4780_soc_info
, },
393 static struct platform_driver jz4780_nemc_driver
= {
394 .probe
= jz4780_nemc_probe
,
395 .remove
= jz4780_nemc_remove
,
397 .name
= "jz4780-nemc",
398 .of_match_table
= of_match_ptr(jz4780_nemc_dt_match
),
402 static int __init
jz4780_nemc_init(void)
404 return platform_driver_register(&jz4780_nemc_driver
);
406 subsys_initcall(jz4780_nemc_init
);