gpmc.c 10.0 KB

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