gpmc-onenand.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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/kernel.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/mtd/onenand_regs.h>
  15. #include <linux/io.h>
  16. #include <asm/mach/flash.h>
  17. #include <mach/onenand.h>
  18. #include <mach/board.h>
  19. #include <mach/gpmc.h>
  20. static struct omap_onenand_platform_data *gpmc_onenand_data;
  21. static struct platform_device gpmc_onenand_device = {
  22. .name = "omap2-onenand",
  23. .id = -1,
  24. };
  25. static int omap2_onenand_set_async_mode(int cs, void __iomem *onenand_base)
  26. {
  27. struct gpmc_timings t;
  28. const int t_cer = 15;
  29. const int t_avdp = 12;
  30. const int t_aavdh = 7;
  31. const int t_ce = 76;
  32. const int t_aa = 76;
  33. const int t_oe = 20;
  34. const int t_cez = 20; /* max of t_cez, t_oez */
  35. const int t_ds = 30;
  36. const int t_wpl = 40;
  37. const int t_wph = 30;
  38. memset(&t, 0, sizeof(t));
  39. t.sync_clk = 0;
  40. t.cs_on = 0;
  41. t.adv_on = 0;
  42. /* Read */
  43. t.adv_rd_off = gpmc_round_ns_to_ticks(max_t(int, t_avdp, t_cer));
  44. t.oe_on = t.adv_rd_off + gpmc_round_ns_to_ticks(t_aavdh);
  45. t.access = t.adv_on + gpmc_round_ns_to_ticks(t_aa);
  46. t.access = max_t(int, t.access, t.cs_on + gpmc_round_ns_to_ticks(t_ce));
  47. t.access = max_t(int, t.access, t.oe_on + gpmc_round_ns_to_ticks(t_oe));
  48. t.oe_off = t.access + gpmc_round_ns_to_ticks(1);
  49. t.cs_rd_off = t.oe_off;
  50. t.rd_cycle = t.cs_rd_off + gpmc_round_ns_to_ticks(t_cez);
  51. /* Write */
  52. t.adv_wr_off = t.adv_rd_off;
  53. t.we_on = t.oe_on;
  54. if (cpu_is_omap34xx()) {
  55. t.wr_data_mux_bus = t.we_on;
  56. t.wr_access = t.we_on + gpmc_round_ns_to_ticks(t_ds);
  57. }
  58. t.we_off = t.we_on + gpmc_round_ns_to_ticks(t_wpl);
  59. t.cs_wr_off = t.we_off + gpmc_round_ns_to_ticks(t_wph);
  60. t.wr_cycle = t.cs_wr_off + gpmc_round_ns_to_ticks(t_cez);
  61. /* Configure GPMC for asynchronous read */
  62. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1,
  63. GPMC_CONFIG1_DEVICESIZE_16 |
  64. GPMC_CONFIG1_MUXADDDATA);
  65. return gpmc_cs_set_timings(cs, &t);
  66. }
  67. static void set_onenand_cfg(void __iomem *onenand_base, int latency,
  68. int sync_read, int sync_write, int hf)
  69. {
  70. u32 reg;
  71. reg = readw(onenand_base + ONENAND_REG_SYS_CFG1);
  72. reg &= ~((0x7 << ONENAND_SYS_CFG1_BRL_SHIFT) | (0x7 << 9));
  73. reg |= (latency << ONENAND_SYS_CFG1_BRL_SHIFT) |
  74. ONENAND_SYS_CFG1_BL_16;
  75. if (sync_read)
  76. reg |= ONENAND_SYS_CFG1_SYNC_READ;
  77. else
  78. reg &= ~ONENAND_SYS_CFG1_SYNC_READ;
  79. if (sync_write)
  80. reg |= ONENAND_SYS_CFG1_SYNC_WRITE;
  81. else
  82. reg &= ~ONENAND_SYS_CFG1_SYNC_WRITE;
  83. if (hf)
  84. reg |= ONENAND_SYS_CFG1_HF;
  85. else
  86. reg &= ~ONENAND_SYS_CFG1_HF;
  87. writew(reg, onenand_base + ONENAND_REG_SYS_CFG1);
  88. }
  89. static int omap2_onenand_set_sync_mode(struct omap_onenand_platform_data *cfg,
  90. void __iomem *onenand_base,
  91. int freq)
  92. {
  93. struct gpmc_timings t;
  94. const int t_cer = 15;
  95. const int t_avdp = 12;
  96. const int t_cez = 20; /* max of t_cez, t_oez */
  97. const int t_ds = 30;
  98. const int t_wpl = 40;
  99. const int t_wph = 30;
  100. int min_gpmc_clk_period, t_ces, t_avds, t_avdh, t_ach, t_aavdh, t_rdyo;
  101. int tick_ns, div, fclk_offset_ns, fclk_offset, gpmc_clk_ns, latency;
  102. int first_time = 0, hf = 0, sync_read = 0, sync_write = 0;
  103. int err, ticks_cez;
  104. int cs = cfg->cs;
  105. u32 reg;
  106. if (cfg->flags & ONENAND_SYNC_READ) {
  107. sync_read = 1;
  108. } else if (cfg->flags & ONENAND_SYNC_READWRITE) {
  109. sync_read = 1;
  110. sync_write = 1;
  111. }
  112. if (!freq) {
  113. /* Very first call freq is not known */
  114. err = omap2_onenand_set_async_mode(cs, onenand_base);
  115. if (err)
  116. return err;
  117. reg = readw(onenand_base + ONENAND_REG_VERSION_ID);
  118. switch ((reg >> 4) & 0xf) {
  119. case 0:
  120. freq = 40;
  121. break;
  122. case 1:
  123. freq = 54;
  124. break;
  125. case 2:
  126. freq = 66;
  127. break;
  128. case 3:
  129. freq = 83;
  130. break;
  131. case 4:
  132. freq = 104;
  133. break;
  134. default:
  135. freq = 54;
  136. break;
  137. }
  138. first_time = 1;
  139. }
  140. switch (freq) {
  141. case 83:
  142. min_gpmc_clk_period = 12; /* 83 MHz */
  143. t_ces = 5;
  144. t_avds = 4;
  145. t_avdh = 2;
  146. t_ach = 6;
  147. t_aavdh = 6;
  148. t_rdyo = 9;
  149. break;
  150. case 66:
  151. min_gpmc_clk_period = 15; /* 66 MHz */
  152. t_ces = 6;
  153. t_avds = 5;
  154. t_avdh = 2;
  155. t_ach = 6;
  156. t_aavdh = 6;
  157. t_rdyo = 11;
  158. break;
  159. default:
  160. min_gpmc_clk_period = 18; /* 54 MHz */
  161. t_ces = 7;
  162. t_avds = 7;
  163. t_avdh = 7;
  164. t_ach = 9;
  165. t_aavdh = 7;
  166. t_rdyo = 15;
  167. sync_write = 0;
  168. break;
  169. }
  170. tick_ns = gpmc_ticks_to_ns(1);
  171. div = gpmc_cs_calc_divider(cs, min_gpmc_clk_period);
  172. gpmc_clk_ns = gpmc_ticks_to_ns(div);
  173. if (gpmc_clk_ns < 15) /* >66Mhz */
  174. hf = 1;
  175. if (hf)
  176. latency = 6;
  177. else if (gpmc_clk_ns >= 25) /* 40 MHz*/
  178. latency = 3;
  179. else
  180. latency = 4;
  181. if (first_time)
  182. set_onenand_cfg(onenand_base, latency,
  183. sync_read, sync_write, hf);
  184. if (div == 1) {
  185. reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG2);
  186. reg |= (1 << 7);
  187. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG2, reg);
  188. reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG3);
  189. reg |= (1 << 7);
  190. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG3, reg);
  191. reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG4);
  192. reg |= (1 << 7);
  193. reg |= (1 << 23);
  194. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG4, reg);
  195. } else {
  196. reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG2);
  197. reg &= ~(1 << 7);
  198. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG2, reg);
  199. reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG3);
  200. reg &= ~(1 << 7);
  201. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG3, reg);
  202. reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG4);
  203. reg &= ~(1 << 7);
  204. reg &= ~(1 << 23);
  205. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG4, reg);
  206. }
  207. /* Set synchronous read timings */
  208. memset(&t, 0, sizeof(t));
  209. t.sync_clk = min_gpmc_clk_period;
  210. t.cs_on = 0;
  211. t.adv_on = 0;
  212. fclk_offset_ns = gpmc_round_ns_to_ticks(max_t(int, t_ces, t_avds));
  213. fclk_offset = gpmc_ns_to_ticks(fclk_offset_ns);
  214. t.page_burst_access = gpmc_clk_ns;
  215. /* Read */
  216. t.adv_rd_off = gpmc_ticks_to_ns(fclk_offset + gpmc_ns_to_ticks(t_avdh));
  217. t.oe_on = gpmc_ticks_to_ns(fclk_offset + gpmc_ns_to_ticks(t_ach));
  218. t.access = gpmc_ticks_to_ns(fclk_offset + (latency + 1) * div);
  219. t.oe_off = t.access + gpmc_round_ns_to_ticks(1);
  220. t.cs_rd_off = t.oe_off;
  221. ticks_cez = ((gpmc_ns_to_ticks(t_cez) + div - 1) / div) * div;
  222. t.rd_cycle = gpmc_ticks_to_ns(fclk_offset + (latency + 1) * div +
  223. ticks_cez);
  224. /* Write */
  225. if (sync_write) {
  226. t.adv_wr_off = t.adv_rd_off;
  227. t.we_on = 0;
  228. t.we_off = t.cs_rd_off;
  229. t.cs_wr_off = t.cs_rd_off;
  230. t.wr_cycle = t.rd_cycle;
  231. if (cpu_is_omap34xx()) {
  232. t.wr_data_mux_bus = gpmc_ticks_to_ns(fclk_offset +
  233. gpmc_ns_to_ticks(min_gpmc_clk_period +
  234. t_rdyo));
  235. t.wr_access = t.access;
  236. }
  237. } else {
  238. t.adv_wr_off = gpmc_round_ns_to_ticks(max_t(int,
  239. t_avdp, t_cer));
  240. t.we_on = t.adv_wr_off + gpmc_round_ns_to_ticks(t_aavdh);
  241. t.we_off = t.we_on + gpmc_round_ns_to_ticks(t_wpl);
  242. t.cs_wr_off = t.we_off + gpmc_round_ns_to_ticks(t_wph);
  243. t.wr_cycle = t.cs_wr_off + gpmc_round_ns_to_ticks(t_cez);
  244. if (cpu_is_omap34xx()) {
  245. t.wr_data_mux_bus = t.we_on;
  246. t.wr_access = t.we_on + gpmc_round_ns_to_ticks(t_ds);
  247. }
  248. }
  249. /* Configure GPMC for synchronous read */
  250. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1,
  251. GPMC_CONFIG1_WRAPBURST_SUPP |
  252. GPMC_CONFIG1_READMULTIPLE_SUPP |
  253. (sync_read ? GPMC_CONFIG1_READTYPE_SYNC : 0) |
  254. (sync_write ? GPMC_CONFIG1_WRITEMULTIPLE_SUPP : 0) |
  255. (sync_write ? GPMC_CONFIG1_WRITETYPE_SYNC : 0) |
  256. GPMC_CONFIG1_CLKACTIVATIONTIME(fclk_offset) |
  257. GPMC_CONFIG1_PAGE_LEN(2) |
  258. (cpu_is_omap34xx() ? 0 :
  259. (GPMC_CONFIG1_WAIT_READ_MON |
  260. GPMC_CONFIG1_WAIT_PIN_SEL(0))) |
  261. GPMC_CONFIG1_DEVICESIZE_16 |
  262. GPMC_CONFIG1_DEVICETYPE_NOR |
  263. GPMC_CONFIG1_MUXADDDATA);
  264. err = gpmc_cs_set_timings(cs, &t);
  265. if (err)
  266. return err;
  267. set_onenand_cfg(onenand_base, latency, sync_read, sync_write, hf);
  268. return 0;
  269. }
  270. static int gpmc_onenand_setup(void __iomem *onenand_base, int freq)
  271. {
  272. struct device *dev = &gpmc_onenand_device.dev;
  273. /* Set sync timings in GPMC */
  274. if (omap2_onenand_set_sync_mode(gpmc_onenand_data, onenand_base,
  275. freq) < 0) {
  276. dev_err(dev, "Unable to set synchronous mode\n");
  277. return -EINVAL;
  278. }
  279. return 0;
  280. }
  281. void __init gpmc_onenand_init(struct omap_onenand_platform_data *_onenand_data)
  282. {
  283. gpmc_onenand_data = _onenand_data;
  284. gpmc_onenand_data->onenand_setup = gpmc_onenand_setup;
  285. gpmc_onenand_device.dev.platform_data = gpmc_onenand_data;
  286. if (cpu_is_omap24xx() &&
  287. (gpmc_onenand_data->flags & ONENAND_SYNC_READWRITE)) {
  288. printk(KERN_ERR "Onenand using only SYNC_READ on 24xx\n");
  289. gpmc_onenand_data->flags &= ~ONENAND_SYNC_READWRITE;
  290. gpmc_onenand_data->flags |= ONENAND_SYNC_READ;
  291. }
  292. if (platform_device_register(&gpmc_onenand_device) < 0) {
  293. printk(KERN_ERR "Unable to register OneNAND device\n");
  294. return;
  295. }
  296. }