jz4780-nemc.c 9.7 KB

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