gpmc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. #define PREFETCH_FIFOTHRESHOLD (0x40 << 8)
  53. #define CS_NUM_SHIFT 24
  54. #define ENABLE_PREFETCH (0x1 << 7)
  55. #define DMA_MPU_MODE 2
  56. static struct resource gpmc_mem_root;
  57. static struct resource gpmc_cs_mem[GPMC_CS_NUM];
  58. static DEFINE_SPINLOCK(gpmc_mem_lock);
  59. static unsigned gpmc_cs_map;
  60. static void __iomem *gpmc_base;
  61. static struct clk *gpmc_l3_clk;
  62. static void gpmc_write_reg(int idx, u32 val)
  63. {
  64. __raw_writel(val, gpmc_base + idx);
  65. }
  66. static u32 gpmc_read_reg(int idx)
  67. {
  68. return __raw_readl(gpmc_base + idx);
  69. }
  70. void gpmc_cs_write_reg(int cs, int idx, u32 val)
  71. {
  72. void __iomem *reg_addr;
  73. reg_addr = gpmc_base + GPMC_CS0 + (cs * GPMC_CS_SIZE) + idx;
  74. __raw_writel(val, reg_addr);
  75. }
  76. u32 gpmc_cs_read_reg(int cs, int idx)
  77. {
  78. void __iomem *reg_addr;
  79. reg_addr = gpmc_base + GPMC_CS0 + (cs * GPMC_CS_SIZE) + idx;
  80. return __raw_readl(reg_addr);
  81. }
  82. /* TODO: Add support for gpmc_fck to clock framework and use it */
  83. unsigned long gpmc_get_fclk_period(void)
  84. {
  85. unsigned long rate = clk_get_rate(gpmc_l3_clk);
  86. if (rate == 0) {
  87. printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
  88. return 0;
  89. }
  90. rate /= 1000;
  91. rate = 1000000000 / rate; /* In picoseconds */
  92. return rate;
  93. }
  94. unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
  95. {
  96. unsigned long tick_ps;
  97. /* Calculate in picosecs to yield more exact results */
  98. tick_ps = gpmc_get_fclk_period();
  99. return (time_ns * 1000 + tick_ps - 1) / tick_ps;
  100. }
  101. unsigned int gpmc_ticks_to_ns(unsigned int ticks)
  102. {
  103. return ticks * gpmc_get_fclk_period() / 1000;
  104. }
  105. unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns)
  106. {
  107. unsigned long ticks = gpmc_ns_to_ticks(time_ns);
  108. return ticks * gpmc_get_fclk_period() / 1000;
  109. }
  110. #ifdef DEBUG
  111. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  112. int time, const char *name)
  113. #else
  114. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  115. int time)
  116. #endif
  117. {
  118. u32 l;
  119. int ticks, mask, nr_bits;
  120. if (time == 0)
  121. ticks = 0;
  122. else
  123. ticks = gpmc_ns_to_ticks(time);
  124. nr_bits = end_bit - st_bit + 1;
  125. if (ticks >= 1 << nr_bits) {
  126. #ifdef DEBUG
  127. printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
  128. cs, name, time, ticks, 1 << nr_bits);
  129. #endif
  130. return -1;
  131. }
  132. mask = (1 << nr_bits) - 1;
  133. l = gpmc_cs_read_reg(cs, reg);
  134. #ifdef DEBUG
  135. printk(KERN_INFO
  136. "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
  137. cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
  138. (l >> st_bit) & mask, time);
  139. #endif
  140. l &= ~(mask << st_bit);
  141. l |= ticks << st_bit;
  142. gpmc_cs_write_reg(cs, reg, l);
  143. return 0;
  144. }
  145. #ifdef DEBUG
  146. #define GPMC_SET_ONE(reg, st, end, field) \
  147. if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
  148. t->field, #field) < 0) \
  149. return -1
  150. #else
  151. #define GPMC_SET_ONE(reg, st, end, field) \
  152. if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
  153. return -1
  154. #endif
  155. int gpmc_cs_calc_divider(int cs, unsigned int sync_clk)
  156. {
  157. int div;
  158. u32 l;
  159. l = sync_clk * 1000 + (gpmc_get_fclk_period() - 1);
  160. div = l / gpmc_get_fclk_period();
  161. if (div > 4)
  162. return -1;
  163. if (div <= 0)
  164. div = 1;
  165. return div;
  166. }
  167. int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
  168. {
  169. int div;
  170. u32 l;
  171. div = gpmc_cs_calc_divider(cs, t->sync_clk);
  172. if (div < 0)
  173. return -1;
  174. GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
  175. GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
  176. GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
  177. GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
  178. GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
  179. GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
  180. GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
  181. GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
  182. GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
  183. GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
  184. GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
  185. GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
  186. GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
  187. GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
  188. if (cpu_is_omap34xx()) {
  189. GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
  190. GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
  191. }
  192. /* caller is expected to have initialized CONFIG1 to cover
  193. * at least sync vs async
  194. */
  195. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  196. if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
  197. #ifdef DEBUG
  198. printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
  199. cs, (div * gpmc_get_fclk_period()) / 1000, div);
  200. #endif
  201. l &= ~0x03;
  202. l |= (div - 1);
  203. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
  204. }
  205. return 0;
  206. }
  207. static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
  208. {
  209. u32 l;
  210. u32 mask;
  211. mask = (1 << GPMC_SECTION_SHIFT) - size;
  212. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  213. l &= ~0x3f;
  214. l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
  215. l &= ~(0x0f << 8);
  216. l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
  217. l |= 1 << 6; /* CSVALID */
  218. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  219. }
  220. static void gpmc_cs_disable_mem(int cs)
  221. {
  222. u32 l;
  223. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  224. l &= ~(1 << 6); /* CSVALID */
  225. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  226. }
  227. static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
  228. {
  229. u32 l;
  230. u32 mask;
  231. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  232. *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
  233. mask = (l >> 8) & 0x0f;
  234. *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
  235. }
  236. static int gpmc_cs_mem_enabled(int cs)
  237. {
  238. u32 l;
  239. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  240. return l & (1 << 6);
  241. }
  242. int gpmc_cs_set_reserved(int cs, int reserved)
  243. {
  244. if (cs > GPMC_CS_NUM)
  245. return -ENODEV;
  246. gpmc_cs_map &= ~(1 << cs);
  247. gpmc_cs_map |= (reserved ? 1 : 0) << cs;
  248. return 0;
  249. }
  250. int gpmc_cs_reserved(int cs)
  251. {
  252. if (cs > GPMC_CS_NUM)
  253. return -ENODEV;
  254. return gpmc_cs_map & (1 << cs);
  255. }
  256. static unsigned long gpmc_mem_align(unsigned long size)
  257. {
  258. int order;
  259. size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
  260. order = GPMC_CHUNK_SHIFT - 1;
  261. do {
  262. size >>= 1;
  263. order++;
  264. } while (size);
  265. size = 1 << order;
  266. return size;
  267. }
  268. static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
  269. {
  270. struct resource *res = &gpmc_cs_mem[cs];
  271. int r;
  272. size = gpmc_mem_align(size);
  273. spin_lock(&gpmc_mem_lock);
  274. res->start = base;
  275. res->end = base + size - 1;
  276. r = request_resource(&gpmc_mem_root, res);
  277. spin_unlock(&gpmc_mem_lock);
  278. return r;
  279. }
  280. int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
  281. {
  282. struct resource *res = &gpmc_cs_mem[cs];
  283. int r = -1;
  284. if (cs > GPMC_CS_NUM)
  285. return -ENODEV;
  286. size = gpmc_mem_align(size);
  287. if (size > (1 << GPMC_SECTION_SHIFT))
  288. return -ENOMEM;
  289. spin_lock(&gpmc_mem_lock);
  290. if (gpmc_cs_reserved(cs)) {
  291. r = -EBUSY;
  292. goto out;
  293. }
  294. if (gpmc_cs_mem_enabled(cs))
  295. r = adjust_resource(res, res->start & ~(size - 1), size);
  296. if (r < 0)
  297. r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
  298. size, NULL, NULL);
  299. if (r < 0)
  300. goto out;
  301. gpmc_cs_enable_mem(cs, res->start, resource_size(res));
  302. *base = res->start;
  303. gpmc_cs_set_reserved(cs, 1);
  304. out:
  305. spin_unlock(&gpmc_mem_lock);
  306. return r;
  307. }
  308. EXPORT_SYMBOL(gpmc_cs_request);
  309. void gpmc_cs_free(int cs)
  310. {
  311. spin_lock(&gpmc_mem_lock);
  312. if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) {
  313. printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
  314. BUG();
  315. spin_unlock(&gpmc_mem_lock);
  316. return;
  317. }
  318. gpmc_cs_disable_mem(cs);
  319. release_resource(&gpmc_cs_mem[cs]);
  320. gpmc_cs_set_reserved(cs, 0);
  321. spin_unlock(&gpmc_mem_lock);
  322. }
  323. EXPORT_SYMBOL(gpmc_cs_free);
  324. /**
  325. * gpmc_prefetch_enable - configures and starts prefetch transfer
  326. * @cs: nand cs (chip select) number
  327. * @dma_mode: dma mode enable (1) or disable (0)
  328. * @u32_count: number of bytes to be transferred
  329. * @is_write: prefetch read(0) or write post(1) mode
  330. */
  331. int gpmc_prefetch_enable(int cs, int dma_mode,
  332. unsigned int u32_count, int is_write)
  333. {
  334. uint32_t prefetch_config1;
  335. if (!(gpmc_read_reg(GPMC_PREFETCH_CONTROL))) {
  336. /* Set the amount of bytes to be prefetched */
  337. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, u32_count);
  338. /* Set dma/mpu mode, the prefetch read / post write and
  339. * enable the engine. Set which cs is has requested for.
  340. */
  341. prefetch_config1 = ((cs << CS_NUM_SHIFT) |
  342. PREFETCH_FIFOTHRESHOLD |
  343. ENABLE_PREFETCH |
  344. (dma_mode << DMA_MPU_MODE) |
  345. (0x1 & is_write));
  346. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, prefetch_config1);
  347. } else {
  348. return -EBUSY;
  349. }
  350. /* Start the prefetch engine */
  351. gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x1);
  352. return 0;
  353. }
  354. EXPORT_SYMBOL(gpmc_prefetch_enable);
  355. /**
  356. * gpmc_prefetch_reset - disables and stops the prefetch engine
  357. */
  358. void gpmc_prefetch_reset(void)
  359. {
  360. /* Stop the PFPW engine */
  361. gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x0);
  362. /* Reset/disable the PFPW engine */
  363. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, 0x0);
  364. }
  365. EXPORT_SYMBOL(gpmc_prefetch_reset);
  366. /**
  367. * gpmc_prefetch_status - reads prefetch status of engine
  368. */
  369. int gpmc_prefetch_status(void)
  370. {
  371. return gpmc_read_reg(GPMC_PREFETCH_STATUS);
  372. }
  373. EXPORT_SYMBOL(gpmc_prefetch_status);
  374. static void __init gpmc_mem_init(void)
  375. {
  376. int cs;
  377. unsigned long boot_rom_space = 0;
  378. /* never allocate the first page, to facilitate bug detection;
  379. * even if we didn't boot from ROM.
  380. */
  381. boot_rom_space = BOOT_ROM_SPACE;
  382. /* In apollon the CS0 is mapped as 0x0000 0000 */
  383. if (machine_is_omap_apollon())
  384. boot_rom_space = 0;
  385. gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
  386. gpmc_mem_root.end = GPMC_MEM_END;
  387. /* Reserve all regions that has been set up by bootloader */
  388. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  389. u32 base, size;
  390. if (!gpmc_cs_mem_enabled(cs))
  391. continue;
  392. gpmc_cs_get_memconf(cs, &base, &size);
  393. if (gpmc_cs_insert_mem(cs, base, size) < 0)
  394. BUG();
  395. }
  396. }
  397. void __init gpmc_init(void)
  398. {
  399. u32 l;
  400. char *ck;
  401. if (cpu_is_omap24xx()) {
  402. ck = "core_l3_ck";
  403. if (cpu_is_omap2420())
  404. l = OMAP2420_GPMC_BASE;
  405. else
  406. l = OMAP34XX_GPMC_BASE;
  407. } else if (cpu_is_omap34xx()) {
  408. ck = "gpmc_fck";
  409. l = OMAP34XX_GPMC_BASE;
  410. } else if (cpu_is_omap44xx()) {
  411. ck = "gpmc_fck";
  412. l = OMAP44XX_GPMC_BASE;
  413. }
  414. gpmc_l3_clk = clk_get(NULL, ck);
  415. if (IS_ERR(gpmc_l3_clk)) {
  416. printk(KERN_ERR "Could not get GPMC clock %s\n", ck);
  417. BUG();
  418. }
  419. gpmc_base = ioremap(l, SZ_4K);
  420. if (!gpmc_base) {
  421. clk_put(gpmc_l3_clk);
  422. printk(KERN_ERR "Could not get GPMC register memory\n");
  423. BUG();
  424. }
  425. l = gpmc_read_reg(GPMC_REVISION);
  426. printk(KERN_INFO "GPMC revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
  427. /* Set smart idle mode and automatic L3 clock gating */
  428. l = gpmc_read_reg(GPMC_SYSCONFIG);
  429. l &= 0x03 << 3;
  430. l |= (0x02 << 3) | (1 << 0);
  431. gpmc_write_reg(GPMC_SYSCONFIG, l);
  432. gpmc_mem_init();
  433. }