gpmc-onenand.c 10 KB

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