blob: e984abf3e4d987c43d8f824da5a63f6829c8e2bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Generic driver for the MPSC (UART mode) on Marvell parts (e.g., GT64240,
3 * GT64260, MV64340, MV64360, GT96100, ... ).
4 *
5 * Author: Mark A. Greer <mgreer@mvista.com>
6 *
7 * Based on an old MPSC driver that was in the linuxppc tree. It appears to
8 * have been created by Chris Zankel (formerly of MontaVista) but there
9 * is no proper Copyright so I'm not sure. Apparently, parts were also
10 * taken from PPCBoot (now U-Boot). Also based on drivers/serial/8250.c
11 * by Russell King.
12 *
13 * 2004 (c) MontaVista, Software, Inc. This file is licensed under
14 * the terms of the GNU General Public License version 2. This program
15 * is licensed "as is" without any warranty of any kind, whether express
16 * or implied.
17 */
18/*
19 * The MPSC interface is much like a typical network controller's interface.
20 * That is, you set up separate rings of descriptors for transmitting and
21 * receiving data. There is also a pool of buffers with (one buffer per
22 * descriptor) that incoming data are dma'd into or outgoing data are dma'd
23 * out of.
24 *
25 * The MPSC requires two other controllers to be able to work. The Baud Rate
26 * Generator (BRG) provides a clock at programmable frequencies which determines
27 * the baud rate. The Serial DMA Controller (SDMA) takes incoming data from the
28 * MPSC and DMA's it into memory or DMA's outgoing data and passes it to the
29 * MPSC. It is actually the SDMA interrupt that the driver uses to keep the
30 * transmit and receive "engines" going (i.e., indicate data has been
31 * transmitted or received).
32 *
33 * NOTES:
34 *
35 * 1) Some chips have an erratum where several regs cannot be
36 * read. To work around that, we keep a local copy of those regs in
37 * 'mpsc_port_info'.
38 *
39 * 2) Some chips have an erratum where the ctlr will hang when the SDMA ctlr
40 * accesses system mem with coherency enabled. For that reason, the driver
41 * assumes that coherency for that ctlr has been disabled. This means
42 * that when in a cache coherent system, the driver has to manually manage
43 * the data cache on the areas that it touches because the dma_* macro are
44 * basically no-ops.
45 *
46 * 3) There is an erratum (on PPC) where you can't use the instruction to do
47 * a DMA_TO_DEVICE/cache clean so DMA_BIDIRECTIONAL/flushes are used in places
48 * where a DMA_TO_DEVICE/clean would have [otherwise] sufficed.
49 *
50 * 4) AFAICT, hardware flow control isn't supported by the controller --MAG.
51 */
52
Mark A. Greere4294b32006-03-25 03:08:28 -080053
54#if defined(CONFIG_SERIAL_MPSC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
55#define SUPPORT_SYSRQ
56#endif
57
58#include <linux/module.h>
59#include <linux/moduleparam.h>
60#include <linux/tty.h>
61#include <linux/tty_flip.h>
62#include <linux/ioport.h>
63#include <linux/init.h>
64#include <linux/console.h>
65#include <linux/sysrq.h>
66#include <linux/serial.h>
67#include <linux/serial_core.h>
68#include <linux/delay.h>
69#include <linux/device.h>
70#include <linux/dma-mapping.h>
71#include <linux/mv643xx.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010072#include <linux/platform_device.h>
73
Mark A. Greere4294b32006-03-25 03:08:28 -080074#include <asm/io.h>
75#include <asm/irq.h>
76
77#if defined(CONFIG_SERIAL_MPSC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
78#define SUPPORT_SYSRQ
79#endif
80
81#define MPSC_NUM_CTLRS 2
82
83/*
84 * Descriptors and buffers must be cache line aligned.
85 * Buffers lengths must be multiple of cache line size.
86 * Number of Tx & Rx descriptors must be powers of 2.
87 */
88#define MPSC_RXR_ENTRIES 32
89#define MPSC_RXRE_SIZE dma_get_cache_alignment()
90#define MPSC_RXR_SIZE (MPSC_RXR_ENTRIES * MPSC_RXRE_SIZE)
91#define MPSC_RXBE_SIZE dma_get_cache_alignment()
92#define MPSC_RXB_SIZE (MPSC_RXR_ENTRIES * MPSC_RXBE_SIZE)
93
94#define MPSC_TXR_ENTRIES 32
95#define MPSC_TXRE_SIZE dma_get_cache_alignment()
96#define MPSC_TXR_SIZE (MPSC_TXR_ENTRIES * MPSC_TXRE_SIZE)
97#define MPSC_TXBE_SIZE dma_get_cache_alignment()
98#define MPSC_TXB_SIZE (MPSC_TXR_ENTRIES * MPSC_TXBE_SIZE)
99
100#define MPSC_DMA_ALLOC_SIZE (MPSC_RXR_SIZE + MPSC_RXB_SIZE + \
101 MPSC_TXR_SIZE + MPSC_TXB_SIZE + \
102 dma_get_cache_alignment() /* for alignment */)
103
104/* Rx and Tx Ring entry descriptors -- assume entry size is <= cacheline size */
105struct mpsc_rx_desc {
106 u16 bufsize;
107 u16 bytecnt;
108 u32 cmdstat;
109 u32 link;
110 u32 buf_ptr;
111} __attribute((packed));
112
113struct mpsc_tx_desc {
114 u16 bytecnt;
115 u16 shadow;
116 u32 cmdstat;
117 u32 link;
118 u32 buf_ptr;
119} __attribute((packed));
120
121/*
122 * Some regs that have the erratum that you can't read them are are shared
123 * between the two MPSC controllers. This struct contains those shared regs.
124 */
125struct mpsc_shared_regs {
126 phys_addr_t mpsc_routing_base_p;
127 phys_addr_t sdma_intr_base_p;
128
129 void __iomem *mpsc_routing_base;
130 void __iomem *sdma_intr_base;
131
132 u32 MPSC_MRR_m;
133 u32 MPSC_RCRR_m;
134 u32 MPSC_TCRR_m;
135 u32 SDMA_INTR_CAUSE_m;
136 u32 SDMA_INTR_MASK_m;
137};
138
139/* The main driver data structure */
140struct mpsc_port_info {
141 struct uart_port port; /* Overlay uart_port structure */
142
143 /* Internal driver state for this ctlr */
144 u8 ready;
145 u8 rcv_data;
146 tcflag_t c_iflag; /* save termios->c_iflag */
147 tcflag_t c_cflag; /* save termios->c_cflag */
148
149 /* Info passed in from platform */
150 u8 mirror_regs; /* Need to mirror regs? */
151 u8 cache_mgmt; /* Need manual cache mgmt? */
152 u8 brg_can_tune; /* BRG has baud tuning? */
153 u32 brg_clk_src;
154 u16 mpsc_max_idle;
155 int default_baud;
156 int default_bits;
157 int default_parity;
158 int default_flow;
159
160 /* Physical addresses of various blocks of registers (from platform) */
161 phys_addr_t mpsc_base_p;
162 phys_addr_t sdma_base_p;
163 phys_addr_t brg_base_p;
164
165 /* Virtual addresses of various blocks of registers (from platform) */
166 void __iomem *mpsc_base;
167 void __iomem *sdma_base;
168 void __iomem *brg_base;
169
170 /* Descriptor ring and buffer allocations */
171 void *dma_region;
172 dma_addr_t dma_region_p;
173
174 dma_addr_t rxr; /* Rx descriptor ring */
175 dma_addr_t rxr_p; /* Phys addr of rxr */
176 u8 *rxb; /* Rx Ring I/O buf */
177 u8 *rxb_p; /* Phys addr of rxb */
178 u32 rxr_posn; /* First desc w/ Rx data */
179
180 dma_addr_t txr; /* Tx descriptor ring */
181 dma_addr_t txr_p; /* Phys addr of txr */
182 u8 *txb; /* Tx Ring I/O buf */
183 u8 *txb_p; /* Phys addr of txb */
184 int txr_head; /* Where new data goes */
185 int txr_tail; /* Where sent data comes off */
Dave Jiang17333102007-05-06 14:48:50 -0700186 spinlock_t tx_lock; /* transmit lock */
Mark A. Greere4294b32006-03-25 03:08:28 -0800187
188 /* Mirrored values of regs we can't read (if 'mirror_regs' set) */
189 u32 MPSC_MPCR_m;
190 u32 MPSC_CHR_1_m;
191 u32 MPSC_CHR_2_m;
192 u32 MPSC_CHR_10_m;
193 u32 BRG_BCR_m;
194 struct mpsc_shared_regs *shared_regs;
195};
196
197/* Hooks to platform-specific code */
198int mpsc_platform_register_driver(void);
199void mpsc_platform_unregister_driver(void);
200
201/* Hooks back in to mpsc common to be called by platform-specific code */
202struct mpsc_port_info *mpsc_device_probe(int index);
203struct mpsc_port_info *mpsc_device_remove(int index);
204
205/* Main MPSC Configuration Register Offsets */
206#define MPSC_MMCRL 0x0000
207#define MPSC_MMCRH 0x0004
208#define MPSC_MPCR 0x0008
209#define MPSC_CHR_1 0x000c
210#define MPSC_CHR_2 0x0010
211#define MPSC_CHR_3 0x0014
212#define MPSC_CHR_4 0x0018
213#define MPSC_CHR_5 0x001c
214#define MPSC_CHR_6 0x0020
215#define MPSC_CHR_7 0x0024
216#define MPSC_CHR_8 0x0028
217#define MPSC_CHR_9 0x002c
218#define MPSC_CHR_10 0x0030
219#define MPSC_CHR_11 0x0034
220
221#define MPSC_MPCR_FRZ (1 << 9)
222#define MPSC_MPCR_CL_5 0
223#define MPSC_MPCR_CL_6 1
224#define MPSC_MPCR_CL_7 2
225#define MPSC_MPCR_CL_8 3
226#define MPSC_MPCR_SBL_1 0
227#define MPSC_MPCR_SBL_2 1
228
229#define MPSC_CHR_2_TEV (1<<1)
230#define MPSC_CHR_2_TA (1<<7)
231#define MPSC_CHR_2_TTCS (1<<9)
232#define MPSC_CHR_2_REV (1<<17)
233#define MPSC_CHR_2_RA (1<<23)
234#define MPSC_CHR_2_CRD (1<<25)
235#define MPSC_CHR_2_EH (1<<31)
236#define MPSC_CHR_2_PAR_ODD 0
237#define MPSC_CHR_2_PAR_SPACE 1
238#define MPSC_CHR_2_PAR_EVEN 2
239#define MPSC_CHR_2_PAR_MARK 3
240
241/* MPSC Signal Routing */
242#define MPSC_MRR 0x0000
243#define MPSC_RCRR 0x0004
244#define MPSC_TCRR 0x0008
245
246/* Serial DMA Controller Interface Registers */
247#define SDMA_SDC 0x0000
248#define SDMA_SDCM 0x0008
249#define SDMA_RX_DESC 0x0800
250#define SDMA_RX_BUF_PTR 0x0808
251#define SDMA_SCRDP 0x0810
252#define SDMA_TX_DESC 0x0c00
253#define SDMA_SCTDP 0x0c10
254#define SDMA_SFTDP 0x0c14
255
256#define SDMA_DESC_CMDSTAT_PE (1<<0)
257#define SDMA_DESC_CMDSTAT_CDL (1<<1)
258#define SDMA_DESC_CMDSTAT_FR (1<<3)
259#define SDMA_DESC_CMDSTAT_OR (1<<6)
260#define SDMA_DESC_CMDSTAT_BR (1<<9)
261#define SDMA_DESC_CMDSTAT_MI (1<<10)
262#define SDMA_DESC_CMDSTAT_A (1<<11)
263#define SDMA_DESC_CMDSTAT_AM (1<<12)
264#define SDMA_DESC_CMDSTAT_CT (1<<13)
265#define SDMA_DESC_CMDSTAT_C (1<<14)
266#define SDMA_DESC_CMDSTAT_ES (1<<15)
267#define SDMA_DESC_CMDSTAT_L (1<<16)
268#define SDMA_DESC_CMDSTAT_F (1<<17)
269#define SDMA_DESC_CMDSTAT_P (1<<18)
270#define SDMA_DESC_CMDSTAT_EI (1<<23)
271#define SDMA_DESC_CMDSTAT_O (1<<31)
272
273#define SDMA_DESC_DFLT (SDMA_DESC_CMDSTAT_O | \
274 SDMA_DESC_CMDSTAT_EI)
275
276#define SDMA_SDC_RFT (1<<0)
277#define SDMA_SDC_SFM (1<<1)
278#define SDMA_SDC_BLMR (1<<6)
279#define SDMA_SDC_BLMT (1<<7)
280#define SDMA_SDC_POVR (1<<8)
281#define SDMA_SDC_RIFB (1<<9)
282
283#define SDMA_SDCM_ERD (1<<7)
284#define SDMA_SDCM_AR (1<<15)
285#define SDMA_SDCM_STD (1<<16)
286#define SDMA_SDCM_TXD (1<<23)
287#define SDMA_SDCM_AT (1<<31)
288
289#define SDMA_0_CAUSE_RXBUF (1<<0)
290#define SDMA_0_CAUSE_RXERR (1<<1)
291#define SDMA_0_CAUSE_TXBUF (1<<2)
292#define SDMA_0_CAUSE_TXEND (1<<3)
293#define SDMA_1_CAUSE_RXBUF (1<<8)
294#define SDMA_1_CAUSE_RXERR (1<<9)
295#define SDMA_1_CAUSE_TXBUF (1<<10)
296#define SDMA_1_CAUSE_TXEND (1<<11)
297
298#define SDMA_CAUSE_RX_MASK (SDMA_0_CAUSE_RXBUF | SDMA_0_CAUSE_RXERR | \
299 SDMA_1_CAUSE_RXBUF | SDMA_1_CAUSE_RXERR)
300#define SDMA_CAUSE_TX_MASK (SDMA_0_CAUSE_TXBUF | SDMA_0_CAUSE_TXEND | \
301 SDMA_1_CAUSE_TXBUF | SDMA_1_CAUSE_TXEND)
302
303/* SDMA Interrupt registers */
304#define SDMA_INTR_CAUSE 0x0000
305#define SDMA_INTR_MASK 0x0080
306
307/* Baud Rate Generator Interface Registers */
308#define BRG_BCR 0x0000
309#define BRG_BTR 0x0004
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311/*
312 * Define how this driver is known to the outside (we've been assigned a
313 * range on the "Low-density serial ports" major).
314 */
315#define MPSC_MAJOR 204
316#define MPSC_MINOR_START 44
317#define MPSC_DRIVER_NAME "MPSC"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318#define MPSC_DEV_NAME "ttyMM"
319#define MPSC_VERSION "1.00"
320
321static struct mpsc_port_info mpsc_ports[MPSC_NUM_CTLRS];
322static struct mpsc_shared_regs mpsc_shared_regs;
Lee Nicks4d0145a2005-06-25 14:55:36 -0700323static struct uart_driver mpsc_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Lee Nicks4d0145a2005-06-25 14:55:36 -0700325static void mpsc_start_rx(struct mpsc_port_info *pi);
326static void mpsc_free_ring_mem(struct mpsc_port_info *pi);
327static void mpsc_release_port(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328/*
329 ******************************************************************************
330 *
331 * Baud Rate Generator Routines (BRG)
332 *
333 ******************************************************************************
334 */
335static void
336mpsc_brg_init(struct mpsc_port_info *pi, u32 clk_src)
337{
338 u32 v;
339
340 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
341 v = (v & ~(0xf << 18)) | ((clk_src & 0xf) << 18);
342
343 if (pi->brg_can_tune)
344 v &= ~(1 << 25);
345
346 if (pi->mirror_regs)
347 pi->BRG_BCR_m = v;
348 writel(v, pi->brg_base + BRG_BCR);
349
350 writel(readl(pi->brg_base + BRG_BTR) & 0xffff0000,
351 pi->brg_base + BRG_BTR);
352 return;
353}
354
355static void
356mpsc_brg_enable(struct mpsc_port_info *pi)
357{
358 u32 v;
359
360 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
361 v |= (1 << 16);
362
363 if (pi->mirror_regs)
364 pi->BRG_BCR_m = v;
365 writel(v, pi->brg_base + BRG_BCR);
366 return;
367}
368
369static void
370mpsc_brg_disable(struct mpsc_port_info *pi)
371{
372 u32 v;
373
374 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
375 v &= ~(1 << 16);
376
377 if (pi->mirror_regs)
378 pi->BRG_BCR_m = v;
379 writel(v, pi->brg_base + BRG_BCR);
380 return;
381}
382
383static inline void
384mpsc_set_baudrate(struct mpsc_port_info *pi, u32 baud)
385{
386 /*
387 * To set the baud, we adjust the CDV field in the BRG_BCR reg.
388 * From manual: Baud = clk / ((CDV+1)*2) ==> CDV = (clk / (baud*2)) - 1.
389 * However, the input clock is divided by 16 in the MPSC b/c of how
390 * 'MPSC_MMCRH' was set up so we have to divide the 'clk' used in our
391 * calculation by 16 to account for that. So the real calculation
392 * that accounts for the way the mpsc is set up is:
393 * CDV = (clk / (baud*2*16)) - 1 ==> CDV = (clk / (baud << 5)) - 1.
394 */
395 u32 cdv = (pi->port.uartclk / (baud << 5)) - 1;
396 u32 v;
397
398 mpsc_brg_disable(pi);
399 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
400 v = (v & 0xffff0000) | (cdv & 0xffff);
401
402 if (pi->mirror_regs)
403 pi->BRG_BCR_m = v;
404 writel(v, pi->brg_base + BRG_BCR);
405 mpsc_brg_enable(pi);
406
407 return;
408}
409
410/*
411 ******************************************************************************
412 *
413 * Serial DMA Routines (SDMA)
414 *
415 ******************************************************************************
416 */
417
418static void
419mpsc_sdma_burstsize(struct mpsc_port_info *pi, u32 burst_size)
420{
421 u32 v;
422
423 pr_debug("mpsc_sdma_burstsize[%d]: burst_size: %d\n",
424 pi->port.line, burst_size);
425
426 burst_size >>= 3; /* Divide by 8 b/c reg values are 8-byte chunks */
427
428 if (burst_size < 2)
429 v = 0x0; /* 1 64-bit word */
430 else if (burst_size < 4)
431 v = 0x1; /* 2 64-bit words */
432 else if (burst_size < 8)
433 v = 0x2; /* 4 64-bit words */
434 else
435 v = 0x3; /* 8 64-bit words */
436
437 writel((readl(pi->sdma_base + SDMA_SDC) & (0x3 << 12)) | (v << 12),
438 pi->sdma_base + SDMA_SDC);
439 return;
440}
441
442static void
443mpsc_sdma_init(struct mpsc_port_info *pi, u32 burst_size)
444{
445 pr_debug("mpsc_sdma_init[%d]: burst_size: %d\n", pi->port.line,
446 burst_size);
447
448 writel((readl(pi->sdma_base + SDMA_SDC) & 0x3ff) | 0x03f,
449 pi->sdma_base + SDMA_SDC);
450 mpsc_sdma_burstsize(pi, burst_size);
451 return;
452}
453
454static inline u32
455mpsc_sdma_intr_mask(struct mpsc_port_info *pi, u32 mask)
456{
457 u32 old, v;
458
459 pr_debug("mpsc_sdma_intr_mask[%d]: mask: 0x%x\n", pi->port.line, mask);
460
461 old = v = (pi->mirror_regs) ? pi->shared_regs->SDMA_INTR_MASK_m :
462 readl(pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
463
464 mask &= 0xf;
465 if (pi->port.line)
466 mask <<= 8;
467 v &= ~mask;
468
469 if (pi->mirror_regs)
470 pi->shared_regs->SDMA_INTR_MASK_m = v;
471 writel(v, pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
472
473 if (pi->port.line)
474 old >>= 8;
475 return old & 0xf;
476}
477
478static inline void
479mpsc_sdma_intr_unmask(struct mpsc_port_info *pi, u32 mask)
480{
481 u32 v;
482
483 pr_debug("mpsc_sdma_intr_unmask[%d]: mask: 0x%x\n", pi->port.line,mask);
484
485 v = (pi->mirror_regs) ? pi->shared_regs->SDMA_INTR_MASK_m :
486 readl(pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
487
488 mask &= 0xf;
489 if (pi->port.line)
490 mask <<= 8;
491 v |= mask;
492
493 if (pi->mirror_regs)
494 pi->shared_regs->SDMA_INTR_MASK_m = v;
495 writel(v, pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
496 return;
497}
498
499static inline void
500mpsc_sdma_intr_ack(struct mpsc_port_info *pi)
501{
502 pr_debug("mpsc_sdma_intr_ack[%d]: Acknowledging IRQ\n", pi->port.line);
503
504 if (pi->mirror_regs)
505 pi->shared_regs->SDMA_INTR_CAUSE_m = 0;
Jay Lubomirski2f4d4da2007-06-27 14:10:09 -0700506 writeb(0x00, pi->shared_regs->sdma_intr_base + SDMA_INTR_CAUSE +
507 pi->port.line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return;
509}
510
511static inline void
512mpsc_sdma_set_rx_ring(struct mpsc_port_info *pi, struct mpsc_rx_desc *rxre_p)
513{
514 pr_debug("mpsc_sdma_set_rx_ring[%d]: rxre_p: 0x%x\n",
515 pi->port.line, (u32) rxre_p);
516
517 writel((u32)rxre_p, pi->sdma_base + SDMA_SCRDP);
518 return;
519}
520
521static inline void
522mpsc_sdma_set_tx_ring(struct mpsc_port_info *pi, struct mpsc_tx_desc *txre_p)
523{
524 writel((u32)txre_p, pi->sdma_base + SDMA_SFTDP);
525 writel((u32)txre_p, pi->sdma_base + SDMA_SCTDP);
526 return;
527}
528
529static inline void
530mpsc_sdma_cmd(struct mpsc_port_info *pi, u32 val)
531{
532 u32 v;
533
534 v = readl(pi->sdma_base + SDMA_SDCM);
535 if (val)
536 v |= val;
537 else
538 v = 0;
539 wmb();
540 writel(v, pi->sdma_base + SDMA_SDCM);
541 wmb();
542 return;
543}
544
545static inline uint
546mpsc_sdma_tx_active(struct mpsc_port_info *pi)
547{
548 return readl(pi->sdma_base + SDMA_SDCM) & SDMA_SDCM_TXD;
549}
550
551static inline void
552mpsc_sdma_start_tx(struct mpsc_port_info *pi)
553{
554 struct mpsc_tx_desc *txre, *txre_p;
555
556 /* If tx isn't running & there's a desc ready to go, start it */
557 if (!mpsc_sdma_tx_active(pi)) {
558 txre = (struct mpsc_tx_desc *)(pi->txr +
559 (pi->txr_tail * MPSC_TXRE_SIZE));
Ralf Baechled3fa72e2006-12-06 20:38:56 -0800560 dma_cache_sync(pi->port.dev, (void *) txre, MPSC_TXRE_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
562 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
563 invalidate_dcache_range((ulong)txre,
564 (ulong)txre + MPSC_TXRE_SIZE);
565#endif
566
567 if (be32_to_cpu(txre->cmdstat) & SDMA_DESC_CMDSTAT_O) {
568 txre_p = (struct mpsc_tx_desc *)(pi->txr_p +
569 (pi->txr_tail *
570 MPSC_TXRE_SIZE));
571
572 mpsc_sdma_set_tx_ring(pi, txre_p);
573 mpsc_sdma_cmd(pi, SDMA_SDCM_STD | SDMA_SDCM_TXD);
574 }
575 }
576
577 return;
578}
579
580static inline void
581mpsc_sdma_stop(struct mpsc_port_info *pi)
582{
583 pr_debug("mpsc_sdma_stop[%d]: Stopping SDMA\n", pi->port.line);
584
585 /* Abort any SDMA transfers */
586 mpsc_sdma_cmd(pi, 0);
587 mpsc_sdma_cmd(pi, SDMA_SDCM_AR | SDMA_SDCM_AT);
588
589 /* Clear the SDMA current and first TX and RX pointers */
Al Viro2c6e7592005-04-25 18:32:12 -0700590 mpsc_sdma_set_tx_ring(pi, NULL);
591 mpsc_sdma_set_rx_ring(pi, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 /* Disable interrupts */
594 mpsc_sdma_intr_mask(pi, 0xf);
595 mpsc_sdma_intr_ack(pi);
596
597 return;
598}
599
600/*
601 ******************************************************************************
602 *
603 * Multi-Protocol Serial Controller Routines (MPSC)
604 *
605 ******************************************************************************
606 */
607
608static void
609mpsc_hw_init(struct mpsc_port_info *pi)
610{
611 u32 v;
612
613 pr_debug("mpsc_hw_init[%d]: Initializing hardware\n", pi->port.line);
614
615 /* Set up clock routing */
616 if (pi->mirror_regs) {
617 v = pi->shared_regs->MPSC_MRR_m;
618 v &= ~0x1c7;
619 pi->shared_regs->MPSC_MRR_m = v;
620 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_MRR);
621
622 v = pi->shared_regs->MPSC_RCRR_m;
623 v = (v & ~0xf0f) | 0x100;
624 pi->shared_regs->MPSC_RCRR_m = v;
625 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
626
627 v = pi->shared_regs->MPSC_TCRR_m;
628 v = (v & ~0xf0f) | 0x100;
629 pi->shared_regs->MPSC_TCRR_m = v;
630 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
631 }
632 else {
633 v = readl(pi->shared_regs->mpsc_routing_base + MPSC_MRR);
634 v &= ~0x1c7;
635 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_MRR);
636
637 v = readl(pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
638 v = (v & ~0xf0f) | 0x100;
639 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
640
641 v = readl(pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
642 v = (v & ~0xf0f) | 0x100;
643 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
644 }
645
646 /* Put MPSC in UART mode & enabel Tx/Rx egines */
647 writel(0x000004c4, pi->mpsc_base + MPSC_MMCRL);
648
649 /* No preamble, 16x divider, low-latency, */
650 writel(0x04400400, pi->mpsc_base + MPSC_MMCRH);
651
652 if (pi->mirror_regs) {
653 pi->MPSC_CHR_1_m = 0;
654 pi->MPSC_CHR_2_m = 0;
655 }
656 writel(0, pi->mpsc_base + MPSC_CHR_1);
657 writel(0, pi->mpsc_base + MPSC_CHR_2);
658 writel(pi->mpsc_max_idle, pi->mpsc_base + MPSC_CHR_3);
659 writel(0, pi->mpsc_base + MPSC_CHR_4);
660 writel(0, pi->mpsc_base + MPSC_CHR_5);
661 writel(0, pi->mpsc_base + MPSC_CHR_6);
662 writel(0, pi->mpsc_base + MPSC_CHR_7);
663 writel(0, pi->mpsc_base + MPSC_CHR_8);
664 writel(0, pi->mpsc_base + MPSC_CHR_9);
665 writel(0, pi->mpsc_base + MPSC_CHR_10);
666
667 return;
668}
669
670static inline void
671mpsc_enter_hunt(struct mpsc_port_info *pi)
672{
673 pr_debug("mpsc_enter_hunt[%d]: Hunting...\n", pi->port.line);
674
675 if (pi->mirror_regs) {
676 writel(pi->MPSC_CHR_2_m | MPSC_CHR_2_EH,
677 pi->mpsc_base + MPSC_CHR_2);
678 /* Erratum prevents reading CHR_2 so just delay for a while */
679 udelay(100);
680 }
681 else {
682 writel(readl(pi->mpsc_base + MPSC_CHR_2) | MPSC_CHR_2_EH,
683 pi->mpsc_base + MPSC_CHR_2);
684
685 while (readl(pi->mpsc_base + MPSC_CHR_2) & MPSC_CHR_2_EH)
686 udelay(10);
687 }
688
689 return;
690}
691
692static inline void
693mpsc_freeze(struct mpsc_port_info *pi)
694{
695 u32 v;
696
697 pr_debug("mpsc_freeze[%d]: Freezing\n", pi->port.line);
698
699 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
700 readl(pi->mpsc_base + MPSC_MPCR);
701 v |= MPSC_MPCR_FRZ;
702
703 if (pi->mirror_regs)
704 pi->MPSC_MPCR_m = v;
705 writel(v, pi->mpsc_base + MPSC_MPCR);
706 return;
707}
708
709static inline void
710mpsc_unfreeze(struct mpsc_port_info *pi)
711{
712 u32 v;
713
714 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
715 readl(pi->mpsc_base + MPSC_MPCR);
716 v &= ~MPSC_MPCR_FRZ;
717
718 if (pi->mirror_regs)
719 pi->MPSC_MPCR_m = v;
720 writel(v, pi->mpsc_base + MPSC_MPCR);
721
722 pr_debug("mpsc_unfreeze[%d]: Unfrozen\n", pi->port.line);
723 return;
724}
725
726static inline void
727mpsc_set_char_length(struct mpsc_port_info *pi, u32 len)
728{
729 u32 v;
730
731 pr_debug("mpsc_set_char_length[%d]: char len: %d\n", pi->port.line,len);
732
733 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
734 readl(pi->mpsc_base + MPSC_MPCR);
735 v = (v & ~(0x3 << 12)) | ((len & 0x3) << 12);
736
737 if (pi->mirror_regs)
738 pi->MPSC_MPCR_m = v;
739 writel(v, pi->mpsc_base + MPSC_MPCR);
740 return;
741}
742
743static inline void
744mpsc_set_stop_bit_length(struct mpsc_port_info *pi, u32 len)
745{
746 u32 v;
747
748 pr_debug("mpsc_set_stop_bit_length[%d]: stop bits: %d\n",
749 pi->port.line, len);
750
751 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
752 readl(pi->mpsc_base + MPSC_MPCR);
753
754 v = (v & ~(1 << 14)) | ((len & 0x1) << 14);
755
756 if (pi->mirror_regs)
757 pi->MPSC_MPCR_m = v;
758 writel(v, pi->mpsc_base + MPSC_MPCR);
759 return;
760}
761
762static inline void
763mpsc_set_parity(struct mpsc_port_info *pi, u32 p)
764{
765 u32 v;
766
767 pr_debug("mpsc_set_parity[%d]: parity bits: 0x%x\n", pi->port.line, p);
768
769 v = (pi->mirror_regs) ? pi->MPSC_CHR_2_m :
770 readl(pi->mpsc_base + MPSC_CHR_2);
771
772 p &= 0x3;
773 v = (v & ~0xc000c) | (p << 18) | (p << 2);
774
775 if (pi->mirror_regs)
776 pi->MPSC_CHR_2_m = v;
777 writel(v, pi->mpsc_base + MPSC_CHR_2);
778 return;
779}
780
781/*
782 ******************************************************************************
783 *
784 * Driver Init Routines
785 *
786 ******************************************************************************
787 */
788
789static void
790mpsc_init_hw(struct mpsc_port_info *pi)
791{
792 pr_debug("mpsc_init_hw[%d]: Initializing\n", pi->port.line);
793
794 mpsc_brg_init(pi, pi->brg_clk_src);
795 mpsc_brg_enable(pi);
796 mpsc_sdma_init(pi, dma_get_cache_alignment()); /* burst a cacheline */
797 mpsc_sdma_stop(pi);
798 mpsc_hw_init(pi);
799
800 return;
801}
802
803static int
804mpsc_alloc_ring_mem(struct mpsc_port_info *pi)
805{
806 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 pr_debug("mpsc_alloc_ring_mem[%d]: Allocating ring mem\n",
809 pi->port.line);
810
811 if (!pi->dma_region) {
812 if (!dma_supported(pi->port.dev, 0xffffffff)) {
813 printk(KERN_ERR "MPSC: Inadequate DMA support\n");
814 rc = -ENXIO;
815 }
816 else if ((pi->dma_region = dma_alloc_noncoherent(pi->port.dev,
817 MPSC_DMA_ALLOC_SIZE, &pi->dma_region_p, GFP_KERNEL))
818 == NULL) {
819
820 printk(KERN_ERR "MPSC: Can't alloc Desc region\n");
821 rc = -ENOMEM;
822 }
823 }
824
825 return rc;
826}
827
828static void
829mpsc_free_ring_mem(struct mpsc_port_info *pi)
830{
831 pr_debug("mpsc_free_ring_mem[%d]: Freeing ring mem\n", pi->port.line);
832
833 if (pi->dma_region) {
834 dma_free_noncoherent(pi->port.dev, MPSC_DMA_ALLOC_SIZE,
835 pi->dma_region, pi->dma_region_p);
836 pi->dma_region = NULL;
837 pi->dma_region_p = (dma_addr_t) NULL;
838 }
839
840 return;
841}
842
843static void
844mpsc_init_rings(struct mpsc_port_info *pi)
845{
846 struct mpsc_rx_desc *rxre;
847 struct mpsc_tx_desc *txre;
848 dma_addr_t dp, dp_p;
849 u8 *bp, *bp_p;
850 int i;
851
852 pr_debug("mpsc_init_rings[%d]: Initializing rings\n", pi->port.line);
853
854 BUG_ON(pi->dma_region == NULL);
855
856 memset(pi->dma_region, 0, MPSC_DMA_ALLOC_SIZE);
857
858 /*
859 * Descriptors & buffers are multiples of cacheline size and must be
860 * cacheline aligned.
861 */
862 dp = ALIGN((u32) pi->dma_region, dma_get_cache_alignment());
863 dp_p = ALIGN((u32) pi->dma_region_p, dma_get_cache_alignment());
864
865 /*
866 * Partition dma region into rx ring descriptor, rx buffers,
867 * tx ring descriptors, and tx buffers.
868 */
869 pi->rxr = dp;
870 pi->rxr_p = dp_p;
871 dp += MPSC_RXR_SIZE;
872 dp_p += MPSC_RXR_SIZE;
873
874 pi->rxb = (u8 *) dp;
875 pi->rxb_p = (u8 *) dp_p;
876 dp += MPSC_RXB_SIZE;
877 dp_p += MPSC_RXB_SIZE;
878
879 pi->rxr_posn = 0;
880
881 pi->txr = dp;
882 pi->txr_p = dp_p;
883 dp += MPSC_TXR_SIZE;
884 dp_p += MPSC_TXR_SIZE;
885
886 pi->txb = (u8 *) dp;
887 pi->txb_p = (u8 *) dp_p;
888
889 pi->txr_head = 0;
890 pi->txr_tail = 0;
891
892 /* Init rx ring descriptors */
893 dp = pi->rxr;
894 dp_p = pi->rxr_p;
895 bp = pi->rxb;
896 bp_p = pi->rxb_p;
897
898 for (i = 0; i < MPSC_RXR_ENTRIES; i++) {
899 rxre = (struct mpsc_rx_desc *)dp;
900
901 rxre->bufsize = cpu_to_be16(MPSC_RXBE_SIZE);
902 rxre->bytecnt = cpu_to_be16(0);
903 rxre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O |
904 SDMA_DESC_CMDSTAT_EI |
905 SDMA_DESC_CMDSTAT_F |
906 SDMA_DESC_CMDSTAT_L);
907 rxre->link = cpu_to_be32(dp_p + MPSC_RXRE_SIZE);
908 rxre->buf_ptr = cpu_to_be32(bp_p);
909
910 dp += MPSC_RXRE_SIZE;
911 dp_p += MPSC_RXRE_SIZE;
912 bp += MPSC_RXBE_SIZE;
913 bp_p += MPSC_RXBE_SIZE;
914 }
915 rxre->link = cpu_to_be32(pi->rxr_p); /* Wrap last back to first */
916
917 /* Init tx ring descriptors */
918 dp = pi->txr;
919 dp_p = pi->txr_p;
920 bp = pi->txb;
921 bp_p = pi->txb_p;
922
923 for (i = 0; i < MPSC_TXR_ENTRIES; i++) {
924 txre = (struct mpsc_tx_desc *)dp;
925
926 txre->link = cpu_to_be32(dp_p + MPSC_TXRE_SIZE);
927 txre->buf_ptr = cpu_to_be32(bp_p);
928
929 dp += MPSC_TXRE_SIZE;
930 dp_p += MPSC_TXRE_SIZE;
931 bp += MPSC_TXBE_SIZE;
932 bp_p += MPSC_TXBE_SIZE;
933 }
934 txre->link = cpu_to_be32(pi->txr_p); /* Wrap last back to first */
935
Ralf Baechled3fa72e2006-12-06 20:38:56 -0800936 dma_cache_sync(pi->port.dev, (void *) pi->dma_region, MPSC_DMA_ALLOC_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 DMA_BIDIRECTIONAL);
938#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
939 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
940 flush_dcache_range((ulong)pi->dma_region,
941 (ulong)pi->dma_region + MPSC_DMA_ALLOC_SIZE);
942#endif
943
944 return;
945}
946
947static void
948mpsc_uninit_rings(struct mpsc_port_info *pi)
949{
950 pr_debug("mpsc_uninit_rings[%d]: Uninitializing rings\n",pi->port.line);
951
952 BUG_ON(pi->dma_region == NULL);
953
954 pi->rxr = 0;
955 pi->rxr_p = 0;
956 pi->rxb = NULL;
957 pi->rxb_p = NULL;
958 pi->rxr_posn = 0;
959
960 pi->txr = 0;
961 pi->txr_p = 0;
962 pi->txb = NULL;
963 pi->txb_p = NULL;
964 pi->txr_head = 0;
965 pi->txr_tail = 0;
966
967 return;
968}
969
970static int
971mpsc_make_ready(struct mpsc_port_info *pi)
972{
973 int rc;
974
975 pr_debug("mpsc_make_ready[%d]: Making cltr ready\n", pi->port.line);
976
977 if (!pi->ready) {
978 mpsc_init_hw(pi);
979 if ((rc = mpsc_alloc_ring_mem(pi)))
980 return rc;
981 mpsc_init_rings(pi);
982 pi->ready = 1;
983 }
984
985 return 0;
986}
987
988/*
989 ******************************************************************************
990 *
991 * Interrupt Handling Routines
992 *
993 ******************************************************************************
994 */
995
996static inline int
David Howells7d12e782006-10-05 14:55:46 +0100997mpsc_rx_intr(struct mpsc_port_info *pi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 struct mpsc_rx_desc *rxre;
1000 struct tty_struct *tty = pi->port.info->tty;
1001 u32 cmdstat, bytes_in, i;
1002 int rc = 0;
1003 u8 *bp;
1004 char flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 pr_debug("mpsc_rx_intr[%d]: Handling Rx intr\n", pi->port.line);
1007
1008 rxre = (struct mpsc_rx_desc *)(pi->rxr + (pi->rxr_posn*MPSC_RXRE_SIZE));
1009
Ralf Baechled3fa72e2006-12-06 20:38:56 -08001010 dma_cache_sync(pi->port.dev, (void *)rxre, MPSC_RXRE_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1012 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1013 invalidate_dcache_range((ulong)rxre,
1014 (ulong)rxre + MPSC_RXRE_SIZE);
1015#endif
1016
1017 /*
1018 * Loop through Rx descriptors handling ones that have been completed.
1019 */
1020 while (!((cmdstat = be32_to_cpu(rxre->cmdstat)) & SDMA_DESC_CMDSTAT_O)){
1021 bytes_in = be16_to_cpu(rxre->bytecnt);
1022
1023 /* Following use of tty struct directly is deprecated */
Alan Cox33f0f882006-01-09 20:54:13 -08001024 if (unlikely(tty_buffer_request_room(tty, bytes_in) < bytes_in)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (tty->low_latency)
1026 tty_flip_buffer_push(tty);
1027 /*
Alan Cox33f0f882006-01-09 20:54:13 -08001028 * If this failed then we will throw away the bytes
1029 * but must do so to clear interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 */
1031 }
1032
1033 bp = pi->rxb + (pi->rxr_posn * MPSC_RXBE_SIZE);
Ralf Baechled3fa72e2006-12-06 20:38:56 -08001034 dma_cache_sync(pi->port.dev, (void *) bp, MPSC_RXBE_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1036 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1037 invalidate_dcache_range((ulong)bp,
1038 (ulong)bp + MPSC_RXBE_SIZE);
1039#endif
1040
1041 /*
1042 * Other than for parity error, the manual provides little
1043 * info on what data will be in a frame flagged by any of
1044 * these errors. For parity error, it is the last byte in
1045 * the buffer that had the error. As for the rest, I guess
1046 * we'll assume there is no data in the buffer.
1047 * If there is...it gets lost.
1048 */
1049 if (unlikely(cmdstat & (SDMA_DESC_CMDSTAT_BR |
1050 SDMA_DESC_CMDSTAT_FR | SDMA_DESC_CMDSTAT_OR))) {
1051
1052 pi->port.icount.rx++;
1053
1054 if (cmdstat & SDMA_DESC_CMDSTAT_BR) { /* Break */
1055 pi->port.icount.brk++;
1056
1057 if (uart_handle_break(&pi->port))
1058 goto next_frame;
1059 }
1060 else if (cmdstat & SDMA_DESC_CMDSTAT_FR)/* Framing */
1061 pi->port.icount.frame++;
1062 else if (cmdstat & SDMA_DESC_CMDSTAT_OR) /* Overrun */
1063 pi->port.icount.overrun++;
1064
1065 cmdstat &= pi->port.read_status_mask;
1066
1067 if (cmdstat & SDMA_DESC_CMDSTAT_BR)
1068 flag = TTY_BREAK;
1069 else if (cmdstat & SDMA_DESC_CMDSTAT_FR)
1070 flag = TTY_FRAME;
1071 else if (cmdstat & SDMA_DESC_CMDSTAT_OR)
1072 flag = TTY_OVERRUN;
1073 else if (cmdstat & SDMA_DESC_CMDSTAT_PE)
1074 flag = TTY_PARITY;
1075 }
1076
David Howells7d12e782006-10-05 14:55:46 +01001077 if (uart_handle_sysrq_char(&pi->port, *bp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 bp++;
1079 bytes_in--;
1080 goto next_frame;
1081 }
1082
1083 if ((unlikely(cmdstat & (SDMA_DESC_CMDSTAT_BR |
1084 SDMA_DESC_CMDSTAT_FR | SDMA_DESC_CMDSTAT_OR))) &&
1085 !(cmdstat & pi->port.ignore_status_mask))
1086
1087 tty_insert_flip_char(tty, *bp, flag);
1088 else {
1089 for (i=0; i<bytes_in; i++)
1090 tty_insert_flip_char(tty, *bp++, TTY_NORMAL);
1091
1092 pi->port.icount.rx += bytes_in;
1093 }
1094
1095next_frame:
1096 rxre->bytecnt = cpu_to_be16(0);
1097 wmb();
1098 rxre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O |
1099 SDMA_DESC_CMDSTAT_EI |
1100 SDMA_DESC_CMDSTAT_F |
1101 SDMA_DESC_CMDSTAT_L);
1102 wmb();
Ralf Baechled3fa72e2006-12-06 20:38:56 -08001103 dma_cache_sync(pi->port.dev, (void *)rxre, MPSC_RXRE_SIZE, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1105 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1106 flush_dcache_range((ulong)rxre,
1107 (ulong)rxre + MPSC_RXRE_SIZE);
1108#endif
1109
1110 /* Advance to next descriptor */
1111 pi->rxr_posn = (pi->rxr_posn + 1) & (MPSC_RXR_ENTRIES - 1);
1112 rxre = (struct mpsc_rx_desc *)(pi->rxr +
1113 (pi->rxr_posn * MPSC_RXRE_SIZE));
Ralf Baechled3fa72e2006-12-06 20:38:56 -08001114 dma_cache_sync(pi->port.dev, (void *)rxre, MPSC_RXRE_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1116 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1117 invalidate_dcache_range((ulong)rxre,
1118 (ulong)rxre + MPSC_RXRE_SIZE);
1119#endif
1120
1121 rc = 1;
1122 }
1123
1124 /* Restart rx engine, if its stopped */
1125 if ((readl(pi->sdma_base + SDMA_SDCM) & SDMA_SDCM_ERD) == 0)
1126 mpsc_start_rx(pi);
1127
1128 tty_flip_buffer_push(tty);
1129 return rc;
1130}
1131
1132static inline void
1133mpsc_setup_tx_desc(struct mpsc_port_info *pi, u32 count, u32 intr)
1134{
1135 struct mpsc_tx_desc *txre;
1136
1137 txre = (struct mpsc_tx_desc *)(pi->txr +
1138 (pi->txr_head * MPSC_TXRE_SIZE));
1139
1140 txre->bytecnt = cpu_to_be16(count);
1141 txre->shadow = txre->bytecnt;
1142 wmb(); /* ensure cmdstat is last field updated */
1143 txre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O | SDMA_DESC_CMDSTAT_F |
1144 SDMA_DESC_CMDSTAT_L | ((intr) ?
1145 SDMA_DESC_CMDSTAT_EI
1146 : 0));
1147 wmb();
Ralf Baechled3fa72e2006-12-06 20:38:56 -08001148 dma_cache_sync(pi->port.dev, (void *) txre, MPSC_TXRE_SIZE, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1150 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1151 flush_dcache_range((ulong)txre,
1152 (ulong)txre + MPSC_TXRE_SIZE);
1153#endif
1154
1155 return;
1156}
1157
1158static inline void
1159mpsc_copy_tx_data(struct mpsc_port_info *pi)
1160{
1161 struct circ_buf *xmit = &pi->port.info->xmit;
1162 u8 *bp;
1163 u32 i;
1164
1165 /* Make sure the desc ring isn't full */
1166 while (CIRC_CNT(pi->txr_head, pi->txr_tail, MPSC_TXR_ENTRIES) <
1167 (MPSC_TXR_ENTRIES - 1)) {
1168 if (pi->port.x_char) {
1169 /*
1170 * Ideally, we should use the TCS field in
1171 * CHR_1 to put the x_char out immediately but
1172 * errata prevents us from being able to read
1173 * CHR_2 to know that its safe to write to
1174 * CHR_1. Instead, just put it in-band with
1175 * all the other Tx data.
1176 */
1177 bp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
1178 *bp = pi->port.x_char;
1179 pi->port.x_char = 0;
1180 i = 1;
1181 }
1182 else if (!uart_circ_empty(xmit) && !uart_tx_stopped(&pi->port)){
1183 i = min((u32) MPSC_TXBE_SIZE,
1184 (u32) uart_circ_chars_pending(xmit));
1185 i = min(i, (u32) CIRC_CNT_TO_END(xmit->head, xmit->tail,
1186 UART_XMIT_SIZE));
1187 bp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
1188 memcpy(bp, &xmit->buf[xmit->tail], i);
1189 xmit->tail = (xmit->tail + i) & (UART_XMIT_SIZE - 1);
1190
1191 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1192 uart_write_wakeup(&pi->port);
1193 }
1194 else /* All tx data copied into ring bufs */
1195 return;
1196
Ralf Baechled3fa72e2006-12-06 20:38:56 -08001197 dma_cache_sync(pi->port.dev, (void *) bp, MPSC_TXBE_SIZE, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1199 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1200 flush_dcache_range((ulong)bp,
1201 (ulong)bp + MPSC_TXBE_SIZE);
1202#endif
1203 mpsc_setup_tx_desc(pi, i, 1);
1204
1205 /* Advance to next descriptor */
1206 pi->txr_head = (pi->txr_head + 1) & (MPSC_TXR_ENTRIES - 1);
1207 }
1208
1209 return;
1210}
1211
1212static inline int
1213mpsc_tx_intr(struct mpsc_port_info *pi)
1214{
1215 struct mpsc_tx_desc *txre;
1216 int rc = 0;
Dave Jiang17333102007-05-06 14:48:50 -07001217 unsigned long iflags;
1218
1219 spin_lock_irqsave(&pi->tx_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 if (!mpsc_sdma_tx_active(pi)) {
1222 txre = (struct mpsc_tx_desc *)(pi->txr +
1223 (pi->txr_tail * MPSC_TXRE_SIZE));
1224
Ralf Baechled3fa72e2006-12-06 20:38:56 -08001225 dma_cache_sync(pi->port.dev, (void *) txre, MPSC_TXRE_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1227 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1228 invalidate_dcache_range((ulong)txre,
1229 (ulong)txre + MPSC_TXRE_SIZE);
1230#endif
1231
1232 while (!(be32_to_cpu(txre->cmdstat) & SDMA_DESC_CMDSTAT_O)) {
1233 rc = 1;
1234 pi->port.icount.tx += be16_to_cpu(txre->bytecnt);
1235 pi->txr_tail = (pi->txr_tail+1) & (MPSC_TXR_ENTRIES-1);
1236
1237 /* If no more data to tx, fall out of loop */
1238 if (pi->txr_head == pi->txr_tail)
1239 break;
1240
1241 txre = (struct mpsc_tx_desc *)(pi->txr +
1242 (pi->txr_tail * MPSC_TXRE_SIZE));
Ralf Baechled3fa72e2006-12-06 20:38:56 -08001243 dma_cache_sync(pi->port.dev, (void *) txre, MPSC_TXRE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 DMA_FROM_DEVICE);
1245#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1246 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1247 invalidate_dcache_range((ulong)txre,
1248 (ulong)txre + MPSC_TXRE_SIZE);
1249#endif
1250 }
1251
1252 mpsc_copy_tx_data(pi);
1253 mpsc_sdma_start_tx(pi); /* start next desc if ready */
1254 }
1255
Dave Jiang17333102007-05-06 14:48:50 -07001256 spin_unlock_irqrestore(&pi->tx_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return rc;
1258}
1259
1260/*
1261 * This is the driver's interrupt handler. To avoid a race, we first clear
1262 * the interrupt, then handle any completed Rx/Tx descriptors. When done
1263 * handling those descriptors, we restart the Rx/Tx engines if they're stopped.
1264 */
1265static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001266mpsc_sdma_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 struct mpsc_port_info *pi = dev_id;
1269 ulong iflags;
1270 int rc = IRQ_NONE;
1271
1272 pr_debug("mpsc_sdma_intr[%d]: SDMA Interrupt Received\n",pi->port.line);
1273
1274 spin_lock_irqsave(&pi->port.lock, iflags);
1275 mpsc_sdma_intr_ack(pi);
David Howells7d12e782006-10-05 14:55:46 +01001276 if (mpsc_rx_intr(pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 rc = IRQ_HANDLED;
1278 if (mpsc_tx_intr(pi))
1279 rc = IRQ_HANDLED;
1280 spin_unlock_irqrestore(&pi->port.lock, iflags);
1281
1282 pr_debug("mpsc_sdma_intr[%d]: SDMA Interrupt Handled\n", pi->port.line);
1283 return rc;
1284}
1285
1286/*
1287 ******************************************************************************
1288 *
1289 * serial_core.c Interface routines
1290 *
1291 ******************************************************************************
1292 */
1293static uint
1294mpsc_tx_empty(struct uart_port *port)
1295{
1296 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1297 ulong iflags;
1298 uint rc;
1299
1300 spin_lock_irqsave(&pi->port.lock, iflags);
1301 rc = mpsc_sdma_tx_active(pi) ? 0 : TIOCSER_TEMT;
1302 spin_unlock_irqrestore(&pi->port.lock, iflags);
1303
1304 return rc;
1305}
1306
1307static void
1308mpsc_set_mctrl(struct uart_port *port, uint mctrl)
1309{
1310 /* Have no way to set modem control lines AFAICT */
1311 return;
1312}
1313
1314static uint
1315mpsc_get_mctrl(struct uart_port *port)
1316{
1317 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1318 u32 mflags, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 status = (pi->mirror_regs) ? pi->MPSC_CHR_10_m :
1321 readl(pi->mpsc_base + MPSC_CHR_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 mflags = 0;
1324 if (status & 0x1)
1325 mflags |= TIOCM_CTS;
1326 if (status & 0x2)
1327 mflags |= TIOCM_CAR;
1328
1329 return mflags | TIOCM_DSR; /* No way to tell if DSR asserted */
1330}
1331
1332static void
Russell Kingb129a8c2005-08-31 10:12:14 +01001333mpsc_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1336
Russell Kingb129a8c2005-08-31 10:12:14 +01001337 pr_debug("mpsc_stop_tx[%d]\n", port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 mpsc_freeze(pi);
1340 return;
1341}
1342
1343static void
Russell Kingb129a8c2005-08-31 10:12:14 +01001344mpsc_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
Dave Jiang17333102007-05-06 14:48:50 -07001347 unsigned long iflags;
1348
1349 spin_lock_irqsave(&pi->tx_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 mpsc_unfreeze(pi);
1352 mpsc_copy_tx_data(pi);
1353 mpsc_sdma_start_tx(pi);
1354
Dave Jiang17333102007-05-06 14:48:50 -07001355 spin_unlock_irqrestore(&pi->tx_lock, iflags);
1356
Russell Kingb129a8c2005-08-31 10:12:14 +01001357 pr_debug("mpsc_start_tx[%d]\n", port->line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return;
1359}
1360
1361static void
1362mpsc_start_rx(struct mpsc_port_info *pi)
1363{
1364 pr_debug("mpsc_start_rx[%d]: Starting...\n", pi->port.line);
1365
1366 if (pi->rcv_data) {
1367 mpsc_enter_hunt(pi);
1368 mpsc_sdma_cmd(pi, SDMA_SDCM_ERD);
1369 }
1370 return;
1371}
1372
1373static void
1374mpsc_stop_rx(struct uart_port *port)
1375{
1376 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1377
1378 pr_debug("mpsc_stop_rx[%d]: Stopping...\n", port->line);
1379
Carlos Sanchez6c1ead52007-07-31 00:38:59 -07001380 if (pi->mirror_regs) {
1381 writel(pi->MPSC_CHR_2_m | MPSC_CHR_2_RA,
1382 pi->mpsc_base + MPSC_CHR_2);
1383 /* Erratum prevents reading CHR_2 so just delay for a while */
1384 udelay(100);
1385 }
1386 else {
1387 writel(readl(pi->mpsc_base + MPSC_CHR_2) | MPSC_CHR_2_RA,
1388 pi->mpsc_base + MPSC_CHR_2);
1389
1390 while (readl(pi->mpsc_base + MPSC_CHR_2) & MPSC_CHR_2_RA)
1391 udelay(10);
1392 }
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 mpsc_sdma_cmd(pi, SDMA_SDCM_AR);
1395 return;
1396}
1397
1398static void
1399mpsc_enable_ms(struct uart_port *port)
1400{
1401 return; /* Not supported */
1402}
1403
1404static void
1405mpsc_break_ctl(struct uart_port *port, int ctl)
1406{
1407 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1408 ulong flags;
1409 u32 v;
1410
1411 v = ctl ? 0x00ff0000 : 0;
1412
1413 spin_lock_irqsave(&pi->port.lock, flags);
1414 if (pi->mirror_regs)
1415 pi->MPSC_CHR_1_m = v;
1416 writel(v, pi->mpsc_base + MPSC_CHR_1);
1417 spin_unlock_irqrestore(&pi->port.lock, flags);
1418
1419 return;
1420}
1421
1422static int
1423mpsc_startup(struct uart_port *port)
1424{
1425 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1426 u32 flag = 0;
1427 int rc;
1428
1429 pr_debug("mpsc_startup[%d]: Starting up MPSC, irq: %d\n",
1430 port->line, pi->port.irq);
1431
1432 if ((rc = mpsc_make_ready(pi)) == 0) {
1433 /* Setup IRQ handler */
1434 mpsc_sdma_intr_ack(pi);
1435
1436 /* If irq's are shared, need to set flag */
1437 if (mpsc_ports[0].port.irq == mpsc_ports[1].port.irq)
Thomas Gleixner40663cc2006-07-01 19:29:43 -07001438 flag = IRQF_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 if (request_irq(pi->port.irq, mpsc_sdma_intr, flag,
Mark A. Greera30ff2e2006-03-25 03:08:26 -08001441 "mpsc-sdma", pi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 printk(KERN_ERR "MPSC: Can't get SDMA IRQ %d\n",
1443 pi->port.irq);
1444
1445 mpsc_sdma_intr_unmask(pi, 0xf);
1446 mpsc_sdma_set_rx_ring(pi, (struct mpsc_rx_desc *)(pi->rxr_p +
1447 (pi->rxr_posn * MPSC_RXRE_SIZE)));
1448 }
1449
1450 return rc;
1451}
1452
1453static void
1454mpsc_shutdown(struct uart_port *port)
1455{
1456 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 pr_debug("mpsc_shutdown[%d]: Shutting down MPSC\n", port->line);
1459
1460 mpsc_sdma_stop(pi);
1461 free_irq(pi->port.irq, pi);
1462 return;
1463}
1464
1465static void
Alan Cox606d0992006-12-08 02:38:45 -08001466mpsc_set_termios(struct uart_port *port, struct ktermios *termios,
1467 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1470 u32 baud;
1471 ulong flags;
1472 u32 chr_bits, stop_bits, par;
1473
1474 pi->c_iflag = termios->c_iflag;
1475 pi->c_cflag = termios->c_cflag;
1476
1477 switch (termios->c_cflag & CSIZE) {
1478 case CS5:
1479 chr_bits = MPSC_MPCR_CL_5;
1480 break;
1481 case CS6:
1482 chr_bits = MPSC_MPCR_CL_6;
1483 break;
1484 case CS7:
1485 chr_bits = MPSC_MPCR_CL_7;
1486 break;
1487 case CS8:
1488 default:
1489 chr_bits = MPSC_MPCR_CL_8;
1490 break;
1491 }
1492
1493 if (termios->c_cflag & CSTOPB)
1494 stop_bits = MPSC_MPCR_SBL_2;
1495 else
1496 stop_bits = MPSC_MPCR_SBL_1;
1497
1498 par = MPSC_CHR_2_PAR_EVEN;
1499 if (termios->c_cflag & PARENB)
1500 if (termios->c_cflag & PARODD)
1501 par = MPSC_CHR_2_PAR_ODD;
1502#ifdef CMSPAR
1503 if (termios->c_cflag & CMSPAR) {
1504 if (termios->c_cflag & PARODD)
1505 par = MPSC_CHR_2_PAR_MARK;
1506 else
1507 par = MPSC_CHR_2_PAR_SPACE;
1508 }
1509#endif
1510
1511 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk);
1512
1513 spin_lock_irqsave(&pi->port.lock, flags);
1514
1515 uart_update_timeout(port, termios->c_cflag, baud);
1516
1517 mpsc_set_char_length(pi, chr_bits);
1518 mpsc_set_stop_bit_length(pi, stop_bits);
1519 mpsc_set_parity(pi, par);
1520 mpsc_set_baudrate(pi, baud);
1521
1522 /* Characters/events to read */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 pi->port.read_status_mask = SDMA_DESC_CMDSTAT_OR;
1524
1525 if (termios->c_iflag & INPCK)
1526 pi->port.read_status_mask |= SDMA_DESC_CMDSTAT_PE |
1527 SDMA_DESC_CMDSTAT_FR;
1528
1529 if (termios->c_iflag & (BRKINT | PARMRK))
1530 pi->port.read_status_mask |= SDMA_DESC_CMDSTAT_BR;
1531
1532 /* Characters/events to ignore */
1533 pi->port.ignore_status_mask = 0;
1534
1535 if (termios->c_iflag & IGNPAR)
1536 pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_PE |
1537 SDMA_DESC_CMDSTAT_FR;
1538
1539 if (termios->c_iflag & IGNBRK) {
1540 pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_BR;
1541
1542 if (termios->c_iflag & IGNPAR)
1543 pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_OR;
1544 }
1545
Stephane Chazelas5797ae32007-07-31 00:38:59 -07001546 if ((termios->c_cflag & CREAD)) {
1547 if (!pi->rcv_data) {
1548 pi->rcv_data = 1;
1549 mpsc_start_rx(pi);
1550 }
1551 } else if (pi->rcv_data) {
1552 mpsc_stop_rx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 pi->rcv_data = 0;
Stephane Chazelas5797ae32007-07-31 00:38:59 -07001554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 spin_unlock_irqrestore(&pi->port.lock, flags);
1557 return;
1558}
1559
1560static const char *
1561mpsc_type(struct uart_port *port)
1562{
1563 pr_debug("mpsc_type[%d]: port type: %s\n", port->line,MPSC_DRIVER_NAME);
1564 return MPSC_DRIVER_NAME;
1565}
1566
1567static int
1568mpsc_request_port(struct uart_port *port)
1569{
1570 /* Should make chip/platform specific call */
1571 return 0;
1572}
1573
1574static void
1575mpsc_release_port(struct uart_port *port)
1576{
1577 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1578
1579 if (pi->ready) {
1580 mpsc_uninit_rings(pi);
1581 mpsc_free_ring_mem(pi);
1582 pi->ready = 0;
1583 }
1584
1585 return;
1586}
1587
1588static void
1589mpsc_config_port(struct uart_port *port, int flags)
1590{
1591 return;
1592}
1593
1594static int
1595mpsc_verify_port(struct uart_port *port, struct serial_struct *ser)
1596{
1597 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1598 int rc = 0;
1599
1600 pr_debug("mpsc_verify_port[%d]: Verifying port data\n", pi->port.line);
1601
1602 if (ser->type != PORT_UNKNOWN && ser->type != PORT_MPSC)
1603 rc = -EINVAL;
1604 else if (pi->port.irq != ser->irq)
1605 rc = -EINVAL;
1606 else if (ser->io_type != SERIAL_IO_MEM)
1607 rc = -EINVAL;
1608 else if (pi->port.uartclk / 16 != ser->baud_base) /* Not sure */
1609 rc = -EINVAL;
1610 else if ((void *)pi->port.mapbase != ser->iomem_base)
1611 rc = -EINVAL;
1612 else if (pi->port.iobase != ser->port)
1613 rc = -EINVAL;
1614 else if (ser->hub6 != 0)
1615 rc = -EINVAL;
1616
1617 return rc;
1618}
1619
1620static struct uart_ops mpsc_pops = {
1621 .tx_empty = mpsc_tx_empty,
1622 .set_mctrl = mpsc_set_mctrl,
1623 .get_mctrl = mpsc_get_mctrl,
1624 .stop_tx = mpsc_stop_tx,
1625 .start_tx = mpsc_start_tx,
1626 .stop_rx = mpsc_stop_rx,
1627 .enable_ms = mpsc_enable_ms,
1628 .break_ctl = mpsc_break_ctl,
1629 .startup = mpsc_startup,
1630 .shutdown = mpsc_shutdown,
1631 .set_termios = mpsc_set_termios,
1632 .type = mpsc_type,
1633 .release_port = mpsc_release_port,
1634 .request_port = mpsc_request_port,
1635 .config_port = mpsc_config_port,
1636 .verify_port = mpsc_verify_port,
1637};
1638
1639/*
1640 ******************************************************************************
1641 *
1642 * Console Interface Routines
1643 *
1644 ******************************************************************************
1645 */
1646
1647#ifdef CONFIG_SERIAL_MPSC_CONSOLE
1648static void
1649mpsc_console_write(struct console *co, const char *s, uint count)
1650{
1651 struct mpsc_port_info *pi = &mpsc_ports[co->index];
1652 u8 *bp, *dp, add_cr = 0;
1653 int i;
Dave Jiang17333102007-05-06 14:48:50 -07001654 unsigned long iflags;
1655
1656 spin_lock_irqsave(&pi->tx_lock, iflags);
1657
1658 while (pi->txr_head != pi->txr_tail) {
1659 while (mpsc_sdma_tx_active(pi))
1660 udelay(100);
1661 mpsc_sdma_intr_ack(pi);
1662 mpsc_tx_intr(pi);
1663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 while (mpsc_sdma_tx_active(pi))
1666 udelay(100);
1667
1668 while (count > 0) {
1669 bp = dp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
1670
1671 for (i = 0; i < MPSC_TXBE_SIZE; i++) {
1672 if (count == 0)
1673 break;
1674
1675 if (add_cr) {
1676 *(dp++) = '\r';
1677 add_cr = 0;
1678 }
1679 else {
1680 *(dp++) = *s;
1681
1682 if (*(s++) == '\n') { /* add '\r' after '\n' */
1683 add_cr = 1;
1684 count++;
1685 }
1686 }
1687
1688 count--;
1689 }
1690
Ralf Baechled3fa72e2006-12-06 20:38:56 -08001691 dma_cache_sync(pi->port.dev, (void *) bp, MPSC_TXBE_SIZE, DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1693 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1694 flush_dcache_range((ulong)bp,
1695 (ulong)bp + MPSC_TXBE_SIZE);
1696#endif
1697 mpsc_setup_tx_desc(pi, i, 0);
1698 pi->txr_head = (pi->txr_head + 1) & (MPSC_TXR_ENTRIES - 1);
1699 mpsc_sdma_start_tx(pi);
1700
1701 while (mpsc_sdma_tx_active(pi))
1702 udelay(100);
1703
1704 pi->txr_tail = (pi->txr_tail + 1) & (MPSC_TXR_ENTRIES - 1);
1705 }
1706
Dave Jiang17333102007-05-06 14:48:50 -07001707 spin_unlock_irqrestore(&pi->tx_lock, iflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return;
1709}
1710
1711static int __init
1712mpsc_console_setup(struct console *co, char *options)
1713{
1714 struct mpsc_port_info *pi;
1715 int baud, bits, parity, flow;
1716
1717 pr_debug("mpsc_console_setup[%d]: options: %s\n", co->index, options);
1718
1719 if (co->index >= MPSC_NUM_CTLRS)
1720 co->index = 0;
1721
1722 pi = &mpsc_ports[co->index];
1723
1724 baud = pi->default_baud;
1725 bits = pi->default_bits;
1726 parity = pi->default_parity;
1727 flow = pi->default_flow;
1728
1729 if (!pi->port.ops)
1730 return -ENODEV;
1731
1732 spin_lock_init(&pi->port.lock); /* Temporary fix--copied from 8250.c */
1733
1734 if (options)
1735 uart_parse_options(options, &baud, &parity, &bits, &flow);
1736
1737 return uart_set_options(&pi->port, co, baud, parity, bits, flow);
1738}
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740static struct console mpsc_console = {
1741 .name = MPSC_DEV_NAME,
1742 .write = mpsc_console_write,
1743 .device = uart_console_device,
1744 .setup = mpsc_console_setup,
1745 .flags = CON_PRINTBUFFER,
1746 .index = -1,
1747 .data = &mpsc_reg,
1748};
1749
1750static int __init
1751mpsc_late_console_init(void)
1752{
1753 pr_debug("mpsc_late_console_init: Enter\n");
1754
1755 if (!(mpsc_console.flags & CON_ENABLED))
1756 register_console(&mpsc_console);
1757 return 0;
1758}
1759
1760late_initcall(mpsc_late_console_init);
1761
1762#define MPSC_CONSOLE &mpsc_console
1763#else
1764#define MPSC_CONSOLE NULL
1765#endif
1766/*
1767 ******************************************************************************
1768 *
1769 * Dummy Platform Driver to extract & map shared register regions
1770 *
1771 ******************************************************************************
1772 */
1773static void
1774mpsc_resource_err(char *s)
1775{
1776 printk(KERN_WARNING "MPSC: Platform device resource error in %s\n", s);
1777 return;
1778}
1779
1780static int
1781mpsc_shared_map_regs(struct platform_device *pd)
1782{
1783 struct resource *r;
1784
1785 if ((r = platform_get_resource(pd, IORESOURCE_MEM,
1786 MPSC_ROUTING_BASE_ORDER)) && request_mem_region(r->start,
1787 MPSC_ROUTING_REG_BLOCK_SIZE, "mpsc_routing_regs")) {
1788
1789 mpsc_shared_regs.mpsc_routing_base = ioremap(r->start,
1790 MPSC_ROUTING_REG_BLOCK_SIZE);
1791 mpsc_shared_regs.mpsc_routing_base_p = r->start;
1792 }
1793 else {
1794 mpsc_resource_err("MPSC routing base");
1795 return -ENOMEM;
1796 }
1797
1798 if ((r = platform_get_resource(pd, IORESOURCE_MEM,
1799 MPSC_SDMA_INTR_BASE_ORDER)) && request_mem_region(r->start,
1800 MPSC_SDMA_INTR_REG_BLOCK_SIZE, "sdma_intr_regs")) {
1801
1802 mpsc_shared_regs.sdma_intr_base = ioremap(r->start,
1803 MPSC_SDMA_INTR_REG_BLOCK_SIZE);
1804 mpsc_shared_regs.sdma_intr_base_p = r->start;
1805 }
1806 else {
1807 iounmap(mpsc_shared_regs.mpsc_routing_base);
1808 release_mem_region(mpsc_shared_regs.mpsc_routing_base_p,
1809 MPSC_ROUTING_REG_BLOCK_SIZE);
1810 mpsc_resource_err("SDMA intr base");
1811 return -ENOMEM;
1812 }
1813
1814 return 0;
1815}
1816
1817static void
1818mpsc_shared_unmap_regs(void)
1819{
1820 if (!mpsc_shared_regs.mpsc_routing_base) {
1821 iounmap(mpsc_shared_regs.mpsc_routing_base);
1822 release_mem_region(mpsc_shared_regs.mpsc_routing_base_p,
1823 MPSC_ROUTING_REG_BLOCK_SIZE);
1824 }
1825 if (!mpsc_shared_regs.sdma_intr_base) {
1826 iounmap(mpsc_shared_regs.sdma_intr_base);
1827 release_mem_region(mpsc_shared_regs.sdma_intr_base_p,
1828 MPSC_SDMA_INTR_REG_BLOCK_SIZE);
1829 }
1830
Al Viro2c6e7592005-04-25 18:32:12 -07001831 mpsc_shared_regs.mpsc_routing_base = NULL;
1832 mpsc_shared_regs.sdma_intr_base = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 mpsc_shared_regs.mpsc_routing_base_p = 0;
1835 mpsc_shared_regs.sdma_intr_base_p = 0;
1836
1837 return;
1838}
1839
1840static int
Russell King3ae5eae2005-11-09 22:32:44 +00001841mpsc_shared_drv_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 struct mpsc_shared_pdata *pdata;
1844 int rc = -ENODEV;
1845
Russell King3ae5eae2005-11-09 22:32:44 +00001846 if (dev->id == 0) {
1847 if (!(rc = mpsc_shared_map_regs(dev))) {
1848 pdata = (struct mpsc_shared_pdata *)dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 mpsc_shared_regs.MPSC_MRR_m = pdata->mrr_val;
1851 mpsc_shared_regs.MPSC_RCRR_m= pdata->rcrr_val;
1852 mpsc_shared_regs.MPSC_TCRR_m= pdata->tcrr_val;
1853 mpsc_shared_regs.SDMA_INTR_CAUSE_m =
1854 pdata->intr_cause_val;
1855 mpsc_shared_regs.SDMA_INTR_MASK_m =
1856 pdata->intr_mask_val;
1857
1858 rc = 0;
1859 }
1860 }
1861
1862 return rc;
1863}
1864
1865static int
Russell King3ae5eae2005-11-09 22:32:44 +00001866mpsc_shared_drv_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 int rc = -ENODEV;
1869
Russell King3ae5eae2005-11-09 22:32:44 +00001870 if (dev->id == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 mpsc_shared_unmap_regs();
1872 mpsc_shared_regs.MPSC_MRR_m = 0;
1873 mpsc_shared_regs.MPSC_RCRR_m = 0;
1874 mpsc_shared_regs.MPSC_TCRR_m = 0;
1875 mpsc_shared_regs.SDMA_INTR_CAUSE_m = 0;
1876 mpsc_shared_regs.SDMA_INTR_MASK_m = 0;
1877 rc = 0;
1878 }
1879
1880 return rc;
1881}
1882
Russell King3ae5eae2005-11-09 22:32:44 +00001883static struct platform_driver mpsc_shared_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 .probe = mpsc_shared_drv_probe,
1885 .remove = mpsc_shared_drv_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00001886 .driver = {
1887 .name = MPSC_SHARED_NAME,
1888 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889};
1890
1891/*
1892 ******************************************************************************
1893 *
1894 * Driver Interface Routines
1895 *
1896 ******************************************************************************
1897 */
1898static struct uart_driver mpsc_reg = {
1899 .owner = THIS_MODULE,
1900 .driver_name = MPSC_DRIVER_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 .dev_name = MPSC_DEV_NAME,
1902 .major = MPSC_MAJOR,
1903 .minor = MPSC_MINOR_START,
1904 .nr = MPSC_NUM_CTLRS,
1905 .cons = MPSC_CONSOLE,
1906};
1907
1908static int
1909mpsc_drv_map_regs(struct mpsc_port_info *pi, struct platform_device *pd)
1910{
1911 struct resource *r;
1912
1913 if ((r = platform_get_resource(pd, IORESOURCE_MEM, MPSC_BASE_ORDER)) &&
1914 request_mem_region(r->start, MPSC_REG_BLOCK_SIZE, "mpsc_regs")){
1915
1916 pi->mpsc_base = ioremap(r->start, MPSC_REG_BLOCK_SIZE);
1917 pi->mpsc_base_p = r->start;
1918 }
1919 else {
1920 mpsc_resource_err("MPSC base");
1921 return -ENOMEM;
1922 }
1923
1924 if ((r = platform_get_resource(pd, IORESOURCE_MEM,
1925 MPSC_SDMA_BASE_ORDER)) && request_mem_region(r->start,
1926 MPSC_SDMA_REG_BLOCK_SIZE, "sdma_regs")) {
1927
1928 pi->sdma_base = ioremap(r->start,MPSC_SDMA_REG_BLOCK_SIZE);
1929 pi->sdma_base_p = r->start;
1930 }
1931 else {
1932 mpsc_resource_err("SDMA base");
Amol Lada141a042006-09-30 23:29:24 -07001933 if (pi->mpsc_base) {
1934 iounmap(pi->mpsc_base);
1935 pi->mpsc_base = NULL;
1936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return -ENOMEM;
1938 }
1939
1940 if ((r = platform_get_resource(pd,IORESOURCE_MEM,MPSC_BRG_BASE_ORDER))
1941 && request_mem_region(r->start, MPSC_BRG_REG_BLOCK_SIZE,
1942 "brg_regs")) {
1943
1944 pi->brg_base = ioremap(r->start, MPSC_BRG_REG_BLOCK_SIZE);
1945 pi->brg_base_p = r->start;
1946 }
1947 else {
1948 mpsc_resource_err("BRG base");
Amol Lada141a042006-09-30 23:29:24 -07001949 if (pi->mpsc_base) {
1950 iounmap(pi->mpsc_base);
1951 pi->mpsc_base = NULL;
1952 }
1953 if (pi->sdma_base) {
1954 iounmap(pi->sdma_base);
1955 pi->sdma_base = NULL;
1956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return -ENOMEM;
1958 }
1959
1960 return 0;
1961}
1962
1963static void
1964mpsc_drv_unmap_regs(struct mpsc_port_info *pi)
1965{
1966 if (!pi->mpsc_base) {
1967 iounmap(pi->mpsc_base);
1968 release_mem_region(pi->mpsc_base_p, MPSC_REG_BLOCK_SIZE);
1969 }
1970 if (!pi->sdma_base) {
1971 iounmap(pi->sdma_base);
1972 release_mem_region(pi->sdma_base_p, MPSC_SDMA_REG_BLOCK_SIZE);
1973 }
1974 if (!pi->brg_base) {
1975 iounmap(pi->brg_base);
1976 release_mem_region(pi->brg_base_p, MPSC_BRG_REG_BLOCK_SIZE);
1977 }
1978
Al Viro2c6e7592005-04-25 18:32:12 -07001979 pi->mpsc_base = NULL;
1980 pi->sdma_base = NULL;
1981 pi->brg_base = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 pi->mpsc_base_p = 0;
1984 pi->sdma_base_p = 0;
1985 pi->brg_base_p = 0;
1986
1987 return;
1988}
1989
1990static void
1991mpsc_drv_get_platform_data(struct mpsc_port_info *pi,
1992 struct platform_device *pd, int num)
1993{
1994 struct mpsc_pdata *pdata;
1995
1996 pdata = (struct mpsc_pdata *)pd->dev.platform_data;
1997
1998 pi->port.uartclk = pdata->brg_clk_freq;
1999 pi->port.iotype = UPIO_MEM;
2000 pi->port.line = num;
2001 pi->port.type = PORT_MPSC;
2002 pi->port.fifosize = MPSC_TXBE_SIZE;
2003 pi->port.membase = pi->mpsc_base;
2004 pi->port.mapbase = (ulong)pi->mpsc_base;
2005 pi->port.ops = &mpsc_pops;
2006
2007 pi->mirror_regs = pdata->mirror_regs;
2008 pi->cache_mgmt = pdata->cache_mgmt;
2009 pi->brg_can_tune = pdata->brg_can_tune;
2010 pi->brg_clk_src = pdata->brg_clk_src;
2011 pi->mpsc_max_idle = pdata->max_idle;
2012 pi->default_baud = pdata->default_baud;
2013 pi->default_bits = pdata->default_bits;
2014 pi->default_parity = pdata->default_parity;
2015 pi->default_flow = pdata->default_flow;
2016
2017 /* Initial values of mirrored regs */
2018 pi->MPSC_CHR_1_m = pdata->chr_1_val;
2019 pi->MPSC_CHR_2_m = pdata->chr_2_val;
2020 pi->MPSC_CHR_10_m = pdata->chr_10_val;
2021 pi->MPSC_MPCR_m = pdata->mpcr_val;
2022 pi->BRG_BCR_m = pdata->bcr_val;
2023
2024 pi->shared_regs = &mpsc_shared_regs;
2025
2026 pi->port.irq = platform_get_irq(pd, 0);
2027
2028 return;
2029}
2030
2031static int
Russell King3ae5eae2005-11-09 22:32:44 +00002032mpsc_drv_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 struct mpsc_port_info *pi;
2035 int rc = -ENODEV;
2036
Russell King3ae5eae2005-11-09 22:32:44 +00002037 pr_debug("mpsc_drv_probe: Adding MPSC %d\n", dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Russell King3ae5eae2005-11-09 22:32:44 +00002039 if (dev->id < MPSC_NUM_CTLRS) {
2040 pi = &mpsc_ports[dev->id];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Russell King3ae5eae2005-11-09 22:32:44 +00002042 if (!(rc = mpsc_drv_map_regs(pi, dev))) {
2043 mpsc_drv_get_platform_data(pi, dev, dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Dave Jiang17333102007-05-06 14:48:50 -07002045 if (!(rc = mpsc_make_ready(pi))) {
2046 spin_lock_init(&pi->tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 if (!(rc = uart_add_one_port(&mpsc_reg,
2048 &pi->port)))
2049 rc = 0;
2050 else {
2051 mpsc_release_port(
2052 (struct uart_port *)pi);
2053 mpsc_drv_unmap_regs(pi);
2054 }
Dave Jiang17333102007-05-06 14:48:50 -07002055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 else
2057 mpsc_drv_unmap_regs(pi);
2058 }
2059 }
2060
2061 return rc;
2062}
2063
2064static int
Russell King3ae5eae2005-11-09 22:32:44 +00002065mpsc_drv_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Russell King3ae5eae2005-11-09 22:32:44 +00002067 pr_debug("mpsc_drv_exit: Removing MPSC %d\n", dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Russell King3ae5eae2005-11-09 22:32:44 +00002069 if (dev->id < MPSC_NUM_CTLRS) {
2070 uart_remove_one_port(&mpsc_reg, &mpsc_ports[dev->id].port);
2071 mpsc_release_port((struct uart_port *)&mpsc_ports[dev->id].port);
2072 mpsc_drv_unmap_regs(&mpsc_ports[dev->id]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return 0;
2074 }
2075 else
2076 return -ENODEV;
2077}
2078
Russell King3ae5eae2005-11-09 22:32:44 +00002079static struct platform_driver mpsc_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 .probe = mpsc_drv_probe,
2081 .remove = mpsc_drv_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00002082 .driver = {
2083 .name = MPSC_CTLR_NAME,
2084 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085};
2086
2087static int __init
2088mpsc_drv_init(void)
2089{
2090 int rc;
2091
2092 printk(KERN_INFO "Serial: MPSC driver $Revision: 1.00 $\n");
2093
2094 memset(mpsc_ports, 0, sizeof(mpsc_ports));
2095 memset(&mpsc_shared_regs, 0, sizeof(mpsc_shared_regs));
2096
2097 if (!(rc = uart_register_driver(&mpsc_reg))) {
Russell King3ae5eae2005-11-09 22:32:44 +00002098 if (!(rc = platform_driver_register(&mpsc_shared_driver))) {
2099 if ((rc = platform_driver_register(&mpsc_driver))) {
2100 platform_driver_unregister(&mpsc_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 uart_unregister_driver(&mpsc_reg);
2102 }
2103 }
2104 else
2105 uart_unregister_driver(&mpsc_reg);
2106 }
2107
2108 return rc;
2109
2110}
2111
2112static void __exit
2113mpsc_drv_exit(void)
2114{
Russell King3ae5eae2005-11-09 22:32:44 +00002115 platform_driver_unregister(&mpsc_driver);
2116 platform_driver_unregister(&mpsc_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 uart_unregister_driver(&mpsc_reg);
2118 memset(mpsc_ports, 0, sizeof(mpsc_ports));
2119 memset(&mpsc_shared_regs, 0, sizeof(mpsc_shared_regs));
2120 return;
2121}
2122
2123module_init(mpsc_drv_init);
2124module_exit(mpsc_drv_exit);
2125
2126MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
2127MODULE_DESCRIPTION("Generic Marvell MPSC serial/UART driver $Revision: 1.00 $");
2128MODULE_VERSION(MPSC_VERSION);
2129MODULE_LICENSE("GPL");
2130MODULE_ALIAS_CHARDEV_MAJOR(MPSC_MAJOR);