cs.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/log2.h>
  12. #include <bcm63xx_cpu.h>
  13. #include <bcm63xx_io.h>
  14. #include <bcm63xx_regs.h>
  15. #include <bcm63xx_cs.h>
  16. static DEFINE_SPINLOCK(bcm63xx_cs_lock);
  17. /*
  18. * check if given chip select exists
  19. */
  20. static int is_valid_cs(unsigned int cs)
  21. {
  22. if (cs > 6)
  23. return 0;
  24. return 1;
  25. }
  26. /*
  27. * Configure chipselect base address and size (bytes).
  28. * Size must be a power of two between 8k and 256M.
  29. */
  30. int bcm63xx_set_cs_base(unsigned int cs, u32 base, unsigned int size)
  31. {
  32. unsigned long flags;
  33. u32 val;
  34. if (!is_valid_cs(cs))
  35. return -EINVAL;
  36. /* sanity check on size */
  37. if (size != roundup_pow_of_two(size))
  38. return -EINVAL;
  39. if (size < 8 * 1024 || size > 256 * 1024 * 1024)
  40. return -EINVAL;
  41. val = (base & MPI_CSBASE_BASE_MASK);
  42. /* 8k => 0 - 256M => 15 */
  43. val |= (ilog2(size) - ilog2(8 * 1024)) << MPI_CSBASE_SIZE_SHIFT;
  44. spin_lock_irqsave(&bcm63xx_cs_lock, flags);
  45. bcm_mpi_writel(val, MPI_CSBASE_REG(cs));
  46. spin_unlock_irqrestore(&bcm63xx_cs_lock, flags);
  47. return 0;
  48. }
  49. EXPORT_SYMBOL(bcm63xx_set_cs_base);
  50. /*
  51. * configure chipselect timing (ns)
  52. */
  53. int bcm63xx_set_cs_timing(unsigned int cs, unsigned int wait,
  54. unsigned int setup, unsigned int hold)
  55. {
  56. unsigned long flags;
  57. u32 val;
  58. if (!is_valid_cs(cs))
  59. return -EINVAL;
  60. spin_lock_irqsave(&bcm63xx_cs_lock, flags);
  61. val = bcm_mpi_readl(MPI_CSCTL_REG(cs));
  62. val &= ~(MPI_CSCTL_WAIT_MASK);
  63. val &= ~(MPI_CSCTL_SETUP_MASK);
  64. val &= ~(MPI_CSCTL_HOLD_MASK);
  65. val |= wait << MPI_CSCTL_WAIT_SHIFT;
  66. val |= setup << MPI_CSCTL_SETUP_SHIFT;
  67. val |= hold << MPI_CSCTL_HOLD_SHIFT;
  68. bcm_mpi_writel(val, MPI_CSCTL_REG(cs));
  69. spin_unlock_irqrestore(&bcm63xx_cs_lock, flags);
  70. return 0;
  71. }
  72. EXPORT_SYMBOL(bcm63xx_set_cs_timing);
  73. /*
  74. * configure other chipselect parameter (data bus size, ...)
  75. */
  76. int bcm63xx_set_cs_param(unsigned int cs, u32 params)
  77. {
  78. unsigned long flags;
  79. u32 val;
  80. if (!is_valid_cs(cs))
  81. return -EINVAL;
  82. /* none of this fields apply to pcmcia */
  83. if (cs == MPI_CS_PCMCIA_COMMON ||
  84. cs == MPI_CS_PCMCIA_ATTR ||
  85. cs == MPI_CS_PCMCIA_IO)
  86. return -EINVAL;
  87. spin_lock_irqsave(&bcm63xx_cs_lock, flags);
  88. val = bcm_mpi_readl(MPI_CSCTL_REG(cs));
  89. val &= ~(MPI_CSCTL_DATA16_MASK);
  90. val &= ~(MPI_CSCTL_SYNCMODE_MASK);
  91. val &= ~(MPI_CSCTL_TSIZE_MASK);
  92. val &= ~(MPI_CSCTL_ENDIANSWAP_MASK);
  93. val |= params;
  94. bcm_mpi_writel(val, MPI_CSCTL_REG(cs));
  95. spin_unlock_irqrestore(&bcm63xx_cs_lock, flags);
  96. return 0;
  97. }
  98. EXPORT_SYMBOL(bcm63xx_set_cs_param);
  99. /*
  100. * set cs status (enable/disable)
  101. */
  102. int bcm63xx_set_cs_status(unsigned int cs, int enable)
  103. {
  104. unsigned long flags;
  105. u32 val;
  106. if (!is_valid_cs(cs))
  107. return -EINVAL;
  108. spin_lock_irqsave(&bcm63xx_cs_lock, flags);
  109. val = bcm_mpi_readl(MPI_CSCTL_REG(cs));
  110. if (enable)
  111. val |= MPI_CSCTL_ENABLE_MASK;
  112. else
  113. val &= ~MPI_CSCTL_ENABLE_MASK;
  114. bcm_mpi_writel(val, MPI_CSCTL_REG(cs));
  115. spin_unlock_irqrestore(&bcm63xx_cs_lock, flags);
  116. return 0;
  117. }
  118. EXPORT_SYMBOL(bcm63xx_set_cs_status);