alarmDataOperation.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * GPMC support functions
  3. *
  4. * Copyright (C) 2005-2006 Nokia Corporation
  5. *
  6. * Author: Juha Yrjola
  7. *
  8. * Copyright (C) 2009 Texas Instruments
  9. * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #undef DEBUG
  16. #include <linux/irq.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/err.h>
  20. #include <linux/clk.h>
  21. #include <linux/ioport.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/io.h>
  24. #include <linux/module.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/platform_data/mtd-nand-omap2.h>
  28. #include <asm/mach-types.h>
  29. #include "soc.h"
  30. #include "common.h"
  31. #include "omap_device.h"
  32. #include "gpmc.h"
  33. #define DEVICE_NAME "omap-gpmc"
  34. /* GPMC register offsets */
  35. #define GPMC_REVISION 0x00
  36. #define GPMC_SYSCONFIG 0x10
  37. #define GPMC_SYSSTATUS 0x14
  38. #define GPMC_IRQSTATUS 0x18
  39. #define GPMC_IRQENABLE 0x1c
  40. #define GPMC_TIMEOUT_CONTROL 0x40
  41. #define GPMC_ERR_ADDRESS 0x44
  42. #define GPMC_ERR_TYPE 0x48
  43. #define GPMC_CONFIG 0x50
  44. #define GPMC_STATUS 0x54
  45. #define GPMC_PREFETCH_CONFIG1 0x1e0
  46. #define GPMC_PREFETCH_CONFIG2 0x1e4
  47. #define GPMC_PREFETCH_CONTROL 0x1ec
  48. #define GPMC_PREFETCH_STATUS 0x1f0
  49. #define GPMC_ECC_CONFIG 0x1f4
  50. #define GPMC_ECC_CONTROL 0x1f8
  51. #define GPMC_ECC_SIZE_CONFIG 0x1fc
  52. #define GPMC_ECC1_RESULT 0x200
  53. #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
  54. #define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */
  55. #define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */
  56. #define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */
  57. /* GPMC ECC control settings */
  58. #define GPMC_ECC_CTRL_ECCCLEAR 0x100
  59. #define GPMC_ECC_CTRL_ECCDISABLE 0x000
  60. #define GPMC_ECC_CTRL_ECCREG1 0x001
  61. #define GPMC_ECC_CTRL_ECCREG2 0x002
  62. #define GPMC_ECC_CTRL_ECCREG3 0x003
  63. #define GPMC_ECC_CTRL_ECCREG4 0x004
  64. #define GPMC_ECC_CTRL_ECCREG5 0x005
  65. #define GPMC_ECC_CTRL_ECCREG6 0x006
  66. #define GPMC_ECC_CTRL_ECCREG7 0x007
  67. #define GPMC_ECC_CTRL_ECCREG8 0x008
  68. #define GPMC_ECC_CTRL_ECCREG9 0x009
  69. #define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
  70. #define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
  71. #define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
  72. #define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
  73. #define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
  74. #define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
  75. #define GPMC_CS0_OFFSET 0x60
  76. #define GPMC_CS_SIZE 0x30
  77. #define GPMC_BCH_SIZE 0x10
  78. #define GPMC_MEM_START 0x00000000
  79. #define GPMC_MEM_END 0x3FFFFFFF
  80. #define BOOT_ROM_SPACE 0x100000 /* 1MB */
  81. #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
  82. #define GPMC_SECTION_SHIFT 28 /* 128 MB */
  83. #define CS_NUM_SHIFT 24
  84. #define ENABLE_PREFETCH (0x1 << 7)
  85. #define DMA_MPU_MODE 2
  86. #define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
  87. #define GPMC_REVISION_MINOR(l) (l & 0xf)
  88. #define GPMC_HAS_WR_ACCESS 0x1
  89. #define GPMC_HAS_WR_DATA_MUX_BUS 0x2
  90. /* XXX: Only NAND irq has been considered,currently these are the only ones used
  91. */
  92. #define GPMC_NR_IRQ 2
  93. struct gpmc_client_irq {
  94. unsigned irq;
  95. u32 bitmask;
  96. };
  97. /* Structure to save gpmc cs context */
  98. struct gpmc_cs_config {
  99. u32 config1;
  100. u32 config2;
  101. u32 config3;
  102. u32 config4;
  103. u32 config5;
  104. u32 config6;
  105. u32 config7;
  106. int is_valid;
  107. };
  108. /*
  109. * Structure to save/restore gpmc context
  110. * to support core off on OMAP3
  111. */
  112. struct omap3_gpmc_regs {
  113. u32 sysconfig;
  114. u32 irqenable;
  115. u32 timeout_ctrl;
  116. u32 config;
  117. u32 prefetch_config1;
  118. u32 prefetch_config2;
  119. u32 prefetch_control;
  120. struct gpmc_cs_config cs_context[GPMC_CS_NUM];
  121. };
  122. static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
  123. static struct irq_chip gpmc_irq_chip;
  124. static unsigned gpmc_irq_start;
  125. static struct resource gpmc_mem_root;
  126. static struct resource gpmc_cs_mem[GPMC_CS_NUM];
  127. static DEFINE_SPINLOCK(gpmc_mem_lock);
  128. static unsigned int gpmc_cs_map; /* flag for cs which are initialized */
  129. static struct device *gpmc_dev;
  130. static int gpmc_irq;
  131. static resource_size_t phys_base, mem_size;
  132. static unsigned gpmc_capability;
  133. static void __iomem *gpmc_base;
  134. static struct clk *gpmc_l3_clk;
  135. static irqreturn_t gpmc_handle_irq(int irq, void *dev);
  136. static void gpmc_write_reg(int idx, u32 val)
  137. {
  138. __raw_writel(val, gpmc_base + idx);
  139. }
  140. static u32 gpmc_read_reg(int idx)
  141. {
  142. return __raw_readl(gpmc_base + idx);
  143. }
  144. void gpmc_cs_write_reg(int cs, int idx, u32 val)
  145. {
  146. void __iomem *reg_addr;
  147. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  148. __raw_writel(val, reg_addr);
  149. }
  150. u32 gpmc_cs_read_reg(int cs, int idx)
  151. {
  152. void __iomem *reg_addr;
  153. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  154. return __raw_readl(reg_addr);
  155. }
  156. /* TODO: Add support for gpmc_fck to clock framework and use it */
  157. unsigned long gpmc_get_fclk_period(void)
  158. {
  159. unsigned long rate = clk_get_rate(gpmc_l3_clk);
  160. if (rate == 0) {
  161. printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
  162. return 0;
  163. }
  164. rate /= 1000;
  165. rate = 1000000000 / rate; /* In picoseconds */
  166. return rate;
  167. }
  168. unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
  169. {
  170. unsigned long tick_ps;
  171. /* Calculate in picosecs to yield more exact results */
  172. tick_ps = gpmc_get_fclk_period();
  173. return (time_ns * 1000 + tick_ps - 1) / tick_ps;
  174. }
  175. unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
  176. {
  177. unsigned long tick_ps;
  178. /* Calculate in picosecs to yield more exact results */
  179. tick_ps = gpmc_get_fclk_period();
  180. return (time_ps + tick_ps - 1) / tick_ps;
  181. }
  182. unsigned int gpmc_ticks_to_ns(unsigned int ticks)
  183. {
  184. return ticks * gpmc_get_fclk_period() / 1000;
  185. }
  186. unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns)
  187. {
  188. unsigned long ticks = gpmc_ns_to_ticks(time_ns);
  189. return ticks * gpmc_get_fclk_period() / 1000;
  190. }
  191. static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
  192. {
  193. return ticks * gpmc_get_fclk_period();
  194. }
  195. static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
  196. {
  197. unsigned long ticks = gpmc_ps_to_ticks(time_ps);
  198. return ticks * gpmc_get_fclk_period();
  199. }
  200. static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
  201. {
  202. u32 l;
  203. l = gpmc_cs_read_reg(cs, reg);
  204. if (value)
  205. l |= mask;
  206. else
  207. l &= ~mask;
  208. gpmc_cs_write_reg(cs, reg, l);
  209. }
  210. static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
  211. {
  212. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
  213. GPMC_CONFIG1_TIME_PARA_GRAN,
  214. p->time_para_granularity);
  215. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
  216. GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
  217. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
  218. GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
  219. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
  220. GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
  221. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
  222. GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay);
  223. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
  224. GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
  225. p->cycle2cyclesamecsen);
  226. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
  227. GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
  228. p->cycle2cyclediffcsen);
  229. }
  230. #ifdef DEBUG
  231. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  232. int time, const char *name)
  233. #else
  234. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  235. int time)
  236. #endif
  237. {
  238. u32 l;
  239. int ticks, mask, nr_bits;
  240. if (time == 0)
  241. ticks = 0;
  242. else
  243. ticks = gpmc_ns_to_ticks(time);
  244. nr_bits = end_bit - st_bit + 1;
  245. if (ticks >= 1 << nr_bits) {
  246. #ifdef DEBUG
  247. printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
  248. cs, name, time, ticks, 1 << nr_bits);
  249. #endif
  250. return -1;
  251. }
  252. mask = (1 << nr_bits) - 1;
  253. l = gpmc_cs_read_reg(cs, reg);
  254. #ifdef DEBUG
  255. printk(KERN_INFO
  256. "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
  257. cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
  258. (l >> st_bit) & mask, time);
  259. #endif
  260. l &= ~(mask << st_bit);
  261. l |= ticks << st_bit;
  262. gpmc_cs_write_reg(cs, reg, l);
  263. return 0;
  264. }
  265. #ifdef DEBUG
  266. #define GPMC_SET_ONE(reg, st, end, field) \
  267. if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
  268. t->field, #field) < 0) \
  269. return -1
  270. #else
  271. #define GPMC_SET_ONE(reg, st, end, field) \
  272. if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
  273. return -1
  274. #endif
  275. int gpmc_calc_divider(unsigned int sync_clk)
  276. {
  277. int div;
  278. u32 l;
  279. l = sync_clk + (gpmc_get_fclk_period() - 1);
  280. div = l / gpmc_get_fclk_period();
  281. if (div > 4)
  282. return -1;
  283. if (div <= 0)
  284. div = 1;
  285. return div;
  286. }
  287. int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
  288. {
  289. int div;
  290. u32 l;
  291. div = gpmc_calc_divider(t->sync_clk);
  292. if (div < 0)
  293. return div;
  294. GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
  295. GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
  296. GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
  297. GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
  298. GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
  299. GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
  300. GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
  301. GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
  302. GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
  303. GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
  304. GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
  305. GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
  306. GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
  307. GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
  308. GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
  309. GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
  310. GPMC_SET_ONE(GPMC_CS_CONFIG1, 18, 19, wait_monitoring);
  311. GPMC_SET_ONE(GPMC_CS_CONFIG1, 25, 26, clk_activation);
  312. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  313. GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
  314. if (gpmc_capability & GPMC_HAS_WR_ACCESS)
  315. GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
  316. /* caller is expected to have initialized CONFIG1 to cover
  317. * at least sync vs async
  318. */
  319. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  320. if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
  321. #ifdef DEBUG
  322. printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
  323. cs, (div * gpmc_get_fclk_period()) / 1000, div);
  324. #endif
  325. l &= ~0x03;
  326. l |= (div - 1);
  327. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
  328. }
  329. gpmc_cs_bool_timings(cs, &t->bool_timings);
  330. return 0;
  331. }
  332. static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
  333. {
  334. u32 l;
  335. u32 mask;
  336. mask = (1 << GPMC_SECTION_SHIFT) - size;
  337. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  338. l &= ~0x3f;
  339. l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
  340. l &= ~(0x0f << 8);
  341. l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
  342. l |= GPMC_CONFIG7_CSVALID;
  343. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  344. }
  345. static void gpmc_cs_disable_mem(int cs)
  346. {
  347. u32 l;
  348. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  349. l &= ~GPMC_CONFIG7_CSVALID;
  350. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  351. }
  352. static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
  353. {
  354. u32 l;
  355. u32 mask;
  356. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  357. *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;