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