blob: a2a643318002892201db6c0e2b478401e4d93cdc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/serial/mpsc.c
3 *
4 * Generic driver for the MPSC (UART mode) on Marvell parts (e.g., GT64240,
5 * GT64260, MV64340, MV64360, GT96100, ... ).
6 *
7 * Author: Mark A. Greer <mgreer@mvista.com>
8 *
9 * Based on an old MPSC driver that was in the linuxppc tree. It appears to
10 * have been created by Chris Zankel (formerly of MontaVista) but there
11 * is no proper Copyright so I'm not sure. Apparently, parts were also
12 * taken from PPCBoot (now U-Boot). Also based on drivers/serial/8250.c
13 * by Russell King.
14 *
15 * 2004 (c) MontaVista, Software, Inc. This file is licensed under
16 * the terms of the GNU General Public License version 2. This program
17 * is licensed "as is" without any warranty of any kind, whether express
18 * or implied.
19 */
20/*
21 * The MPSC interface is much like a typical network controller's interface.
22 * That is, you set up separate rings of descriptors for transmitting and
23 * receiving data. There is also a pool of buffers with (one buffer per
24 * descriptor) that incoming data are dma'd into or outgoing data are dma'd
25 * out of.
26 *
27 * The MPSC requires two other controllers to be able to work. The Baud Rate
28 * Generator (BRG) provides a clock at programmable frequencies which determines
29 * the baud rate. The Serial DMA Controller (SDMA) takes incoming data from the
30 * MPSC and DMA's it into memory or DMA's outgoing data and passes it to the
31 * MPSC. It is actually the SDMA interrupt that the driver uses to keep the
32 * transmit and receive "engines" going (i.e., indicate data has been
33 * transmitted or received).
34 *
35 * NOTES:
36 *
37 * 1) Some chips have an erratum where several regs cannot be
38 * read. To work around that, we keep a local copy of those regs in
39 * 'mpsc_port_info'.
40 *
41 * 2) Some chips have an erratum where the ctlr will hang when the SDMA ctlr
42 * accesses system mem with coherency enabled. For that reason, the driver
43 * assumes that coherency for that ctlr has been disabled. This means
44 * that when in a cache coherent system, the driver has to manually manage
45 * the data cache on the areas that it touches because the dma_* macro are
46 * basically no-ops.
47 *
48 * 3) There is an erratum (on PPC) where you can't use the instruction to do
49 * a DMA_TO_DEVICE/cache clean so DMA_BIDIRECTIONAL/flushes are used in places
50 * where a DMA_TO_DEVICE/clean would have [otherwise] sufficed.
51 *
52 * 4) AFAICT, hardware flow control isn't supported by the controller --MAG.
53 */
54
55#include "mpsc.h"
56
57/*
58 * Define how this driver is known to the outside (we've been assigned a
59 * range on the "Low-density serial ports" major).
60 */
61#define MPSC_MAJOR 204
62#define MPSC_MINOR_START 44
63#define MPSC_DRIVER_NAME "MPSC"
64#define MPSC_DEVFS_NAME "ttymm/"
65#define MPSC_DEV_NAME "ttyMM"
66#define MPSC_VERSION "1.00"
67
68static struct mpsc_port_info mpsc_ports[MPSC_NUM_CTLRS];
69static struct mpsc_shared_regs mpsc_shared_regs;
Lee Nicks4d0145a2005-06-25 14:55:36 -070070static struct uart_driver mpsc_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Lee Nicks4d0145a2005-06-25 14:55:36 -070072static void mpsc_start_rx(struct mpsc_port_info *pi);
73static void mpsc_free_ring_mem(struct mpsc_port_info *pi);
74static void mpsc_release_port(struct uart_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
76 ******************************************************************************
77 *
78 * Baud Rate Generator Routines (BRG)
79 *
80 ******************************************************************************
81 */
82static void
83mpsc_brg_init(struct mpsc_port_info *pi, u32 clk_src)
84{
85 u32 v;
86
87 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
88 v = (v & ~(0xf << 18)) | ((clk_src & 0xf) << 18);
89
90 if (pi->brg_can_tune)
91 v &= ~(1 << 25);
92
93 if (pi->mirror_regs)
94 pi->BRG_BCR_m = v;
95 writel(v, pi->brg_base + BRG_BCR);
96
97 writel(readl(pi->brg_base + BRG_BTR) & 0xffff0000,
98 pi->brg_base + BRG_BTR);
99 return;
100}
101
102static void
103mpsc_brg_enable(struct mpsc_port_info *pi)
104{
105 u32 v;
106
107 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
108 v |= (1 << 16);
109
110 if (pi->mirror_regs)
111 pi->BRG_BCR_m = v;
112 writel(v, pi->brg_base + BRG_BCR);
113 return;
114}
115
116static void
117mpsc_brg_disable(struct mpsc_port_info *pi)
118{
119 u32 v;
120
121 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
122 v &= ~(1 << 16);
123
124 if (pi->mirror_regs)
125 pi->BRG_BCR_m = v;
126 writel(v, pi->brg_base + BRG_BCR);
127 return;
128}
129
130static inline void
131mpsc_set_baudrate(struct mpsc_port_info *pi, u32 baud)
132{
133 /*
134 * To set the baud, we adjust the CDV field in the BRG_BCR reg.
135 * From manual: Baud = clk / ((CDV+1)*2) ==> CDV = (clk / (baud*2)) - 1.
136 * However, the input clock is divided by 16 in the MPSC b/c of how
137 * 'MPSC_MMCRH' was set up so we have to divide the 'clk' used in our
138 * calculation by 16 to account for that. So the real calculation
139 * that accounts for the way the mpsc is set up is:
140 * CDV = (clk / (baud*2*16)) - 1 ==> CDV = (clk / (baud << 5)) - 1.
141 */
142 u32 cdv = (pi->port.uartclk / (baud << 5)) - 1;
143 u32 v;
144
145 mpsc_brg_disable(pi);
146 v = (pi->mirror_regs) ? pi->BRG_BCR_m : readl(pi->brg_base + BRG_BCR);
147 v = (v & 0xffff0000) | (cdv & 0xffff);
148
149 if (pi->mirror_regs)
150 pi->BRG_BCR_m = v;
151 writel(v, pi->brg_base + BRG_BCR);
152 mpsc_brg_enable(pi);
153
154 return;
155}
156
157/*
158 ******************************************************************************
159 *
160 * Serial DMA Routines (SDMA)
161 *
162 ******************************************************************************
163 */
164
165static void
166mpsc_sdma_burstsize(struct mpsc_port_info *pi, u32 burst_size)
167{
168 u32 v;
169
170 pr_debug("mpsc_sdma_burstsize[%d]: burst_size: %d\n",
171 pi->port.line, burst_size);
172
173 burst_size >>= 3; /* Divide by 8 b/c reg values are 8-byte chunks */
174
175 if (burst_size < 2)
176 v = 0x0; /* 1 64-bit word */
177 else if (burst_size < 4)
178 v = 0x1; /* 2 64-bit words */
179 else if (burst_size < 8)
180 v = 0x2; /* 4 64-bit words */
181 else
182 v = 0x3; /* 8 64-bit words */
183
184 writel((readl(pi->sdma_base + SDMA_SDC) & (0x3 << 12)) | (v << 12),
185 pi->sdma_base + SDMA_SDC);
186 return;
187}
188
189static void
190mpsc_sdma_init(struct mpsc_port_info *pi, u32 burst_size)
191{
192 pr_debug("mpsc_sdma_init[%d]: burst_size: %d\n", pi->port.line,
193 burst_size);
194
195 writel((readl(pi->sdma_base + SDMA_SDC) & 0x3ff) | 0x03f,
196 pi->sdma_base + SDMA_SDC);
197 mpsc_sdma_burstsize(pi, burst_size);
198 return;
199}
200
201static inline u32
202mpsc_sdma_intr_mask(struct mpsc_port_info *pi, u32 mask)
203{
204 u32 old, v;
205
206 pr_debug("mpsc_sdma_intr_mask[%d]: mask: 0x%x\n", pi->port.line, mask);
207
208 old = v = (pi->mirror_regs) ? pi->shared_regs->SDMA_INTR_MASK_m :
209 readl(pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
210
211 mask &= 0xf;
212 if (pi->port.line)
213 mask <<= 8;
214 v &= ~mask;
215
216 if (pi->mirror_regs)
217 pi->shared_regs->SDMA_INTR_MASK_m = v;
218 writel(v, pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
219
220 if (pi->port.line)
221 old >>= 8;
222 return old & 0xf;
223}
224
225static inline void
226mpsc_sdma_intr_unmask(struct mpsc_port_info *pi, u32 mask)
227{
228 u32 v;
229
230 pr_debug("mpsc_sdma_intr_unmask[%d]: mask: 0x%x\n", pi->port.line,mask);
231
232 v = (pi->mirror_regs) ? pi->shared_regs->SDMA_INTR_MASK_m :
233 readl(pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
234
235 mask &= 0xf;
236 if (pi->port.line)
237 mask <<= 8;
238 v |= mask;
239
240 if (pi->mirror_regs)
241 pi->shared_regs->SDMA_INTR_MASK_m = v;
242 writel(v, pi->shared_regs->sdma_intr_base + SDMA_INTR_MASK);
243 return;
244}
245
246static inline void
247mpsc_sdma_intr_ack(struct mpsc_port_info *pi)
248{
249 pr_debug("mpsc_sdma_intr_ack[%d]: Acknowledging IRQ\n", pi->port.line);
250
251 if (pi->mirror_regs)
252 pi->shared_regs->SDMA_INTR_CAUSE_m = 0;
253 writel(0, pi->shared_regs->sdma_intr_base + SDMA_INTR_CAUSE);
254 return;
255}
256
257static inline void
258mpsc_sdma_set_rx_ring(struct mpsc_port_info *pi, struct mpsc_rx_desc *rxre_p)
259{
260 pr_debug("mpsc_sdma_set_rx_ring[%d]: rxre_p: 0x%x\n",
261 pi->port.line, (u32) rxre_p);
262
263 writel((u32)rxre_p, pi->sdma_base + SDMA_SCRDP);
264 return;
265}
266
267static inline void
268mpsc_sdma_set_tx_ring(struct mpsc_port_info *pi, struct mpsc_tx_desc *txre_p)
269{
270 writel((u32)txre_p, pi->sdma_base + SDMA_SFTDP);
271 writel((u32)txre_p, pi->sdma_base + SDMA_SCTDP);
272 return;
273}
274
275static inline void
276mpsc_sdma_cmd(struct mpsc_port_info *pi, u32 val)
277{
278 u32 v;
279
280 v = readl(pi->sdma_base + SDMA_SDCM);
281 if (val)
282 v |= val;
283 else
284 v = 0;
285 wmb();
286 writel(v, pi->sdma_base + SDMA_SDCM);
287 wmb();
288 return;
289}
290
291static inline uint
292mpsc_sdma_tx_active(struct mpsc_port_info *pi)
293{
294 return readl(pi->sdma_base + SDMA_SDCM) & SDMA_SDCM_TXD;
295}
296
297static inline void
298mpsc_sdma_start_tx(struct mpsc_port_info *pi)
299{
300 struct mpsc_tx_desc *txre, *txre_p;
301
302 /* If tx isn't running & there's a desc ready to go, start it */
303 if (!mpsc_sdma_tx_active(pi)) {
304 txre = (struct mpsc_tx_desc *)(pi->txr +
305 (pi->txr_tail * MPSC_TXRE_SIZE));
306 dma_cache_sync((void *) txre, MPSC_TXRE_SIZE, DMA_FROM_DEVICE);
307#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
308 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
309 invalidate_dcache_range((ulong)txre,
310 (ulong)txre + MPSC_TXRE_SIZE);
311#endif
312
313 if (be32_to_cpu(txre->cmdstat) & SDMA_DESC_CMDSTAT_O) {
314 txre_p = (struct mpsc_tx_desc *)(pi->txr_p +
315 (pi->txr_tail *
316 MPSC_TXRE_SIZE));
317
318 mpsc_sdma_set_tx_ring(pi, txre_p);
319 mpsc_sdma_cmd(pi, SDMA_SDCM_STD | SDMA_SDCM_TXD);
320 }
321 }
322
323 return;
324}
325
326static inline void
327mpsc_sdma_stop(struct mpsc_port_info *pi)
328{
329 pr_debug("mpsc_sdma_stop[%d]: Stopping SDMA\n", pi->port.line);
330
331 /* Abort any SDMA transfers */
332 mpsc_sdma_cmd(pi, 0);
333 mpsc_sdma_cmd(pi, SDMA_SDCM_AR | SDMA_SDCM_AT);
334
335 /* Clear the SDMA current and first TX and RX pointers */
Al Viro2c6e7592005-04-25 18:32:12 -0700336 mpsc_sdma_set_tx_ring(pi, NULL);
337 mpsc_sdma_set_rx_ring(pi, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /* Disable interrupts */
340 mpsc_sdma_intr_mask(pi, 0xf);
341 mpsc_sdma_intr_ack(pi);
342
343 return;
344}
345
346/*
347 ******************************************************************************
348 *
349 * Multi-Protocol Serial Controller Routines (MPSC)
350 *
351 ******************************************************************************
352 */
353
354static void
355mpsc_hw_init(struct mpsc_port_info *pi)
356{
357 u32 v;
358
359 pr_debug("mpsc_hw_init[%d]: Initializing hardware\n", pi->port.line);
360
361 /* Set up clock routing */
362 if (pi->mirror_regs) {
363 v = pi->shared_regs->MPSC_MRR_m;
364 v &= ~0x1c7;
365 pi->shared_regs->MPSC_MRR_m = v;
366 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_MRR);
367
368 v = pi->shared_regs->MPSC_RCRR_m;
369 v = (v & ~0xf0f) | 0x100;
370 pi->shared_regs->MPSC_RCRR_m = v;
371 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
372
373 v = pi->shared_regs->MPSC_TCRR_m;
374 v = (v & ~0xf0f) | 0x100;
375 pi->shared_regs->MPSC_TCRR_m = v;
376 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
377 }
378 else {
379 v = readl(pi->shared_regs->mpsc_routing_base + MPSC_MRR);
380 v &= ~0x1c7;
381 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_MRR);
382
383 v = readl(pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
384 v = (v & ~0xf0f) | 0x100;
385 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_RCRR);
386
387 v = readl(pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
388 v = (v & ~0xf0f) | 0x100;
389 writel(v, pi->shared_regs->mpsc_routing_base + MPSC_TCRR);
390 }
391
392 /* Put MPSC in UART mode & enabel Tx/Rx egines */
393 writel(0x000004c4, pi->mpsc_base + MPSC_MMCRL);
394
395 /* No preamble, 16x divider, low-latency, */
396 writel(0x04400400, pi->mpsc_base + MPSC_MMCRH);
397
398 if (pi->mirror_regs) {
399 pi->MPSC_CHR_1_m = 0;
400 pi->MPSC_CHR_2_m = 0;
401 }
402 writel(0, pi->mpsc_base + MPSC_CHR_1);
403 writel(0, pi->mpsc_base + MPSC_CHR_2);
404 writel(pi->mpsc_max_idle, pi->mpsc_base + MPSC_CHR_3);
405 writel(0, pi->mpsc_base + MPSC_CHR_4);
406 writel(0, pi->mpsc_base + MPSC_CHR_5);
407 writel(0, pi->mpsc_base + MPSC_CHR_6);
408 writel(0, pi->mpsc_base + MPSC_CHR_7);
409 writel(0, pi->mpsc_base + MPSC_CHR_8);
410 writel(0, pi->mpsc_base + MPSC_CHR_9);
411 writel(0, pi->mpsc_base + MPSC_CHR_10);
412
413 return;
414}
415
416static inline void
417mpsc_enter_hunt(struct mpsc_port_info *pi)
418{
419 pr_debug("mpsc_enter_hunt[%d]: Hunting...\n", pi->port.line);
420
421 if (pi->mirror_regs) {
422 writel(pi->MPSC_CHR_2_m | MPSC_CHR_2_EH,
423 pi->mpsc_base + MPSC_CHR_2);
424 /* Erratum prevents reading CHR_2 so just delay for a while */
425 udelay(100);
426 }
427 else {
428 writel(readl(pi->mpsc_base + MPSC_CHR_2) | MPSC_CHR_2_EH,
429 pi->mpsc_base + MPSC_CHR_2);
430
431 while (readl(pi->mpsc_base + MPSC_CHR_2) & MPSC_CHR_2_EH)
432 udelay(10);
433 }
434
435 return;
436}
437
438static inline void
439mpsc_freeze(struct mpsc_port_info *pi)
440{
441 u32 v;
442
443 pr_debug("mpsc_freeze[%d]: Freezing\n", pi->port.line);
444
445 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
446 readl(pi->mpsc_base + MPSC_MPCR);
447 v |= MPSC_MPCR_FRZ;
448
449 if (pi->mirror_regs)
450 pi->MPSC_MPCR_m = v;
451 writel(v, pi->mpsc_base + MPSC_MPCR);
452 return;
453}
454
455static inline void
456mpsc_unfreeze(struct mpsc_port_info *pi)
457{
458 u32 v;
459
460 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
461 readl(pi->mpsc_base + MPSC_MPCR);
462 v &= ~MPSC_MPCR_FRZ;
463
464 if (pi->mirror_regs)
465 pi->MPSC_MPCR_m = v;
466 writel(v, pi->mpsc_base + MPSC_MPCR);
467
468 pr_debug("mpsc_unfreeze[%d]: Unfrozen\n", pi->port.line);
469 return;
470}
471
472static inline void
473mpsc_set_char_length(struct mpsc_port_info *pi, u32 len)
474{
475 u32 v;
476
477 pr_debug("mpsc_set_char_length[%d]: char len: %d\n", pi->port.line,len);
478
479 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
480 readl(pi->mpsc_base + MPSC_MPCR);
481 v = (v & ~(0x3 << 12)) | ((len & 0x3) << 12);
482
483 if (pi->mirror_regs)
484 pi->MPSC_MPCR_m = v;
485 writel(v, pi->mpsc_base + MPSC_MPCR);
486 return;
487}
488
489static inline void
490mpsc_set_stop_bit_length(struct mpsc_port_info *pi, u32 len)
491{
492 u32 v;
493
494 pr_debug("mpsc_set_stop_bit_length[%d]: stop bits: %d\n",
495 pi->port.line, len);
496
497 v = (pi->mirror_regs) ? pi->MPSC_MPCR_m :
498 readl(pi->mpsc_base + MPSC_MPCR);
499
500 v = (v & ~(1 << 14)) | ((len & 0x1) << 14);
501
502 if (pi->mirror_regs)
503 pi->MPSC_MPCR_m = v;
504 writel(v, pi->mpsc_base + MPSC_MPCR);
505 return;
506}
507
508static inline void
509mpsc_set_parity(struct mpsc_port_info *pi, u32 p)
510{
511 u32 v;
512
513 pr_debug("mpsc_set_parity[%d]: parity bits: 0x%x\n", pi->port.line, p);
514
515 v = (pi->mirror_regs) ? pi->MPSC_CHR_2_m :
516 readl(pi->mpsc_base + MPSC_CHR_2);
517
518 p &= 0x3;
519 v = (v & ~0xc000c) | (p << 18) | (p << 2);
520
521 if (pi->mirror_regs)
522 pi->MPSC_CHR_2_m = v;
523 writel(v, pi->mpsc_base + MPSC_CHR_2);
524 return;
525}
526
527/*
528 ******************************************************************************
529 *
530 * Driver Init Routines
531 *
532 ******************************************************************************
533 */
534
535static void
536mpsc_init_hw(struct mpsc_port_info *pi)
537{
538 pr_debug("mpsc_init_hw[%d]: Initializing\n", pi->port.line);
539
540 mpsc_brg_init(pi, pi->brg_clk_src);
541 mpsc_brg_enable(pi);
542 mpsc_sdma_init(pi, dma_get_cache_alignment()); /* burst a cacheline */
543 mpsc_sdma_stop(pi);
544 mpsc_hw_init(pi);
545
546 return;
547}
548
549static int
550mpsc_alloc_ring_mem(struct mpsc_port_info *pi)
551{
552 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 pr_debug("mpsc_alloc_ring_mem[%d]: Allocating ring mem\n",
555 pi->port.line);
556
557 if (!pi->dma_region) {
558 if (!dma_supported(pi->port.dev, 0xffffffff)) {
559 printk(KERN_ERR "MPSC: Inadequate DMA support\n");
560 rc = -ENXIO;
561 }
562 else if ((pi->dma_region = dma_alloc_noncoherent(pi->port.dev,
563 MPSC_DMA_ALLOC_SIZE, &pi->dma_region_p, GFP_KERNEL))
564 == NULL) {
565
566 printk(KERN_ERR "MPSC: Can't alloc Desc region\n");
567 rc = -ENOMEM;
568 }
569 }
570
571 return rc;
572}
573
574static void
575mpsc_free_ring_mem(struct mpsc_port_info *pi)
576{
577 pr_debug("mpsc_free_ring_mem[%d]: Freeing ring mem\n", pi->port.line);
578
579 if (pi->dma_region) {
580 dma_free_noncoherent(pi->port.dev, MPSC_DMA_ALLOC_SIZE,
581 pi->dma_region, pi->dma_region_p);
582 pi->dma_region = NULL;
583 pi->dma_region_p = (dma_addr_t) NULL;
584 }
585
586 return;
587}
588
589static void
590mpsc_init_rings(struct mpsc_port_info *pi)
591{
592 struct mpsc_rx_desc *rxre;
593 struct mpsc_tx_desc *txre;
594 dma_addr_t dp, dp_p;
595 u8 *bp, *bp_p;
596 int i;
597
598 pr_debug("mpsc_init_rings[%d]: Initializing rings\n", pi->port.line);
599
600 BUG_ON(pi->dma_region == NULL);
601
602 memset(pi->dma_region, 0, MPSC_DMA_ALLOC_SIZE);
603
604 /*
605 * Descriptors & buffers are multiples of cacheline size and must be
606 * cacheline aligned.
607 */
608 dp = ALIGN((u32) pi->dma_region, dma_get_cache_alignment());
609 dp_p = ALIGN((u32) pi->dma_region_p, dma_get_cache_alignment());
610
611 /*
612 * Partition dma region into rx ring descriptor, rx buffers,
613 * tx ring descriptors, and tx buffers.
614 */
615 pi->rxr = dp;
616 pi->rxr_p = dp_p;
617 dp += MPSC_RXR_SIZE;
618 dp_p += MPSC_RXR_SIZE;
619
620 pi->rxb = (u8 *) dp;
621 pi->rxb_p = (u8 *) dp_p;
622 dp += MPSC_RXB_SIZE;
623 dp_p += MPSC_RXB_SIZE;
624
625 pi->rxr_posn = 0;
626
627 pi->txr = dp;
628 pi->txr_p = dp_p;
629 dp += MPSC_TXR_SIZE;
630 dp_p += MPSC_TXR_SIZE;
631
632 pi->txb = (u8 *) dp;
633 pi->txb_p = (u8 *) dp_p;
634
635 pi->txr_head = 0;
636 pi->txr_tail = 0;
637
638 /* Init rx ring descriptors */
639 dp = pi->rxr;
640 dp_p = pi->rxr_p;
641 bp = pi->rxb;
642 bp_p = pi->rxb_p;
643
644 for (i = 0; i < MPSC_RXR_ENTRIES; i++) {
645 rxre = (struct mpsc_rx_desc *)dp;
646
647 rxre->bufsize = cpu_to_be16(MPSC_RXBE_SIZE);
648 rxre->bytecnt = cpu_to_be16(0);
649 rxre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O |
650 SDMA_DESC_CMDSTAT_EI |
651 SDMA_DESC_CMDSTAT_F |
652 SDMA_DESC_CMDSTAT_L);
653 rxre->link = cpu_to_be32(dp_p + MPSC_RXRE_SIZE);
654 rxre->buf_ptr = cpu_to_be32(bp_p);
655
656 dp += MPSC_RXRE_SIZE;
657 dp_p += MPSC_RXRE_SIZE;
658 bp += MPSC_RXBE_SIZE;
659 bp_p += MPSC_RXBE_SIZE;
660 }
661 rxre->link = cpu_to_be32(pi->rxr_p); /* Wrap last back to first */
662
663 /* Init tx ring descriptors */
664 dp = pi->txr;
665 dp_p = pi->txr_p;
666 bp = pi->txb;
667 bp_p = pi->txb_p;
668
669 for (i = 0; i < MPSC_TXR_ENTRIES; i++) {
670 txre = (struct mpsc_tx_desc *)dp;
671
672 txre->link = cpu_to_be32(dp_p + MPSC_TXRE_SIZE);
673 txre->buf_ptr = cpu_to_be32(bp_p);
674
675 dp += MPSC_TXRE_SIZE;
676 dp_p += MPSC_TXRE_SIZE;
677 bp += MPSC_TXBE_SIZE;
678 bp_p += MPSC_TXBE_SIZE;
679 }
680 txre->link = cpu_to_be32(pi->txr_p); /* Wrap last back to first */
681
682 dma_cache_sync((void *) pi->dma_region, MPSC_DMA_ALLOC_SIZE,
683 DMA_BIDIRECTIONAL);
684#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
685 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
686 flush_dcache_range((ulong)pi->dma_region,
687 (ulong)pi->dma_region + MPSC_DMA_ALLOC_SIZE);
688#endif
689
690 return;
691}
692
693static void
694mpsc_uninit_rings(struct mpsc_port_info *pi)
695{
696 pr_debug("mpsc_uninit_rings[%d]: Uninitializing rings\n",pi->port.line);
697
698 BUG_ON(pi->dma_region == NULL);
699
700 pi->rxr = 0;
701 pi->rxr_p = 0;
702 pi->rxb = NULL;
703 pi->rxb_p = NULL;
704 pi->rxr_posn = 0;
705
706 pi->txr = 0;
707 pi->txr_p = 0;
708 pi->txb = NULL;
709 pi->txb_p = NULL;
710 pi->txr_head = 0;
711 pi->txr_tail = 0;
712
713 return;
714}
715
716static int
717mpsc_make_ready(struct mpsc_port_info *pi)
718{
719 int rc;
720
721 pr_debug("mpsc_make_ready[%d]: Making cltr ready\n", pi->port.line);
722
723 if (!pi->ready) {
724 mpsc_init_hw(pi);
725 if ((rc = mpsc_alloc_ring_mem(pi)))
726 return rc;
727 mpsc_init_rings(pi);
728 pi->ready = 1;
729 }
730
731 return 0;
732}
733
734/*
735 ******************************************************************************
736 *
737 * Interrupt Handling Routines
738 *
739 ******************************************************************************
740 */
741
742static inline int
743mpsc_rx_intr(struct mpsc_port_info *pi, struct pt_regs *regs)
744{
745 struct mpsc_rx_desc *rxre;
746 struct tty_struct *tty = pi->port.info->tty;
747 u32 cmdstat, bytes_in, i;
748 int rc = 0;
749 u8 *bp;
750 char flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 pr_debug("mpsc_rx_intr[%d]: Handling Rx intr\n", pi->port.line);
753
754 rxre = (struct mpsc_rx_desc *)(pi->rxr + (pi->rxr_posn*MPSC_RXRE_SIZE));
755
756 dma_cache_sync((void *)rxre, MPSC_RXRE_SIZE, DMA_FROM_DEVICE);
757#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
758 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
759 invalidate_dcache_range((ulong)rxre,
760 (ulong)rxre + MPSC_RXRE_SIZE);
761#endif
762
763 /*
764 * Loop through Rx descriptors handling ones that have been completed.
765 */
766 while (!((cmdstat = be32_to_cpu(rxre->cmdstat)) & SDMA_DESC_CMDSTAT_O)){
767 bytes_in = be16_to_cpu(rxre->bytecnt);
768
769 /* Following use of tty struct directly is deprecated */
770 if (unlikely((tty->flip.count + bytes_in) >= TTY_FLIPBUF_SIZE)){
771 if (tty->low_latency)
772 tty_flip_buffer_push(tty);
773 /*
774 * If this failed then we will throw awa the bytes
775 * but mst do so to clear interrupts.
776 */
777 }
778
779 bp = pi->rxb + (pi->rxr_posn * MPSC_RXBE_SIZE);
780 dma_cache_sync((void *) bp, MPSC_RXBE_SIZE, DMA_FROM_DEVICE);
781#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
782 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
783 invalidate_dcache_range((ulong)bp,
784 (ulong)bp + MPSC_RXBE_SIZE);
785#endif
786
787 /*
788 * Other than for parity error, the manual provides little
789 * info on what data will be in a frame flagged by any of
790 * these errors. For parity error, it is the last byte in
791 * the buffer that had the error. As for the rest, I guess
792 * we'll assume there is no data in the buffer.
793 * If there is...it gets lost.
794 */
795 if (unlikely(cmdstat & (SDMA_DESC_CMDSTAT_BR |
796 SDMA_DESC_CMDSTAT_FR | SDMA_DESC_CMDSTAT_OR))) {
797
798 pi->port.icount.rx++;
799
800 if (cmdstat & SDMA_DESC_CMDSTAT_BR) { /* Break */
801 pi->port.icount.brk++;
802
803 if (uart_handle_break(&pi->port))
804 goto next_frame;
805 }
806 else if (cmdstat & SDMA_DESC_CMDSTAT_FR)/* Framing */
807 pi->port.icount.frame++;
808 else if (cmdstat & SDMA_DESC_CMDSTAT_OR) /* Overrun */
809 pi->port.icount.overrun++;
810
811 cmdstat &= pi->port.read_status_mask;
812
813 if (cmdstat & SDMA_DESC_CMDSTAT_BR)
814 flag = TTY_BREAK;
815 else if (cmdstat & SDMA_DESC_CMDSTAT_FR)
816 flag = TTY_FRAME;
817 else if (cmdstat & SDMA_DESC_CMDSTAT_OR)
818 flag = TTY_OVERRUN;
819 else if (cmdstat & SDMA_DESC_CMDSTAT_PE)
820 flag = TTY_PARITY;
821 }
822
823 if (uart_handle_sysrq_char(&pi->port, *bp, regs)) {
824 bp++;
825 bytes_in--;
826 goto next_frame;
827 }
828
829 if ((unlikely(cmdstat & (SDMA_DESC_CMDSTAT_BR |
830 SDMA_DESC_CMDSTAT_FR | SDMA_DESC_CMDSTAT_OR))) &&
831 !(cmdstat & pi->port.ignore_status_mask))
832
833 tty_insert_flip_char(tty, *bp, flag);
834 else {
835 for (i=0; i<bytes_in; i++)
836 tty_insert_flip_char(tty, *bp++, TTY_NORMAL);
837
838 pi->port.icount.rx += bytes_in;
839 }
840
841next_frame:
842 rxre->bytecnt = cpu_to_be16(0);
843 wmb();
844 rxre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O |
845 SDMA_DESC_CMDSTAT_EI |
846 SDMA_DESC_CMDSTAT_F |
847 SDMA_DESC_CMDSTAT_L);
848 wmb();
849 dma_cache_sync((void *)rxre, MPSC_RXRE_SIZE, DMA_BIDIRECTIONAL);
850#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
851 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
852 flush_dcache_range((ulong)rxre,
853 (ulong)rxre + MPSC_RXRE_SIZE);
854#endif
855
856 /* Advance to next descriptor */
857 pi->rxr_posn = (pi->rxr_posn + 1) & (MPSC_RXR_ENTRIES - 1);
858 rxre = (struct mpsc_rx_desc *)(pi->rxr +
859 (pi->rxr_posn * MPSC_RXRE_SIZE));
860 dma_cache_sync((void *)rxre, MPSC_RXRE_SIZE, DMA_FROM_DEVICE);
861#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
862 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
863 invalidate_dcache_range((ulong)rxre,
864 (ulong)rxre + MPSC_RXRE_SIZE);
865#endif
866
867 rc = 1;
868 }
869
870 /* Restart rx engine, if its stopped */
871 if ((readl(pi->sdma_base + SDMA_SDCM) & SDMA_SDCM_ERD) == 0)
872 mpsc_start_rx(pi);
873
874 tty_flip_buffer_push(tty);
875 return rc;
876}
877
878static inline void
879mpsc_setup_tx_desc(struct mpsc_port_info *pi, u32 count, u32 intr)
880{
881 struct mpsc_tx_desc *txre;
882
883 txre = (struct mpsc_tx_desc *)(pi->txr +
884 (pi->txr_head * MPSC_TXRE_SIZE));
885
886 txre->bytecnt = cpu_to_be16(count);
887 txre->shadow = txre->bytecnt;
888 wmb(); /* ensure cmdstat is last field updated */
889 txre->cmdstat = cpu_to_be32(SDMA_DESC_CMDSTAT_O | SDMA_DESC_CMDSTAT_F |
890 SDMA_DESC_CMDSTAT_L | ((intr) ?
891 SDMA_DESC_CMDSTAT_EI
892 : 0));
893 wmb();
894 dma_cache_sync((void *) txre, MPSC_TXRE_SIZE, DMA_BIDIRECTIONAL);
895#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
896 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
897 flush_dcache_range((ulong)txre,
898 (ulong)txre + MPSC_TXRE_SIZE);
899#endif
900
901 return;
902}
903
904static inline void
905mpsc_copy_tx_data(struct mpsc_port_info *pi)
906{
907 struct circ_buf *xmit = &pi->port.info->xmit;
908 u8 *bp;
909 u32 i;
910
911 /* Make sure the desc ring isn't full */
912 while (CIRC_CNT(pi->txr_head, pi->txr_tail, MPSC_TXR_ENTRIES) <
913 (MPSC_TXR_ENTRIES - 1)) {
914 if (pi->port.x_char) {
915 /*
916 * Ideally, we should use the TCS field in
917 * CHR_1 to put the x_char out immediately but
918 * errata prevents us from being able to read
919 * CHR_2 to know that its safe to write to
920 * CHR_1. Instead, just put it in-band with
921 * all the other Tx data.
922 */
923 bp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
924 *bp = pi->port.x_char;
925 pi->port.x_char = 0;
926 i = 1;
927 }
928 else if (!uart_circ_empty(xmit) && !uart_tx_stopped(&pi->port)){
929 i = min((u32) MPSC_TXBE_SIZE,
930 (u32) uart_circ_chars_pending(xmit));
931 i = min(i, (u32) CIRC_CNT_TO_END(xmit->head, xmit->tail,
932 UART_XMIT_SIZE));
933 bp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
934 memcpy(bp, &xmit->buf[xmit->tail], i);
935 xmit->tail = (xmit->tail + i) & (UART_XMIT_SIZE - 1);
936
937 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
938 uart_write_wakeup(&pi->port);
939 }
940 else /* All tx data copied into ring bufs */
941 return;
942
943 dma_cache_sync((void *) bp, MPSC_TXBE_SIZE, DMA_BIDIRECTIONAL);
944#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
945 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
946 flush_dcache_range((ulong)bp,
947 (ulong)bp + MPSC_TXBE_SIZE);
948#endif
949 mpsc_setup_tx_desc(pi, i, 1);
950
951 /* Advance to next descriptor */
952 pi->txr_head = (pi->txr_head + 1) & (MPSC_TXR_ENTRIES - 1);
953 }
954
955 return;
956}
957
958static inline int
959mpsc_tx_intr(struct mpsc_port_info *pi)
960{
961 struct mpsc_tx_desc *txre;
962 int rc = 0;
963
964 if (!mpsc_sdma_tx_active(pi)) {
965 txre = (struct mpsc_tx_desc *)(pi->txr +
966 (pi->txr_tail * MPSC_TXRE_SIZE));
967
968 dma_cache_sync((void *) txre, MPSC_TXRE_SIZE, DMA_FROM_DEVICE);
969#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
970 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
971 invalidate_dcache_range((ulong)txre,
972 (ulong)txre + MPSC_TXRE_SIZE);
973#endif
974
975 while (!(be32_to_cpu(txre->cmdstat) & SDMA_DESC_CMDSTAT_O)) {
976 rc = 1;
977 pi->port.icount.tx += be16_to_cpu(txre->bytecnt);
978 pi->txr_tail = (pi->txr_tail+1) & (MPSC_TXR_ENTRIES-1);
979
980 /* If no more data to tx, fall out of loop */
981 if (pi->txr_head == pi->txr_tail)
982 break;
983
984 txre = (struct mpsc_tx_desc *)(pi->txr +
985 (pi->txr_tail * MPSC_TXRE_SIZE));
986 dma_cache_sync((void *) txre, MPSC_TXRE_SIZE,
987 DMA_FROM_DEVICE);
988#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
989 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
990 invalidate_dcache_range((ulong)txre,
991 (ulong)txre + MPSC_TXRE_SIZE);
992#endif
993 }
994
995 mpsc_copy_tx_data(pi);
996 mpsc_sdma_start_tx(pi); /* start next desc if ready */
997 }
998
999 return rc;
1000}
1001
1002/*
1003 * This is the driver's interrupt handler. To avoid a race, we first clear
1004 * the interrupt, then handle any completed Rx/Tx descriptors. When done
1005 * handling those descriptors, we restart the Rx/Tx engines if they're stopped.
1006 */
1007static irqreturn_t
1008mpsc_sdma_intr(int irq, void *dev_id, struct pt_regs *regs)
1009{
1010 struct mpsc_port_info *pi = dev_id;
1011 ulong iflags;
1012 int rc = IRQ_NONE;
1013
1014 pr_debug("mpsc_sdma_intr[%d]: SDMA Interrupt Received\n",pi->port.line);
1015
1016 spin_lock_irqsave(&pi->port.lock, iflags);
1017 mpsc_sdma_intr_ack(pi);
1018 if (mpsc_rx_intr(pi, regs))
1019 rc = IRQ_HANDLED;
1020 if (mpsc_tx_intr(pi))
1021 rc = IRQ_HANDLED;
1022 spin_unlock_irqrestore(&pi->port.lock, iflags);
1023
1024 pr_debug("mpsc_sdma_intr[%d]: SDMA Interrupt Handled\n", pi->port.line);
1025 return rc;
1026}
1027
1028/*
1029 ******************************************************************************
1030 *
1031 * serial_core.c Interface routines
1032 *
1033 ******************************************************************************
1034 */
1035static uint
1036mpsc_tx_empty(struct uart_port *port)
1037{
1038 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1039 ulong iflags;
1040 uint rc;
1041
1042 spin_lock_irqsave(&pi->port.lock, iflags);
1043 rc = mpsc_sdma_tx_active(pi) ? 0 : TIOCSER_TEMT;
1044 spin_unlock_irqrestore(&pi->port.lock, iflags);
1045
1046 return rc;
1047}
1048
1049static void
1050mpsc_set_mctrl(struct uart_port *port, uint mctrl)
1051{
1052 /* Have no way to set modem control lines AFAICT */
1053 return;
1054}
1055
1056static uint
1057mpsc_get_mctrl(struct uart_port *port)
1058{
1059 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1060 u32 mflags, status;
1061 ulong iflags;
1062
1063 spin_lock_irqsave(&pi->port.lock, iflags);
1064 status = (pi->mirror_regs) ? pi->MPSC_CHR_10_m :
1065 readl(pi->mpsc_base + MPSC_CHR_10);
1066 spin_unlock_irqrestore(&pi->port.lock, iflags);
1067
1068 mflags = 0;
1069 if (status & 0x1)
1070 mflags |= TIOCM_CTS;
1071 if (status & 0x2)
1072 mflags |= TIOCM_CAR;
1073
1074 return mflags | TIOCM_DSR; /* No way to tell if DSR asserted */
1075}
1076
1077static void
1078mpsc_stop_tx(struct uart_port *port, uint tty_start)
1079{
1080 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1081
1082 pr_debug("mpsc_stop_tx[%d]: tty_start: %d\n", port->line, tty_start);
1083
1084 mpsc_freeze(pi);
1085 return;
1086}
1087
1088static void
1089mpsc_start_tx(struct uart_port *port, uint tty_start)
1090{
1091 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1092
1093 mpsc_unfreeze(pi);
1094 mpsc_copy_tx_data(pi);
1095 mpsc_sdma_start_tx(pi);
1096
1097 pr_debug("mpsc_start_tx[%d]: tty_start: %d\n", port->line, tty_start);
1098 return;
1099}
1100
1101static void
1102mpsc_start_rx(struct mpsc_port_info *pi)
1103{
1104 pr_debug("mpsc_start_rx[%d]: Starting...\n", pi->port.line);
1105
1106 if (pi->rcv_data) {
1107 mpsc_enter_hunt(pi);
1108 mpsc_sdma_cmd(pi, SDMA_SDCM_ERD);
1109 }
1110 return;
1111}
1112
1113static void
1114mpsc_stop_rx(struct uart_port *port)
1115{
1116 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1117
1118 pr_debug("mpsc_stop_rx[%d]: Stopping...\n", port->line);
1119
1120 mpsc_sdma_cmd(pi, SDMA_SDCM_AR);
1121 return;
1122}
1123
1124static void
1125mpsc_enable_ms(struct uart_port *port)
1126{
1127 return; /* Not supported */
1128}
1129
1130static void
1131mpsc_break_ctl(struct uart_port *port, int ctl)
1132{
1133 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1134 ulong flags;
1135 u32 v;
1136
1137 v = ctl ? 0x00ff0000 : 0;
1138
1139 spin_lock_irqsave(&pi->port.lock, flags);
1140 if (pi->mirror_regs)
1141 pi->MPSC_CHR_1_m = v;
1142 writel(v, pi->mpsc_base + MPSC_CHR_1);
1143 spin_unlock_irqrestore(&pi->port.lock, flags);
1144
1145 return;
1146}
1147
1148static int
1149mpsc_startup(struct uart_port *port)
1150{
1151 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1152 u32 flag = 0;
1153 int rc;
1154
1155 pr_debug("mpsc_startup[%d]: Starting up MPSC, irq: %d\n",
1156 port->line, pi->port.irq);
1157
1158 if ((rc = mpsc_make_ready(pi)) == 0) {
1159 /* Setup IRQ handler */
1160 mpsc_sdma_intr_ack(pi);
1161
1162 /* If irq's are shared, need to set flag */
1163 if (mpsc_ports[0].port.irq == mpsc_ports[1].port.irq)
1164 flag = SA_SHIRQ;
1165
1166 if (request_irq(pi->port.irq, mpsc_sdma_intr, flag,
1167 "mpsc/sdma", pi))
1168 printk(KERN_ERR "MPSC: Can't get SDMA IRQ %d\n",
1169 pi->port.irq);
1170
1171 mpsc_sdma_intr_unmask(pi, 0xf);
1172 mpsc_sdma_set_rx_ring(pi, (struct mpsc_rx_desc *)(pi->rxr_p +
1173 (pi->rxr_posn * MPSC_RXRE_SIZE)));
1174 }
1175
1176 return rc;
1177}
1178
1179static void
1180mpsc_shutdown(struct uart_port *port)
1181{
1182 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 pr_debug("mpsc_shutdown[%d]: Shutting down MPSC\n", port->line);
1185
1186 mpsc_sdma_stop(pi);
1187 free_irq(pi->port.irq, pi);
1188 return;
1189}
1190
1191static void
1192mpsc_set_termios(struct uart_port *port, struct termios *termios,
1193 struct termios *old)
1194{
1195 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1196 u32 baud;
1197 ulong flags;
1198 u32 chr_bits, stop_bits, par;
1199
1200 pi->c_iflag = termios->c_iflag;
1201 pi->c_cflag = termios->c_cflag;
1202
1203 switch (termios->c_cflag & CSIZE) {
1204 case CS5:
1205 chr_bits = MPSC_MPCR_CL_5;
1206 break;
1207 case CS6:
1208 chr_bits = MPSC_MPCR_CL_6;
1209 break;
1210 case CS7:
1211 chr_bits = MPSC_MPCR_CL_7;
1212 break;
1213 case CS8:
1214 default:
1215 chr_bits = MPSC_MPCR_CL_8;
1216 break;
1217 }
1218
1219 if (termios->c_cflag & CSTOPB)
1220 stop_bits = MPSC_MPCR_SBL_2;
1221 else
1222 stop_bits = MPSC_MPCR_SBL_1;
1223
1224 par = MPSC_CHR_2_PAR_EVEN;
1225 if (termios->c_cflag & PARENB)
1226 if (termios->c_cflag & PARODD)
1227 par = MPSC_CHR_2_PAR_ODD;
1228#ifdef CMSPAR
1229 if (termios->c_cflag & CMSPAR) {
1230 if (termios->c_cflag & PARODD)
1231 par = MPSC_CHR_2_PAR_MARK;
1232 else
1233 par = MPSC_CHR_2_PAR_SPACE;
1234 }
1235#endif
1236
1237 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk);
1238
1239 spin_lock_irqsave(&pi->port.lock, flags);
1240
1241 uart_update_timeout(port, termios->c_cflag, baud);
1242
1243 mpsc_set_char_length(pi, chr_bits);
1244 mpsc_set_stop_bit_length(pi, stop_bits);
1245 mpsc_set_parity(pi, par);
1246 mpsc_set_baudrate(pi, baud);
1247
1248 /* Characters/events to read */
1249 pi->rcv_data = 1;
1250 pi->port.read_status_mask = SDMA_DESC_CMDSTAT_OR;
1251
1252 if (termios->c_iflag & INPCK)
1253 pi->port.read_status_mask |= SDMA_DESC_CMDSTAT_PE |
1254 SDMA_DESC_CMDSTAT_FR;
1255
1256 if (termios->c_iflag & (BRKINT | PARMRK))
1257 pi->port.read_status_mask |= SDMA_DESC_CMDSTAT_BR;
1258
1259 /* Characters/events to ignore */
1260 pi->port.ignore_status_mask = 0;
1261
1262 if (termios->c_iflag & IGNPAR)
1263 pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_PE |
1264 SDMA_DESC_CMDSTAT_FR;
1265
1266 if (termios->c_iflag & IGNBRK) {
1267 pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_BR;
1268
1269 if (termios->c_iflag & IGNPAR)
1270 pi->port.ignore_status_mask |= SDMA_DESC_CMDSTAT_OR;
1271 }
1272
1273 /* Ignore all chars if CREAD not set */
1274 if (!(termios->c_cflag & CREAD))
1275 pi->rcv_data = 0;
1276 else
1277 mpsc_start_rx(pi);
1278
1279 spin_unlock_irqrestore(&pi->port.lock, flags);
1280 return;
1281}
1282
1283static const char *
1284mpsc_type(struct uart_port *port)
1285{
1286 pr_debug("mpsc_type[%d]: port type: %s\n", port->line,MPSC_DRIVER_NAME);
1287 return MPSC_DRIVER_NAME;
1288}
1289
1290static int
1291mpsc_request_port(struct uart_port *port)
1292{
1293 /* Should make chip/platform specific call */
1294 return 0;
1295}
1296
1297static void
1298mpsc_release_port(struct uart_port *port)
1299{
1300 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1301
1302 if (pi->ready) {
1303 mpsc_uninit_rings(pi);
1304 mpsc_free_ring_mem(pi);
1305 pi->ready = 0;
1306 }
1307
1308 return;
1309}
1310
1311static void
1312mpsc_config_port(struct uart_port *port, int flags)
1313{
1314 return;
1315}
1316
1317static int
1318mpsc_verify_port(struct uart_port *port, struct serial_struct *ser)
1319{
1320 struct mpsc_port_info *pi = (struct mpsc_port_info *)port;
1321 int rc = 0;
1322
1323 pr_debug("mpsc_verify_port[%d]: Verifying port data\n", pi->port.line);
1324
1325 if (ser->type != PORT_UNKNOWN && ser->type != PORT_MPSC)
1326 rc = -EINVAL;
1327 else if (pi->port.irq != ser->irq)
1328 rc = -EINVAL;
1329 else if (ser->io_type != SERIAL_IO_MEM)
1330 rc = -EINVAL;
1331 else if (pi->port.uartclk / 16 != ser->baud_base) /* Not sure */
1332 rc = -EINVAL;
1333 else if ((void *)pi->port.mapbase != ser->iomem_base)
1334 rc = -EINVAL;
1335 else if (pi->port.iobase != ser->port)
1336 rc = -EINVAL;
1337 else if (ser->hub6 != 0)
1338 rc = -EINVAL;
1339
1340 return rc;
1341}
1342
1343static struct uart_ops mpsc_pops = {
1344 .tx_empty = mpsc_tx_empty,
1345 .set_mctrl = mpsc_set_mctrl,
1346 .get_mctrl = mpsc_get_mctrl,
1347 .stop_tx = mpsc_stop_tx,
1348 .start_tx = mpsc_start_tx,
1349 .stop_rx = mpsc_stop_rx,
1350 .enable_ms = mpsc_enable_ms,
1351 .break_ctl = mpsc_break_ctl,
1352 .startup = mpsc_startup,
1353 .shutdown = mpsc_shutdown,
1354 .set_termios = mpsc_set_termios,
1355 .type = mpsc_type,
1356 .release_port = mpsc_release_port,
1357 .request_port = mpsc_request_port,
1358 .config_port = mpsc_config_port,
1359 .verify_port = mpsc_verify_port,
1360};
1361
1362/*
1363 ******************************************************************************
1364 *
1365 * Console Interface Routines
1366 *
1367 ******************************************************************************
1368 */
1369
1370#ifdef CONFIG_SERIAL_MPSC_CONSOLE
1371static void
1372mpsc_console_write(struct console *co, const char *s, uint count)
1373{
1374 struct mpsc_port_info *pi = &mpsc_ports[co->index];
1375 u8 *bp, *dp, add_cr = 0;
1376 int i;
1377
1378 while (mpsc_sdma_tx_active(pi))
1379 udelay(100);
1380
1381 while (count > 0) {
1382 bp = dp = pi->txb + (pi->txr_head * MPSC_TXBE_SIZE);
1383
1384 for (i = 0; i < MPSC_TXBE_SIZE; i++) {
1385 if (count == 0)
1386 break;
1387
1388 if (add_cr) {
1389 *(dp++) = '\r';
1390 add_cr = 0;
1391 }
1392 else {
1393 *(dp++) = *s;
1394
1395 if (*(s++) == '\n') { /* add '\r' after '\n' */
1396 add_cr = 1;
1397 count++;
1398 }
1399 }
1400
1401 count--;
1402 }
1403
1404 dma_cache_sync((void *) bp, MPSC_TXBE_SIZE, DMA_BIDIRECTIONAL);
1405#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
1406 if (pi->cache_mgmt) /* GT642[46]0 Res #COMM-2 */
1407 flush_dcache_range((ulong)bp,
1408 (ulong)bp + MPSC_TXBE_SIZE);
1409#endif
1410 mpsc_setup_tx_desc(pi, i, 0);
1411 pi->txr_head = (pi->txr_head + 1) & (MPSC_TXR_ENTRIES - 1);
1412 mpsc_sdma_start_tx(pi);
1413
1414 while (mpsc_sdma_tx_active(pi))
1415 udelay(100);
1416
1417 pi->txr_tail = (pi->txr_tail + 1) & (MPSC_TXR_ENTRIES - 1);
1418 }
1419
1420 return;
1421}
1422
1423static int __init
1424mpsc_console_setup(struct console *co, char *options)
1425{
1426 struct mpsc_port_info *pi;
1427 int baud, bits, parity, flow;
1428
1429 pr_debug("mpsc_console_setup[%d]: options: %s\n", co->index, options);
1430
1431 if (co->index >= MPSC_NUM_CTLRS)
1432 co->index = 0;
1433
1434 pi = &mpsc_ports[co->index];
1435
1436 baud = pi->default_baud;
1437 bits = pi->default_bits;
1438 parity = pi->default_parity;
1439 flow = pi->default_flow;
1440
1441 if (!pi->port.ops)
1442 return -ENODEV;
1443
1444 spin_lock_init(&pi->port.lock); /* Temporary fix--copied from 8250.c */
1445
1446 if (options)
1447 uart_parse_options(options, &baud, &parity, &bits, &flow);
1448
1449 return uart_set_options(&pi->port, co, baud, parity, bits, flow);
1450}
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452static struct console mpsc_console = {
1453 .name = MPSC_DEV_NAME,
1454 .write = mpsc_console_write,
1455 .device = uart_console_device,
1456 .setup = mpsc_console_setup,
1457 .flags = CON_PRINTBUFFER,
1458 .index = -1,
1459 .data = &mpsc_reg,
1460};
1461
1462static int __init
1463mpsc_late_console_init(void)
1464{
1465 pr_debug("mpsc_late_console_init: Enter\n");
1466
1467 if (!(mpsc_console.flags & CON_ENABLED))
1468 register_console(&mpsc_console);
1469 return 0;
1470}
1471
1472late_initcall(mpsc_late_console_init);
1473
1474#define MPSC_CONSOLE &mpsc_console
1475#else
1476#define MPSC_CONSOLE NULL
1477#endif
1478/*
1479 ******************************************************************************
1480 *
1481 * Dummy Platform Driver to extract & map shared register regions
1482 *
1483 ******************************************************************************
1484 */
1485static void
1486mpsc_resource_err(char *s)
1487{
1488 printk(KERN_WARNING "MPSC: Platform device resource error in %s\n", s);
1489 return;
1490}
1491
1492static int
1493mpsc_shared_map_regs(struct platform_device *pd)
1494{
1495 struct resource *r;
1496
1497 if ((r = platform_get_resource(pd, IORESOURCE_MEM,
1498 MPSC_ROUTING_BASE_ORDER)) && request_mem_region(r->start,
1499 MPSC_ROUTING_REG_BLOCK_SIZE, "mpsc_routing_regs")) {
1500
1501 mpsc_shared_regs.mpsc_routing_base = ioremap(r->start,
1502 MPSC_ROUTING_REG_BLOCK_SIZE);
1503 mpsc_shared_regs.mpsc_routing_base_p = r->start;
1504 }
1505 else {
1506 mpsc_resource_err("MPSC routing base");
1507 return -ENOMEM;
1508 }
1509
1510 if ((r = platform_get_resource(pd, IORESOURCE_MEM,
1511 MPSC_SDMA_INTR_BASE_ORDER)) && request_mem_region(r->start,
1512 MPSC_SDMA_INTR_REG_BLOCK_SIZE, "sdma_intr_regs")) {
1513
1514 mpsc_shared_regs.sdma_intr_base = ioremap(r->start,
1515 MPSC_SDMA_INTR_REG_BLOCK_SIZE);
1516 mpsc_shared_regs.sdma_intr_base_p = r->start;
1517 }
1518 else {
1519 iounmap(mpsc_shared_regs.mpsc_routing_base);
1520 release_mem_region(mpsc_shared_regs.mpsc_routing_base_p,
1521 MPSC_ROUTING_REG_BLOCK_SIZE);
1522 mpsc_resource_err("SDMA intr base");
1523 return -ENOMEM;
1524 }
1525
1526 return 0;
1527}
1528
1529static void
1530mpsc_shared_unmap_regs(void)
1531{
1532 if (!mpsc_shared_regs.mpsc_routing_base) {
1533 iounmap(mpsc_shared_regs.mpsc_routing_base);
1534 release_mem_region(mpsc_shared_regs.mpsc_routing_base_p,
1535 MPSC_ROUTING_REG_BLOCK_SIZE);
1536 }
1537 if (!mpsc_shared_regs.sdma_intr_base) {
1538 iounmap(mpsc_shared_regs.sdma_intr_base);
1539 release_mem_region(mpsc_shared_regs.sdma_intr_base_p,
1540 MPSC_SDMA_INTR_REG_BLOCK_SIZE);
1541 }
1542
Al Viro2c6e7592005-04-25 18:32:12 -07001543 mpsc_shared_regs.mpsc_routing_base = NULL;
1544 mpsc_shared_regs.sdma_intr_base = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 mpsc_shared_regs.mpsc_routing_base_p = 0;
1547 mpsc_shared_regs.sdma_intr_base_p = 0;
1548
1549 return;
1550}
1551
1552static int
1553mpsc_shared_drv_probe(struct device *dev)
1554{
1555 struct platform_device *pd = to_platform_device(dev);
1556 struct mpsc_shared_pdata *pdata;
1557 int rc = -ENODEV;
1558
1559 if (pd->id == 0) {
1560 if (!(rc = mpsc_shared_map_regs(pd))) {
1561 pdata = (struct mpsc_shared_pdata *)dev->platform_data;
1562
1563 mpsc_shared_regs.MPSC_MRR_m = pdata->mrr_val;
1564 mpsc_shared_regs.MPSC_RCRR_m= pdata->rcrr_val;
1565 mpsc_shared_regs.MPSC_TCRR_m= pdata->tcrr_val;
1566 mpsc_shared_regs.SDMA_INTR_CAUSE_m =
1567 pdata->intr_cause_val;
1568 mpsc_shared_regs.SDMA_INTR_MASK_m =
1569 pdata->intr_mask_val;
1570
1571 rc = 0;
1572 }
1573 }
1574
1575 return rc;
1576}
1577
1578static int
1579mpsc_shared_drv_remove(struct device *dev)
1580{
1581 struct platform_device *pd = to_platform_device(dev);
1582 int rc = -ENODEV;
1583
1584 if (pd->id == 0) {
1585 mpsc_shared_unmap_regs();
1586 mpsc_shared_regs.MPSC_MRR_m = 0;
1587 mpsc_shared_regs.MPSC_RCRR_m = 0;
1588 mpsc_shared_regs.MPSC_TCRR_m = 0;
1589 mpsc_shared_regs.SDMA_INTR_CAUSE_m = 0;
1590 mpsc_shared_regs.SDMA_INTR_MASK_m = 0;
1591 rc = 0;
1592 }
1593
1594 return rc;
1595}
1596
1597static struct device_driver mpsc_shared_driver = {
1598 .name = MPSC_SHARED_NAME,
1599 .bus = &platform_bus_type,
1600 .probe = mpsc_shared_drv_probe,
1601 .remove = mpsc_shared_drv_remove,
1602};
1603
1604/*
1605 ******************************************************************************
1606 *
1607 * Driver Interface Routines
1608 *
1609 ******************************************************************************
1610 */
1611static struct uart_driver mpsc_reg = {
1612 .owner = THIS_MODULE,
1613 .driver_name = MPSC_DRIVER_NAME,
1614 .devfs_name = MPSC_DEVFS_NAME,
1615 .dev_name = MPSC_DEV_NAME,
1616 .major = MPSC_MAJOR,
1617 .minor = MPSC_MINOR_START,
1618 .nr = MPSC_NUM_CTLRS,
1619 .cons = MPSC_CONSOLE,
1620};
1621
1622static int
1623mpsc_drv_map_regs(struct mpsc_port_info *pi, struct platform_device *pd)
1624{
1625 struct resource *r;
1626
1627 if ((r = platform_get_resource(pd, IORESOURCE_MEM, MPSC_BASE_ORDER)) &&
1628 request_mem_region(r->start, MPSC_REG_BLOCK_SIZE, "mpsc_regs")){
1629
1630 pi->mpsc_base = ioremap(r->start, MPSC_REG_BLOCK_SIZE);
1631 pi->mpsc_base_p = r->start;
1632 }
1633 else {
1634 mpsc_resource_err("MPSC base");
1635 return -ENOMEM;
1636 }
1637
1638 if ((r = platform_get_resource(pd, IORESOURCE_MEM,
1639 MPSC_SDMA_BASE_ORDER)) && request_mem_region(r->start,
1640 MPSC_SDMA_REG_BLOCK_SIZE, "sdma_regs")) {
1641
1642 pi->sdma_base = ioremap(r->start,MPSC_SDMA_REG_BLOCK_SIZE);
1643 pi->sdma_base_p = r->start;
1644 }
1645 else {
1646 mpsc_resource_err("SDMA base");
1647 return -ENOMEM;
1648 }
1649
1650 if ((r = platform_get_resource(pd,IORESOURCE_MEM,MPSC_BRG_BASE_ORDER))
1651 && request_mem_region(r->start, MPSC_BRG_REG_BLOCK_SIZE,
1652 "brg_regs")) {
1653
1654 pi->brg_base = ioremap(r->start, MPSC_BRG_REG_BLOCK_SIZE);
1655 pi->brg_base_p = r->start;
1656 }
1657 else {
1658 mpsc_resource_err("BRG base");
1659 return -ENOMEM;
1660 }
1661
1662 return 0;
1663}
1664
1665static void
1666mpsc_drv_unmap_regs(struct mpsc_port_info *pi)
1667{
1668 if (!pi->mpsc_base) {
1669 iounmap(pi->mpsc_base);
1670 release_mem_region(pi->mpsc_base_p, MPSC_REG_BLOCK_SIZE);
1671 }
1672 if (!pi->sdma_base) {
1673 iounmap(pi->sdma_base);
1674 release_mem_region(pi->sdma_base_p, MPSC_SDMA_REG_BLOCK_SIZE);
1675 }
1676 if (!pi->brg_base) {
1677 iounmap(pi->brg_base);
1678 release_mem_region(pi->brg_base_p, MPSC_BRG_REG_BLOCK_SIZE);
1679 }
1680
Al Viro2c6e7592005-04-25 18:32:12 -07001681 pi->mpsc_base = NULL;
1682 pi->sdma_base = NULL;
1683 pi->brg_base = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 pi->mpsc_base_p = 0;
1686 pi->sdma_base_p = 0;
1687 pi->brg_base_p = 0;
1688
1689 return;
1690}
1691
1692static void
1693mpsc_drv_get_platform_data(struct mpsc_port_info *pi,
1694 struct platform_device *pd, int num)
1695{
1696 struct mpsc_pdata *pdata;
1697
1698 pdata = (struct mpsc_pdata *)pd->dev.platform_data;
1699
1700 pi->port.uartclk = pdata->brg_clk_freq;
1701 pi->port.iotype = UPIO_MEM;
1702 pi->port.line = num;
1703 pi->port.type = PORT_MPSC;
1704 pi->port.fifosize = MPSC_TXBE_SIZE;
1705 pi->port.membase = pi->mpsc_base;
1706 pi->port.mapbase = (ulong)pi->mpsc_base;
1707 pi->port.ops = &mpsc_pops;
1708
1709 pi->mirror_regs = pdata->mirror_regs;
1710 pi->cache_mgmt = pdata->cache_mgmt;
1711 pi->brg_can_tune = pdata->brg_can_tune;
1712 pi->brg_clk_src = pdata->brg_clk_src;
1713 pi->mpsc_max_idle = pdata->max_idle;
1714 pi->default_baud = pdata->default_baud;
1715 pi->default_bits = pdata->default_bits;
1716 pi->default_parity = pdata->default_parity;
1717 pi->default_flow = pdata->default_flow;
1718
1719 /* Initial values of mirrored regs */
1720 pi->MPSC_CHR_1_m = pdata->chr_1_val;
1721 pi->MPSC_CHR_2_m = pdata->chr_2_val;
1722 pi->MPSC_CHR_10_m = pdata->chr_10_val;
1723 pi->MPSC_MPCR_m = pdata->mpcr_val;
1724 pi->BRG_BCR_m = pdata->bcr_val;
1725
1726 pi->shared_regs = &mpsc_shared_regs;
1727
1728 pi->port.irq = platform_get_irq(pd, 0);
1729
1730 return;
1731}
1732
1733static int
1734mpsc_drv_probe(struct device *dev)
1735{
1736 struct platform_device *pd = to_platform_device(dev);
1737 struct mpsc_port_info *pi;
1738 int rc = -ENODEV;
1739
1740 pr_debug("mpsc_drv_probe: Adding MPSC %d\n", pd->id);
1741
1742 if (pd->id < MPSC_NUM_CTLRS) {
1743 pi = &mpsc_ports[pd->id];
1744
1745 if (!(rc = mpsc_drv_map_regs(pi, pd))) {
1746 mpsc_drv_get_platform_data(pi, pd, pd->id);
1747
1748 if (!(rc = mpsc_make_ready(pi)))
1749 if (!(rc = uart_add_one_port(&mpsc_reg,
1750 &pi->port)))
1751 rc = 0;
1752 else {
1753 mpsc_release_port(
1754 (struct uart_port *)pi);
1755 mpsc_drv_unmap_regs(pi);
1756 }
1757 else
1758 mpsc_drv_unmap_regs(pi);
1759 }
1760 }
1761
1762 return rc;
1763}
1764
1765static int
1766mpsc_drv_remove(struct device *dev)
1767{
1768 struct platform_device *pd = to_platform_device(dev);
1769
1770 pr_debug("mpsc_drv_exit: Removing MPSC %d\n", pd->id);
1771
1772 if (pd->id < MPSC_NUM_CTLRS) {
1773 uart_remove_one_port(&mpsc_reg, &mpsc_ports[pd->id].port);
1774 mpsc_release_port((struct uart_port *)&mpsc_ports[pd->id].port);
1775 mpsc_drv_unmap_regs(&mpsc_ports[pd->id]);
1776 return 0;
1777 }
1778 else
1779 return -ENODEV;
1780}
1781
1782static struct device_driver mpsc_driver = {
1783 .name = MPSC_CTLR_NAME,
1784 .bus = &platform_bus_type,
1785 .probe = mpsc_drv_probe,
1786 .remove = mpsc_drv_remove,
1787};
1788
1789static int __init
1790mpsc_drv_init(void)
1791{
1792 int rc;
1793
1794 printk(KERN_INFO "Serial: MPSC driver $Revision: 1.00 $\n");
1795
1796 memset(mpsc_ports, 0, sizeof(mpsc_ports));
1797 memset(&mpsc_shared_regs, 0, sizeof(mpsc_shared_regs));
1798
1799 if (!(rc = uart_register_driver(&mpsc_reg))) {
1800 if (!(rc = driver_register(&mpsc_shared_driver))) {
1801 if ((rc = driver_register(&mpsc_driver))) {
1802 driver_unregister(&mpsc_shared_driver);
1803 uart_unregister_driver(&mpsc_reg);
1804 }
1805 }
1806 else
1807 uart_unregister_driver(&mpsc_reg);
1808 }
1809
1810 return rc;
1811
1812}
1813
1814static void __exit
1815mpsc_drv_exit(void)
1816{
1817 driver_unregister(&mpsc_driver);
1818 driver_unregister(&mpsc_shared_driver);
1819 uart_unregister_driver(&mpsc_reg);
1820 memset(mpsc_ports, 0, sizeof(mpsc_ports));
1821 memset(&mpsc_shared_regs, 0, sizeof(mpsc_shared_regs));
1822 return;
1823}
1824
1825module_init(mpsc_drv_init);
1826module_exit(mpsc_drv_exit);
1827
1828MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
1829MODULE_DESCRIPTION("Generic Marvell MPSC serial/UART driver $Revision: 1.00 $");
1830MODULE_VERSION(MPSC_VERSION);
1831MODULE_LICENSE("GPL");
1832MODULE_ALIAS_CHARDEV_MAJOR(MPSC_MAJOR);