gpmc-onenand.c 10 KB

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