gpmc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/err.h>
  15. #include <linux/clk.h>
  16. #include <asm/io.h>
  17. #include <asm/arch/gpmc.h>
  18. #undef DEBUG
  19. #define GPMC_BASE 0x6800a000
  20. #define GPMC_REVISION 0x00
  21. #define GPMC_SYSCONFIG 0x10
  22. #define GPMC_SYSSTATUS 0x14
  23. #define GPMC_IRQSTATUS 0x18
  24. #define GPMC_IRQENABLE 0x1c
  25. #define GPMC_TIMEOUT_CONTROL 0x40
  26. #define GPMC_ERR_ADDRESS 0x44
  27. #define GPMC_ERR_TYPE 0x48
  28. #define GPMC_CONFIG 0x50
  29. #define GPMC_STATUS 0x54
  30. #define GPMC_PREFETCH_CONFIG1 0x1e0
  31. #define GPMC_PREFETCH_CONFIG2 0x1e4
  32. #define GPMC_PREFETCH_CONTROL 0x1e8
  33. #define GPMC_PREFETCH_STATUS 0x1f0
  34. #define GPMC_ECC_CONFIG 0x1f4
  35. #define GPMC_ECC_CONTROL 0x1f8
  36. #define GPMC_ECC_SIZE_CONFIG 0x1fc
  37. #define GPMC_CS0 0x60
  38. #define GPMC_CS_SIZE 0x30
  39. static void __iomem *gpmc_base =
  40. (void __iomem *) IO_ADDRESS(GPMC_BASE);
  41. static void __iomem *gpmc_cs_base =
  42. (void __iomem *) IO_ADDRESS(GPMC_BASE) + GPMC_CS0;
  43. static struct clk *gpmc_l3_clk;
  44. static void gpmc_write_reg(int idx, u32 val)
  45. {
  46. __raw_writel(val, gpmc_base + idx);
  47. }
  48. static u32 gpmc_read_reg(int idx)
  49. {
  50. return __raw_readl(gpmc_base + idx);
  51. }
  52. void gpmc_cs_write_reg(int cs, int idx, u32 val)
  53. {
  54. void __iomem *reg_addr;
  55. reg_addr = gpmc_cs_base + (cs * GPMC_CS_SIZE) + idx;
  56. __raw_writel(val, reg_addr);
  57. }
  58. u32 gpmc_cs_read_reg(int cs, int idx)
  59. {
  60. return __raw_readl(gpmc_cs_base + (cs * GPMC_CS_SIZE) + idx);
  61. }
  62. /* TODO: Add support for gpmc_fck to clock framework and use it */
  63. static unsigned long gpmc_get_fclk_period(void)
  64. {
  65. /* In picoseconds */
  66. return 1000000000 / ((clk_get_rate(gpmc_l3_clk)) / 1000);
  67. }
  68. unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
  69. {
  70. unsigned long tick_ps;
  71. /* Calculate in picosecs to yield more exact results */
  72. tick_ps = gpmc_get_fclk_period();
  73. return (time_ns * 1000 + tick_ps - 1) / tick_ps;
  74. }
  75. #ifdef DEBUG
  76. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  77. int time, const char *name)
  78. #else
  79. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  80. int time)
  81. #endif
  82. {
  83. u32 l;
  84. int ticks, mask, nr_bits;
  85. if (time == 0)
  86. ticks = 0;
  87. else
  88. ticks = gpmc_ns_to_ticks(time);
  89. nr_bits = end_bit - st_bit + 1;
  90. if (ticks >= 1 << nr_bits)
  91. return -1;
  92. mask = (1 << nr_bits) - 1;
  93. l = gpmc_cs_read_reg(cs, reg);
  94. #ifdef DEBUG
  95. printk(KERN_INFO "GPMC CS%d: %-10s: %d ticks, %3lu ns (was %i ticks)\n",
  96. cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
  97. (l >> st_bit) & mask);
  98. #endif
  99. l &= ~(mask << st_bit);
  100. l |= ticks << st_bit;
  101. gpmc_cs_write_reg(cs, reg, l);
  102. return 0;
  103. }
  104. #ifdef DEBUG
  105. #define GPMC_SET_ONE(reg, st, end, field) \
  106. if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
  107. t->field, #field) < 0) \
  108. return -1
  109. #else
  110. #define GPMC_SET_ONE(reg, st, end, field) \
  111. if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
  112. return -1
  113. #endif
  114. int gpmc_cs_calc_divider(int cs, unsigned int sync_clk)
  115. {
  116. int div;
  117. u32 l;
  118. l = sync_clk * 1000 + (gpmc_get_fclk_period() - 1);
  119. div = l / gpmc_get_fclk_period();
  120. if (div > 4)
  121. return -1;
  122. if (div < 0)
  123. div = 1;
  124. return div;
  125. }
  126. int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
  127. {
  128. int div;
  129. u32 l;
  130. div = gpmc_cs_calc_divider(cs, t->sync_clk);
  131. if (div < 0)
  132. return -1;
  133. GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
  134. GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
  135. GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
  136. GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
  137. GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
  138. GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
  139. GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
  140. GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
  141. GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
  142. GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
  143. GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
  144. GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
  145. GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
  146. GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
  147. #ifdef DEBUG
  148. printk(KERN_INFO "GPMC CS%d CLK period is %lu (div %d)\n",
  149. cs, gpmc_get_fclk_period(), div);
  150. #endif
  151. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  152. l &= ~0x03;
  153. l |= (div - 1);
  154. return 0;
  155. }
  156. unsigned long gpmc_cs_get_base_addr(int cs)
  157. {
  158. return (gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7) & 0x1f) << 24;
  159. }
  160. void __init gpmc_init(void)
  161. {
  162. u32 l;
  163. gpmc_l3_clk = clk_get(NULL, "core_l3_ck");
  164. BUG_ON(IS_ERR(gpmc_l3_clk));
  165. l = gpmc_read_reg(GPMC_REVISION);
  166. printk(KERN_INFO "GPMC revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
  167. /* Set smart idle mode and automatic L3 clock gating */
  168. l = gpmc_read_reg(GPMC_SYSCONFIG);
  169. l &= 0x03 << 3;
  170. l |= (0x02 << 3) | (1 << 0);
  171. gpmc_write_reg(GPMC_SYSCONFIG, l);
  172. }