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