aemif.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * AEMIF support for DaVinci SoCs
  3. *
  4. * Copyright (C) 2010 Texas Instruments Incorporated. http://www.ti.com/
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/io.h>
  12. #include <linux/err.h>
  13. #include <linux/clk.h>
  14. #include <linux/module.h>
  15. #include <linux/time.h>
  16. #include <mach/aemif.h>
  17. /* Timing value configuration */
  18. #define TA(x) ((x) << 2)
  19. #define RHOLD(x) ((x) << 4)
  20. #define RSTROBE(x) ((x) << 7)
  21. #define RSETUP(x) ((x) << 13)
  22. #define WHOLD(x) ((x) << 17)
  23. #define WSTROBE(x) ((x) << 20)
  24. #define WSETUP(x) ((x) << 26)
  25. #define TA_MAX 0x3
  26. #define RHOLD_MAX 0x7
  27. #define RSTROBE_MAX 0x3f
  28. #define RSETUP_MAX 0xf
  29. #define WHOLD_MAX 0x7
  30. #define WSTROBE_MAX 0x3f
  31. #define WSETUP_MAX 0xf
  32. #define TIMING_MASK (TA(TA_MAX) | \
  33. RHOLD(RHOLD_MAX) | \
  34. RSTROBE(RSTROBE_MAX) | \
  35. RSETUP(RSETUP_MAX) | \
  36. WHOLD(WHOLD_MAX) | \
  37. WSTROBE(WSTROBE_MAX) | \
  38. WSETUP(WSETUP_MAX))
  39. /*
  40. * aemif_calc_rate - calculate timing data.
  41. * @wanted: The cycle time needed in nanoseconds.
  42. * @clk: The input clock rate in kHz.
  43. * @max: The maximum divider value that can be programmed.
  44. *
  45. * On success, returns the calculated timing value minus 1 for easy
  46. * programming into AEMIF timing registers, else negative errno.
  47. */
  48. static int aemif_calc_rate(int wanted, unsigned long clk, int max)
  49. {
  50. int result;
  51. result = DIV_ROUND_UP((wanted * clk), NSEC_PER_MSEC) - 1;
  52. pr_debug("%s: result %d from %ld, %d\n", __func__, result, clk, wanted);
  53. /* It is generally OK to have a more relaxed timing than requested... */
  54. if (result < 0)
  55. result = 0;
  56. /* ... But configuring tighter timings is not an option. */
  57. else if (result > max)
  58. result = -EINVAL;
  59. return result;
  60. }
  61. /**
  62. * davinci_aemif_setup_timing - setup timing values for a given AEMIF interface
  63. * @t: timing values to be progammed
  64. * @base: The virtual base address of the AEMIF interface
  65. * @cs: chip-select to program the timing values for
  66. *
  67. * This function programs the given timing values (in real clock) into the
  68. * AEMIF registers taking the AEMIF clock into account.
  69. *
  70. * This function does not use any locking while programming the AEMIF
  71. * because it is expected that there is only one user of a given
  72. * chip-select.
  73. *
  74. * Returns 0 on success, else negative errno.
  75. */
  76. int davinci_aemif_setup_timing(struct davinci_aemif_timing *t,
  77. void __iomem *base, unsigned cs)
  78. {
  79. unsigned set, val;
  80. int ta, rhold, rstrobe, rsetup, whold, wstrobe, wsetup;
  81. unsigned offset = A1CR_OFFSET + cs * 4;
  82. struct clk *aemif_clk;
  83. unsigned long clkrate;
  84. if (!t)
  85. return 0; /* Nothing to do */
  86. aemif_clk = clk_get(NULL, "aemif");
  87. if (IS_ERR(aemif_clk))
  88. return PTR_ERR(aemif_clk);
  89. clkrate = clk_get_rate(aemif_clk);
  90. clkrate /= 1000; /* turn clock into kHz for ease of use */
  91. ta = aemif_calc_rate(t->ta, clkrate, TA_MAX);
  92. rhold = aemif_calc_rate(t->rhold, clkrate, RHOLD_MAX);
  93. rstrobe = aemif_calc_rate(t->rstrobe, clkrate, RSTROBE_MAX);
  94. rsetup = aemif_calc_rate(t->rsetup, clkrate, RSETUP_MAX);
  95. whold = aemif_calc_rate(t->whold, clkrate, WHOLD_MAX);
  96. wstrobe = aemif_calc_rate(t->wstrobe, clkrate, WSTROBE_MAX);
  97. wsetup = aemif_calc_rate(t->wsetup, clkrate, WSETUP_MAX);
  98. if (ta < 0 || rhold < 0 || rstrobe < 0 || rsetup < 0 ||
  99. whold < 0 || wstrobe < 0 || wsetup < 0) {
  100. pr_err("%s: cannot get suitable timings\n", __func__);
  101. return -EINVAL;
  102. }
  103. set = TA(ta) | RHOLD(rhold) | RSTROBE(rstrobe) | RSETUP(rsetup) |
  104. WHOLD(whold) | WSTROBE(wstrobe) | WSETUP(wsetup);
  105. val = __raw_readl(base + offset);
  106. val &= ~TIMING_MASK;
  107. val |= set;
  108. __raw_writel(val, base + offset);
  109. return 0;
  110. }
  111. EXPORT_SYMBOL(davinci_aemif_setup_timing);