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>
12 #include <linux/math64.h>
14 #include <linux/of_address.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_REG_LEN 0x54
27 #define NEMC_SMCR_SMT BIT(0)
28 #define NEMC_SMCR_BW_SHIFT 6
29 #define NEMC_SMCR_BW_MASK (0x3 << NEMC_SMCR_BW_SHIFT)
30 #define NEMC_SMCR_BW_8 (0 << 6)
31 #define NEMC_SMCR_TAS_SHIFT 8
32 #define NEMC_SMCR_TAS_MASK (0xf << NEMC_SMCR_TAS_SHIFT)
33 #define NEMC_SMCR_TAH_SHIFT 12
34 #define NEMC_SMCR_TAH_MASK (0xf << NEMC_SMCR_TAH_SHIFT)
35 #define NEMC_SMCR_TBP_SHIFT 16
36 #define NEMC_SMCR_TBP_MASK (0xf << NEMC_SMCR_TBP_SHIFT)
37 #define NEMC_SMCR_TAW_SHIFT 20
38 #define NEMC_SMCR_TAW_MASK (0xf << NEMC_SMCR_TAW_SHIFT)
39 #define NEMC_SMCR_TSTRV_SHIFT 24
40 #define NEMC_SMCR_TSTRV_MASK (0x3f << NEMC_SMCR_TSTRV_SHIFT)
42 #define NEMC_NFCSR_NFEn(n) BIT(((n) - 1) << 1)
43 #define NEMC_NFCSR_NFCEn(n) BIT((((n) - 1) << 1) + 1)
44 #define NEMC_NFCSR_TNFEn(n) BIT(16 + (n) - 1)
47 u8 tas_tah_cycles_max
;
53 const struct jz_soc_info
*soc_info
;
57 unsigned long banks_present
;
61 * jz4780_nemc_num_banks() - count the number of banks referenced by a device
62 * @dev: device to count banks for, must be a child of the NEMC.
64 * Return: The number of unique NEMC banks referred to by the specified NEMC
65 * child device. Unique here means that a device that references the same bank
66 * multiple times in its "reg" property will only count once.
68 unsigned int jz4780_nemc_num_banks(struct device
*dev
)
71 unsigned int bank
, count
= 0;
72 unsigned long referenced
= 0;
75 while ((prop
= of_get_address(dev
->of_node
, i
++, NULL
, NULL
))) {
76 bank
= of_read_number(prop
, 1);
77 if (!(referenced
& BIT(bank
))) {
78 referenced
|= BIT(bank
);
85 EXPORT_SYMBOL(jz4780_nemc_num_banks
);
88 * jz4780_nemc_set_type() - set the type of device connected to a bank
89 * @dev: child device of the NEMC.
90 * @bank: bank number to configure.
91 * @type: type of device connected to the bank.
93 void jz4780_nemc_set_type(struct device
*dev
, unsigned int bank
,
94 enum jz4780_nemc_bank_type type
)
96 struct jz4780_nemc
*nemc
= dev_get_drvdata(dev
->parent
);
99 nfcsr
= readl(nemc
->base
+ NEMC_NFCSR
);
101 /* TODO: Support toggle NAND devices. */
103 case JZ4780_NEMC_BANK_SRAM
:
104 nfcsr
&= ~(NEMC_NFCSR_TNFEn(bank
) | NEMC_NFCSR_NFEn(bank
));
106 case JZ4780_NEMC_BANK_NAND
:
107 nfcsr
&= ~NEMC_NFCSR_TNFEn(bank
);
108 nfcsr
|= NEMC_NFCSR_NFEn(bank
);
112 writel(nfcsr
, nemc
->base
+ NEMC_NFCSR
);
114 EXPORT_SYMBOL(jz4780_nemc_set_type
);
117 * jz4780_nemc_assert() - (de-)assert a NAND device's chip enable pin
118 * @dev: child device of the NEMC.
119 * @bank: bank number of device.
120 * @assert: whether the chip enable pin should be asserted.
122 * (De-)asserts the chip enable pin for the NAND device connected to the
125 void jz4780_nemc_assert(struct device
*dev
, unsigned int bank
, bool assert)
127 struct jz4780_nemc
*nemc
= dev_get_drvdata(dev
->parent
);
130 nfcsr
= readl(nemc
->base
+ NEMC_NFCSR
);
133 nfcsr
|= NEMC_NFCSR_NFCEn(bank
);
135 nfcsr
&= ~NEMC_NFCSR_NFCEn(bank
);
137 writel(nfcsr
, nemc
->base
+ NEMC_NFCSR
);
139 EXPORT_SYMBOL(jz4780_nemc_assert
);
141 static uint32_t jz4780_nemc_clk_period(struct jz4780_nemc
*nemc
)
145 rate
= clk_get_rate(nemc
->clk
);
149 /* Return in picoseconds. */
150 return div64_ul(1000000000000ull, rate
);
153 static uint32_t jz4780_nemc_ns_to_cycles(struct jz4780_nemc
*nemc
, uint32_t ns
)
155 return ((ns
* 1000) + nemc
->clk_period
- 1) / nemc
->clk_period
;
158 static bool jz4780_nemc_configure_bank(struct jz4780_nemc
*nemc
,
160 struct device_node
*node
)
162 uint32_t smcr
, val
, cycles
;
165 * Conversion of tBP and tAW cycle counts to values supported by the
166 * hardware (round up to the next supported value).
168 static const u8 convert_tBP_tAW
[] = {
169 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
171 /* 11 - 12 -> 12 cycles */
174 /* 13 - 15 -> 15 cycles */
177 /* 16 - 20 -> 20 cycles */
180 /* 21 - 25 -> 25 cycles */
183 /* 26 - 31 -> 31 cycles */
184 15, 15, 15, 15, 15, 15
187 smcr
= readl(nemc
->base
+ NEMC_SMCRn(bank
));
188 smcr
&= ~NEMC_SMCR_SMT
;
190 if (!of_property_read_u32(node
, "ingenic,nemc-bus-width", &val
)) {
191 smcr
&= ~NEMC_SMCR_BW_MASK
;
194 smcr
|= NEMC_SMCR_BW_8
;
198 * Earlier SoCs support a 16 bit bus width (the 4780
199 * does not), until those are properly supported, error.
201 dev_err(nemc
->dev
, "unsupported bus width: %u\n", val
);
206 if (of_property_read_u32(node
, "ingenic,nemc-tAS", &val
) == 0) {
207 smcr
&= ~NEMC_SMCR_TAS_MASK
;
208 cycles
= jz4780_nemc_ns_to_cycles(nemc
, val
);
209 if (cycles
> nemc
->soc_info
->tas_tah_cycles_max
) {
210 dev_err(nemc
->dev
, "tAS %u is too high (%u cycles)\n",
215 smcr
|= cycles
<< NEMC_SMCR_TAS_SHIFT
;
218 if (of_property_read_u32(node
, "ingenic,nemc-tAH", &val
) == 0) {
219 smcr
&= ~NEMC_SMCR_TAH_MASK
;
220 cycles
= jz4780_nemc_ns_to_cycles(nemc
, val
);
221 if (cycles
> nemc
->soc_info
->tas_tah_cycles_max
) {
222 dev_err(nemc
->dev
, "tAH %u is too high (%u cycles)\n",
227 smcr
|= cycles
<< NEMC_SMCR_TAH_SHIFT
;
230 if (of_property_read_u32(node
, "ingenic,nemc-tBP", &val
) == 0) {
231 smcr
&= ~NEMC_SMCR_TBP_MASK
;
232 cycles
= jz4780_nemc_ns_to_cycles(nemc
, val
);
234 dev_err(nemc
->dev
, "tBP %u is too high (%u cycles)\n",
239 smcr
|= convert_tBP_tAW
[cycles
] << NEMC_SMCR_TBP_SHIFT
;
242 if (of_property_read_u32(node
, "ingenic,nemc-tAW", &val
) == 0) {
243 smcr
&= ~NEMC_SMCR_TAW_MASK
;
244 cycles
= jz4780_nemc_ns_to_cycles(nemc
, val
);
246 dev_err(nemc
->dev
, "tAW %u is too high (%u cycles)\n",
251 smcr
|= convert_tBP_tAW
[cycles
] << NEMC_SMCR_TAW_SHIFT
;
254 if (of_property_read_u32(node
, "ingenic,nemc-tSTRV", &val
) == 0) {
255 smcr
&= ~NEMC_SMCR_TSTRV_MASK
;
256 cycles
= jz4780_nemc_ns_to_cycles(nemc
, val
);
258 dev_err(nemc
->dev
, "tSTRV %u is too high (%u cycles)\n",
263 smcr
|= cycles
<< NEMC_SMCR_TSTRV_SHIFT
;
266 writel(smcr
, nemc
->base
+ NEMC_SMCRn(bank
));
270 static int jz4780_nemc_probe(struct platform_device
*pdev
)
272 struct device
*dev
= &pdev
->dev
;
273 struct jz4780_nemc
*nemc
;
274 struct resource
*res
;
275 struct device_node
*child
;
278 unsigned long referenced
;
281 nemc
= devm_kzalloc(dev
, sizeof(*nemc
), GFP_KERNEL
);
285 nemc
->soc_info
= device_get_match_data(dev
);
289 spin_lock_init(&nemc
->lock
);
292 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
297 * The driver currently only uses the registers up to offset
298 * NEMC_REG_LEN. Since the EFUSE registers are in the middle of the
299 * NEMC registers, we only request the registers we will use for now;
300 * that way the EFUSE driver can probe too.
302 if (!devm_request_mem_region(dev
, res
->start
, NEMC_REG_LEN
, dev_name(dev
))) {
303 dev_err(dev
, "unable to request I/O memory region\n");
307 nemc
->base
= devm_ioremap(dev
, res
->start
, NEMC_REG_LEN
);
309 dev_err(dev
, "failed to get I/O memory\n");
313 writel(0, nemc
->base
+ NEMC_NFCSR
);
315 nemc
->clk
= devm_clk_get(dev
, NULL
);
316 if (IS_ERR(nemc
->clk
)) {
317 dev_err(dev
, "failed to get clock\n");
318 return PTR_ERR(nemc
->clk
);
321 ret
= clk_prepare_enable(nemc
->clk
);
323 dev_err(dev
, "failed to enable clock: %d\n", ret
);
327 nemc
->clk_period
= jz4780_nemc_clk_period(nemc
);
328 if (!nemc
->clk_period
) {
329 dev_err(dev
, "failed to calculate clock period\n");
330 clk_disable_unprepare(nemc
->clk
);
335 * Iterate over child devices, check that they do not conflict with
336 * each other, and register child devices for them. If a child device
337 * has invalid properties, it is ignored and no platform device is
340 for_each_child_of_node(nemc
->dev
->of_node
, child
) {
343 while ((prop
= of_get_address(child
, i
++, NULL
, NULL
))) {
344 bank
= of_read_number(prop
, 1);
345 if (bank
< 1 || bank
>= JZ4780_NEMC_NUM_BANKS
) {
347 "%pOF requests invalid bank %u\n",
350 /* Will continue the outer loop below. */
355 referenced
|= BIT(bank
);
359 dev_err(nemc
->dev
, "%pOF has no addresses\n",
362 } else if (nemc
->banks_present
& referenced
) {
363 dev_err(nemc
->dev
, "%pOF conflicts with another node\n",
368 /* Configure bank parameters. */
369 for_each_set_bit(bank
, &referenced
, JZ4780_NEMC_NUM_BANKS
) {
370 if (!jz4780_nemc_configure_bank(nemc
, bank
, child
)) {
377 if (of_platform_device_create(child
, NULL
, nemc
->dev
))
378 nemc
->banks_present
|= referenced
;
382 platform_set_drvdata(pdev
, nemc
);
383 dev_info(dev
, "JZ4780 NEMC initialised\n");
387 static void jz4780_nemc_remove(struct platform_device
*pdev
)
389 struct jz4780_nemc
*nemc
= platform_get_drvdata(pdev
);
391 clk_disable_unprepare(nemc
->clk
);
394 static const struct jz_soc_info jz4740_soc_info
= {
395 .tas_tah_cycles_max
= 7,
398 static const struct jz_soc_info jz4780_soc_info
= {
399 .tas_tah_cycles_max
= 15,
402 static const struct of_device_id jz4780_nemc_dt_match
[] = {
403 { .compatible
= "ingenic,jz4740-nemc", .data
= &jz4740_soc_info
, },
404 { .compatible
= "ingenic,jz4780-nemc", .data
= &jz4780_soc_info
, },
408 static struct platform_driver jz4780_nemc_driver
= {
409 .probe
= jz4780_nemc_probe
,
410 .remove
= jz4780_nemc_remove
,
412 .name
= "jz4780-nemc",
413 .of_match_table
= of_match_ptr(jz4780_nemc_dt_match
),
417 static int __init
jz4780_nemc_init(void)
419 return platform_driver_register(&jz4780_nemc_driver
);
421 subsys_initcall(jz4780_nemc_init
);