gpmc-onenand.c 9.1 KB

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