Fedora Core 2.6.11-1.35_FC3 kernel
[linux-2.6.git] / drivers / serial / mpsc / mpsc.h
1 /*
2  * drivers/serial/mpsc/mpsc.h
3  *
4  * Author: Mark A. Greer <mgreer@mvista.com>
5  *
6  * 2004 (c) MontaVista, Software, Inc.  This file is licensed under
7  * the terms of the GNU General Public License version 2.  This program
8  * is licensed "as is" without any warranty of any kind, whether express
9  * or implied.
10  */
11
12 #ifndef __MPSC_H__
13 #define __MPSC_H__
14
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/tty.h>
19 #include <linux/ioport.h>
20 #include <linux/init.h>
21 #include <linux/console.h>
22 #include <linux/sysrq.h>
23 #include <linux/serial.h>
24 #include <linux/delay.h>
25 #include <linux/device.h>
26 #include <linux/dma-mapping.h>
27
28 #include <asm/io.h>
29 #include <asm/irq.h>
30
31 #if defined(CONFIG_SERIAL_MPSC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
32 #define SUPPORT_SYSRQ
33 #endif
34
35 #include <linux/serial_core.h>
36 #include "mpsc_defs.h"
37
38
39 /*
40  * Descriptors and buffers must be cache line aligned.
41  * Buffers lengths must be multiple of cache line size.
42  * Number of Tx & Rx descriptors must be power of 2.
43  */
44 #define MPSC_DESC_ALIGN         dma_get_cache_alignment()
45 #define MPSC_BUF_ALIGN          dma_get_cache_alignment()
46
47 #define MPSC_RXR_ENTRIES        32
48 #define MPSC_RXRE_SIZE          sizeof(mpsc_rx_desc_t)
49 #define MPSC_RXR_SIZE           (MPSC_RXR_ENTRIES * MPSC_RXRE_SIZE)
50 #define MPSC_RXBE_SIZE          dma_get_cache_alignment()
51 #define MPSC_RXB_SIZE           (MPSC_RXR_ENTRIES * MPSC_RXBE_SIZE)
52
53 #define MPSC_TXR_ENTRIES        32
54 #define MPSC_TXRE_SIZE          sizeof(mpsc_tx_desc_t)
55 #define MPSC_TXR_SIZE           (MPSC_TXR_ENTRIES * MPSC_TXRE_SIZE)
56 #define MPSC_TXBE_SIZE          dma_get_cache_alignment()
57 #define MPSC_TXB_SIZE           (MPSC_TXR_ENTRIES * MPSC_TXBE_SIZE)
58
59 typedef struct {
60         u16   bufsize;
61         u16   bytecnt;
62         u32   cmdstat;
63         u32   link;
64         u32   buf_ptr;
65 } mpsc_rx_desc_t __attribute((packed));
66
67 /* Tx and Rx Ring entry descriptors */
68 typedef struct {
69         u16   bytecnt;
70         u16   shadow;
71         u32   cmdstat;
72         u32   link;
73         u32   buf_ptr;
74 } mpsc_tx_desc_t __attribute((packed));
75
76 /* The main driver data structure */
77 typedef struct {
78         struct uart_port        port;   /* Overlay uart_port structure */
79
80         /* Internal driver state for this ctlr */
81         u8                      ready;
82         u8                      rcv_data;
83         tcflag_t                c_iflag;        /* save termios->c_iflag */
84         tcflag_t                c_cflag;        /* save termios->c_cflag */
85
86         /* Info passed in from platform */
87         u8                      mirror_regs;    /* Need to mirror regs? */
88         u8                      cache_mgmt;     /* Need manual cache mgmt? */
89         u8                      brg_can_tune;   /* BRG has baud tuning? */
90         u32                     brg_clk_src;
91         u16                     mpsc_max_idle;
92         int                     default_baud;
93         int                     default_bits;
94         int                     default_parity;
95         int                     default_flow;
96
97         /* Physical addresses of various blocks of registers (from platform) */
98         u32                     mpsc_base_p;
99         u32                     mpsc_routing_base_p;
100         u32                     sdma_base_p;
101         u32                     sdma_intr_base_p;
102         u32                     brg_base_p;
103
104         /* Virtual addresses of various blocks of registers (from platform) */
105         u32                     mpsc_base;
106         u32                     mpsc_routing_base;
107         u32                     sdma_base;
108         u32                     sdma_intr_base;
109         u32                     brg_base;
110
111         /* Descriptor ring and buffer allocations */
112         void                    *desc_region;   /* Region for desc rings */
113         dma_addr_t              desc_region_p;
114         u32                     desc_region_size;
115
116         void                    *buf_region;    /* kmalloc region for bufs */
117         u32                     buf_region_size;
118
119         mpsc_rx_desc_t          *rxr;           /* Rx descriptor ring */
120         mpsc_rx_desc_t          *rxr_p;         /* Phys addr of rxr */
121         u32                     rxr_posn;       /* First desc w/ Rx data */
122         u8                      *rxb;           /* Rx Ring I/O buf */
123         dma_addr_t              rxb_p;          /* Phys addr of rxb */
124
125         mpsc_tx_desc_t          *txr;           /* Tx descriptor ring */
126         mpsc_tx_desc_t          *txr_p;         /* Phys addr of txr */
127         u32                     txr_posn;       /* First unused desc */
128         u8                      *txb;           /* Tx Ring I/O buf */
129         dma_addr_t              txb_p;          /* Phys addr of txb */
130
131         /* Mirrored values of regs we can't read (if 'mirror_regs' set) */
132         u32                     MPSC_CHR_1_m;
133         u32                     MPSC_CHR_2_m;
134         u32                     MPSC_CHR_10_m;
135         u32                     MPSC_MPCR_m;
136         u32                     MPSC_MRR_m;
137         u32                     MPSC_RCRR_m;
138         u32                     MPSC_TCRR_m;
139         u32                     SDMA_INTR_MASK_m;
140         u32                     BRG_BCR_m;
141 } mpsc_port_info_t;
142
143 /*
144  * Some MPSC ctlrs have an erratum where they aren't supposed to access
145  * cache coherent memory regions.  From practical experience, the erratum
146  * is not triggered as long as there isn't a snoop hit.  Therefore, if
147  * the MPSC in used has this erratum and coherency is enabled on the platform,
148  * we must manually manage the cache for ring descriptors and the I/O buffers.
149  */
150 #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
151 #define MPSC_CACHE_FLUSH(pi, s, e) {                    \
152         if (pi->cache_mgmt) {                           \
153                 /* 64x60 erratum: can't use dcbst/clean_dcache_range() */ \
154                 flush_dcache_range((ulong)s, (ulong)e); \
155                 mb();                                   \
156         }                                               \
157 }
158
159 #define MPSC_CACHE_INVALIDATE(pi, s, e) {                       \
160         if (pi->cache_mgmt) {                                   \
161                 invalidate_dcache_range((ulong)s, (ulong)e);    \
162                 mb();                                           \
163         }                                                       \
164 }
165
166 #define MPSC_CACHE_FLUSH_INVALIDATE(pi, s, e) {         \
167         if (pi->cache_mgmt) {                           \
168                 flush_dcache_range((ulong)s, (ulong)e); \
169                 mb();                                   \
170         }                                               \
171 }
172 #else
173 #define MPSC_CACHE_FLUSH(pi, s, e)
174 #define MPSC_CACHE_INVALIDATE(pi, s, e)
175 #define MPSC_CACHE_FLUSH_INVALIDATE(pi, s, e)
176 #endif
177
178 /*
179  * 'MASK_INSERT' takes the low-order 'n' bits of 'i', shifts it 'b' bits to
180  * the left, and inserts it into the target 't'.  The corresponding bits in
181  * 't' will have been cleared before the bits in 'i' are inserted.
182  */
183 #ifdef CONFIG_PPC32
184 #define MASK_INSERT(t, i, n, b) ({                              \
185         u32     rval = (t);                                     \
186         __asm__ __volatile__(                                   \
187                 "rlwimi %0,%2,%4,32-(%3+%4),31-%4\n"            \
188                 : "=r" (rval)                                   \
189                 : "0" (rval), "r" (i), "i" (n), "i" (b));       \
190         rval;                                                   \
191 })
192 #else
193 /* These macros are really just examples.  Feel free to change them --MAG */
194 #define GEN_MASK(n, b)                  \
195 ({                                      \
196         u32     m, sl, sr;              \
197         sl = 32 - (n);                  \
198         sr = sl - (b);                  \
199         m = (0xffffffff << sl) >> sr;   \
200 })
201
202 #define MASK_INSERT(t, i, n, b)         \
203 ({                                      \
204         u32     m, rval = (t);          \
205         m = GEN_MASK((n), (b));         \
206         rval &= ~m;                     \
207         rval |= (((i) << (b)) & m);     \
208 })
209 #endif
210
211 /* I/O macros for regs that you can read */
212 #define MPSC_READ(pi, unit, offset)     readl((pi)->unit##_base + (offset))
213 #define MPSC_WRITE(pi, unit, offset, v) writel(v, (pi)->unit##_base + (offset))
214 #define MPSC_MOD_FIELD(pi, unit, offset, num_bits, shift, val)  \
215 {                                                               \
216         u32     v;                                              \
217         v = readl((pi)->unit##_base + (offset));                \
218         writel(MASK_INSERT(v,val,num_bits,shift), (pi)->unit##_base+(offset));\
219 }
220
221 #define MPSC_READ_M(pi, unit, offset)                   \
222 ({                                                      \
223         u32     v;                                      \
224         if ((pi)->mirror_regs) v = (pi)->offset##_m;    \
225         else v = readl((pi)->unit##_base + (offset));   \
226         v;                                              \
227 })
228
229 #define MPSC_WRITE_M(pi, unit, offset, v)               \
230 ({                                                      \
231         if ((pi)->mirror_regs) (pi)->offset##_m = v;    \
232         writel(v, (pi)->unit##_base + (offset));        \
233 })
234
235 #define MPSC_MOD_FIELD_M(pi, unit, offset, num_bits, shift, val)        \
236 ({                                                                      \
237         u32     v;                                                      \
238         if ((pi)->mirror_regs) v = (pi)->offset##_m;                    \
239         else v = readl((pi)->unit##_base + (offset));                   \
240         v = MASK_INSERT(v, val, num_bits, shift);                       \
241         if ((pi)->mirror_regs) (pi)->offset##_m = v;                    \
242         writel(v, (pi)->unit##_base + (offset));                        \
243 })
244
245 #if !defined(MIN)
246 #define MIN(a, b)       (((a) < (b)) ? (a) : (b))
247 #endif
248
249 /* Hooks to platform-specific code */
250 int mpsc_platform_register_driver(void);
251 void mpsc_platform_unregister_driver(void);
252
253 /* Hooks back in to mpsc common to be called by platform-specific code */
254 mpsc_port_info_t *mpsc_device_probe(int index);
255 mpsc_port_info_t *mpsc_device_remove(int index);
256
257 #endif  /* __MPSC_H__ */