20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
26 #include <linux/serial.h>
27 #include <linux/serial_core.h>
28 #include <linux/module.h>
29 #include <mach/da8xx.h>
30 #include <linux/platform_device.h>
31 #include <linux/firmware.h>
32 #include <linux/clk.h>
33 #include <linux/serial_reg.h>
34 #include <linux/delay.h>
35 #include <linux/ti_omapl_pru_suart.h>
43 #define NR_SUART 2 //8
44 #define DRV_NAME "ti_omapl_pru_suart"
45 #define DRV_DESC "TI PRU SUART Controller Driver v0.1"
46 #define MAX_SUART_RETRIES 100
47 #define SUART_CNTX_SZ 512
48 #define PLATFORM_SUART_RES_SZ 5
49 #define SUART_FIFO_TIMEOUT_DFLT 10 //5
50 #define SUART_FIFO_TIMEOUT_MIN 4
51 #define SUART_FIFO_TIMEOUT_MAX 500
55 #define __suart_debug(fmt, args...) printk(KERN_ERR "suart_debug: " fmt, ## args)
57 #define __suart_debug(fmt, args...)
62 #define __ssc_debug(fmt, args...) printk(fmt, ## args)
64 #define __ssc_debug(fmt, args...)
67 #define __suart_err(fmt, args...) printk(KERN_ERR "suart_err: " fmt, ## args)
69 #if defined(CONFIG_SERIAL_SUART_OMAPL_PRU) && defined(CONFIG_MAGIC_SYSRQ)
76 MODULE_PARM_DESC(suart_timeout,
"fifo timeout in milli seconds (min: 4; max: 500)");
85 static dma_addr_t dma_phys_addr;
86 static void *dma_vaddr_buff;
91 #define BUFFER_SIZE 1024
92 #define BUFFER_MASK (BUFFER_SIZE-1)
100 const struct firmware *
fw;
131 for (i = 0, txready = 1; (i < 10000) && txready; i++) {
149 __stop_tx(soft_uart, port->line);
155 struct circ_buf *xmit = &soft_uart->
write_buf[uart_no];
163 if (down_trylock(&soft_uart->
port_sem[uart_no]))
166 if (uart_circ_empty(xmit) ) {
172 for (count = 0; count <= soft_uart->
tx_loadsz; count++) {
174 count) = xmit->buf[xmit->tail];
176 soft_uart->
port[uart_no].icount.tx++;
177 if (uart_circ_empty(xmit)) {
199 if (uart_circ_empty(xmit)){
200 __stop_tx(soft_uart, uart_no);
210 unsigned int data_len_read = 0;
212 struct circ_buf *buf;
221 data_len + 1 , &data_len_read);
223 if ((rx_status != 0x01) && (rx_status != 0x03))
__ssc_debug(
"R%02x ",rx_status);
233 soft_uart->
port[uart_no].icount.frame++;
236 soft_uart->
port[uart_no].icount.overrun++;
239 soft_uart->
port[uart_no].icount.brk++;
244 rx_status &= (~soft_uart->
port[uart_no].ignore_status_mask
245 & soft_uart->
port[uart_no].read_status_mask);
250 buf = &soft_uart->
read_buf[uart_no];
255 space = CIRC_SPACE(buf->head, buf->tail,
BUFFER_SIZE);
257 if (space < data_len_read)
260 data_len_read = space;
264 for(index = 0; index < data_len_read; index++)
266 buf->buf[buf->head] = suart_data[index];
275 static irqreturn_t omapl_pru_suart_interrupt(
int irq,
void *dev_id)
277 struct uart_port *port = dev_id;
282 unsigned long flags = 0;
283 u16 uartNum = port->line + 1;
285 spin_lock_irqsave(&soft_uart->
port[port->line].lock, flags);
294 (
"suart%d: failed to get interrupt, ret: 0x%X txrx_flag 0x%X\n",
295 port->line, ret, txrx_flag);
296 spin_unlock_irqrestore(&soft_uart->
port[port->line].lock, flags);
302 if ((soft_uart->
port[port->line].ignore_status_mask &
306 omapl_pru_rx_chars(soft_uart, port->line);
314 up(&soft_uart->
port_sem[port->line]);
315 omapl_pru_tx_chars(soft_uart, port->line);
321 spin_unlock_irqrestore(&soft_uart->
port[port->line].lock, flags);
349 __suart_err(
"modem control timer not supported\n");
358 __suart_debug(
"Enter pru_suart_start_tx (%x)\n", port->line);
362 omapl_pru_tx_chars(soft_uart, port->line);
370 __suart_debug(
"Enter pru_suart_tx_empty (%x)\n", port->line);
378 __suart_debug(
"Enter pru_suart_get_mctrl (%x)\n", port->line);
384 __suart_debug(
"Enter pru_suart_set_mctrl (%x)\n", port->line);
392 unsigned long flags = 0;
393 __suart_debug(
"Enter pru_suart_break_ctl (%x)\n", port->line);
395 spin_lock_irqsave(&port->lock, flags);
397 if (break_state == -1)
404 spin_unlock_irqrestore(&port->lock, flags);
408 struct ktermios *termios,
409 struct ktermios *old,
unsigned int baud)
413 unsigned char cval = 0;
414 unsigned long flags = 0;
416 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
418 __suart_debug(
"Enter pru_suart_set_termios (%x)\n", port->line);
424 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR | CSTOPB
425 | PARENB | PARODD | CMSPAR);
426 termios->c_cflag |= CLOCAL;
435 switch (termios->c_cflag & CSIZE) {
450 if ((termios->c_cflag & CSIZE) == CS5) {
451 termios->c_cflag &= ~CSIZE;
452 termios->c_cflag |= old_csize;
458 __suart_err(
"failed to set data bits to: %d\n", cval);
473 spin_lock_irqsave(&port->lock, flags);
487 if (termios->c_iflag & INPCK) {
492 if (termios->c_iflag & (BRKINT | PARMRK)) {
500 port->ignore_status_mask = 0;
501 if (termios->c_iflag & IGNPAR)
503 if (termios->c_iflag & IGNBRK) {
509 if (termios->c_iflag & IGNPAR)
515 if ((termios->c_cflag & CREAD) == 0) {
522 uart_update_timeout(port,termios->c_cflag,baud);
524 spin_unlock_irqrestore(&port->lock, flags);
561 retval = request_irq(port->irq, omapl_pru_suart_interrupt,
562 port->irqflags,
"suart_irq", port);
564 free_irq(port->irq, port);
584 if ((suart_get_duplex(soft_uart, port->line) & ePRU_SUART_HALF_TX)
585 == ePRU_SUART_HALF_TX) {
590 if ((suart_get_duplex(soft_uart, port->line) & ePRU_SUART_HALF_RX)
591 == ePRU_SUART_HALF_RX) {
624 __suart_debug(
"Enter pru_suart_shutdown (%x)\n", port->line);
640 free_irq(port->irq, port);
657 struct platform_device *pdev = to_platform_device(port->dev);
659 __suart_debug(
"Enter pru_suart_release_port (%x)\n", port->line);
662 dev_err(&pdev->dev,
"failed to close suart\n");
683 struct platform_device *pdev = to_platform_device(port->dev);
688 __suart_debug(
"Enter pru_suart_request_port (%x)\n", port->line);
690 if (soft_uart ==
NULL) {
696 dev_err(&pdev->dev,
"failed to open suart: %d\n", err);
751 &pru_suart_config)) {
753 "pru_softuart_setconfig: failed to set config: %X\n",
779 __suart_debug(
"Enter pru_suart_config_port (%x)\n", port->line);
782 port->type = OMAPL_PRU_SUART;
793 struct serial_struct *ser)
799 __suart_debug(
"Enter pru_suart_verify_port (%x)\n", port->line);
801 if (ser->type != PORT_UNKNOWN && ser->type != OMAPL_PRU_SUART)
803 if (soft_uart->
port[port->line].irq != ser->irq)
805 if (ser->io_type != UPIO_MEM)
807 if (soft_uart->
port[port->line].uartclk / 16 != ser->baud_base)
809 if ((
void *)soft_uart->
port[port->line].mapbase != ser->iomem_base)
811 if (soft_uart->
port[port->line].iobase != ser->port)
851 struct ti_pru_suart_platform_data *pdata;
854 unsigned char *fw_data =
NULL;
858 pdata = pdev->dev.platform_data;
860 dev_err(&pdev->dev,
"no platform data provided for pru!\n");
869 res_mem[i] = platform_get_resource(pdev, IORESOURCE_MEM, i);
872 "unable to get pru memory resources!\n");
878 if (!request_mem_region(res_mem[0]->start, resource_size(res_mem[0]),
879 dev_name(&pdev->dev))) {
880 dev_err(&pdev->dev,
"pru memory region already claimed!\n");
884 if (!request_mem_region(res_mem[1]->start, resource_size(res_mem[1]),
885 dev_name(&pdev->dev))) {
886 dev_err(&pdev->dev,
"mcasp memory region already claimed!\n");
890 if (!request_mem_region(res_mem[2]->start, resource_size(res_mem[2]),
891 dev_name(&pdev->dev))) {
892 dev_err(&pdev->dev,
"psc0 memory region already claimed!\n");
896 if (!request_mem_region(res_mem[3]->start, resource_size(res_mem[3]),
897 dev_name(&pdev->dev))) {
898 dev_err(&pdev->dev,
"psc1 memory region already claimed!\n");
904 resource_size(res_mem
907 dev_err(&pdev->dev,
"ioremap failed\n");
909 goto probe_exit_free_region;
912 resource_size(res_mem
915 dev_err(&pdev->dev,
"ioremap failed\n");
917 goto probe_exit_iounmap_1;
920 resource_size(res_mem
923 dev_err(&pdev->dev,
"ioremap failed\n");
925 goto probe_exit_iounmap_2;
928 resource_size(res_mem
931 dev_err(&pdev->dev,
"ioremap failed\n");
933 goto probe_exit_iounmap_3;
936 IO_ADDRESS(DA8XX_SYSCFG0_BASE);
938 dev_err(&pdev->dev,
"ioremap failed\n");
940 goto probe_exit_iounmap_4;
943 soft_uart->
clk_pru = clk_get(&pdev->dev,
"pru_ck");
944 if (IS_ERR(soft_uart->
clk_pru)) {
945 dev_err(&pdev->dev,
"no clock available: pru_ck\n");
946 err = PTR_ERR(soft_uart->
clk_pru);
948 goto probe_exit_iounmap_2;
954 dev_err(&pdev->dev,
"no clock available: mcasp\n");
957 goto probe_exit_clk_pru;
961 clk_enable(soft_uart->
clk_pru);
962 err = request_firmware(&soft_uart->
fw,
"PRU_SUART.bin",
965 dev_err(&pdev->dev,
"can't load firmware\n");
975 fw_data = kmalloc(soft_uart->
fw->size, GFP_KERNEL);
976 memcpy((
void *)fw_data, (
const void *)soft_uart->
fw->data,
977 soft_uart->
fw->size);
979 dma_phys_addr = res_mem[4]->start;
980 dma_vaddr_buff = ioremap(res_mem[4]->start, resource_size(res_mem[4]));
981 if (!dma_vaddr_buff) {
995 dev_err(&pdev->dev,
"pru init error\n");
997 kfree((
const void *)fw_data);
998 goto probe_release_fw;
1000 kfree((
const void *)fw_data);
1004 soft_uart->
port[i].iotype = UPIO_MEM;
1005 soft_uart->
port[i].flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
1006 soft_uart->
port[i].mapbase = res_mem[1]->start;
1007 soft_uart->
port[i].membase =
1009 soft_uart->
port[i].type = OMAPL_PRU_SUART;
1010 soft_uart->
port[i].irq = platform_get_irq(pdev, i);
1011 soft_uart->
port[i].dev = &pdev->dev;
1012 soft_uart->
port[i].irqflags = IRQF_SHARED;
1016 soft_uart->
port[i].custom_divisor = 1;
1017 soft_uart->
port[i].line = i;
1019 spin_lock_init(&soft_uart->
port[i].lock);
1020 soft_uart->
port[i].serial_in =
NULL;
1022 soft_uart->
baud[i] = 0;
1038 soft_uart->
port[i].serial_out =
NULL;
1041 init_MUTEX(&soft_uart->
port_sem[i]);
1043 platform_set_drvdata(pdev, &soft_uart->
port[0]);
1054 release_firmware(soft_uart->
fw);
1059 probe_exit_iounmap_4:
1061 probe_exit_iounmap_3:
1063 probe_exit_iounmap_2:
1065 probe_exit_iounmap_1:
1067 probe_exit_free_region:
1068 release_mem_region(res_mem[3]->start, resource_size(res_mem[3]));
1070 release_mem_region(res_mem[2]->start, resource_size(res_mem[2]));
1072 release_mem_region(res_mem[1]->start, resource_size(res_mem[1]));
1074 release_mem_region(res_mem[0]->start, resource_size(res_mem[0]));
1082 struct ti_pru_suart_platform_data *pdata;
1084 struct resource *res_mem[4];
1090 pdata = pdev->dev.platform_data;
1092 dev_err(&pdev->dev,
"no platform data provided for pru!\n");
1095 platform_set_drvdata(pdev,
NULL);
1097 for (i = 0; i < 4; i++) {
1098 res_mem[i] = platform_get_resource(pdev, IORESOURCE_MEM, i);
1101 "unable to get pru memory resources!\n");
1113 release_firmware(soft_uart->
fw);
1119 clk_disable(soft_uart->
clk_pru);
1124 release_mem_region(res_mem[0]->start, resource_size(res_mem[0]));
1125 release_mem_region(res_mem[1]->start, resource_size(res_mem[1]));
1126 release_mem_region(res_mem[2]->start, resource_size(res_mem[2]));
1127 release_mem_region(res_mem[3]->start, resource_size(res_mem[3]));
1133 #define omapl_pru_suart_suspend NULL
1134 #define omapl_pru__suart_resume NULL
1137 static struct platform_driver serial_omapl_pru_driver = {
1144 .owner = THIS_MODULE,
1154 __suart_debug(
"SUART serial driver - integrated with d_uart - loaded\n");
1160 ret = platform_driver_register(&serial_omapl_pru_driver);
1176 platform_driver_unregister(&serial_omapl_pru_driver);
1178 iounmap(dma_vaddr_buff);
1189 struct circ_buf *buf;
1252 struct ktermios config;
1254 if (baud != soft_uart->
baud[port]) {
1255 soft_uart->
baud[port] = baud;
1258 config.c_cflag = CS8 | CREAD;
1290 struct circ_buf *buf;
1296 space = CIRC_SPACE(buf->head, buf->tail,
BUFFER_SIZE);
1298 if (space < size) size = space;
1300 for(index = 0; index < size; index++)
1302 buf->buf[buf->head] = pdata[index];
1313 struct circ_buf *buf;
1319 space = CIRC_CNT(buf->head, buf->tail,
BUFFER_SIZE);
1333 if (buf->buf[buf->tail] == 0x40) {
1341 if (size > space) size = space;
1343 for(index = 0; index < size; index++)
1345 pdata[index] = buf->buf[buf->tail];
1354 struct circ_buf *buf;
1357 return CIRC_CNT(buf->head, buf->tail,
BUFFER_SIZE);
u8 write_data[NR_SUART][BUFFER_SIZE]
#define CHN_TXRX_IE_MASK_TIMEOUT
#define PRU_SUART8_CONFIG_RX_SER
#define PRU_SUART3_CONFIG_TX_SER
#define PRU_SUART6_CONFIG_TX_SER
#define SUART_FIFO_TIMEOUT_MIN
arm_pru_iomap pru_arm_iomap
void pru_suart_set_mctrl(struct uart_port *port, unsigned int mctrl)
short pru_softuart_setdatabits(suart_handle hUart, unsigned short txDataBits, unsigned short rxDataBits)
#define CHN_TXRX_STATUS_RDY
int lego_pru_uart_init(int port)
unsigned int pru_suart_tx_empty(struct uart_port *port)
#define SUART_GBL_INTR_ERR_MASK
struct uart_port port[NR_SUART]
short pru_softuart_init(unsigned int txBaudValue, unsigned int rxBaudValue, unsigned int oversampling, unsigned char *pru_suart_emu_code, unsigned int fw_size, arm_pru_iomap *pru_arm_iomap1)
void pru_suart_stop_tx(struct uart_port *port)
#define PRU_SUART3_CONFIG_RX_SER
const struct firmware * fw
int __devinit omapl_pru_suart_probe(struct platform_device *pdev)
void pru_set_fifo_timeout(Uint32 timeout)
unsigned char Oversampling
unsigned int pru_clk_freq
void * pFifoBufferPhysBase
int lego_pru_write_bytes(int port, unsigned char *pdata, int size)
unsigned char rxBitsPerChar
int suart_pru_to_host_intr_enable(unsigned short uartNum, unsigned int txrxmode, int s32Flag)
#define PLATFORM_SUART_RES_SZ
u32 port_activated[NR_SUART]
#define omapl_pru_suart_suspend
#define PRU_SUART1_CONFIG_RX_SER
int lego_pru_size_data_rx_buffer(int port)
short pru_softuart_close(suart_handle hUart)
short pru_softuart_clrRxFifo(suart_handle hUart)
unsigned short rxClkDivisor
#define PRU_SUART6_CONFIG_RX_SER
void lego_pru_uart_exit(int port)
struct suart_dma suart_dma_addr[NR_SUART]
void pru_suart_start_tx(struct uart_port *port)
int pru_suart_verify_port(struct uart_port *port, struct serial_struct *ser)
short pru_softuart_getRxStatus(suart_handle hUart)
unsigned char RXSerializer
#define CHN_TXRX_STATUS_FE
#define __suart_debug(fmt, args...)
unsigned short txClkDivisor
#define PRU_SUART2_CONFIG_RX_SER
int pru_intr_clr_isrstatus(unsigned short uartNum, unsigned int txrxmode)
void pru_suart_config_port(struct uart_port *port, int flags)
int lego_pru_uart_get_break_state(int port)
#define CHN_TXRX_IE_MASK_CMPLT
void lego_pru_suart_exit(void)
dma_addr_t dma_phys_addr_tx
void pru_suart_release_port(struct uart_port *port)
int suart_intr_setmask(unsigned short uartNum, unsigned int txrxmode, unsigned int intrmask)
#define CHN_TXRX_STATUS_OVRNERR
#define PRU_SUART4_CONFIG_RX_SER
void pru_suart_shutdown(struct uart_port *port, int exit)
void omapl_pru_suart_exit(void)
#define CHN_TXRX_IE_MASK_BI
int lego_pru_uart_activate(int port)
short pru_softuart_open(suart_handle hSuart)
struct omapl_pru_suart * soft_uart
#define PRU_SUART5_CONFIG_RX_SER
void pru_suart_set_termios(struct uart_port *port, struct ktermios *termios, struct ktermios *old, unsigned int baud)
#define CHN_TXRX_STATUS_BI
#define omapl_pru__suart_resume
int __devexit omapl_pru_suart_remove(struct platform_device *pdev)
One line description of the structure.
#define PRU_SUART2_CONFIG_TX_SER
#define SUART_FIFO_TIMEOUT_MAX
#define CHN_RX_IE_MASK_OVRN
u8 read_data[NR_SUART][BUFFER_SIZE]
void lego_pru_uart_deactivate(int port)
#define PRU_SUART1_CONFIG_TX_SER
MODULE_PARM_DESC(suart_timeout,"fifo timeout in milli seconds (min: 4; max: 500)")
module_param(suart_timeout, int, S_IRUGO)
void pru_suart_enable_ms(struct uart_port *port)
#define SUART_DEFAULT_BAUD
short pru_softuart_getTxStatus(suart_handle hUart)
#define __suart_err(fmt, args...)
suart_struct_handle suart_hdl[NR_SUART]
#define PRU_SUART4_CONFIG_TX_SER
#define CHN_TXRX_STATUS_ERR
int lego_pru_read_bytes(int port, unsigned char *pdata, int size)
struct circ_buf write_buf[NR_SUART]
#define PRU_SUART8_CONFIG_TX_SER
dma_addr_t dma_phys_addr_rx
short pru_softuart_read_data(suart_handle hUart, Uint8 *pDataBuffer, Int32 s32MaxLen, Uint32 *pu32DataRead)
#define SUART_DEFAULT_OVRSMPL
#define PRU_SUART7_CONFIG_RX_SER
int pru_suart_startup(struct uart_port *port, int init)
short pru_softuart_clrRxStatus(suart_handle hUart)
int suart_intr_clrmask(unsigned short uartNum, unsigned int txrxmode, unsigned int intrmask)
u8 sensor_inited[NR_SUART]
#define CHN_TXRX_IE_MASK_FE
One line description of the structure.
short pru_softuart_setconfig(suart_handle hUart, suart_config *configUart)
short pru_softuart_deinit(void)
short pru_softuart_get_isrstatus(unsigned short uartNum, unsigned short *txrxFlag)
#define PRU_SUART_SUCCESS
#define PRU_SUART5_CONFIG_TX_SER
void pru_suart_break_ctl(struct uart_port *port, int break_state)
u8 search_for_first_byte[NR_SUART]
struct semaphore port_sem[NR_SUART]
#define PRU_SUART7_CONFIG_TX_SER
short pru_softuart_setbaud(suart_handle hUart, unsigned short txClkDivisor, unsigned short rxClkDivisor)
short pru_softuart_read(suart_handle hUart, unsigned int *ptDataBuf, unsigned short dataLen)
unsigned char txBitsPerChar
unsigned char TXSerializer
void * pFifoBufferVirtBase
short pru_softuart_stopReceive(suart_handle hUart)
void lego_pru_set_baudrate(int port, unsigned int baud)
unsigned int pru_suart_get_mctrl(struct uart_port *port)
int pru_suart_request_port(struct uart_port *port)
#define __ssc_debug(fmt, args...)
#define SUART_FIFO_TIMEOUT_DFLT
void pru_mcasp_deinit(void)
short pru_softuart_clrTxStatus(suart_handle hUart)
short pru_softuart_write(suart_handle hUart, unsigned int *ptTxDataBuf, unsigned short dataLen)
void pru_suart_stop_rx(struct uart_port *port)
int lego_pru_suart_init(void)
struct circ_buf read_buf[NR_SUART]