gpmc-onenand.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * linux/arch/arm/mach-omap2/gpmc-onenand.c
  3. *
  4. * Copyright (C) 2006 - 2009 Nokia Corporation
  5. * Contacts: Juha Yrjola
  6. * Tony Lindgren
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/string.h>
  13. #include <linux/kernel.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/mtd/onenand_regs.h>
  16. #include <linux/io.h>
  17. #include <linux/omap-gpmc.h>
  18. #include <linux/platform_data/mtd-onenand-omap2.h>
  19. #include <linux/err.h>
  20. #include <asm/mach/flash.h>
  21. #include "soc.h"
  22. #define ONENAND_IO_SIZE SZ_128K
  23. #define ONENAND_FLAG_SYNCREAD (1 << 0)
  24. #define ONENAND_FLAG_SYNCWRITE (1 << 1)
  25. #define ONENAND_FLAG_HF (1 << 2)
  26. #define ONENAND_FLAG_VHF (1 << 3)
  27. static unsigned onenand_flags;
  28. static unsigned latency;
  29. static struct omap_onenand_platform_data *gpmc_onenand_data;
  30. static struct resource gpmc_onenand_resource = {
  31. .flags = IORESOURCE_MEM,
  32. };
  33. static struct platform_device gpmc_onenand_device = {
  34. .name = "omap2-onenand",
  35. .id = -1,
  36. .num_resources = 1,
  37. .resource = &gpmc_onenand_resource,
  38. };
  39. static struct gpmc_settings onenand_async = {
  40. .device_width = GPMC_DEVWIDTH_16BIT,
  41. .mux_add_data = GPMC_MUX_AD,
  42. };
  43. static struct gpmc_settings onenand_sync = {
  44. .burst_read = true,
  45. .burst_wrap = true,
  46. .burst_len = GPMC_BURST_16,
  47. .device_width = GPMC_DEVWIDTH_16BIT,
  48. .mux_add_data = GPMC_MUX_AD,
  49. .wait_pin = 0,
  50. };
  51. static void omap2_onenand_calc_async_timings(struct gpmc_timings *t)
  52. {
  53. struct gpmc_device_timings dev_t;
  54. const int t_cer = 15;
  55. const int t_avdp = 12;
  56. const int t_aavdh = 7;
  57. const int t_ce = 76;
  58. const int t_aa = 76;
  59. const int t_oe = 20;
  60. const int t_cez = 20; /* max of t_cez, t_oez */
  61. const int t_wpl = 40;
  62. const int t_wph = 30;
  63. memset(&dev_t, 0, sizeof(dev_t));
  64. dev_t.t_avdp_r = max_t(int, t_avdp, t_cer) * 1000;
  65. dev_t.t_avdp_w = dev_t.t_avdp_r;
  66. dev_t.t_aavdh = t_aavdh * 1000;
  67. dev_t.t_aa = t_aa * 1000;
  68. dev_t.t_ce = t_ce * 1000;
  69. dev_t.t_oe = t_oe * 1000;
  70. dev_t.t_cez_r = t_cez * 1000;
  71. dev_t.t_cez_w = dev_t.t_cez_r;
  72. dev_t.t_wpl = t_wpl * 1000;
  73. dev_t.t_wph = t_wph * 1000;
  74. gpmc_calc_timings(t, &onenand_async, &dev_t);
  75. }
  76. static void omap2_onenand_set_async_mode(void __iomem *onenand_base)
  77. {
  78. u32 reg;
  79. /* Ensure sync read and sync write are disabled */
  80. reg = readw(onenand_base + ONENAND_REG_SYS_CFG1);
  81. reg &= ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE;
  82. writew(reg, onenand_base + ONENAND_REG_SYS_CFG1);
  83. }
  84. static void set_onenand_cfg(void __iomem *onenand_base)
  85. {
  86. u32 reg = ONENAND_SYS_CFG1_RDY | ONENAND_SYS_CFG1_INT;
  87. reg |= (latency << ONENAND_SYS_CFG1_BRL_SHIFT) |
  88. ONENAND_SYS_CFG1_BL_16;
  89. if (onenand_flags & ONENAND_FLAG_SYNCREAD)
  90. reg |= ONENAND_SYS_CFG1_SYNC_READ;
  91. else
  92. reg &= ~ONENAND_SYS_CFG1_SYNC_READ;
  93. if (onenand_flags & ONENAND_FLAG_SYNCWRITE)
  94. reg |= ONENAND_SYS_CFG1_SYNC_WRITE;
  95. else
  96. reg &= ~ONENAND_SYS_CFG1_SYNC_WRITE;
  97. if (onenand_flags & ONENAND_FLAG_HF)
  98. reg |= ONENAND_SYS_CFG1_HF;
  99. else
  100. reg &= ~ONENAND_SYS_CFG1_HF;
  101. if (onenand_flags & ONENAND_FLAG_VHF)
  102. reg |= ONENAND_SYS_CFG1_VHF;
  103. else
  104. reg &= ~ONENAND_SYS_CFG1_VHF;
  105. writew(reg, onenand_base + ONENAND_REG_SYS_CFG1);
  106. }
  107. static int omap2_onenand_get_freq(struct omap_onenand_platform_data *cfg,
  108. void __iomem *onenand_base)
  109. {
  110. u16 ver = readw(onenand_base + ONENAND_REG_VERSION_ID);
  111. int freq;
  112. switch ((ver >> 4) & 0xf) {
  113. case 0:
  114. freq = 40;
  115. break;
  116. case 1:
  117. freq = 54;
  118. break;
  119. case 2:
  120. freq = 66;
  121. break;
  122. case 3:
  123. freq = 83;
  124. break;
  125. case 4:
  126. freq = 104;
  127. break;
  128. default:
  129. pr_err("onenand rate not detected, bad GPMC async timings?\n");
  130. freq = 0;
  131. }
  132. return freq;
  133. }
  134. static void omap2_onenand_calc_sync_timings(struct gpmc_timings *t,
  135. unsigned int flags,
  136. int freq)
  137. {
  138. struct gpmc_device_timings dev_t;
  139. const int t_cer = 15;
  140. const int t_avdp = 12;
  141. const int t_cez = 20; /* max of t_cez, t_oez */
  142. const int t_wpl = 40;
  143. const int t_wph = 30;
  144. int min_gpmc_clk_period, t_ces, t_avds, t_avdh, t_ach, t_aavdh, t_rdyo;
  145. int div, gpmc_clk_ns;
  146. if (flags & ONENAND_SYNC_READ)
  147. onenand_flags = ONENAND_FLAG_SYNCREAD;
  148. else if (flags & ONENAND_SYNC_READWRITE)
  149. onenand_flags = ONENAND_FLAG_SYNCREAD | ONENAND_FLAG_SYNCWRITE;
  150. switch (freq) {
  151. case 104:
  152. min_gpmc_clk_period = 9600; /* 104 MHz */
  153. t_ces = 3;
  154. t_avds = 4;
  155. t_avdh = 2;
  156. t_ach = 3;
  157. t_aavdh = 6;
  158. t_rdyo = 6;
  159. break;
  160. case 83:
  161. min_gpmc_clk_period = 12000; /* 83 MHz */
  162. t_ces = 5;
  163. t_avds = 4;
  164. t_avdh = 2;
  165. t_ach = 6;
  166. t_aavdh = 6;
  167. t_rdyo = 9;
  168. break;
  169. case 66:
  170. min_gpmc_clk_period = 15000; /* 66 MHz */
  171. t_ces = 6;
  172. t_avds = 5;
  173. t_avdh = 2;
  174. t_ach = 6;
  175. t_aavdh = 6;
  176. t_rdyo = 11;
  177. break;
  178. default:
  179. min_gpmc_clk_period = 18500; /* 54 MHz */
  180. t_ces = 7;
  181. t_avds = 7;
  182. t_avdh = 7;
  183. t_ach = 9;
  184. t_aavdh = 7;
  185. t_rdyo = 15;
  186. onenand_flags &= ~ONENAND_FLAG_SYNCWRITE;
  187. break;
  188. }
  189. div = gpmc_calc_divider(min_gpmc_clk_period);
  190. gpmc_clk_ns = gpmc_ticks_to_ns(div);
  191. if (gpmc_clk_ns < 15) /* >66MHz */
  192. onenand_flags |= ONENAND_FLAG_HF;
  193. else
  194. onenand_flags &= ~ONENAND_FLAG_HF;
  195. if (gpmc_clk_ns < 12) /* >83MHz */
  196. onenand_flags |= ONENAND_FLAG_VHF;
  197. else
  198. onenand_flags &= ~ONENAND_FLAG_VHF;
  199. if (onenand_flags & ONENAND_FLAG_VHF)
  200. latency = 8;
  201. else if (onenand_flags & ONENAND_FLAG_HF)
  202. latency = 6;
  203. else if (gpmc_clk_ns >= 25) /* 40 MHz*/
  204. latency = 3;
  205. else
  206. latency = 4;
  207. /* Set synchronous read timings */
  208. memset(&dev_t, 0, sizeof(dev_t));
  209. if (onenand_flags & ONENAND_FLAG_SYNCREAD)
  210. onenand_sync.sync_read = true;
  211. if (onenand_flags & ONENAND_FLAG_SYNCWRITE) {
  212. onenand_sync.sync_write = true;
  213. onenand_sync.burst_write = true;
  214. } else {
  215. dev_t.t_avdp_w = max(t_avdp, t_cer) * 1000;
  216. dev_t.t_wpl = t_wpl * 1000;
  217. dev_t.t_wph = t_wph * 1000;
  218. dev_t.t_aavdh = t_aavdh * 1000;
  219. }
  220. dev_t.ce_xdelay = true;
  221. dev_t.avd_xdelay = true;
  222. dev_t.oe_xdelay = true;
  223. dev_t.we_xdelay = true;
  224. dev_t.clk = min_gpmc_clk_period;
  225. dev_t.t_bacc = dev_t.clk;
  226. dev_t.t_ces = t_ces * 1000;
  227. dev_t.t_avds = t_avds * 1000;
  228. dev_t.t_avdh = t_avdh * 1000;
  229. dev_t.t_ach = t_ach * 1000;
  230. dev_t.cyc_iaa = (latency + 1);
  231. dev_t.t_cez_r = t_cez * 1000;
  232. dev_t.t_cez_w = dev_t.t_cez_r;
  233. dev_t.cyc_aavdh_oe = 1;
  234. dev_t.t_rdyo = t_rdyo * 1000 + min_gpmc_clk_period;
  235. gpmc_calc_timings(t, &onenand_sync, &dev_t);
  236. }
  237. static int omap2_onenand_setup_async(void __iomem *onenand_base)
  238. {
  239. struct gpmc_timings t;
  240. int ret;
  241. /*
  242. * Note that we need to keep sync_write set for the call to
  243. * omap2_onenand_set_async_mode() to work to detect the onenand
  244. * supported clock rate for the sync timings.
  245. */
  246. if (gpmc_onenand_data->of_node) {
  247. gpmc_read_settings_dt(gpmc_onenand_data->of_node,
  248. &onenand_async);
  249. if (onenand_async.sync_read || onenand_async.sync_write) {
  250. if (onenand_async.sync_write)
  251. gpmc_onenand_data->flags |=
  252. ONENAND_SYNC_READWRITE;
  253. else
  254. gpmc_onenand_data->flags |= ONENAND_SYNC_READ;
  255. onenand_async.sync_read = false;
  256. }
  257. }
  258. onenand_async.sync_write = true;
  259. omap2_onenand_calc_async_timings(&t);
  260. ret = gpmc_cs_program_settings(gpmc_onenand_data->cs, &onenand_async);
  261. if (ret < 0)
  262. return ret;
  263. ret = gpmc_cs_set_timings(gpmc_onenand_data->cs, &t, &onenand_async);
  264. if (ret < 0)
  265. return ret;
  266. omap2_onenand_set_async_mode(onenand_base);
  267. return 0;
  268. }
  269. static int omap2_onenand_setup_sync(void __iomem *onenand_base, int *freq_ptr)
  270. {
  271. int ret, freq = *freq_ptr;
  272. struct gpmc_timings t;
  273. if (!freq) {
  274. /* Very first call freq is not known */
  275. freq = omap2_onenand_get_freq(gpmc_onenand_data, onenand_base);
  276. if (!freq)
  277. return -ENODEV;
  278. set_onenand_cfg(onenand_base);
  279. }
  280. if (gpmc_onenand_data->of_node) {
  281. gpmc_read_settings_dt(gpmc_onenand_data->of_node,
  282. &onenand_sync);
  283. } else {
  284. /*
  285. * FIXME: Appears to be legacy code from initial ONENAND commit.
  286. * Unclear what boards this is for and if this can be removed.
  287. */
  288. if (!cpu_is_omap34xx())
  289. onenand_sync.wait_on_read = true;
  290. }
  291. omap2_onenand_calc_sync_timings(&t, gpmc_onenand_data->flags, freq);
  292. ret = gpmc_cs_program_settings(gpmc_onenand_data->cs, &onenand_sync);
  293. if (ret < 0)
  294. return ret;
  295. ret = gpmc_cs_set_timings(gpmc_onenand_data->cs, &t, &onenand_sync);
  296. if (ret < 0)
  297. return ret;
  298. set_onenand_cfg(onenand_base);
  299. *freq_ptr = freq;
  300. return 0;
  301. }
  302. static int gpmc_onenand_setup(void __iomem *onenand_base, int *freq_ptr)
  303. {
  304. struct device *dev = &gpmc_onenand_device.dev;
  305. unsigned l = ONENAND_SYNC_READ | ONENAND_SYNC_READWRITE;
  306. int ret;
  307. ret = omap2_onenand_setup_async(onenand_base);
  308. if (ret) {
  309. dev_err(dev, "unable to set to async mode\n");
  310. return ret;
  311. }
  312. if (!(gpmc_onenand_data->flags & l))
  313. return 0;
  314. ret = omap2_onenand_setup_sync(onenand_base, freq_ptr);
  315. if (ret)
  316. dev_err(dev, "unable to set to sync mode\n");
  317. return ret;
  318. }
  319. int gpmc_onenand_init(struct omap_onenand_platform_data *_onenand_data)
  320. {
  321. int err;
  322. struct device *dev = &gpmc_onenand_device.dev;
  323. gpmc_onenand_data = _onenand_data;
  324. gpmc_onenand_data->onenand_setup = gpmc_onenand_setup;
  325. gpmc_onenand_device.dev.platform_data = gpmc_onenand_data;
  326. if (cpu_is_omap24xx() &&
  327. (gpmc_onenand_data->flags & ONENAND_SYNC_READWRITE)) {
  328. dev_warn(dev, "OneNAND using only SYNC_READ on 24xx\n");
  329. gpmc_onenand_data->flags &= ~ONENAND_SYNC_READWRITE;
  330. gpmc_onenand_data->flags |= ONENAND_SYNC_READ;
  331. }
  332. if (cpu_is_omap34xx())
  333. gpmc_onenand_data->flags |= ONENAND_IN_OMAP34XX;
  334. else
  335. gpmc_onenand_data->flags &= ~ONENAND_IN_OMAP34XX;
  336. err = gpmc_cs_request(gpmc_onenand_data->cs, ONENAND_IO_SIZE,
  337. (unsigned long *)&gpmc_onenand_resource.start);
  338. if (err < 0) {
  339. dev_err(dev, "Cannot request GPMC CS %d, error %d\n",
  340. gpmc_onenand_data->cs, err);
  341. return err;
  342. }
  343. gpmc_onenand_resource.end = gpmc_onenand_resource.start +
  344. ONENAND_IO_SIZE - 1;
  345. err = platform_device_register(&gpmc_onenand_device);
  346. if (err) {
  347. dev_err(dev, "Unable to register OneNAND device\n");
  348. gpmc_cs_free(gpmc_onenand_data->cs);
  349. }
  350. return err;
  351. }