gpmc-onenand.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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/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 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. div = gpmc_cs_calc_divider(cs, min_gpmc_clk_period);
  216. gpmc_clk_ns = gpmc_ticks_to_ns(div);
  217. if (gpmc_clk_ns < 15) /* >66Mhz */
  218. hf = 1;
  219. if (gpmc_clk_ns < 12) /* >83Mhz */
  220. vhf = 1;
  221. if (vhf)
  222. latency = 8;
  223. else if (hf)
  224. latency = 6;
  225. else if (gpmc_clk_ns >= 25) /* 40 MHz*/
  226. latency = 3;
  227. else
  228. latency = 4;
  229. if (clk_dep) {
  230. if (gpmc_clk_ns < 12) { /* >83Mhz */
  231. t_ces = 3;
  232. t_avds = 4;
  233. } else if (gpmc_clk_ns < 15) { /* >66Mhz */
  234. t_ces = 5;
  235. t_avds = 4;
  236. } else if (gpmc_clk_ns < 25) { /* >40Mhz */
  237. t_ces = 6;
  238. t_avds = 5;
  239. } else {
  240. t_ces = 7;
  241. t_avds = 7;
  242. }
  243. }
  244. if (first_time)
  245. set_onenand_cfg(onenand_base, latency,
  246. sync_read, sync_write, hf, vhf);
  247. if (div == 1) {
  248. reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG2);
  249. reg |= (1 << 7);
  250. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG2, reg);
  251. reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG3);
  252. reg |= (1 << 7);
  253. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG3, reg);
  254. reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG4);
  255. reg |= (1 << 7);
  256. reg |= (1 << 23);
  257. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG4, reg);
  258. } else {
  259. reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG2);
  260. reg &= ~(1 << 7);
  261. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG2, reg);
  262. reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG3);
  263. reg &= ~(1 << 7);
  264. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG3, reg);
  265. reg = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG4);
  266. reg &= ~(1 << 7);
  267. reg &= ~(1 << 23);
  268. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG4, reg);
  269. }
  270. /* Set synchronous read timings */
  271. memset(&t, 0, sizeof(t));
  272. t.sync_clk = min_gpmc_clk_period;
  273. t.cs_on = 0;
  274. t.adv_on = 0;
  275. fclk_offset_ns = gpmc_round_ns_to_ticks(max_t(int, t_ces, t_avds));
  276. fclk_offset = gpmc_ns_to_ticks(fclk_offset_ns);
  277. t.page_burst_access = gpmc_clk_ns;
  278. /* Read */
  279. t.adv_rd_off = gpmc_ticks_to_ns(fclk_offset + gpmc_ns_to_ticks(t_avdh));
  280. t.oe_on = gpmc_ticks_to_ns(fclk_offset + gpmc_ns_to_ticks(t_ach));
  281. /* Force at least 1 clk between AVD High to OE Low */
  282. if (t.oe_on <= t.adv_rd_off)
  283. t.oe_on = t.adv_rd_off + gpmc_round_ns_to_ticks(1);
  284. t.access = gpmc_ticks_to_ns(fclk_offset + (latency + 1) * div);
  285. t.oe_off = t.access + gpmc_round_ns_to_ticks(1);
  286. t.cs_rd_off = t.oe_off;
  287. ticks_cez = ((gpmc_ns_to_ticks(t_cez) + div - 1) / div) * div;
  288. t.rd_cycle = gpmc_ticks_to_ns(fclk_offset + (latency + 1) * div +
  289. ticks_cez);
  290. /* Write */
  291. if (sync_write) {
  292. t.adv_wr_off = t.adv_rd_off;
  293. t.we_on = 0;
  294. t.we_off = t.cs_rd_off;
  295. t.cs_wr_off = t.cs_rd_off;
  296. t.wr_cycle = t.rd_cycle;
  297. if (cpu_is_omap34xx()) {
  298. t.wr_data_mux_bus = gpmc_ticks_to_ns(fclk_offset +
  299. gpmc_ps_to_ticks(min_gpmc_clk_period +
  300. t_rdyo * 1000));
  301. t.wr_access = t.access;
  302. }
  303. } else {
  304. t.adv_wr_off = gpmc_round_ns_to_ticks(max_t(int,
  305. t_avdp, t_cer));
  306. t.we_on = t.adv_wr_off + gpmc_round_ns_to_ticks(t_aavdh);
  307. t.we_off = t.we_on + gpmc_round_ns_to_ticks(t_wpl);
  308. t.cs_wr_off = t.we_off + gpmc_round_ns_to_ticks(t_wph);
  309. t.wr_cycle = t.cs_wr_off + gpmc_round_ns_to_ticks(t_cez);
  310. if (cpu_is_omap34xx()) {
  311. t.wr_data_mux_bus = t.we_on;
  312. t.wr_access = t.we_on + gpmc_round_ns_to_ticks(t_ds);
  313. }
  314. }
  315. /* Configure GPMC for synchronous read */
  316. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1,
  317. GPMC_CONFIG1_WRAPBURST_SUPP |
  318. GPMC_CONFIG1_READMULTIPLE_SUPP |
  319. (sync_read ? GPMC_CONFIG1_READTYPE_SYNC : 0) |
  320. (sync_write ? GPMC_CONFIG1_WRITEMULTIPLE_SUPP : 0) |
  321. (sync_write ? GPMC_CONFIG1_WRITETYPE_SYNC : 0) |
  322. GPMC_CONFIG1_CLKACTIVATIONTIME(fclk_offset) |
  323. GPMC_CONFIG1_PAGE_LEN(2) |
  324. (cpu_is_omap34xx() ? 0 :
  325. (GPMC_CONFIG1_WAIT_READ_MON |
  326. GPMC_CONFIG1_WAIT_PIN_SEL(0))) |
  327. GPMC_CONFIG1_DEVICESIZE_16 |
  328. GPMC_CONFIG1_DEVICETYPE_NOR |
  329. GPMC_CONFIG1_MUXADDDATA);
  330. err = gpmc_cs_set_timings(cs, &t);
  331. if (err)
  332. return err;
  333. set_onenand_cfg(onenand_base, latency, sync_read, sync_write, hf, vhf);
  334. *freq_ptr = freq;
  335. return 0;
  336. }
  337. static int gpmc_onenand_setup(void __iomem *onenand_base, int *freq_ptr)
  338. {
  339. struct device *dev = &gpmc_onenand_device.dev;
  340. /* Set sync timings in GPMC */
  341. if (omap2_onenand_set_sync_mode(gpmc_onenand_data, onenand_base,
  342. freq_ptr) < 0) {
  343. dev_err(dev, "Unable to set synchronous mode\n");
  344. return -EINVAL;
  345. }
  346. return 0;
  347. }
  348. void __init gpmc_onenand_init(struct omap_onenand_platform_data *_onenand_data)
  349. {
  350. gpmc_onenand_data = _onenand_data;
  351. gpmc_onenand_data->onenand_setup = gpmc_onenand_setup;
  352. gpmc_onenand_device.dev.platform_data = gpmc_onenand_data;
  353. if (cpu_is_omap24xx() &&
  354. (gpmc_onenand_data->flags & ONENAND_SYNC_READWRITE)) {
  355. printk(KERN_ERR "Onenand using only SYNC_READ on 24xx\n");
  356. gpmc_onenand_data->flags &= ~ONENAND_SYNC_READWRITE;
  357. gpmc_onenand_data->flags |= ONENAND_SYNC_READ;
  358. }
  359. if (platform_device_register(&gpmc_onenand_device) < 0) {
  360. printk(KERN_ERR "Unable to register OneNAND device\n");
  361. return;
  362. }
  363. }