jz4780-nemc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * JZ4780 NAND/external memory controller (NEMC)
  4. *
  5. * Copyright (c) 2015 Imagination Technologies
  6. * Author: Alex Smith <alex@alex-smith.me.uk>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/init.h>
  10. #include <linux/io.h>
  11. #include <linux/math64.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/jz4780-nemc.h>
  19. #define NEMC_SMCRn(n) (0x14 + (((n) - 1) * 4))
  20. #define NEMC_NFCSR 0x50
  21. #define NEMC_REG_LEN 0x54
  22. #define NEMC_SMCR_SMT BIT(0)
  23. #define NEMC_SMCR_BW_SHIFT 6
  24. #define NEMC_SMCR_BW_MASK (0x3 << NEMC_SMCR_BW_SHIFT)
  25. #define NEMC_SMCR_BW_8 (0 << 6)
  26. #define NEMC_SMCR_TAS_SHIFT 8
  27. #define NEMC_SMCR_TAS_MASK (0xf << NEMC_SMCR_TAS_SHIFT)
  28. #define NEMC_SMCR_TAH_SHIFT 12
  29. #define NEMC_SMCR_TAH_MASK (0xf << NEMC_SMCR_TAH_SHIFT)
  30. #define NEMC_SMCR_TBP_SHIFT 16
  31. #define NEMC_SMCR_TBP_MASK (0xf << NEMC_SMCR_TBP_SHIFT)
  32. #define NEMC_SMCR_TAW_SHIFT 20
  33. #define NEMC_SMCR_TAW_MASK (0xf << NEMC_SMCR_TAW_SHIFT)
  34. #define NEMC_SMCR_TSTRV_SHIFT 24
  35. #define NEMC_SMCR_TSTRV_MASK (0x3f << NEMC_SMCR_TSTRV_SHIFT)
  36. #define NEMC_NFCSR_NFEn(n) BIT(((n) - 1) << 1)
  37. #define NEMC_NFCSR_NFCEn(n) BIT((((n) - 1) << 1) + 1)
  38. #define NEMC_NFCSR_TNFEn(n) BIT(16 + (n) - 1)
  39. struct jz_soc_info {
  40. u8 tas_tah_cycles_max;
  41. };
  42. struct jz4780_nemc {
  43. spinlock_t lock;
  44. struct device *dev;
  45. const struct jz_soc_info *soc_info;
  46. void __iomem *base;
  47. struct clk *clk;
  48. uint32_t clk_period;
  49. unsigned long banks_present;
  50. };
  51. /**
  52. * jz4780_nemc_num_banks() - count the number of banks referenced by a device
  53. * @dev: device to count banks for, must be a child of the NEMC.
  54. *
  55. * Return: The number of unique NEMC banks referred to by the specified NEMC
  56. * child device. Unique here means that a device that references the same bank
  57. * multiple times in its "reg" property will only count once.
  58. */
  59. unsigned int jz4780_nemc_num_banks(struct device *dev)
  60. {
  61. const __be32 *prop;
  62. unsigned int bank, count = 0;
  63. unsigned long referenced = 0;
  64. int i = 0;
  65. while ((prop = of_get_address(dev->of_node, i++, NULL, NULL))) {
  66. bank = of_read_number(prop, 1);
  67. if (!(referenced & BIT(bank))) {
  68. referenced |= BIT(bank);
  69. count++;
  70. }
  71. }
  72. return count;
  73. }
  74. EXPORT_SYMBOL(jz4780_nemc_num_banks);
  75. /**
  76. * jz4780_nemc_set_type() - set the type of device connected to a bank
  77. * @dev: child device of the NEMC.
  78. * @bank: bank number to configure.
  79. * @type: type of device connected to the bank.
  80. */
  81. void jz4780_nemc_set_type(struct device *dev, unsigned int bank,
  82. enum jz4780_nemc_bank_type type)
  83. {
  84. struct jz4780_nemc *nemc = dev_get_drvdata(dev->parent);
  85. uint32_t nfcsr;
  86. nfcsr = readl(nemc->base + NEMC_NFCSR);
  87. /* TODO: Support toggle NAND devices. */
  88. switch (type) {
  89. case JZ4780_NEMC_BANK_SRAM:
  90. nfcsr &= ~(NEMC_NFCSR_TNFEn(bank) | NEMC_NFCSR_NFEn(bank));
  91. break;
  92. case JZ4780_NEMC_BANK_NAND:
  93. nfcsr &= ~NEMC_NFCSR_TNFEn(bank);
  94. nfcsr |= NEMC_NFCSR_NFEn(bank);
  95. break;
  96. }
  97. writel(nfcsr, nemc->base + NEMC_NFCSR);
  98. }
  99. EXPORT_SYMBOL(jz4780_nemc_set_type);
  100. /**
  101. * jz4780_nemc_assert() - (de-)assert a NAND device's chip enable pin
  102. * @dev: child device of the NEMC.
  103. * @bank: bank number of device.
  104. * @assert: whether the chip enable pin should be asserted.
  105. *
  106. * (De-)asserts the chip enable pin for the NAND device connected to the
  107. * specified bank.
  108. */
  109. void jz4780_nemc_assert(struct device *dev, unsigned int bank, bool assert)
  110. {
  111. struct jz4780_nemc *nemc = dev_get_drvdata(dev->parent);
  112. uint32_t nfcsr;
  113. nfcsr = readl(nemc->base + NEMC_NFCSR);
  114. if (assert)
  115. nfcsr |= NEMC_NFCSR_NFCEn(bank);
  116. else
  117. nfcsr &= ~NEMC_NFCSR_NFCEn(bank);
  118. writel(nfcsr, nemc->base + NEMC_NFCSR);
  119. }
  120. EXPORT_SYMBOL(jz4780_nemc_assert);
  121. static uint32_t jz4780_nemc_clk_period(struct jz4780_nemc *nemc)
  122. {
  123. unsigned long rate;
  124. rate = clk_get_rate(nemc->clk);
  125. if (!rate)
  126. return 0;
  127. /* Return in picoseconds. */
  128. return div64_ul(1000000000000ull, rate);
  129. }
  130. static uint32_t jz4780_nemc_ns_to_cycles(struct jz4780_nemc *nemc, uint32_t ns)
  131. {
  132. return ((ns * 1000) + nemc->clk_period - 1) / nemc->clk_period;
  133. }
  134. static bool jz4780_nemc_configure_bank(struct jz4780_nemc *nemc,
  135. unsigned int bank,
  136. struct device_node *node)
  137. {
  138. uint32_t smcr, val, cycles;
  139. /*
  140. * Conversion of tBP and tAW cycle counts to values supported by the
  141. * hardware (round up to the next supported value).
  142. */
  143. static const u8 convert_tBP_tAW[] = {
  144. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  145. /* 11 - 12 -> 12 cycles */
  146. 11, 11,
  147. /* 13 - 15 -> 15 cycles */
  148. 12, 12, 12,
  149. /* 16 - 20 -> 20 cycles */
  150. 13, 13, 13, 13, 13,
  151. /* 21 - 25 -> 25 cycles */
  152. 14, 14, 14, 14, 14,
  153. /* 26 - 31 -> 31 cycles */
  154. 15, 15, 15, 15, 15, 15
  155. };
  156. smcr = readl(nemc->base + NEMC_SMCRn(bank));
  157. smcr &= ~NEMC_SMCR_SMT;
  158. if (!of_property_read_u32(node, "ingenic,nemc-bus-width", &val)) {
  159. smcr &= ~NEMC_SMCR_BW_MASK;
  160. switch (val) {
  161. case 8:
  162. smcr |= NEMC_SMCR_BW_8;
  163. break;
  164. default:
  165. /*
  166. * Earlier SoCs support a 16 bit bus width (the 4780
  167. * does not), until those are properly supported, error.
  168. */
  169. dev_err(nemc->dev, "unsupported bus width: %u\n", val);
  170. return false;
  171. }
  172. }
  173. if (of_property_read_u32(node, "ingenic,nemc-tAS", &val) == 0) {
  174. smcr &= ~NEMC_SMCR_TAS_MASK;
  175. cycles = jz4780_nemc_ns_to_cycles(nemc, val);
  176. if (cycles > nemc->soc_info->tas_tah_cycles_max) {
  177. dev_err(nemc->dev, "tAS %u is too high (%u cycles)\n",
  178. val, cycles);
  179. return false;
  180. }
  181. smcr |= cycles << NEMC_SMCR_TAS_SHIFT;
  182. }
  183. if (of_property_read_u32(node, "ingenic,nemc-tAH", &val) == 0) {
  184. smcr &= ~NEMC_SMCR_TAH_MASK;
  185. cycles = jz4780_nemc_ns_to_cycles(nemc, val);
  186. if (cycles > nemc->soc_info->tas_tah_cycles_max) {
  187. dev_err(nemc->dev, "tAH %u is too high (%u cycles)\n",
  188. val, cycles);
  189. return false;
  190. }
  191. smcr |= cycles << NEMC_SMCR_TAH_SHIFT;
  192. }
  193. if (of_property_read_u32(node, "ingenic,nemc-tBP", &val) == 0) {
  194. smcr &= ~NEMC_SMCR_TBP_MASK;
  195. cycles = jz4780_nemc_ns_to_cycles(nemc, val);
  196. if (cycles > 31) {
  197. dev_err(nemc->dev, "tBP %u is too high (%u cycles)\n",
  198. val, cycles);
  199. return false;
  200. }
  201. smcr |= convert_tBP_tAW[cycles] << NEMC_SMCR_TBP_SHIFT;
  202. }
  203. if (of_property_read_u32(node, "ingenic,nemc-tAW", &val) == 0) {
  204. smcr &= ~NEMC_SMCR_TAW_MASK;
  205. cycles = jz4780_nemc_ns_to_cycles(nemc, val);
  206. if (cycles > 31) {
  207. dev_err(nemc->dev, "tAW %u is too high (%u cycles)\n",
  208. val, cycles);
  209. return false;
  210. }
  211. smcr |= convert_tBP_tAW[cycles] << NEMC_SMCR_TAW_SHIFT;
  212. }
  213. if (of_property_read_u32(node, "ingenic,nemc-tSTRV", &val) == 0) {
  214. smcr &= ~NEMC_SMCR_TSTRV_MASK;
  215. cycles = jz4780_nemc_ns_to_cycles(nemc, val);
  216. if (cycles > 63) {
  217. dev_err(nemc->dev, "tSTRV %u is too high (%u cycles)\n",
  218. val, cycles);
  219. return false;
  220. }
  221. smcr |= cycles << NEMC_SMCR_TSTRV_SHIFT;
  222. }
  223. writel(smcr, nemc->base + NEMC_SMCRn(bank));
  224. return true;
  225. }
  226. static int jz4780_nemc_probe(struct platform_device *pdev)
  227. {
  228. struct device *dev = &pdev->dev;
  229. struct jz4780_nemc *nemc;
  230. struct resource *res;
  231. struct device_node *child;
  232. const __be32 *prop;
  233. unsigned int bank;
  234. unsigned long referenced;
  235. int i, ret;
  236. nemc = devm_kzalloc(dev, sizeof(*nemc), GFP_KERNEL);
  237. if (!nemc)
  238. return -ENOMEM;
  239. nemc->soc_info = device_get_match_data(dev);
  240. if (!nemc->soc_info)
  241. return -EINVAL;
  242. spin_lock_init(&nemc->lock);
  243. nemc->dev = dev;
  244. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  245. if (!res)
  246. return -EINVAL;
  247. /*
  248. * The driver currently only uses the registers up to offset
  249. * NEMC_REG_LEN. Since the EFUSE registers are in the middle of the
  250. * NEMC registers, we only request the registers we will use for now;
  251. * that way the EFUSE driver can probe too.
  252. */
  253. if (!devm_request_mem_region(dev, res->start, NEMC_REG_LEN, dev_name(dev))) {
  254. dev_err(dev, "unable to request I/O memory region\n");
  255. return -EBUSY;
  256. }
  257. nemc->base = devm_ioremap(dev, res->start, NEMC_REG_LEN);
  258. if (!nemc->base) {
  259. dev_err(dev, "failed to get I/O memory\n");
  260. return -ENOMEM;
  261. }
  262. writel(0, nemc->base + NEMC_NFCSR);
  263. nemc->clk = devm_clk_get(dev, NULL);
  264. if (IS_ERR(nemc->clk)) {
  265. dev_err(dev, "failed to get clock\n");
  266. return PTR_ERR(nemc->clk);
  267. }
  268. ret = clk_prepare_enable(nemc->clk);
  269. if (ret) {
  270. dev_err(dev, "failed to enable clock: %d\n", ret);
  271. return ret;
  272. }
  273. nemc->clk_period = jz4780_nemc_clk_period(nemc);
  274. if (!nemc->clk_period) {
  275. dev_err(dev, "failed to calculate clock period\n");
  276. clk_disable_unprepare(nemc->clk);
  277. return -EINVAL;
  278. }
  279. /*
  280. * Iterate over child devices, check that they do not conflict with
  281. * each other, and register child devices for them. If a child device
  282. * has invalid properties, it is ignored and no platform device is
  283. * registered for it.
  284. */
  285. for_each_child_of_node(nemc->dev->of_node, child) {
  286. referenced = 0;
  287. i = 0;
  288. while ((prop = of_get_address(child, i++, NULL, NULL))) {
  289. bank = of_read_number(prop, 1);
  290. if (bank < 1 || bank >= JZ4780_NEMC_NUM_BANKS) {
  291. dev_err(nemc->dev,
  292. "%pOF requests invalid bank %u\n",
  293. child, bank);
  294. /* Will continue the outer loop below. */
  295. referenced = 0;
  296. break;
  297. }
  298. referenced |= BIT(bank);
  299. }
  300. if (!referenced) {
  301. dev_err(nemc->dev, "%pOF has no addresses\n",
  302. child);
  303. continue;
  304. } else if (nemc->banks_present & referenced) {
  305. dev_err(nemc->dev, "%pOF conflicts with another node\n",
  306. child);
  307. continue;
  308. }
  309. /* Configure bank parameters. */
  310. for_each_set_bit(bank, &referenced, JZ4780_NEMC_NUM_BANKS) {
  311. if (!jz4780_nemc_configure_bank(nemc, bank, child)) {
  312. referenced = 0;
  313. break;
  314. }
  315. }
  316. if (referenced) {
  317. if (of_platform_device_create(child, NULL, nemc->dev))
  318. nemc->banks_present |= referenced;
  319. }
  320. }
  321. platform_set_drvdata(pdev, nemc);
  322. dev_info(dev, "JZ4780 NEMC initialised\n");
  323. return 0;
  324. }
  325. static void jz4780_nemc_remove(struct platform_device *pdev)
  326. {
  327. struct jz4780_nemc *nemc = platform_get_drvdata(pdev);
  328. clk_disable_unprepare(nemc->clk);
  329. }
  330. static const struct jz_soc_info jz4740_soc_info = {
  331. .tas_tah_cycles_max = 7,
  332. };
  333. static const struct jz_soc_info jz4780_soc_info = {
  334. .tas_tah_cycles_max = 15,
  335. };
  336. static const struct of_device_id jz4780_nemc_dt_match[] = {
  337. { .compatible = "ingenic,jz4740-nemc", .data = &jz4740_soc_info, },
  338. { .compatible = "ingenic,jz4780-nemc", .data = &jz4780_soc_info, },
  339. {},
  340. };
  341. static struct platform_driver jz4780_nemc_driver = {
  342. .probe = jz4780_nemc_probe,
  343. .remove_new = jz4780_nemc_remove,
  344. .driver = {
  345. .name = "jz4780-nemc",
  346. .of_match_table = of_match_ptr(jz4780_nemc_dt_match),
  347. },
  348. };
  349. static int __init jz4780_nemc_init(void)
  350. {
  351. return platform_driver_register(&jz4780_nemc_driver);
  352. }
  353. subsys_initcall(jz4780_nemc_init);