1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2911a8882SAlex Smith /* 3911a8882SAlex Smith * JZ4780 NAND/external memory controller (NEMC) 4911a8882SAlex Smith * 5911a8882SAlex Smith * Copyright (c) 2015 Imagination Technologies 6911a8882SAlex Smith * Author: Alex Smith <alex@alex-smith.me.uk> 7911a8882SAlex Smith */ 8911a8882SAlex Smith 9911a8882SAlex Smith #include <linux/clk.h> 10911a8882SAlex Smith #include <linux/init.h> 11911a8882SAlex Smith #include <linux/math64.h> 12911a8882SAlex Smith #include <linux/of.h> 13911a8882SAlex Smith #include <linux/of_address.h> 14911a8882SAlex Smith #include <linux/of_device.h> 15911a8882SAlex Smith #include <linux/of_platform.h> 16911a8882SAlex Smith #include <linux/platform_device.h> 17911a8882SAlex Smith #include <linux/slab.h> 18911a8882SAlex Smith #include <linux/spinlock.h> 19911a8882SAlex Smith 20911a8882SAlex Smith #include <linux/jz4780-nemc.h> 21911a8882SAlex Smith 22911a8882SAlex Smith #define NEMC_SMCRn(n) (0x14 + (((n) - 1) * 4)) 23911a8882SAlex Smith #define NEMC_NFCSR 0x50 24911a8882SAlex Smith 25911a8882SAlex Smith #define NEMC_SMCR_SMT BIT(0) 26911a8882SAlex Smith #define NEMC_SMCR_BW_SHIFT 6 27911a8882SAlex Smith #define NEMC_SMCR_BW_MASK (0x3 << NEMC_SMCR_BW_SHIFT) 28911a8882SAlex Smith #define NEMC_SMCR_BW_8 (0 << 6) 29911a8882SAlex Smith #define NEMC_SMCR_TAS_SHIFT 8 30911a8882SAlex Smith #define NEMC_SMCR_TAS_MASK (0xf << NEMC_SMCR_TAS_SHIFT) 31911a8882SAlex Smith #define NEMC_SMCR_TAH_SHIFT 12 32911a8882SAlex Smith #define NEMC_SMCR_TAH_MASK (0xf << NEMC_SMCR_TAH_SHIFT) 33911a8882SAlex Smith #define NEMC_SMCR_TBP_SHIFT 16 34911a8882SAlex Smith #define NEMC_SMCR_TBP_MASK (0xf << NEMC_SMCR_TBP_SHIFT) 35911a8882SAlex Smith #define NEMC_SMCR_TAW_SHIFT 20 36911a8882SAlex Smith #define NEMC_SMCR_TAW_MASK (0xf << NEMC_SMCR_TAW_SHIFT) 37911a8882SAlex Smith #define NEMC_SMCR_TSTRV_SHIFT 24 38911a8882SAlex Smith #define NEMC_SMCR_TSTRV_MASK (0x3f << NEMC_SMCR_TSTRV_SHIFT) 39911a8882SAlex Smith 40911a8882SAlex Smith #define NEMC_NFCSR_NFEn(n) BIT(((n) - 1) << 1) 41911a8882SAlex Smith #define NEMC_NFCSR_NFCEn(n) BIT((((n) - 1) << 1) + 1) 42911a8882SAlex Smith #define NEMC_NFCSR_TNFEn(n) BIT(16 + (n) - 1) 43911a8882SAlex Smith 44911a8882SAlex Smith struct jz4780_nemc { 45911a8882SAlex Smith spinlock_t lock; 46911a8882SAlex Smith struct device *dev; 47911a8882SAlex Smith void __iomem *base; 48911a8882SAlex Smith struct clk *clk; 49911a8882SAlex Smith uint32_t clk_period; 50911a8882SAlex Smith unsigned long banks_present; 51911a8882SAlex Smith }; 52911a8882SAlex Smith 53911a8882SAlex Smith /** 54911a8882SAlex Smith * jz4780_nemc_num_banks() - count the number of banks referenced by a device 55911a8882SAlex Smith * @dev: device to count banks for, must be a child of the NEMC. 56911a8882SAlex Smith * 57911a8882SAlex Smith * Return: The number of unique NEMC banks referred to by the specified NEMC 58911a8882SAlex Smith * child device. Unique here means that a device that references the same bank 59911a8882SAlex Smith * multiple times in the its "reg" property will only count once. 60911a8882SAlex Smith */ 61911a8882SAlex Smith unsigned int jz4780_nemc_num_banks(struct device *dev) 62911a8882SAlex Smith { 63911a8882SAlex Smith const __be32 *prop; 64911a8882SAlex Smith unsigned int bank, count = 0; 65911a8882SAlex Smith unsigned long referenced = 0; 66911a8882SAlex Smith int i = 0; 67911a8882SAlex Smith 68911a8882SAlex Smith while ((prop = of_get_address(dev->of_node, i++, NULL, NULL))) { 69911a8882SAlex Smith bank = of_read_number(prop, 1); 70911a8882SAlex Smith if (!(referenced & BIT(bank))) { 71911a8882SAlex Smith referenced |= BIT(bank); 72911a8882SAlex Smith count++; 73911a8882SAlex Smith } 74911a8882SAlex Smith } 75911a8882SAlex Smith 76911a8882SAlex Smith return count; 77911a8882SAlex Smith } 78911a8882SAlex Smith EXPORT_SYMBOL(jz4780_nemc_num_banks); 79911a8882SAlex Smith 80911a8882SAlex Smith /** 81911a8882SAlex Smith * jz4780_nemc_set_type() - set the type of device connected to a bank 82911a8882SAlex Smith * @dev: child device of the NEMC. 83911a8882SAlex Smith * @bank: bank number to configure. 84911a8882SAlex Smith * @type: type of device connected to the bank. 85911a8882SAlex Smith */ 86911a8882SAlex Smith void jz4780_nemc_set_type(struct device *dev, unsigned int bank, 87911a8882SAlex Smith enum jz4780_nemc_bank_type type) 88911a8882SAlex Smith { 89911a8882SAlex Smith struct jz4780_nemc *nemc = dev_get_drvdata(dev->parent); 90911a8882SAlex Smith uint32_t nfcsr; 91911a8882SAlex Smith 92911a8882SAlex Smith nfcsr = readl(nemc->base + NEMC_NFCSR); 93911a8882SAlex Smith 94911a8882SAlex Smith /* TODO: Support toggle NAND devices. */ 95911a8882SAlex Smith switch (type) { 96911a8882SAlex Smith case JZ4780_NEMC_BANK_SRAM: 97911a8882SAlex Smith nfcsr &= ~(NEMC_NFCSR_TNFEn(bank) | NEMC_NFCSR_NFEn(bank)); 98911a8882SAlex Smith break; 99911a8882SAlex Smith case JZ4780_NEMC_BANK_NAND: 100911a8882SAlex Smith nfcsr &= ~NEMC_NFCSR_TNFEn(bank); 101911a8882SAlex Smith nfcsr |= NEMC_NFCSR_NFEn(bank); 102911a8882SAlex Smith break; 103911a8882SAlex Smith } 104911a8882SAlex Smith 105911a8882SAlex Smith writel(nfcsr, nemc->base + NEMC_NFCSR); 106911a8882SAlex Smith } 107911a8882SAlex Smith EXPORT_SYMBOL(jz4780_nemc_set_type); 108911a8882SAlex Smith 109911a8882SAlex Smith /** 110911a8882SAlex Smith * jz4780_nemc_assert() - (de-)assert a NAND device's chip enable pin 111911a8882SAlex Smith * @dev: child device of the NEMC. 112911a8882SAlex Smith * @bank: bank number of device. 113911a8882SAlex Smith * @assert: whether the chip enable pin should be asserted. 114911a8882SAlex Smith * 115911a8882SAlex Smith * (De-)asserts the chip enable pin for the NAND device connected to the 116911a8882SAlex Smith * specified bank. 117911a8882SAlex Smith */ 118911a8882SAlex Smith void jz4780_nemc_assert(struct device *dev, unsigned int bank, bool assert) 119911a8882SAlex Smith { 120911a8882SAlex Smith struct jz4780_nemc *nemc = dev_get_drvdata(dev->parent); 121911a8882SAlex Smith uint32_t nfcsr; 122911a8882SAlex Smith 123911a8882SAlex Smith nfcsr = readl(nemc->base + NEMC_NFCSR); 124911a8882SAlex Smith 125911a8882SAlex Smith if (assert) 126911a8882SAlex Smith nfcsr |= NEMC_NFCSR_NFCEn(bank); 127911a8882SAlex Smith else 128911a8882SAlex Smith nfcsr &= ~NEMC_NFCSR_NFCEn(bank); 129911a8882SAlex Smith 130911a8882SAlex Smith writel(nfcsr, nemc->base + NEMC_NFCSR); 131911a8882SAlex Smith } 132911a8882SAlex Smith EXPORT_SYMBOL(jz4780_nemc_assert); 133911a8882SAlex Smith 134911a8882SAlex Smith static uint32_t jz4780_nemc_clk_period(struct jz4780_nemc *nemc) 135911a8882SAlex Smith { 136911a8882SAlex Smith unsigned long rate; 137911a8882SAlex Smith 138911a8882SAlex Smith rate = clk_get_rate(nemc->clk); 139911a8882SAlex Smith if (!rate) 140911a8882SAlex Smith return 0; 141911a8882SAlex Smith 142911a8882SAlex Smith /* Return in picoseconds. */ 143911a8882SAlex Smith return div64_ul(1000000000000ull, rate); 144911a8882SAlex Smith } 145911a8882SAlex Smith 146911a8882SAlex Smith static uint32_t jz4780_nemc_ns_to_cycles(struct jz4780_nemc *nemc, uint32_t ns) 147911a8882SAlex Smith { 148911a8882SAlex Smith return ((ns * 1000) + nemc->clk_period - 1) / nemc->clk_period; 149911a8882SAlex Smith } 150911a8882SAlex Smith 151911a8882SAlex Smith static bool jz4780_nemc_configure_bank(struct jz4780_nemc *nemc, 152911a8882SAlex Smith unsigned int bank, 153911a8882SAlex Smith struct device_node *node) 154911a8882SAlex Smith { 155911a8882SAlex Smith uint32_t smcr, val, cycles; 156911a8882SAlex Smith 157911a8882SAlex Smith /* 158911a8882SAlex Smith * Conversion of tBP and tAW cycle counts to values supported by the 159911a8882SAlex Smith * hardware (round up to the next supported value). 160911a8882SAlex Smith */ 161911a8882SAlex Smith static const uint32_t convert_tBP_tAW[] = { 162911a8882SAlex Smith 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 163911a8882SAlex Smith 164911a8882SAlex Smith /* 11 - 12 -> 12 cycles */ 165911a8882SAlex Smith 11, 11, 166911a8882SAlex Smith 167911a8882SAlex Smith /* 13 - 15 -> 15 cycles */ 168911a8882SAlex Smith 12, 12, 12, 169911a8882SAlex Smith 170911a8882SAlex Smith /* 16 - 20 -> 20 cycles */ 171911a8882SAlex Smith 13, 13, 13, 13, 13, 172911a8882SAlex Smith 173911a8882SAlex Smith /* 21 - 25 -> 25 cycles */ 174911a8882SAlex Smith 14, 14, 14, 14, 14, 175911a8882SAlex Smith 176911a8882SAlex Smith /* 26 - 31 -> 31 cycles */ 177911a8882SAlex Smith 15, 15, 15, 15, 15, 15 178911a8882SAlex Smith }; 179911a8882SAlex Smith 180911a8882SAlex Smith smcr = readl(nemc->base + NEMC_SMCRn(bank)); 181911a8882SAlex Smith smcr &= ~NEMC_SMCR_SMT; 182911a8882SAlex Smith 183911a8882SAlex Smith if (!of_property_read_u32(node, "ingenic,nemc-bus-width", &val)) { 184911a8882SAlex Smith smcr &= ~NEMC_SMCR_BW_MASK; 185911a8882SAlex Smith switch (val) { 186911a8882SAlex Smith case 8: 187911a8882SAlex Smith smcr |= NEMC_SMCR_BW_8; 188911a8882SAlex Smith break; 189911a8882SAlex Smith default: 190911a8882SAlex Smith /* 191911a8882SAlex Smith * Earlier SoCs support a 16 bit bus width (the 4780 192911a8882SAlex Smith * does not), until those are properly supported, error. 193911a8882SAlex Smith */ 194911a8882SAlex Smith dev_err(nemc->dev, "unsupported bus width: %u\n", val); 195911a8882SAlex Smith return false; 196911a8882SAlex Smith } 197911a8882SAlex Smith } 198911a8882SAlex Smith 199911a8882SAlex Smith if (of_property_read_u32(node, "ingenic,nemc-tAS", &val) == 0) { 200911a8882SAlex Smith smcr &= ~NEMC_SMCR_TAS_MASK; 201911a8882SAlex Smith cycles = jz4780_nemc_ns_to_cycles(nemc, val); 202911a8882SAlex Smith if (cycles > 15) { 203911a8882SAlex Smith dev_err(nemc->dev, "tAS %u is too high (%u cycles)\n", 204911a8882SAlex Smith val, cycles); 205911a8882SAlex Smith return false; 206911a8882SAlex Smith } 207911a8882SAlex Smith 208911a8882SAlex Smith smcr |= cycles << NEMC_SMCR_TAS_SHIFT; 209911a8882SAlex Smith } 210911a8882SAlex Smith 211911a8882SAlex Smith if (of_property_read_u32(node, "ingenic,nemc-tAH", &val) == 0) { 212911a8882SAlex Smith smcr &= ~NEMC_SMCR_TAH_MASK; 213911a8882SAlex Smith cycles = jz4780_nemc_ns_to_cycles(nemc, val); 214911a8882SAlex Smith if (cycles > 15) { 215911a8882SAlex Smith dev_err(nemc->dev, "tAH %u is too high (%u cycles)\n", 216911a8882SAlex Smith val, cycles); 217911a8882SAlex Smith return false; 218911a8882SAlex Smith } 219911a8882SAlex Smith 220911a8882SAlex Smith smcr |= cycles << NEMC_SMCR_TAH_SHIFT; 221911a8882SAlex Smith } 222911a8882SAlex Smith 223911a8882SAlex Smith if (of_property_read_u32(node, "ingenic,nemc-tBP", &val) == 0) { 224911a8882SAlex Smith smcr &= ~NEMC_SMCR_TBP_MASK; 225911a8882SAlex Smith cycles = jz4780_nemc_ns_to_cycles(nemc, val); 226911a8882SAlex Smith if (cycles > 31) { 227911a8882SAlex Smith dev_err(nemc->dev, "tBP %u is too high (%u cycles)\n", 228911a8882SAlex Smith val, cycles); 229911a8882SAlex Smith return false; 230911a8882SAlex Smith } 231911a8882SAlex Smith 232911a8882SAlex Smith smcr |= convert_tBP_tAW[cycles] << NEMC_SMCR_TBP_SHIFT; 233911a8882SAlex Smith } 234911a8882SAlex Smith 235911a8882SAlex Smith if (of_property_read_u32(node, "ingenic,nemc-tAW", &val) == 0) { 236911a8882SAlex Smith smcr &= ~NEMC_SMCR_TAW_MASK; 237911a8882SAlex Smith cycles = jz4780_nemc_ns_to_cycles(nemc, val); 238911a8882SAlex Smith if (cycles > 31) { 239911a8882SAlex Smith dev_err(nemc->dev, "tAW %u is too high (%u cycles)\n", 240911a8882SAlex Smith val, cycles); 241911a8882SAlex Smith return false; 242911a8882SAlex Smith } 243911a8882SAlex Smith 244911a8882SAlex Smith smcr |= convert_tBP_tAW[cycles] << NEMC_SMCR_TAW_SHIFT; 245911a8882SAlex Smith } 246911a8882SAlex Smith 247911a8882SAlex Smith if (of_property_read_u32(node, "ingenic,nemc-tSTRV", &val) == 0) { 248911a8882SAlex Smith smcr &= ~NEMC_SMCR_TSTRV_MASK; 249911a8882SAlex Smith cycles = jz4780_nemc_ns_to_cycles(nemc, val); 250911a8882SAlex Smith if (cycles > 63) { 251911a8882SAlex Smith dev_err(nemc->dev, "tSTRV %u is too high (%u cycles)\n", 252911a8882SAlex Smith val, cycles); 253911a8882SAlex Smith return false; 254911a8882SAlex Smith } 255911a8882SAlex Smith 256911a8882SAlex Smith smcr |= cycles << NEMC_SMCR_TSTRV_SHIFT; 257911a8882SAlex Smith } 258911a8882SAlex Smith 259911a8882SAlex Smith writel(smcr, nemc->base + NEMC_SMCRn(bank)); 260911a8882SAlex Smith return true; 261911a8882SAlex Smith } 262911a8882SAlex Smith 263911a8882SAlex Smith static int jz4780_nemc_probe(struct platform_device *pdev) 264911a8882SAlex Smith { 265911a8882SAlex Smith struct device *dev = &pdev->dev; 266911a8882SAlex Smith struct jz4780_nemc *nemc; 267911a8882SAlex Smith struct resource *res; 268911a8882SAlex Smith struct device_node *child; 269911a8882SAlex Smith const __be32 *prop; 270911a8882SAlex Smith unsigned int bank; 271911a8882SAlex Smith unsigned long referenced; 272911a8882SAlex Smith int i, ret; 273911a8882SAlex Smith 274911a8882SAlex Smith nemc = devm_kzalloc(dev, sizeof(*nemc), GFP_KERNEL); 275911a8882SAlex Smith if (!nemc) 276911a8882SAlex Smith return -ENOMEM; 277911a8882SAlex Smith 278911a8882SAlex Smith spin_lock_init(&nemc->lock); 279911a8882SAlex Smith nemc->dev = dev; 280911a8882SAlex Smith 281911a8882SAlex Smith res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 282911a8882SAlex Smith nemc->base = devm_ioremap_resource(dev, res); 283911a8882SAlex Smith if (IS_ERR(nemc->base)) { 284911a8882SAlex Smith dev_err(dev, "failed to get I/O memory\n"); 285911a8882SAlex Smith return PTR_ERR(nemc->base); 286911a8882SAlex Smith } 287911a8882SAlex Smith 288911a8882SAlex Smith writel(0, nemc->base + NEMC_NFCSR); 289911a8882SAlex Smith 290911a8882SAlex Smith nemc->clk = devm_clk_get(dev, NULL); 291911a8882SAlex Smith if (IS_ERR(nemc->clk)) { 292911a8882SAlex Smith dev_err(dev, "failed to get clock\n"); 293911a8882SAlex Smith return PTR_ERR(nemc->clk); 294911a8882SAlex Smith } 295911a8882SAlex Smith 296911a8882SAlex Smith ret = clk_prepare_enable(nemc->clk); 297911a8882SAlex Smith if (ret) { 298911a8882SAlex Smith dev_err(dev, "failed to enable clock: %d\n", ret); 299911a8882SAlex Smith return ret; 300911a8882SAlex Smith } 301911a8882SAlex Smith 302911a8882SAlex Smith nemc->clk_period = jz4780_nemc_clk_period(nemc); 303911a8882SAlex Smith if (!nemc->clk_period) { 304911a8882SAlex Smith dev_err(dev, "failed to calculate clock period\n"); 305911a8882SAlex Smith clk_disable_unprepare(nemc->clk); 306911a8882SAlex Smith return -EINVAL; 307911a8882SAlex Smith } 308911a8882SAlex Smith 309911a8882SAlex Smith /* 310911a8882SAlex Smith * Iterate over child devices, check that they do not conflict with 311911a8882SAlex Smith * each other, and register child devices for them. If a child device 312911a8882SAlex Smith * has invalid properties, it is ignored and no platform device is 313911a8882SAlex Smith * registered for it. 314911a8882SAlex Smith */ 315911a8882SAlex Smith for_each_child_of_node(nemc->dev->of_node, child) { 316911a8882SAlex Smith referenced = 0; 317911a8882SAlex Smith i = 0; 318911a8882SAlex Smith while ((prop = of_get_address(child, i++, NULL, NULL))) { 319911a8882SAlex Smith bank = of_read_number(prop, 1); 320911a8882SAlex Smith if (bank < 1 || bank >= JZ4780_NEMC_NUM_BANKS) { 321911a8882SAlex Smith dev_err(nemc->dev, 322db749d17SRob Herring "%pOF requests invalid bank %u\n", 323db749d17SRob Herring child, bank); 324911a8882SAlex Smith 325911a8882SAlex Smith /* Will continue the outer loop below. */ 326911a8882SAlex Smith referenced = 0; 327911a8882SAlex Smith break; 328911a8882SAlex Smith } 329911a8882SAlex Smith 330911a8882SAlex Smith referenced |= BIT(bank); 331911a8882SAlex Smith } 332911a8882SAlex Smith 333911a8882SAlex Smith if (!referenced) { 334db749d17SRob Herring dev_err(nemc->dev, "%pOF has no addresses\n", 335db749d17SRob Herring child); 336911a8882SAlex Smith continue; 337911a8882SAlex Smith } else if (nemc->banks_present & referenced) { 338db749d17SRob Herring dev_err(nemc->dev, "%pOF conflicts with another node\n", 339db749d17SRob Herring child); 340911a8882SAlex Smith continue; 341911a8882SAlex Smith } 342911a8882SAlex Smith 343911a8882SAlex Smith /* Configure bank parameters. */ 344911a8882SAlex Smith for_each_set_bit(bank, &referenced, JZ4780_NEMC_NUM_BANKS) { 345911a8882SAlex Smith if (!jz4780_nemc_configure_bank(nemc, bank, child)) { 346911a8882SAlex Smith referenced = 0; 347911a8882SAlex Smith break; 348911a8882SAlex Smith } 349911a8882SAlex Smith } 350911a8882SAlex Smith 351911a8882SAlex Smith if (referenced) { 352911a8882SAlex Smith if (of_platform_device_create(child, NULL, nemc->dev)) 353911a8882SAlex Smith nemc->banks_present |= referenced; 354911a8882SAlex Smith } 355911a8882SAlex Smith } 356911a8882SAlex Smith 357911a8882SAlex Smith platform_set_drvdata(pdev, nemc); 358911a8882SAlex Smith dev_info(dev, "JZ4780 NEMC initialised\n"); 359911a8882SAlex Smith return 0; 360911a8882SAlex Smith } 361911a8882SAlex Smith 362911a8882SAlex Smith static int jz4780_nemc_remove(struct platform_device *pdev) 363911a8882SAlex Smith { 364911a8882SAlex Smith struct jz4780_nemc *nemc = platform_get_drvdata(pdev); 365911a8882SAlex Smith 366911a8882SAlex Smith clk_disable_unprepare(nemc->clk); 367911a8882SAlex Smith return 0; 368911a8882SAlex Smith } 369911a8882SAlex Smith 370911a8882SAlex Smith static const struct of_device_id jz4780_nemc_dt_match[] = { 371911a8882SAlex Smith { .compatible = "ingenic,jz4780-nemc" }, 372911a8882SAlex Smith {}, 373911a8882SAlex Smith }; 374911a8882SAlex Smith 375911a8882SAlex Smith static struct platform_driver jz4780_nemc_driver = { 376911a8882SAlex Smith .probe = jz4780_nemc_probe, 377911a8882SAlex Smith .remove = jz4780_nemc_remove, 378911a8882SAlex Smith .driver = { 379911a8882SAlex Smith .name = "jz4780-nemc", 380911a8882SAlex Smith .of_match_table = of_match_ptr(jz4780_nemc_dt_match), 381911a8882SAlex Smith }, 382911a8882SAlex Smith }; 383911a8882SAlex Smith 384911a8882SAlex Smith static int __init jz4780_nemc_init(void) 385911a8882SAlex Smith { 386911a8882SAlex Smith return platform_driver_register(&jz4780_nemc_driver); 387911a8882SAlex Smith } 388911a8882SAlex Smith subsys_initcall(jz4780_nemc_init); 389