gpmc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/err.h>
  19. #include <linux/clk.h>
  20. #include <linux/ioport.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/io.h>
  23. #include <linux/module.h>
  24. #include <asm/mach-types.h>
  25. #include <mach/gpmc.h>
  26. #include <mach/sdrc.h>
  27. /* GPMC register offsets */
  28. #define GPMC_REVISION 0x00
  29. #define GPMC_SYSCONFIG 0x10
  30. #define GPMC_SYSSTATUS 0x14
  31. #define GPMC_IRQSTATUS 0x18
  32. #define GPMC_IRQENABLE 0x1c
  33. #define GPMC_TIMEOUT_CONTROL 0x40
  34. #define GPMC_ERR_ADDRESS 0x44
  35. #define GPMC_ERR_TYPE 0x48
  36. #define GPMC_CONFIG 0x50
  37. #define GPMC_STATUS 0x54
  38. #define GPMC_PREFETCH_CONFIG1 0x1e0
  39. #define GPMC_PREFETCH_CONFIG2 0x1e4
  40. #define GPMC_PREFETCH_CONTROL 0x1ec
  41. #define GPMC_PREFETCH_STATUS 0x1f0
  42. #define GPMC_ECC_CONFIG 0x1f4
  43. #define GPMC_ECC_CONTROL 0x1f8
  44. #define GPMC_ECC_SIZE_CONFIG 0x1fc
  45. #define GPMC_CS0 0x60
  46. #define GPMC_CS_SIZE 0x30
  47. #define GPMC_MEM_START 0x00000000
  48. #define GPMC_MEM_END 0x3FFFFFFF
  49. #define BOOT_ROM_SPACE 0x100000 /* 1MB */
  50. #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
  51. #define GPMC_SECTION_SHIFT 28 /* 128 MB */
  52. static struct resource gpmc_mem_root;
  53. static struct resource gpmc_cs_mem[GPMC_CS_NUM];
  54. static DEFINE_SPINLOCK(gpmc_mem_lock);
  55. static unsigned gpmc_cs_map;
  56. static void __iomem *gpmc_base;
  57. static struct clk *gpmc_l3_clk;
  58. static void gpmc_write_reg(int idx, u32 val)
  59. {
  60. __raw_writel(val, gpmc_base + idx);
  61. }
  62. static u32 gpmc_read_reg(int idx)
  63. {
  64. return __raw_readl(gpmc_base + idx);
  65. }
  66. void gpmc_cs_write_reg(int cs, int idx, u32 val)
  67. {
  68. void __iomem *reg_addr;
  69. reg_addr = gpmc_base + GPMC_CS0 + (cs * GPMC_CS_SIZE) + idx;
  70. __raw_writel(val, reg_addr);
  71. }
  72. u32 gpmc_cs_read_reg(int cs, int idx)
  73. {
  74. void __iomem *reg_addr;
  75. reg_addr = gpmc_base + GPMC_CS0 + (cs * GPMC_CS_SIZE) + idx;
  76. return __raw_readl(reg_addr);
  77. }
  78. /* TODO: Add support for gpmc_fck to clock framework and use it */
  79. unsigned long gpmc_get_fclk_period(void)
  80. {
  81. unsigned long rate = clk_get_rate(gpmc_l3_clk);
  82. if (rate == 0) {
  83. printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
  84. return 0;
  85. }
  86. rate /= 1000;
  87. rate = 1000000000 / rate; /* In picoseconds */
  88. return rate;
  89. }
  90. unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
  91. {
  92. unsigned long tick_ps;
  93. /* Calculate in picosecs to yield more exact results */
  94. tick_ps = gpmc_get_fclk_period();
  95. return (time_ns * 1000 + tick_ps - 1) / tick_ps;
  96. }
  97. unsigned int gpmc_ticks_to_ns(unsigned int ticks)
  98. {
  99. return ticks * gpmc_get_fclk_period() / 1000;
  100. }
  101. unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns)
  102. {
  103. unsigned long ticks = gpmc_ns_to_ticks(time_ns);
  104. return ticks * gpmc_get_fclk_period() / 1000;
  105. }
  106. #ifdef DEBUG
  107. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  108. int time, const char *name)
  109. #else
  110. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  111. int time)
  112. #endif
  113. {
  114. u32 l;
  115. int ticks, mask, nr_bits;
  116. if (time == 0)
  117. ticks = 0;
  118. else
  119. ticks = gpmc_ns_to_ticks(time);
  120. nr_bits = end_bit - st_bit + 1;
  121. if (ticks >= 1 << nr_bits) {
  122. #ifdef DEBUG
  123. printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
  124. cs, name, time, ticks, 1 << nr_bits);
  125. #endif
  126. return -1;
  127. }
  128. mask = (1 << nr_bits) - 1;
  129. l = gpmc_cs_read_reg(cs, reg);
  130. #ifdef DEBUG
  131. printk(KERN_INFO
  132. "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
  133. cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
  134. (l >> st_bit) & mask, time);
  135. #endif
  136. l &= ~(mask << st_bit);
  137. l |= ticks << st_bit;
  138. gpmc_cs_write_reg(cs, reg, l);
  139. return 0;
  140. }
  141. #ifdef DEBUG
  142. #define GPMC_SET_ONE(reg, st, end, field) \
  143. if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
  144. t->field, #field) < 0) \
  145. return -1
  146. #else
  147. #define GPMC_SET_ONE(reg, st, end, field) \
  148. if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
  149. return -1
  150. #endif
  151. int gpmc_cs_calc_divider(int cs, unsigned int sync_clk)
  152. {
  153. int div;
  154. u32 l;
  155. l = sync_clk * 1000 + (gpmc_get_fclk_period() - 1);
  156. div = l / gpmc_get_fclk_period();
  157. if (div > 4)
  158. return -1;
  159. if (div <= 0)
  160. div = 1;
  161. return div;
  162. }
  163. int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
  164. {
  165. int div;
  166. u32 l;
  167. div = gpmc_cs_calc_divider(cs, t->sync_clk);
  168. if (div < 0)
  169. return -1;
  170. GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
  171. GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
  172. GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
  173. GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
  174. GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
  175. GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
  176. GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
  177. GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
  178. GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
  179. GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
  180. GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
  181. GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
  182. GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
  183. GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
  184. if (cpu_is_omap34xx()) {
  185. GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
  186. GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
  187. }
  188. /* caller is expected to have initialized CONFIG1 to cover
  189. * at least sync vs async
  190. */
  191. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  192. if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
  193. #ifdef DEBUG
  194. printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
  195. cs, (div * gpmc_get_fclk_period()) / 1000, div);
  196. #endif
  197. l &= ~0x03;
  198. l |= (div - 1);
  199. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
  200. }
  201. return 0;
  202. }
  203. static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
  204. {
  205. u32 l;
  206. u32 mask;
  207. mask = (1 << GPMC_SECTION_SHIFT) - size;
  208. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  209. l &= ~0x3f;
  210. l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
  211. l &= ~(0x0f << 8);
  212. l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
  213. l |= 1 << 6; /* CSVALID */
  214. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  215. }
  216. static void gpmc_cs_disable_mem(int cs)
  217. {
  218. u32 l;
  219. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  220. l &= ~(1 << 6); /* CSVALID */
  221. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  222. }
  223. static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
  224. {
  225. u32 l;
  226. u32 mask;
  227. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  228. *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
  229. mask = (l >> 8) & 0x0f;
  230. *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
  231. }
  232. static int gpmc_cs_mem_enabled(int cs)
  233. {
  234. u32 l;
  235. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  236. return l & (1 << 6);
  237. }
  238. int gpmc_cs_set_reserved(int cs, int reserved)
  239. {
  240. if (cs > GPMC_CS_NUM)
  241. return -ENODEV;
  242. gpmc_cs_map &= ~(1 << cs);
  243. gpmc_cs_map |= (reserved ? 1 : 0) << cs;
  244. return 0;
  245. }
  246. int gpmc_cs_reserved(int cs)
  247. {
  248. if (cs > GPMC_CS_NUM)
  249. return -ENODEV;
  250. return gpmc_cs_map & (1 << cs);
  251. }
  252. static unsigned long gpmc_mem_align(unsigned long size)
  253. {
  254. int order;
  255. size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
  256. order = GPMC_CHUNK_SHIFT - 1;
  257. do {
  258. size >>= 1;
  259. order++;
  260. } while (size);
  261. size = 1 << order;
  262. return size;
  263. }
  264. static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
  265. {
  266. struct resource *res = &gpmc_cs_mem[cs];
  267. int r;
  268. size = gpmc_mem_align(size);
  269. spin_lock(&gpmc_mem_lock);
  270. res->start = base;
  271. res->end = base + size - 1;
  272. r = request_resource(&gpmc_mem_root, res);
  273. spin_unlock(&gpmc_mem_lock);
  274. return r;
  275. }
  276. int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
  277. {
  278. struct resource *res = &gpmc_cs_mem[cs];
  279. int r = -1;
  280. if (cs > GPMC_CS_NUM)
  281. return -ENODEV;
  282. size = gpmc_mem_align(size);
  283. if (size > (1 << GPMC_SECTION_SHIFT))
  284. return -ENOMEM;
  285. spin_lock(&gpmc_mem_lock);
  286. if (gpmc_cs_reserved(cs)) {
  287. r = -EBUSY;
  288. goto out;
  289. }
  290. if (gpmc_cs_mem_enabled(cs))
  291. r = adjust_resource(res, res->start & ~(size - 1), size);
  292. if (r < 0)
  293. r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
  294. size, NULL, NULL);
  295. if (r < 0)
  296. goto out;
  297. gpmc_cs_enable_mem(cs, res->start, res->end - res->start + 1);
  298. *base = res->start;
  299. gpmc_cs_set_reserved(cs, 1);
  300. out:
  301. spin_unlock(&gpmc_mem_lock);
  302. return r;
  303. }
  304. EXPORT_SYMBOL(gpmc_cs_request);
  305. void gpmc_cs_free(int cs)
  306. {
  307. spin_lock(&gpmc_mem_lock);
  308. if (cs >= GPMC_CS_NUM || !gpmc_cs_reserved(cs)) {
  309. printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
  310. BUG();
  311. spin_unlock(&gpmc_mem_lock);
  312. return;
  313. }
  314. gpmc_cs_disable_mem(cs);
  315. release_resource(&gpmc_cs_mem[cs]);
  316. gpmc_cs_set_reserved(cs, 0);
  317. spin_unlock(&gpmc_mem_lock);
  318. }
  319. EXPORT_SYMBOL(gpmc_cs_free);
  320. static void __init gpmc_mem_init(void)
  321. {
  322. int cs;
  323. unsigned long boot_rom_space = 0;
  324. /* never allocate the first page, to facilitate bug detection;
  325. * even if we didn't boot from ROM.
  326. */
  327. boot_rom_space = BOOT_ROM_SPACE;
  328. /* In apollon the CS0 is mapped as 0x0000 0000 */
  329. if (machine_is_omap_apollon())
  330. boot_rom_space = 0;
  331. gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
  332. gpmc_mem_root.end = GPMC_MEM_END;
  333. /* Reserve all regions that has been set up by bootloader */
  334. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  335. u32 base, size;
  336. if (!gpmc_cs_mem_enabled(cs))
  337. continue;
  338. gpmc_cs_get_memconf(cs, &base, &size);
  339. if (gpmc_cs_insert_mem(cs, base, size) < 0)
  340. BUG();
  341. }
  342. }
  343. void __init gpmc_init(void)
  344. {
  345. u32 l;
  346. char *ck;
  347. if (cpu_is_omap24xx()) {
  348. ck = "core_l3_ck";
  349. if (cpu_is_omap2420())
  350. l = OMAP2420_GPMC_BASE;
  351. else
  352. l = OMAP34XX_GPMC_BASE;
  353. } else if (cpu_is_omap34xx()) {
  354. ck = "gpmc_fck";
  355. l = OMAP34XX_GPMC_BASE;
  356. } else if (cpu_is_omap44xx()) {
  357. ck = "gpmc_fck";
  358. l = OMAP44XX_GPMC_BASE;
  359. }
  360. gpmc_l3_clk = clk_get(NULL, ck);
  361. if (IS_ERR(gpmc_l3_clk)) {
  362. printk(KERN_ERR "Could not get GPMC clock %s\n", ck);
  363. BUG();
  364. }
  365. gpmc_base = ioremap(l, SZ_4K);
  366. if (!gpmc_base) {
  367. clk_put(gpmc_l3_clk);
  368. printk(KERN_ERR "Could not get GPMC register memory\n");
  369. BUG();
  370. }
  371. l = gpmc_read_reg(GPMC_REVISION);
  372. printk(KERN_INFO "GPMC revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
  373. /* Set smart idle mode and automatic L3 clock gating */
  374. l = gpmc_read_reg(GPMC_SYSCONFIG);
  375. l &= 0x03 << 3;
  376. l |= (0x02 << 3) | (1 << 0);
  377. gpmc_write_reg(GPMC_SYSCONFIG, l);
  378. gpmc_mem_init();
  379. }