Staging: hv: mousevsc: Cleanup alloc_input_device()
[zen-stable.git] / arch / mips / bcm63xx / cs.c
blob50d8190bbf7b25a53400be516392793145946d8f
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.
6 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
7 */
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/spinlock.h>
12 #include <linux/log2.h>
13 #include <bcm63xx_cpu.h>
14 #include <bcm63xx_io.h>
15 #include <bcm63xx_regs.h>
16 #include <bcm63xx_cs.h>
18 static DEFINE_SPINLOCK(bcm63xx_cs_lock);
21 * check if given chip select exists
23 static int is_valid_cs(unsigned int cs)
25 if (cs > 6)
26 return 0;
27 return 1;
31 * Configure chipselect base address and size (bytes).
32 * Size must be a power of two between 8k and 256M.
34 int bcm63xx_set_cs_base(unsigned int cs, u32 base, unsigned int size)
36 unsigned long flags;
37 u32 val;
39 if (!is_valid_cs(cs))
40 return -EINVAL;
42 /* sanity check on size */
43 if (size != roundup_pow_of_two(size))
44 return -EINVAL;
46 if (size < 8 * 1024 || size > 256 * 1024 * 1024)
47 return -EINVAL;
49 val = (base & MPI_CSBASE_BASE_MASK);
50 /* 8k => 0 - 256M => 15 */
51 val |= (ilog2(size) - ilog2(8 * 1024)) << MPI_CSBASE_SIZE_SHIFT;
53 spin_lock_irqsave(&bcm63xx_cs_lock, flags);
54 bcm_mpi_writel(val, MPI_CSBASE_REG(cs));
55 spin_unlock_irqrestore(&bcm63xx_cs_lock, flags);
57 return 0;
60 EXPORT_SYMBOL(bcm63xx_set_cs_base);
63 * configure chipselect timing (ns)
65 int bcm63xx_set_cs_timing(unsigned int cs, unsigned int wait,
66 unsigned int setup, unsigned int hold)
68 unsigned long flags;
69 u32 val;
71 if (!is_valid_cs(cs))
72 return -EINVAL;
74 spin_lock_irqsave(&bcm63xx_cs_lock, flags);
75 val = bcm_mpi_readl(MPI_CSCTL_REG(cs));
76 val &= ~(MPI_CSCTL_WAIT_MASK);
77 val &= ~(MPI_CSCTL_SETUP_MASK);
78 val &= ~(MPI_CSCTL_HOLD_MASK);
79 val |= wait << MPI_CSCTL_WAIT_SHIFT;
80 val |= setup << MPI_CSCTL_SETUP_SHIFT;
81 val |= hold << MPI_CSCTL_HOLD_SHIFT;
82 bcm_mpi_writel(val, MPI_CSCTL_REG(cs));
83 spin_unlock_irqrestore(&bcm63xx_cs_lock, flags);
85 return 0;
88 EXPORT_SYMBOL(bcm63xx_set_cs_timing);
91 * configure other chipselect parameter (data bus size, ...)
93 int bcm63xx_set_cs_param(unsigned int cs, u32 params)
95 unsigned long flags;
96 u32 val;
98 if (!is_valid_cs(cs))
99 return -EINVAL;
101 /* none of this fields apply to pcmcia */
102 if (cs == MPI_CS_PCMCIA_COMMON ||
103 cs == MPI_CS_PCMCIA_ATTR ||
104 cs == MPI_CS_PCMCIA_IO)
105 return -EINVAL;
107 spin_lock_irqsave(&bcm63xx_cs_lock, flags);
108 val = bcm_mpi_readl(MPI_CSCTL_REG(cs));
109 val &= ~(MPI_CSCTL_DATA16_MASK);
110 val &= ~(MPI_CSCTL_SYNCMODE_MASK);
111 val &= ~(MPI_CSCTL_TSIZE_MASK);
112 val &= ~(MPI_CSCTL_ENDIANSWAP_MASK);
113 val |= params;
114 bcm_mpi_writel(val, MPI_CSCTL_REG(cs));
115 spin_unlock_irqrestore(&bcm63xx_cs_lock, flags);
117 return 0;
120 EXPORT_SYMBOL(bcm63xx_set_cs_param);
123 * set cs status (enable/disable)
125 int bcm63xx_set_cs_status(unsigned int cs, int enable)
127 unsigned long flags;
128 u32 val;
130 if (!is_valid_cs(cs))
131 return -EINVAL;
133 spin_lock_irqsave(&bcm63xx_cs_lock, flags);
134 val = bcm_mpi_readl(MPI_CSCTL_REG(cs));
135 if (enable)
136 val |= MPI_CSCTL_ENABLE_MASK;
137 else
138 val &= ~MPI_CSCTL_ENABLE_MASK;
139 bcm_mpi_writel(val, MPI_CSCTL_REG(cs));
140 spin_unlock_irqrestore(&bcm63xx_cs_lock, flags);
141 return 0;
144 EXPORT_SYMBOL(bcm63xx_set_cs_status);