1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include <linux/module.h>
30
31#include <linux/serial.h>
32#include <linux/serial_core.h>
33#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/slab.h>
36#include <linux/init.h>
37#include <linux/interrupt.h>
38#include <linux/irq.h>
39#include <linux/io.h>
40#include <linux/ioport.h>
41#include <linux/kernel.h>
42#include <linux/timer.h>
43#include <linux/clk.h>
44#include <linux/platform_device.h>
45#include <linux/pm_runtime.h>
46#include <linux/dma-mapping.h>
47#include <linux/dmapool.h>
48#include <linux/wait.h>
49#include <linux/workqueue.h>
50
51#include <linux/atomic.h>
52#include <asm/irq.h>
53
54#include <mach/hardware.h>
55#include <mach/dma.h>
56#include <linux/platform_data/msm_serial_hs.h>
57
58
59#define UARTDM_MR1_ADDR 0x0
60#define UARTDM_MR2_ADDR 0x4
61
62
63#define RSLT_FIFO_CNTR_BMSK (0xE << 28)
64#define RSLT_VLD BIT(1)
65
66
67#define UARTDM_CSR_ADDR 0x8
68#define UARTDM_CSR_115200 0xFF
69#define UARTDM_CSR_57600 0xEE
70#define UARTDM_CSR_38400 0xDD
71#define UARTDM_CSR_28800 0xCC
72#define UARTDM_CSR_19200 0xBB
73#define UARTDM_CSR_14400 0xAA
74#define UARTDM_CSR_9600 0x99
75#define UARTDM_CSR_7200 0x88
76#define UARTDM_CSR_4800 0x77
77#define UARTDM_CSR_3600 0x66
78#define UARTDM_CSR_2400 0x55
79#define UARTDM_CSR_1200 0x44
80#define UARTDM_CSR_600 0x33
81#define UARTDM_CSR_300 0x22
82#define UARTDM_CSR_150 0x11
83#define UARTDM_CSR_75 0x00
84
85
86#define UARTDM_TF_ADDR 0x70
87#define UARTDM_TF2_ADDR 0x74
88#define UARTDM_TF3_ADDR 0x78
89#define UARTDM_TF4_ADDR 0x7C
90
91
92#define UARTDM_CR_ADDR 0x10
93#define UARTDM_IMR_ADDR 0x14
94
95#define UARTDM_IPR_ADDR 0x18
96#define UARTDM_TFWR_ADDR 0x1c
97#define UARTDM_RFWR_ADDR 0x20
98#define UARTDM_HCR_ADDR 0x24
99#define UARTDM_DMRX_ADDR 0x34
100#define UARTDM_IRDA_ADDR 0x38
101#define UARTDM_DMEN_ADDR 0x3c
102
103
104#define UARTDM_NCF_TX_ADDR 0x40
105
106#define UARTDM_BADR_ADDR 0x44
107
108#define UARTDM_SIM_CFG_ADDR 0x80
109
110#define UARTDM_SR_ADDR 0x8
111
112
113#define UARTDM_RF_ADDR 0x70
114#define UARTDM_RF2_ADDR 0x74
115#define UARTDM_RF3_ADDR 0x78
116#define UARTDM_RF4_ADDR 0x7C
117
118
119#define UARTDM_MISR_ADDR 0x10
120
121
122#define UARTDM_ISR_ADDR 0x14
123#define UARTDM_RX_TOTAL_SNAP_ADDR 0x38
124
125#define UARTDM_RXFS_ADDR 0x50
126
127
128#define UARTDM_SR_PAR_FRAME_BMSK BIT(5)
129#define UARTDM_SR_OVERRUN_BMSK BIT(4)
130#define UARTDM_SR_TXEMT_BMSK BIT(3)
131#define UARTDM_SR_TXRDY_BMSK BIT(2)
132#define UARTDM_SR_RXRDY_BMSK BIT(0)
133
134#define UARTDM_CR_TX_DISABLE_BMSK BIT(3)
135#define UARTDM_CR_RX_DISABLE_BMSK BIT(1)
136#define UARTDM_CR_TX_EN_BMSK BIT(2)
137#define UARTDM_CR_RX_EN_BMSK BIT(0)
138
139
140#define RESET_RX 0x10
141#define RESET_TX 0x20
142#define RESET_ERROR_STATUS 0x30
143#define RESET_BREAK_INT 0x40
144#define START_BREAK 0x50
145#define STOP_BREAK 0x60
146#define RESET_CTS 0x70
147#define RESET_STALE_INT 0x80
148#define RFR_LOW 0xD0
149#define RFR_HIGH 0xE0
150#define CR_PROTECTION_EN 0x100
151#define STALE_EVENT_ENABLE 0x500
152#define STALE_EVENT_DISABLE 0x600
153#define FORCE_STALE_EVENT 0x400
154#define CLEAR_TX_READY 0x300
155#define RESET_TX_ERROR 0x800
156#define RESET_TX_DONE 0x810
157
158#define UARTDM_MR1_AUTO_RFR_LEVEL1_BMSK 0xffffff00
159#define UARTDM_MR1_AUTO_RFR_LEVEL0_BMSK 0x3f
160#define UARTDM_MR1_CTS_CTL_BMSK 0x40
161#define UARTDM_MR1_RX_RDY_CTL_BMSK 0x80
162
163#define UARTDM_MR2_ERROR_MODE_BMSK 0x40
164#define UARTDM_MR2_BITS_PER_CHAR_BMSK 0x30
165
166
167#define FIVE_BPC (0 << 4)
168#define SIX_BPC (1 << 4)
169#define SEVEN_BPC (2 << 4)
170#define EIGHT_BPC (3 << 4)
171
172#define UARTDM_MR2_STOP_BIT_LEN_BMSK 0xc
173#define STOP_BIT_ONE (1 << 2)
174#define STOP_BIT_TWO (3 << 2)
175
176#define UARTDM_MR2_PARITY_MODE_BMSK 0x3
177
178
179#define NO_PARITY 0x0
180#define EVEN_PARITY 0x1
181#define ODD_PARITY 0x2
182#define SPACE_PARITY 0x3
183
184#define UARTDM_IPR_STALE_TIMEOUT_MSB_BMSK 0xffffff80
185#define UARTDM_IPR_STALE_LSB_BMSK 0x1f
186
187
188#define UARTDM_ISR_TX_READY_BMSK BIT(7)
189#define UARTDM_ISR_CURRENT_CTS_BMSK BIT(6)
190#define UARTDM_ISR_DELTA_CTS_BMSK BIT(5)
191#define UARTDM_ISR_RXLEV_BMSK BIT(4)
192#define UARTDM_ISR_RXSTALE_BMSK BIT(3)
193#define UARTDM_ISR_RXBREAK_BMSK BIT(2)
194#define UARTDM_ISR_RXHUNT_BMSK BIT(1)
195#define UARTDM_ISR_TXLEV_BMSK BIT(0)
196
197
198#define UARTDM_TX_DM_EN_BMSK 0x1
199#define UARTDM_RX_DM_EN_BMSK 0x2
200
201#define UART_FIFOSIZE 64
202#define UARTCLK 7372800
203
204
205enum flush_reason {
206 FLUSH_NONE,
207 FLUSH_DATA_READY,
208 FLUSH_DATA_INVALID,
209 FLUSH_IGNORE = FLUSH_DATA_INVALID,
210 FLUSH_STOP,
211 FLUSH_SHUTDOWN,
212};
213
214
215enum msm_hs_clk_states_e {
216 MSM_HS_CLK_PORT_OFF,
217 MSM_HS_CLK_OFF,
218 MSM_HS_CLK_REQUEST_OFF,
219 MSM_HS_CLK_ON,
220};
221
222
223
224enum msm_hs_clk_req_off_state_e {
225 CLK_REQ_OFF_START,
226 CLK_REQ_OFF_RXSTALE_ISSUED,
227 CLK_REQ_OFF_FLUSH_ISSUED,
228 CLK_REQ_OFF_RXSTALE_FLUSHED,
229};
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249struct msm_hs_tx {
250 unsigned int tx_ready_int_en;
251 unsigned int dma_in_flight;
252 struct msm_dmov_cmd xfer;
253 dmov_box *command_ptr;
254 u32 *command_ptr_ptr;
255 dma_addr_t mapped_cmd_ptr;
256 dma_addr_t mapped_cmd_ptr_ptr;
257 int tx_count;
258 dma_addr_t dma_base;
259};
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278struct msm_hs_rx {
279 enum flush_reason flush;
280 struct msm_dmov_cmd xfer;
281 dma_addr_t cmdptr_dmaaddr;
282 dmov_box *command_ptr;
283 u32 *command_ptr_ptr;
284 dma_addr_t mapped_cmd_ptr;
285 wait_queue_head_t wait;
286 dma_addr_t rbuffer;
287 unsigned char *buffer;
288 struct dma_pool *pool;
289 struct work_struct tty_work;
290};
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307struct msm_hs_rx_wakeup {
308 int irq;
309 unsigned char ignore;
310 unsigned char inject_rx;
311 char rx_to_inject;
312};
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334struct msm_hs_port {
335 struct uart_port uport;
336 unsigned long imr_reg;
337 struct clk *clk;
338 struct msm_hs_tx tx;
339 struct msm_hs_rx rx;
340
341 int dma_tx_channel;
342 int dma_rx_channel;
343 int dma_tx_crci;
344 int dma_rx_crci;
345
346 struct hrtimer clk_off_timer;
347 ktime_t clk_off_delay;
348 enum msm_hs_clk_states_e clk_state;
349 enum msm_hs_clk_req_off_state_e clk_req_off_state;
350
351 struct msm_hs_rx_wakeup rx_wakeup;
352 void (*exit_lpm_cb)(struct uart_port *);
353};
354
355#define MSM_UARTDM_BURST_SIZE 16
356#define UARTDM_TX_BUF_SIZE UART_XMIT_SIZE
357#define UARTDM_RX_BUF_SIZE 512
358
359#define UARTDM_NR 2
360
361static struct msm_hs_port q_uart_port[UARTDM_NR];
362static struct platform_driver msm_serial_hs_platform_driver;
363static struct uart_driver msm_hs_driver;
364static struct uart_ops msm_hs_ops;
365static struct workqueue_struct *msm_hs_workqueue;
366
367#define UARTDM_TO_MSM(uart_port) \
368 container_of((uart_port), struct msm_hs_port, uport)
369
370static unsigned int use_low_power_rx_wakeup(struct msm_hs_port
371 *msm_uport)
372{
373 return (msm_uport->rx_wakeup.irq >= 0);
374}
375
376static unsigned int msm_hs_read(struct uart_port *uport,
377 unsigned int offset)
378{
379 return ioread32(uport->membase + offset);
380}
381
382static void msm_hs_write(struct uart_port *uport, unsigned int offset,
383 unsigned int value)
384{
385 iowrite32(value, uport->membase + offset);
386}
387
388static void msm_hs_release_port(struct uart_port *port)
389{
390 iounmap(port->membase);
391}
392
393static int msm_hs_request_port(struct uart_port *port)
394{
395 port->membase = ioremap(port->mapbase, PAGE_SIZE);
396 if (unlikely(!port->membase))
397 return -ENOMEM;
398
399
400 msm_hs_write(port, UARTDM_CR_ADDR, CR_PROTECTION_EN);
401 return 0;
402}
403
404static int msm_hs_remove(struct platform_device *pdev)
405{
406
407 struct msm_hs_port *msm_uport;
408 struct device *dev;
409
410 if (pdev->id < 0 || pdev->id >= UARTDM_NR) {
411 printk(KERN_ERR "Invalid plaform device ID = %d\n", pdev->id);
412 return -EINVAL;
413 }
414
415 msm_uport = &q_uart_port[pdev->id];
416 dev = msm_uport->uport.dev;
417
418 dma_unmap_single(dev, msm_uport->rx.mapped_cmd_ptr, sizeof(dmov_box),
419 DMA_TO_DEVICE);
420 dma_pool_free(msm_uport->rx.pool, msm_uport->rx.buffer,
421 msm_uport->rx.rbuffer);
422 dma_pool_destroy(msm_uport->rx.pool);
423
424 dma_unmap_single(dev, msm_uport->rx.cmdptr_dmaaddr, sizeof(u32),
425 DMA_TO_DEVICE);
426 dma_unmap_single(dev, msm_uport->tx.mapped_cmd_ptr_ptr, sizeof(u32),
427 DMA_TO_DEVICE);
428 dma_unmap_single(dev, msm_uport->tx.mapped_cmd_ptr, sizeof(dmov_box),
429 DMA_TO_DEVICE);
430
431 uart_remove_one_port(&msm_hs_driver, &msm_uport->uport);
432 clk_put(msm_uport->clk);
433
434
435 kfree(msm_uport->tx.command_ptr);
436 kfree(msm_uport->tx.command_ptr_ptr);
437
438
439 kfree(msm_uport->rx.command_ptr);
440 kfree(msm_uport->rx.command_ptr_ptr);
441
442 iounmap(msm_uport->uport.membase);
443
444 return 0;
445}
446
447static int msm_hs_init_clk_locked(struct uart_port *uport)
448{
449 int ret;
450 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
451
452 ret = clk_enable(msm_uport->clk);
453 if (ret) {
454 printk(KERN_ERR "Error could not turn on UART clk\n");
455 return ret;
456 }
457
458
459 ret = clk_set_rate(msm_uport->clk, uport->uartclk);
460 if (ret) {
461 printk(KERN_WARNING "Error setting clock rate on UART\n");
462 clk_disable(msm_uport->clk);
463 return ret;
464 }
465
466 msm_uport->clk_state = MSM_HS_CLK_ON;
467 return 0;
468}
469
470
471static void msm_hs_pm(struct uart_port *uport, unsigned int state,
472 unsigned int oldstate)
473{
474 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
475
476 if (use_low_power_rx_wakeup(msm_uport) ||
477 msm_uport->exit_lpm_cb)
478 return;
479
480
481 switch (state) {
482 case 0:
483 clk_enable(msm_uport->clk);
484 break;
485 case 3:
486 clk_disable(msm_uport->clk);
487 break;
488 default:
489 dev_err(uport->dev, "msm_serial: Unknown PM state %d\n",
490 state);
491 }
492}
493
494
495
496
497
498
499
500
501
502
503static void msm_hs_set_bps_locked(struct uart_port *uport,
504 unsigned int bps)
505{
506 unsigned long rxstale;
507 unsigned long data;
508 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
509
510 switch (bps) {
511 case 300:
512 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_75);
513 rxstale = 1;
514 break;
515 case 600:
516 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_150);
517 rxstale = 1;
518 break;
519 case 1200:
520 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_300);
521 rxstale = 1;
522 break;
523 case 2400:
524 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_600);
525 rxstale = 1;
526 break;
527 case 4800:
528 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_1200);
529 rxstale = 1;
530 break;
531 case 9600:
532 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_2400);
533 rxstale = 2;
534 break;
535 case 14400:
536 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_3600);
537 rxstale = 3;
538 break;
539 case 19200:
540 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_4800);
541 rxstale = 4;
542 break;
543 case 28800:
544 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_7200);
545 rxstale = 6;
546 break;
547 case 38400:
548 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_9600);
549 rxstale = 8;
550 break;
551 case 57600:
552 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_14400);
553 rxstale = 16;
554 break;
555 case 76800:
556 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_19200);
557 rxstale = 16;
558 break;
559 case 115200:
560 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_28800);
561 rxstale = 31;
562 break;
563 case 230400:
564 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_57600);
565 rxstale = 31;
566 break;
567 case 460800:
568 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_115200);
569 rxstale = 31;
570 break;
571 case 4000000:
572 case 3686400:
573 case 3200000:
574 case 3500000:
575 case 3000000:
576 case 2500000:
577 case 1500000:
578 case 1152000:
579 case 1000000:
580 case 921600:
581 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_115200);
582 rxstale = 31;
583 break;
584 default:
585 msm_hs_write(uport, UARTDM_CSR_ADDR, UARTDM_CSR_2400);
586
587 bps = 9600;
588 rxstale = 2;
589 break;
590 }
591 if (bps > 460800)
592 uport->uartclk = bps * 16;
593 else
594 uport->uartclk = UARTCLK;
595
596 if (clk_set_rate(msm_uport->clk, uport->uartclk)) {
597 printk(KERN_WARNING "Error setting clock rate on UART\n");
598 return;
599 }
600
601 data = rxstale & UARTDM_IPR_STALE_LSB_BMSK;
602 data |= UARTDM_IPR_STALE_TIMEOUT_MSB_BMSK & (rxstale << 2);
603
604 msm_hs_write(uport, UARTDM_IPR_ADDR, data);
605}
606
607
608
609
610
611
612
613static void msm_hs_set_termios(struct uart_port *uport,
614 struct ktermios *termios,
615 struct ktermios *oldtermios)
616{
617 unsigned int bps;
618 unsigned long data;
619 unsigned long flags;
620 unsigned int c_cflag = termios->c_cflag;
621 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
622
623 spin_lock_irqsave(&uport->lock, flags);
624 clk_enable(msm_uport->clk);
625
626
627 bps = uart_get_baud_rate(uport, termios, oldtermios, 200, 4000000);
628
629
630 if (bps == 200)
631 bps = 3200000;
632
633 msm_hs_set_bps_locked(uport, bps);
634
635 data = msm_hs_read(uport, UARTDM_MR2_ADDR);
636 data &= ~UARTDM_MR2_PARITY_MODE_BMSK;
637
638 if (PARENB == (c_cflag & PARENB)) {
639 if (PARODD == (c_cflag & PARODD))
640 data |= ODD_PARITY;
641 else if (CMSPAR == (c_cflag & CMSPAR))
642 data |= SPACE_PARITY;
643 else
644 data |= EVEN_PARITY;
645 }
646
647
648 data &= ~UARTDM_MR2_BITS_PER_CHAR_BMSK;
649
650 switch (c_cflag & CSIZE) {
651 case CS5:
652 data |= FIVE_BPC;
653 break;
654 case CS6:
655 data |= SIX_BPC;
656 break;
657 case CS7:
658 data |= SEVEN_BPC;
659 break;
660 default:
661 data |= EIGHT_BPC;
662 break;
663 }
664
665 if (c_cflag & CSTOPB) {
666 data |= STOP_BIT_TWO;
667 } else {
668
669 data |= STOP_BIT_ONE;
670 }
671 data |= UARTDM_MR2_ERROR_MODE_BMSK;
672
673 msm_hs_write(uport, UARTDM_MR2_ADDR, data);
674
675
676 data = msm_hs_read(uport, UARTDM_MR1_ADDR);
677
678 data &= ~(UARTDM_MR1_CTS_CTL_BMSK | UARTDM_MR1_RX_RDY_CTL_BMSK);
679
680 if (c_cflag & CRTSCTS) {
681 data |= UARTDM_MR1_CTS_CTL_BMSK;
682 data |= UARTDM_MR1_RX_RDY_CTL_BMSK;
683 }
684
685 msm_hs_write(uport, UARTDM_MR1_ADDR, data);
686
687 uport->ignore_status_mask = termios->c_iflag & INPCK;
688 uport->ignore_status_mask |= termios->c_iflag & IGNPAR;
689 uport->read_status_mask = (termios->c_cflag & CREAD);
690
691 msm_hs_write(uport, UARTDM_IMR_ADDR, 0);
692
693
694 uart_update_timeout(uport, c_cflag, bps);
695
696 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_RX);
697 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_TX);
698
699 if (msm_uport->rx.flush == FLUSH_NONE) {
700 msm_uport->rx.flush = FLUSH_IGNORE;
701 msm_dmov_stop_cmd(msm_uport->dma_rx_channel, NULL, 1);
702 }
703
704 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
705
706 clk_disable(msm_uport->clk);
707 spin_unlock_irqrestore(&uport->lock, flags);
708}
709
710
711
712
713
714static unsigned int msm_hs_tx_empty(struct uart_port *uport)
715{
716 unsigned int data;
717 unsigned int ret = 0;
718 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
719
720 clk_enable(msm_uport->clk);
721
722 data = msm_hs_read(uport, UARTDM_SR_ADDR);
723 if (data & UARTDM_SR_TXEMT_BMSK)
724 ret = TIOCSER_TEMT;
725
726 clk_disable(msm_uport->clk);
727
728 return ret;
729}
730
731
732
733
734
735
736static void msm_hs_stop_tx_locked(struct uart_port *uport)
737{
738 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
739
740 msm_uport->tx.tx_ready_int_en = 0;
741}
742
743
744
745
746
747
748
749
750
751static void msm_hs_stop_rx_locked(struct uart_port *uport)
752{
753 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
754 unsigned int data;
755
756 clk_enable(msm_uport->clk);
757
758
759 data = msm_hs_read(uport, UARTDM_DMEN_ADDR);
760 data &= ~UARTDM_RX_DM_EN_BMSK;
761 msm_hs_write(uport, UARTDM_DMEN_ADDR, data);
762
763
764 if (msm_uport->rx.flush == FLUSH_NONE)
765 msm_dmov_stop_cmd(msm_uport->dma_rx_channel, NULL, 1);
766
767 if (msm_uport->rx.flush != FLUSH_SHUTDOWN)
768 msm_uport->rx.flush = FLUSH_STOP;
769
770 clk_disable(msm_uport->clk);
771}
772
773
774static void msm_hs_submit_tx_locked(struct uart_port *uport)
775{
776 int left;
777 int tx_count;
778 dma_addr_t src_addr;
779 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
780 struct msm_hs_tx *tx = &msm_uport->tx;
781 struct circ_buf *tx_buf = &msm_uport->uport.state->xmit;
782
783 if (uart_circ_empty(tx_buf) || uport->state->port.tty->stopped) {
784 msm_hs_stop_tx_locked(uport);
785 return;
786 }
787
788 tx->dma_in_flight = 1;
789
790 tx_count = uart_circ_chars_pending(tx_buf);
791
792 if (UARTDM_TX_BUF_SIZE < tx_count)
793 tx_count = UARTDM_TX_BUF_SIZE;
794
795 left = UART_XMIT_SIZE - tx_buf->tail;
796
797 if (tx_count > left)
798 tx_count = left;
799
800 src_addr = tx->dma_base + tx_buf->tail;
801 dma_sync_single_for_device(uport->dev, src_addr, tx_count,
802 DMA_TO_DEVICE);
803
804 tx->command_ptr->num_rows = (((tx_count + 15) >> 4) << 16) |
805 ((tx_count + 15) >> 4);
806 tx->command_ptr->src_row_addr = src_addr;
807
808 dma_sync_single_for_device(uport->dev, tx->mapped_cmd_ptr,
809 sizeof(dmov_box), DMA_TO_DEVICE);
810
811 *tx->command_ptr_ptr = CMD_PTR_LP | DMOV_CMD_ADDR(tx->mapped_cmd_ptr);
812
813 dma_sync_single_for_device(uport->dev, tx->mapped_cmd_ptr_ptr,
814 sizeof(u32), DMA_TO_DEVICE);
815
816
817 tx->tx_count = tx_count;
818 msm_hs_write(uport, UARTDM_NCF_TX_ADDR, tx_count);
819
820
821 msm_uport->imr_reg &= ~UARTDM_ISR_TX_READY_BMSK;
822 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
823 msm_dmov_enqueue_cmd(msm_uport->dma_tx_channel, &tx->xfer);
824}
825
826
827static void msm_hs_start_rx_locked(struct uart_port *uport)
828{
829 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
830
831 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_STALE_INT);
832 msm_hs_write(uport, UARTDM_DMRX_ADDR, UARTDM_RX_BUF_SIZE);
833 msm_hs_write(uport, UARTDM_CR_ADDR, STALE_EVENT_ENABLE);
834 msm_uport->imr_reg |= UARTDM_ISR_RXLEV_BMSK;
835 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
836
837 msm_uport->rx.flush = FLUSH_NONE;
838 msm_dmov_enqueue_cmd(msm_uport->dma_rx_channel, &msm_uport->rx.xfer);
839
840
841 hrtimer_start(&msm_uport->clk_off_timer, msm_uport->clk_off_delay,
842 HRTIMER_MODE_REL);
843}
844
845
846static void msm_hs_start_tx_locked(struct uart_port *uport)
847{
848 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
849
850 clk_enable(msm_uport->clk);
851
852 if (msm_uport->exit_lpm_cb)
853 msm_uport->exit_lpm_cb(uport);
854
855 if (msm_uport->tx.tx_ready_int_en == 0) {
856 msm_uport->tx.tx_ready_int_en = 1;
857 msm_hs_submit_tx_locked(uport);
858 }
859
860 clk_disable(msm_uport->clk);
861}
862
863
864
865
866
867
868
869
870static void msm_hs_dmov_tx_callback(struct msm_dmov_cmd *cmd_ptr,
871 unsigned int result,
872 struct msm_dmov_errdata *err)
873{
874 unsigned long flags;
875 struct msm_hs_port *msm_uport;
876
877
878 WARN_ON((((result & RSLT_FIFO_CNTR_BMSK) >> 28) == 1) &&
879 !(result & RSLT_VLD));
880
881 msm_uport = container_of(cmd_ptr, struct msm_hs_port, tx.xfer);
882
883 spin_lock_irqsave(&msm_uport->uport.lock, flags);
884 clk_enable(msm_uport->clk);
885
886 msm_uport->imr_reg |= UARTDM_ISR_TX_READY_BMSK;
887 msm_hs_write(&msm_uport->uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
888
889 clk_disable(msm_uport->clk);
890 spin_unlock_irqrestore(&msm_uport->uport.lock, flags);
891}
892
893
894
895
896
897
898
899
900static void msm_hs_dmov_rx_callback(struct msm_dmov_cmd *cmd_ptr,
901 unsigned int result,
902 struct msm_dmov_errdata *err)
903{
904 int retval;
905 int rx_count;
906 unsigned long status;
907 unsigned int error_f = 0;
908 unsigned long flags;
909 unsigned int flush;
910 struct tty_struct *tty;
911 struct tty_port *port;
912 struct uart_port *uport;
913 struct msm_hs_port *msm_uport;
914
915 msm_uport = container_of(cmd_ptr, struct msm_hs_port, rx.xfer);
916 uport = &msm_uport->uport;
917
918 spin_lock_irqsave(&uport->lock, flags);
919 clk_enable(msm_uport->clk);
920
921 port = &uport->state->port;
922 tty = port->tty;
923
924 msm_hs_write(uport, UARTDM_CR_ADDR, STALE_EVENT_DISABLE);
925
926 status = msm_hs_read(uport, UARTDM_SR_ADDR);
927
928
929 if (unlikely((status & UARTDM_SR_OVERRUN_BMSK) &&
930 (uport->read_status_mask & CREAD))) {
931 tty_insert_flip_char(port, 0, TTY_OVERRUN);
932 uport->icount.buf_overrun++;
933 error_f = 1;
934 }
935
936 if (!(uport->ignore_status_mask & INPCK))
937 status = status & ~(UARTDM_SR_PAR_FRAME_BMSK);
938
939 if (unlikely(status & UARTDM_SR_PAR_FRAME_BMSK)) {
940
941 uport->icount.parity++;
942 error_f = 1;
943 if (uport->ignore_status_mask & IGNPAR)
944 tty_insert_flip_char(port, 0, TTY_PARITY);
945 }
946
947 if (error_f)
948 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_ERROR_STATUS);
949
950 if (msm_uport->clk_req_off_state == CLK_REQ_OFF_FLUSH_ISSUED)
951 msm_uport->clk_req_off_state = CLK_REQ_OFF_RXSTALE_FLUSHED;
952
953 flush = msm_uport->rx.flush;
954 if (flush == FLUSH_IGNORE)
955 msm_hs_start_rx_locked(uport);
956 if (flush == FLUSH_STOP)
957 msm_uport->rx.flush = FLUSH_SHUTDOWN;
958 if (flush >= FLUSH_DATA_INVALID)
959 goto out;
960
961 rx_count = msm_hs_read(uport, UARTDM_RX_TOTAL_SNAP_ADDR);
962
963 if (0 != (uport->read_status_mask & CREAD)) {
964 retval = tty_insert_flip_string(port, msm_uport->rx.buffer,
965 rx_count);
966 BUG_ON(retval != rx_count);
967 }
968
969 msm_hs_start_rx_locked(uport);
970
971out:
972 clk_disable(msm_uport->clk);
973
974 spin_unlock_irqrestore(&uport->lock, flags);
975
976 if (flush < FLUSH_DATA_INVALID)
977 queue_work(msm_hs_workqueue, &msm_uport->rx.tty_work);
978}
979
980static void msm_hs_tty_flip_buffer_work(struct work_struct *work)
981{
982 struct msm_hs_port *msm_uport =
983 container_of(work, struct msm_hs_port, rx.tty_work);
984
985 tty_flip_buffer_push(&msm_uport->uport.state->port);
986}
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001static unsigned int msm_hs_get_mctrl_locked(struct uart_port *uport)
1002{
1003 return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
1004}
1005
1006
1007
1008
1009
1010
1011static void set_rfr_locked(struct uart_port *uport, int auto_rfr)
1012{
1013 unsigned int data;
1014
1015 data = msm_hs_read(uport, UARTDM_MR1_ADDR);
1016
1017 if (auto_rfr) {
1018
1019 data |= UARTDM_MR1_RX_RDY_CTL_BMSK;
1020 msm_hs_write(uport, UARTDM_MR1_ADDR, data);
1021 } else {
1022
1023 data &= ~UARTDM_MR1_RX_RDY_CTL_BMSK;
1024 msm_hs_write(uport, UARTDM_MR1_ADDR, data);
1025
1026 msm_hs_write(uport, UARTDM_CR_ADDR, RFR_HIGH);
1027 }
1028}
1029
1030
1031
1032
1033static void msm_hs_set_mctrl_locked(struct uart_port *uport,
1034 unsigned int mctrl)
1035{
1036 unsigned int auto_rfr;
1037 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1038
1039 clk_enable(msm_uport->clk);
1040
1041 auto_rfr = TIOCM_RTS & mctrl ? 1 : 0;
1042 set_rfr_locked(uport, auto_rfr);
1043
1044 clk_disable(msm_uport->clk);
1045}
1046
1047
1048static void msm_hs_enable_ms_locked(struct uart_port *uport)
1049{
1050 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1051
1052 clk_enable(msm_uport->clk);
1053
1054
1055 msm_uport->imr_reg |= UARTDM_ISR_DELTA_CTS_BMSK;
1056 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
1057
1058 clk_disable(msm_uport->clk);
1059
1060}
1061
1062
1063
1064
1065
1066
1067
1068static void msm_hs_break_ctl(struct uart_port *uport, int ctl)
1069{
1070 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1071
1072 clk_enable(msm_uport->clk);
1073 msm_hs_write(uport, UARTDM_CR_ADDR, ctl ? START_BREAK : STOP_BREAK);
1074 clk_disable(msm_uport->clk);
1075}
1076
1077static void msm_hs_config_port(struct uart_port *uport, int cfg_flags)
1078{
1079 unsigned long flags;
1080
1081 spin_lock_irqsave(&uport->lock, flags);
1082 if (cfg_flags & UART_CONFIG_TYPE) {
1083 uport->type = PORT_MSM;
1084 msm_hs_request_port(uport);
1085 }
1086 spin_unlock_irqrestore(&uport->lock, flags);
1087}
1088
1089
1090static void msm_hs_handle_delta_cts_locked(struct uart_port *uport)
1091{
1092 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1093
1094 clk_enable(msm_uport->clk);
1095
1096
1097 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_CTS);
1098 uport->icount.cts++;
1099
1100 clk_disable(msm_uport->clk);
1101
1102
1103 wake_up_interruptible(&uport->state->port.delta_msr_wait);
1104}
1105
1106
1107
1108
1109
1110
1111static int msm_hs_check_clock_off_locked(struct uart_port *uport)
1112{
1113 unsigned long sr_status;
1114 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1115 struct circ_buf *tx_buf = &uport->state->xmit;
1116
1117
1118
1119 if (msm_uport->clk_state != MSM_HS_CLK_REQUEST_OFF ||
1120 !uart_circ_empty(tx_buf) || msm_uport->tx.dma_in_flight ||
1121 (msm_uport->imr_reg & UARTDM_ISR_TXLEV_BMSK) ||
1122 !(msm_uport->imr_reg & UARTDM_ISR_RXLEV_BMSK)) {
1123 return -1;
1124 }
1125
1126
1127 sr_status = msm_hs_read(uport, UARTDM_SR_ADDR);
1128 if (!(sr_status & UARTDM_SR_TXEMT_BMSK))
1129 return 0;
1130
1131
1132 switch (msm_uport->clk_req_off_state) {
1133 case CLK_REQ_OFF_START:
1134 msm_uport->clk_req_off_state = CLK_REQ_OFF_RXSTALE_ISSUED;
1135 msm_hs_write(uport, UARTDM_CR_ADDR, FORCE_STALE_EVENT);
1136 return 0;
1137 case CLK_REQ_OFF_RXSTALE_ISSUED:
1138 case CLK_REQ_OFF_FLUSH_ISSUED:
1139 return 0;
1140 case CLK_REQ_OFF_RXSTALE_FLUSHED:
1141 break;
1142 }
1143
1144 if (msm_uport->rx.flush != FLUSH_SHUTDOWN) {
1145 if (msm_uport->rx.flush == FLUSH_NONE)
1146 msm_hs_stop_rx_locked(uport);
1147 return 0;
1148 }
1149
1150
1151 clk_disable(msm_uport->clk);
1152 msm_uport->clk_state = MSM_HS_CLK_OFF;
1153
1154 if (use_low_power_rx_wakeup(msm_uport)) {
1155 msm_uport->rx_wakeup.ignore = 1;
1156 enable_irq(msm_uport->rx_wakeup.irq);
1157 }
1158 return 1;
1159}
1160
1161static enum hrtimer_restart msm_hs_clk_off_retry(struct hrtimer *timer)
1162{
1163 unsigned long flags;
1164 int ret = HRTIMER_NORESTART;
1165 struct msm_hs_port *msm_uport = container_of(timer, struct msm_hs_port,
1166 clk_off_timer);
1167 struct uart_port *uport = &msm_uport->uport;
1168
1169 spin_lock_irqsave(&uport->lock, flags);
1170
1171 if (!msm_hs_check_clock_off_locked(uport)) {
1172 hrtimer_forward_now(timer, msm_uport->clk_off_delay);
1173 ret = HRTIMER_RESTART;
1174 }
1175
1176 spin_unlock_irqrestore(&uport->lock, flags);
1177
1178 return ret;
1179}
1180
1181static irqreturn_t msm_hs_isr(int irq, void *dev)
1182{
1183 unsigned long flags;
1184 unsigned long isr_status;
1185 struct msm_hs_port *msm_uport = dev;
1186 struct uart_port *uport = &msm_uport->uport;
1187 struct circ_buf *tx_buf = &uport->state->xmit;
1188 struct msm_hs_tx *tx = &msm_uport->tx;
1189 struct msm_hs_rx *rx = &msm_uport->rx;
1190
1191 spin_lock_irqsave(&uport->lock, flags);
1192
1193 isr_status = msm_hs_read(uport, UARTDM_MISR_ADDR);
1194
1195
1196 if (isr_status & UARTDM_ISR_RXLEV_BMSK) {
1197 msm_uport->imr_reg &= ~UARTDM_ISR_RXLEV_BMSK;
1198 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
1199 }
1200
1201 if (isr_status & UARTDM_ISR_RXSTALE_BMSK) {
1202 msm_hs_write(uport, UARTDM_CR_ADDR, STALE_EVENT_DISABLE);
1203 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_STALE_INT);
1204
1205 if (msm_uport->clk_req_off_state == CLK_REQ_OFF_RXSTALE_ISSUED)
1206 msm_uport->clk_req_off_state =
1207 CLK_REQ_OFF_FLUSH_ISSUED;
1208 if (rx->flush == FLUSH_NONE) {
1209 rx->flush = FLUSH_DATA_READY;
1210 msm_dmov_stop_cmd(msm_uport->dma_rx_channel, NULL, 1);
1211 }
1212 }
1213
1214 if (isr_status & UARTDM_ISR_TX_READY_BMSK) {
1215
1216 msm_hs_write(uport, UARTDM_CR_ADDR, CLEAR_TX_READY);
1217
1218 if (msm_uport->clk_state == MSM_HS_CLK_REQUEST_OFF) {
1219 msm_uport->imr_reg |= UARTDM_ISR_TXLEV_BMSK;
1220 msm_hs_write(uport, UARTDM_IMR_ADDR,
1221 msm_uport->imr_reg);
1222 }
1223
1224
1225 tx_buf->tail = (tx_buf->tail + tx->tx_count) & ~UART_XMIT_SIZE;
1226
1227 tx->dma_in_flight = 0;
1228
1229 uport->icount.tx += tx->tx_count;
1230 if (tx->tx_ready_int_en)
1231 msm_hs_submit_tx_locked(uport);
1232
1233 if (uart_circ_chars_pending(tx_buf) < WAKEUP_CHARS)
1234 uart_write_wakeup(uport);
1235 }
1236 if (isr_status & UARTDM_ISR_TXLEV_BMSK) {
1237
1238 msm_uport->imr_reg &= ~UARTDM_ISR_TXLEV_BMSK;
1239 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
1240 if (!msm_hs_check_clock_off_locked(uport))
1241 hrtimer_start(&msm_uport->clk_off_timer,
1242 msm_uport->clk_off_delay,
1243 HRTIMER_MODE_REL);
1244 }
1245
1246
1247 if (isr_status & UARTDM_ISR_DELTA_CTS_BMSK)
1248 msm_hs_handle_delta_cts_locked(uport);
1249
1250 spin_unlock_irqrestore(&uport->lock, flags);
1251
1252 return IRQ_HANDLED;
1253}
1254
1255void msm_hs_request_clock_off_locked(struct uart_port *uport)
1256{
1257 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1258
1259 if (msm_uport->clk_state == MSM_HS_CLK_ON) {
1260 msm_uport->clk_state = MSM_HS_CLK_REQUEST_OFF;
1261 msm_uport->clk_req_off_state = CLK_REQ_OFF_START;
1262 if (!use_low_power_rx_wakeup(msm_uport))
1263 set_rfr_locked(uport, 0);
1264 msm_uport->imr_reg |= UARTDM_ISR_TXLEV_BMSK;
1265 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
1266 }
1267}
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281void msm_hs_request_clock_off(struct uart_port *uport)
1282{
1283 unsigned long flags;
1284
1285 spin_lock_irqsave(&uport->lock, flags);
1286 msm_hs_request_clock_off_locked(uport);
1287 spin_unlock_irqrestore(&uport->lock, flags);
1288}
1289
1290void msm_hs_request_clock_on_locked(struct uart_port *uport)
1291{
1292 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1293 unsigned int data;
1294
1295 switch (msm_uport->clk_state) {
1296 case MSM_HS_CLK_OFF:
1297 clk_enable(msm_uport->clk);
1298 disable_irq_nosync(msm_uport->rx_wakeup.irq);
1299
1300 case MSM_HS_CLK_REQUEST_OFF:
1301 if (msm_uport->rx.flush == FLUSH_STOP ||
1302 msm_uport->rx.flush == FLUSH_SHUTDOWN) {
1303 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_RX);
1304 data = msm_hs_read(uport, UARTDM_DMEN_ADDR);
1305 data |= UARTDM_RX_DM_EN_BMSK;
1306 msm_hs_write(uport, UARTDM_DMEN_ADDR, data);
1307 }
1308 hrtimer_try_to_cancel(&msm_uport->clk_off_timer);
1309 if (msm_uport->rx.flush == FLUSH_SHUTDOWN)
1310 msm_hs_start_rx_locked(uport);
1311 if (!use_low_power_rx_wakeup(msm_uport))
1312 set_rfr_locked(uport, 1);
1313 if (msm_uport->rx.flush == FLUSH_STOP)
1314 msm_uport->rx.flush = FLUSH_IGNORE;
1315 msm_uport->clk_state = MSM_HS_CLK_ON;
1316 break;
1317 case MSM_HS_CLK_ON:
1318 break;
1319 case MSM_HS_CLK_PORT_OFF:
1320 break;
1321 }
1322}
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333void msm_hs_request_clock_on(struct uart_port *uport)
1334{
1335 unsigned long flags;
1336
1337 spin_lock_irqsave(&uport->lock, flags);
1338 msm_hs_request_clock_on_locked(uport);
1339 spin_unlock_irqrestore(&uport->lock, flags);
1340}
1341
1342static irqreturn_t msm_hs_rx_wakeup_isr(int irq, void *dev)
1343{
1344 unsigned int wakeup = 0;
1345 unsigned long flags;
1346 struct msm_hs_port *msm_uport = dev;
1347 struct uart_port *uport = &msm_uport->uport;
1348
1349 spin_lock_irqsave(&uport->lock, flags);
1350 if (msm_uport->clk_state == MSM_HS_CLK_OFF) {
1351
1352
1353 if (msm_uport->rx_wakeup.ignore)
1354 msm_uport->rx_wakeup.ignore = 0;
1355 else
1356 wakeup = 1;
1357 }
1358
1359 if (wakeup) {
1360
1361
1362 msm_hs_request_clock_on_locked(uport);
1363 if (msm_uport->rx_wakeup.inject_rx) {
1364 tty_insert_flip_char(&uport->state->port,
1365 msm_uport->rx_wakeup.rx_to_inject,
1366 TTY_NORMAL);
1367 queue_work(msm_hs_workqueue, &msm_uport->rx.tty_work);
1368 }
1369 }
1370
1371 spin_unlock_irqrestore(&uport->lock, flags);
1372
1373 return IRQ_HANDLED;
1374}
1375
1376static const char *msm_hs_type(struct uart_port *port)
1377{
1378 return (port->type == PORT_MSM) ? "MSM_HS_UART" : NULL;
1379}
1380
1381
1382static int msm_hs_startup(struct uart_port *uport)
1383{
1384 int ret;
1385 int rfr_level;
1386 unsigned long flags;
1387 unsigned int data;
1388 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1389 struct circ_buf *tx_buf = &uport->state->xmit;
1390 struct msm_hs_tx *tx = &msm_uport->tx;
1391 struct msm_hs_rx *rx = &msm_uport->rx;
1392
1393 rfr_level = uport->fifosize;
1394 if (rfr_level > 16)
1395 rfr_level -= 16;
1396
1397 tx->dma_base = dma_map_single(uport->dev, tx_buf->buf, UART_XMIT_SIZE,
1398 DMA_TO_DEVICE);
1399
1400
1401
1402 uport->state->port.low_latency = 1;
1403
1404
1405 ret = msm_hs_init_clk_locked(uport);
1406 if (unlikely(ret)) {
1407 printk(KERN_ERR "Turning uartclk failed!\n");
1408 goto err_msm_hs_init_clk;
1409 }
1410
1411
1412 data = msm_hs_read(uport, UARTDM_MR1_ADDR);
1413 data &= ~UARTDM_MR1_AUTO_RFR_LEVEL1_BMSK;
1414 data &= ~UARTDM_MR1_AUTO_RFR_LEVEL0_BMSK;
1415 data |= (UARTDM_MR1_AUTO_RFR_LEVEL1_BMSK & (rfr_level << 2));
1416 data |= (UARTDM_MR1_AUTO_RFR_LEVEL0_BMSK & rfr_level);
1417 msm_hs_write(uport, UARTDM_MR1_ADDR, data);
1418
1419
1420 data = msm_hs_read(uport, UARTDM_IPR_ADDR);
1421 if (!data) {
1422 data |= 0x1f & UARTDM_IPR_STALE_LSB_BMSK;
1423 msm_hs_write(uport, UARTDM_IPR_ADDR, data);
1424 }
1425
1426
1427 data = UARTDM_TX_DM_EN_BMSK | UARTDM_RX_DM_EN_BMSK;
1428 msm_hs_write(uport, UARTDM_DMEN_ADDR, data);
1429
1430
1431 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_TX);
1432 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_RX);
1433 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_ERROR_STATUS);
1434 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_BREAK_INT);
1435 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_STALE_INT);
1436 msm_hs_write(uport, UARTDM_CR_ADDR, RESET_CTS);
1437 msm_hs_write(uport, UARTDM_CR_ADDR, RFR_LOW);
1438
1439 msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_RX_EN_BMSK);
1440
1441
1442 msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_TX_EN_BMSK);
1443
1444
1445 tx->tx_ready_int_en = 0;
1446 tx->dma_in_flight = 0;
1447
1448 tx->xfer.complete_func = msm_hs_dmov_tx_callback;
1449 tx->xfer.execute_func = NULL;
1450
1451 tx->command_ptr->cmd = CMD_LC |
1452 CMD_DST_CRCI(msm_uport->dma_tx_crci) | CMD_MODE_BOX;
1453
1454 tx->command_ptr->src_dst_len = (MSM_UARTDM_BURST_SIZE << 16)
1455 | (MSM_UARTDM_BURST_SIZE);
1456
1457 tx->command_ptr->row_offset = (MSM_UARTDM_BURST_SIZE << 16);
1458
1459 tx->command_ptr->dst_row_addr =
1460 msm_uport->uport.mapbase + UARTDM_TF_ADDR;
1461
1462
1463
1464 rx->xfer.complete_func = msm_hs_dmov_rx_callback;
1465 rx->xfer.execute_func = NULL;
1466
1467 rx->command_ptr->cmd = CMD_LC |
1468 CMD_SRC_CRCI(msm_uport->dma_rx_crci) | CMD_MODE_BOX;
1469
1470 rx->command_ptr->src_dst_len = (MSM_UARTDM_BURST_SIZE << 16)
1471 | (MSM_UARTDM_BURST_SIZE);
1472 rx->command_ptr->row_offset = MSM_UARTDM_BURST_SIZE;
1473 rx->command_ptr->src_row_addr = uport->mapbase + UARTDM_RF_ADDR;
1474
1475
1476 msm_uport->imr_reg |= UARTDM_ISR_RXSTALE_BMSK;
1477
1478 msm_uport->imr_reg |= UARTDM_ISR_CURRENT_CTS_BMSK;
1479
1480 msm_hs_write(uport, UARTDM_TFWR_ADDR, 0);
1481
1482
1483 ret = request_irq(uport->irq, msm_hs_isr, IRQF_TRIGGER_HIGH,
1484 "msm_hs_uart", msm_uport);
1485 if (unlikely(ret)) {
1486 printk(KERN_ERR "Request msm_hs_uart IRQ failed!\n");
1487 goto err_request_irq;
1488 }
1489 if (use_low_power_rx_wakeup(msm_uport)) {
1490 ret = request_irq(msm_uport->rx_wakeup.irq,
1491 msm_hs_rx_wakeup_isr,
1492 IRQF_TRIGGER_FALLING,
1493 "msm_hs_rx_wakeup", msm_uport);
1494 if (unlikely(ret)) {
1495 printk(KERN_ERR "Request msm_hs_rx_wakeup IRQ failed!\n");
1496 free_irq(uport->irq, msm_uport);
1497 goto err_request_irq;
1498 }
1499 disable_irq(msm_uport->rx_wakeup.irq);
1500 }
1501
1502 spin_lock_irqsave(&uport->lock, flags);
1503
1504 msm_hs_write(uport, UARTDM_RFWR_ADDR, 0);
1505 msm_hs_start_rx_locked(uport);
1506
1507 spin_unlock_irqrestore(&uport->lock, flags);
1508 ret = pm_runtime_set_active(uport->dev);
1509 if (ret)
1510 dev_err(uport->dev, "set active error:%d\n", ret);
1511 pm_runtime_enable(uport->dev);
1512
1513 return 0;
1514
1515err_request_irq:
1516err_msm_hs_init_clk:
1517 dma_unmap_single(uport->dev, tx->dma_base,
1518 UART_XMIT_SIZE, DMA_TO_DEVICE);
1519 return ret;
1520}
1521
1522
1523static int uartdm_init_port(struct uart_port *uport)
1524{
1525 int ret = 0;
1526 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1527 struct msm_hs_tx *tx = &msm_uport->tx;
1528 struct msm_hs_rx *rx = &msm_uport->rx;
1529
1530
1531 tx->command_ptr = kmalloc(sizeof(dmov_box), GFP_KERNEL | __GFP_DMA);
1532 if (!tx->command_ptr)
1533 return -ENOMEM;
1534
1535 tx->command_ptr_ptr = kmalloc(sizeof(u32), GFP_KERNEL | __GFP_DMA);
1536 if (!tx->command_ptr_ptr) {
1537 ret = -ENOMEM;
1538 goto err_tx_command_ptr_ptr;
1539 }
1540
1541 tx->mapped_cmd_ptr = dma_map_single(uport->dev, tx->command_ptr,
1542 sizeof(dmov_box), DMA_TO_DEVICE);
1543 tx->mapped_cmd_ptr_ptr = dma_map_single(uport->dev,
1544 tx->command_ptr_ptr,
1545 sizeof(u32), DMA_TO_DEVICE);
1546 tx->xfer.cmdptr = DMOV_CMD_ADDR(tx->mapped_cmd_ptr_ptr);
1547
1548 init_waitqueue_head(&rx->wait);
1549
1550 rx->pool = dma_pool_create("rx_buffer_pool", uport->dev,
1551 UARTDM_RX_BUF_SIZE, 16, 0);
1552 if (!rx->pool) {
1553 pr_err("%s(): cannot allocate rx_buffer_pool", __func__);
1554 ret = -ENOMEM;
1555 goto err_dma_pool_create;
1556 }
1557
1558 rx->buffer = dma_pool_alloc(rx->pool, GFP_KERNEL, &rx->rbuffer);
1559 if (!rx->buffer) {
1560 pr_err("%s(): cannot allocate rx->buffer", __func__);
1561 ret = -ENOMEM;
1562 goto err_dma_pool_alloc;
1563 }
1564
1565
1566 rx->command_ptr = kmalloc(sizeof(dmov_box), GFP_KERNEL | __GFP_DMA);
1567 if (!rx->command_ptr) {
1568 pr_err("%s(): cannot allocate rx->command_ptr", __func__);
1569 ret = -ENOMEM;
1570 goto err_rx_command_ptr;
1571 }
1572
1573 rx->command_ptr_ptr = kmalloc(sizeof(u32), GFP_KERNEL | __GFP_DMA);
1574 if (!rx->command_ptr_ptr) {
1575 pr_err("%s(): cannot allocate rx->command_ptr_ptr", __func__);
1576 ret = -ENOMEM;
1577 goto err_rx_command_ptr_ptr;
1578 }
1579
1580 rx->command_ptr->num_rows = ((UARTDM_RX_BUF_SIZE >> 4) << 16) |
1581 (UARTDM_RX_BUF_SIZE >> 4);
1582
1583 rx->command_ptr->dst_row_addr = rx->rbuffer;
1584
1585 rx->mapped_cmd_ptr = dma_map_single(uport->dev, rx->command_ptr,
1586 sizeof(dmov_box), DMA_TO_DEVICE);
1587
1588 *rx->command_ptr_ptr = CMD_PTR_LP | DMOV_CMD_ADDR(rx->mapped_cmd_ptr);
1589
1590 rx->cmdptr_dmaaddr = dma_map_single(uport->dev, rx->command_ptr_ptr,
1591 sizeof(u32), DMA_TO_DEVICE);
1592 rx->xfer.cmdptr = DMOV_CMD_ADDR(rx->cmdptr_dmaaddr);
1593
1594 INIT_WORK(&rx->tty_work, msm_hs_tty_flip_buffer_work);
1595
1596 return ret;
1597
1598err_rx_command_ptr_ptr:
1599 kfree(rx->command_ptr);
1600err_rx_command_ptr:
1601 dma_pool_free(msm_uport->rx.pool, msm_uport->rx.buffer,
1602 msm_uport->rx.rbuffer);
1603err_dma_pool_alloc:
1604 dma_pool_destroy(msm_uport->rx.pool);
1605err_dma_pool_create:
1606 dma_unmap_single(uport->dev, msm_uport->tx.mapped_cmd_ptr_ptr,
1607 sizeof(u32), DMA_TO_DEVICE);
1608 dma_unmap_single(uport->dev, msm_uport->tx.mapped_cmd_ptr,
1609 sizeof(dmov_box), DMA_TO_DEVICE);
1610 kfree(msm_uport->tx.command_ptr_ptr);
1611err_tx_command_ptr_ptr:
1612 kfree(msm_uport->tx.command_ptr);
1613 return ret;
1614}
1615
1616static int msm_hs_probe(struct platform_device *pdev)
1617{
1618 int ret;
1619 struct uart_port *uport;
1620 struct msm_hs_port *msm_uport;
1621 struct resource *resource;
1622 const struct msm_serial_hs_platform_data *pdata =
1623 pdev->dev.platform_data;
1624
1625 if (pdev->id < 0 || pdev->id >= UARTDM_NR) {
1626 printk(KERN_ERR "Invalid plaform device ID = %d\n", pdev->id);
1627 return -EINVAL;
1628 }
1629
1630 msm_uport = &q_uart_port[pdev->id];
1631 uport = &msm_uport->uport;
1632
1633 uport->dev = &pdev->dev;
1634
1635 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1636 if (unlikely(!resource))
1637 return -ENXIO;
1638
1639 uport->mapbase = resource->start;
1640 uport->irq = platform_get_irq(pdev, 0);
1641 if (unlikely(uport->irq < 0))
1642 return -ENXIO;
1643
1644 if (unlikely(irq_set_irq_wake(uport->irq, 1)))
1645 return -ENXIO;
1646
1647 if (pdata == NULL || pdata->rx_wakeup_irq < 0)
1648 msm_uport->rx_wakeup.irq = -1;
1649 else {
1650 msm_uport->rx_wakeup.irq = pdata->rx_wakeup_irq;
1651 msm_uport->rx_wakeup.ignore = 1;
1652 msm_uport->rx_wakeup.inject_rx = pdata->inject_rx_on_wakeup;
1653 msm_uport->rx_wakeup.rx_to_inject = pdata->rx_to_inject;
1654
1655 if (unlikely(msm_uport->rx_wakeup.irq < 0))
1656 return -ENXIO;
1657
1658 if (unlikely(irq_set_irq_wake(msm_uport->rx_wakeup.irq, 1)))
1659 return -ENXIO;
1660 }
1661
1662 if (pdata == NULL)
1663 msm_uport->exit_lpm_cb = NULL;
1664 else
1665 msm_uport->exit_lpm_cb = pdata->exit_lpm_cb;
1666
1667 resource = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1668 "uartdm_channels");
1669 if (unlikely(!resource))
1670 return -ENXIO;
1671
1672 msm_uport->dma_tx_channel = resource->start;
1673 msm_uport->dma_rx_channel = resource->end;
1674
1675 resource = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1676 "uartdm_crci");
1677 if (unlikely(!resource))
1678 return -ENXIO;
1679
1680 msm_uport->dma_tx_crci = resource->start;
1681 msm_uport->dma_rx_crci = resource->end;
1682
1683 uport->iotype = UPIO_MEM;
1684 uport->fifosize = UART_FIFOSIZE;
1685 uport->ops = &msm_hs_ops;
1686 uport->flags = UPF_BOOT_AUTOCONF;
1687 uport->uartclk = UARTCLK;
1688 msm_uport->imr_reg = 0x0;
1689 msm_uport->clk = clk_get(&pdev->dev, "uartdm_clk");
1690 if (IS_ERR(msm_uport->clk))
1691 return PTR_ERR(msm_uport->clk);
1692
1693 ret = uartdm_init_port(uport);
1694 if (unlikely(ret))
1695 return ret;
1696
1697 msm_uport->clk_state = MSM_HS_CLK_PORT_OFF;
1698 hrtimer_init(&msm_uport->clk_off_timer, CLOCK_MONOTONIC,
1699 HRTIMER_MODE_REL);
1700 msm_uport->clk_off_timer.function = msm_hs_clk_off_retry;
1701 msm_uport->clk_off_delay = ktime_set(0, 1000000);
1702
1703 uport->line = pdev->id;
1704 return uart_add_one_port(&msm_hs_driver, uport);
1705}
1706
1707static int __init msm_serial_hs_init(void)
1708{
1709 int ret, i;
1710
1711
1712 for (i = 0; i < UARTDM_NR; i++)
1713 q_uart_port[i].uport.type = PORT_UNKNOWN;
1714
1715 msm_hs_workqueue = create_singlethread_workqueue("msm_serial_hs");
1716 if (unlikely(!msm_hs_workqueue))
1717 return -ENOMEM;
1718
1719 ret = uart_register_driver(&msm_hs_driver);
1720 if (unlikely(ret)) {
1721 printk(KERN_ERR "%s failed to load\n", __func__);
1722 goto err_uart_register_driver;
1723 }
1724
1725 ret = platform_driver_register(&msm_serial_hs_platform_driver);
1726 if (ret) {
1727 printk(KERN_ERR "%s failed to load\n", __func__);
1728 goto err_platform_driver_register;
1729 }
1730
1731 return ret;
1732
1733err_platform_driver_register:
1734 uart_unregister_driver(&msm_hs_driver);
1735err_uart_register_driver:
1736 destroy_workqueue(msm_hs_workqueue);
1737 return ret;
1738}
1739module_init(msm_serial_hs_init);
1740
1741
1742
1743
1744
1745
1746static void msm_hs_shutdown(struct uart_port *uport)
1747{
1748 unsigned long flags;
1749 struct msm_hs_port *msm_uport = UARTDM_TO_MSM(uport);
1750
1751 BUG_ON(msm_uport->rx.flush < FLUSH_STOP);
1752
1753 spin_lock_irqsave(&uport->lock, flags);
1754 clk_enable(msm_uport->clk);
1755
1756
1757 msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_TX_DISABLE_BMSK);
1758
1759 msm_hs_write(uport, UARTDM_CR_ADDR, UARTDM_CR_RX_DISABLE_BMSK);
1760
1761 pm_runtime_disable(uport->dev);
1762 pm_runtime_set_suspended(uport->dev);
1763
1764
1765 free_irq(uport->irq, msm_uport);
1766 if (use_low_power_rx_wakeup(msm_uport))
1767 free_irq(msm_uport->rx_wakeup.irq, msm_uport);
1768
1769 msm_uport->imr_reg = 0;
1770 msm_hs_write(uport, UARTDM_IMR_ADDR, msm_uport->imr_reg);
1771
1772 wait_event(msm_uport->rx.wait, msm_uport->rx.flush == FLUSH_SHUTDOWN);
1773
1774 clk_disable(msm_uport->clk);
1775 if (msm_uport->clk_state != MSM_HS_CLK_OFF)
1776 clk_disable(msm_uport->clk);
1777 msm_uport->clk_state = MSM_HS_CLK_PORT_OFF;
1778
1779 dma_unmap_single(uport->dev, msm_uport->tx.dma_base,
1780 UART_XMIT_SIZE, DMA_TO_DEVICE);
1781
1782 spin_unlock_irqrestore(&uport->lock, flags);
1783
1784 if (cancel_work_sync(&msm_uport->rx.tty_work))
1785 msm_hs_tty_flip_buffer_work(&msm_uport->rx.tty_work);
1786}
1787
1788static void __exit msm_serial_hs_exit(void)
1789{
1790 flush_workqueue(msm_hs_workqueue);
1791 destroy_workqueue(msm_hs_workqueue);
1792 platform_driver_unregister(&msm_serial_hs_platform_driver);
1793 uart_unregister_driver(&msm_hs_driver);
1794}
1795module_exit(msm_serial_hs_exit);
1796
1797#ifdef CONFIG_PM_RUNTIME
1798static int msm_hs_runtime_idle(struct device *dev)
1799{
1800
1801
1802
1803
1804 return 0;
1805}
1806
1807static int msm_hs_runtime_resume(struct device *dev)
1808{
1809 struct platform_device *pdev = container_of(dev, struct
1810 platform_device, dev);
1811 struct msm_hs_port *msm_uport = &q_uart_port[pdev->id];
1812
1813 msm_hs_request_clock_on(&msm_uport->uport);
1814 return 0;
1815}
1816
1817static int msm_hs_runtime_suspend(struct device *dev)
1818{
1819 struct platform_device *pdev = container_of(dev, struct
1820 platform_device, dev);
1821 struct msm_hs_port *msm_uport = &q_uart_port[pdev->id];
1822
1823 msm_hs_request_clock_off(&msm_uport->uport);
1824 return 0;
1825}
1826#else
1827#define msm_hs_runtime_idle NULL
1828#define msm_hs_runtime_resume NULL
1829#define msm_hs_runtime_suspend NULL
1830#endif
1831
1832static const struct dev_pm_ops msm_hs_dev_pm_ops = {
1833 .runtime_suspend = msm_hs_runtime_suspend,
1834 .runtime_resume = msm_hs_runtime_resume,
1835 .runtime_idle = msm_hs_runtime_idle,
1836};
1837
1838static struct platform_driver msm_serial_hs_platform_driver = {
1839 .probe = msm_hs_probe,
1840 .remove = msm_hs_remove,
1841 .driver = {
1842 .name = "msm_serial_hs",
1843 .owner = THIS_MODULE,
1844 .pm = &msm_hs_dev_pm_ops,
1845 },
1846};
1847
1848static struct uart_driver msm_hs_driver = {
1849 .owner = THIS_MODULE,
1850 .driver_name = "msm_serial_hs",
1851 .dev_name = "ttyHS",
1852 .nr = UARTDM_NR,
1853 .cons = 0,
1854};
1855
1856static struct uart_ops msm_hs_ops = {
1857 .tx_empty = msm_hs_tx_empty,
1858 .set_mctrl = msm_hs_set_mctrl_locked,
1859 .get_mctrl = msm_hs_get_mctrl_locked,
1860 .stop_tx = msm_hs_stop_tx_locked,
1861 .start_tx = msm_hs_start_tx_locked,
1862 .stop_rx = msm_hs_stop_rx_locked,
1863 .enable_ms = msm_hs_enable_ms_locked,
1864 .break_ctl = msm_hs_break_ctl,
1865 .startup = msm_hs_startup,
1866 .shutdown = msm_hs_shutdown,
1867 .set_termios = msm_hs_set_termios,
1868 .pm = msm_hs_pm,
1869 .type = msm_hs_type,
1870 .config_port = msm_hs_config_port,
1871 .release_port = msm_hs_release_port,
1872 .request_port = msm_hs_request_port,
1873};
1874
1875MODULE_DESCRIPTION("High Speed UART Driver for the MSM chipset");
1876MODULE_VERSION("1.2");
1877MODULE_LICENSE("GPL v2");
1878