30 #include <linux/kernel.h>
31 #include <linux/slab.h>
36 #include <linux/module.h>
40 #include <asm/cacheflush.h>
44 #define FRAME_DELAY (1<<12)
49 int tdm_count,
u32 mask,
int packed)
51 pr_debug(
"%s tdm_count=%d mask:0x%08x packed=%d\n", __func__,
52 tdm_count, mask, packed);
64 sport->
regs->mcmc1 = ((tdm_count>>3)-1) << 12;
70 sport->
regs->mtcs1 = 0;
71 sport->
regs->mrcs1 = 0;
72 sport->
regs->mtcs2 = 0;
73 sport->
regs->mrcs2 = 0;
74 sport->
regs->mtcs3 = 0;
75 sport->
regs->mrcs3 = 0;
77 sport->
regs->mcmc1 = 0;
78 sport->
regs->mcmc2 = 0;
80 sport->
regs->mtcs0 = 0;
81 sport->
regs->mrcs0 = 0;
84 sport->
regs->mtcs1 = 0; sport->
regs->mtcs2 = 0; sport->
regs->mtcs3 = 0;
85 sport->
regs->mrcs1 = 0; sport->
regs->mrcs2 = 0; sport->
regs->mrcs3 = 0;
94 unsigned int rcr2,
unsigned int clkdiv,
unsigned int fsdiv)
99 sport->
regs->rcr1 = rcr1;
100 sport->
regs->rcr2 = rcr2;
101 sport->
regs->rclkdiv = clkdiv;
102 sport->
regs->rfsdiv = fsdiv;
111 unsigned int tcr2,
unsigned int clkdiv,
unsigned int fsdiv)
116 sport->
regs->tcr1 = tcr1;
117 sport->
regs->tcr2 = tcr2;
118 sport->
regs->tclkdiv = clkdiv;
119 sport->
regs->tfsdiv = fsdiv;
127 static void setup_desc(
struct dmasg *
desc,
void *
buf,
int fragcount,
128 size_t fragsize,
unsigned int cfg,
129 unsigned int x_count,
unsigned int ycount,
size_t wdsize)
134 for (i = 0; i < fragcount; ++
i) {
147 pr_debug(
"setup desc: desc0=%p, next0=%p, desc1=%p,"
148 "next1=%p\nx_count=%x,y_count=%x,addr=0x%lx,cfs=0x%x\n",
150 desc+1, desc[1].next_desc_addr,
151 desc[0].x_count, desc[0].
y_count,
177 static inline int sport_hook_rx_dummy(
struct sport_device *sport)
179 struct dmasg *desc, temp_desc;
197 while ((get_dma_curr_desc_ptr(sport->
dma_rx_chan) -
226 static inline int sport_tx_dma_start(
struct sport_device *sport,
int dummy)
256 while ((get_dma_curr_desc_ptr(sport->
dma_rx_chan) -
263 sport_tx_dma_start(sport, 1);
264 sport_rx_dma_start(sport, 0);
282 sport_hook_rx_dummy(sport);
296 static inline int sport_hook_tx_dummy(
struct sport_device *sport)
298 struct dmasg *desc, temp_desc;
316 while ((get_dma_curr_desc_ptr(sport->
dma_tx_chan) - \
329 pr_debug(
"%s: tx_run:%d, rx_run:%d\n", __func__,
338 while ((get_dma_curr_desc_ptr(sport->
dma_tx_chan) -
346 sport_tx_dma_start(sport, 0);
348 sport_rx_dma_start(sport, 1);
362 sport_hook_tx_dummy(sport);
376 static inline int compute_wdsize(
size_t wdsize)
390 int fragcount,
size_t fragsize)
397 pr_debug(
"%s buf:%p, frag:%d, fragsize:0x%lx\n", __func__, \
398 buf, fragcount, fragsize);
400 x_count = fragsize / sport->
wdsize;
406 if (x_count >= 0x10000) {
409 for (i = 16; i > 0; i--) {
411 if ((count & (x_count - 1)) == 0) {
412 y_count = count >>
i;
413 if (y_count < 0x10000)
420 pr_debug(
"%s(x_count:0x%x, y_count:0x%x)\n", __func__,
429 fragcount *
sizeof(
struct dmasg), &addr, 0);
433 pr_err(
"Failed to allocate memory for rx desc\n");
447 setup_desc(sport->
dma_rx_desc, buf, fragcount, fragsize,
455 int fragcount,
size_t fragsize)
462 pr_debug(
"%s buf:%p, fragcount:%d, fragsize:0x%lx\n",
463 __func__, buf, fragcount, fragsize);
465 x_count = fragsize/sport->
wdsize;
471 if (x_count >= 0x10000) {
474 for (i = 16; i > 0; i--) {
476 if ((count & (x_count - 1)) == 0) {
477 y_count = count >>
i;
478 if (y_count < 0x10000)
485 pr_debug(
"%s x_count:0x%x, y_count:0x%x\n", __func__,
495 fragcount *
sizeof(
struct dmasg), &addr, 0);
498 pr_err(
"Failed to allocate memory for tx desc\n");
505 cfg = 0x7000 |
DI_EN | compute_wdsize(sport->
wdsize) | \
511 setup_desc(sport->
dma_tx_desc, buf, fragcount, fragsize,
520 static int sport_config_rx_dummy(
struct sport_device *sport)
531 memset(desc, 0, 2 *
sizeof(*desc));
534 pr_err(
"Failed to allocate memory for dummy rx desc\n");
546 memcpy(desc+1, desc,
sizeof(*desc));
552 static int sport_config_tx_dummy(
struct sport_device *sport)
564 memset(desc, 0, 2 *
sizeof(*desc));
567 pr_err(
"Failed to allocate memory for dummy tx desc\n");
580 memcpy(desc+1, desc,
sizeof(*desc));
590 return (
unsigned char *)curr - sport->
rx_buf;
598 return (
unsigned char *)curr - sport->
tx_buf;
605 if (tx == 1 && *frag == sport->
tx_frags)
608 if (tx == 0 && *frag == sport->
rx_frags)
616 if (tx == 1 && *frag == 0)
619 if (tx == 0 && *frag == 0)
624 static int sport_check_status(
struct sport_device *sport,
625 unsigned int *sport_stat,
633 status = sport->
regs->stat;
667 "rx dma %d sts: 0x%04x tx dma %d sts: 0x%04x\n",
677 "curr_rx_desc:0x%p, curr_tx_desc:0x%p\n"
678 "dma_rx_desc:0x%p, dma_tx_desc:0x%p\n"
679 "dummy_rx_desc:0x%p, dummy_tx_desc:0x%p\n",
693 sport_check_status(sport,
NULL, &rx_stat,
NULL);
695 pr_err(
"rx dma is already stopped\n");
705 static irqreturn_t tx_handler(
int irq,
void *dev_id)
710 sport_check_status(sport,
NULL,
NULL, &tx_stat);
711 if (!(tx_stat & DMA_DONE)) {
712 pr_err(
"tx dma is already stopped\n");
723 static irqreturn_t err_handler(
int irq,
void *dev_id)
725 unsigned int status = 0;
729 if (sport_check_status(sport, &status,
NULL,
NULL)) {
730 pr_err(
"error checking status ??");
735 pr_info(
"sport status error:%s%s%s%s\n",
736 status &
TOVF ?
" TOVF" :
"",
737 status &
TUVF ?
" TUVF" :
"",
738 status &
ROVF ?
" ROVF" :
"",
739 status &
RUVF ?
" RUVF" :
"");
740 if (status &
TOVF || status &
TUVF) {
743 sport_tx_dma_start(sport, 0);
745 sport_tx_dma_start(sport, 1);
750 sport_rx_dma_start(sport, 0);
752 sport_rx_dma_start(sport, 1);
756 status = sport->
regs->stat;
807 param->
num = pdev->
id;
810 dev_err(dev,
"no platform_data\n");
817 dev_err(dev,
"no MEM resource\n");
825 dev_err(dev,
"no rx DMA resource\n");
832 dev_err(dev,
"no tx DMA resource\n");
839 dev_err(dev,
"no irq resource\n");
848 unsigned int wdsize,
unsigned int dummy_count,
size_t priv_size)
855 dev_dbg(dev,
"%s enter\n", __func__);
861 ret = sport_config_pdev(pdev, ¶m);
866 dev_err(dev,
"requesting Peripherals failed\n");
872 dev_err(dev,
"failed to allocate for sport device\n");
908 dev_info(dev,
"dma rx:%d tx:%d, err irq:%d, regs:%p\n",
917 dev_err(dev,
"could not alloc priv data %zu bytes\n", priv_size);
926 dev_err(dev,
"failed to allocate dummy buffer\n");
930 ret = sport_config_rx_dummy(sport);
932 dev_err(dev,
"failed to config rx dummy ring\n");
935 ret = sport_config_tx_dummy(sport);
937 dev_err(dev,
"failed to config tx dummy ring\n");
941 platform_set_drvdata(pdev, sport);
984 #if L1_DATA_A_LENGTH != 0
1011 u8 *in_data,
int len)
1013 unsigned short dma_config;
1015 unsigned long flags;
1016 unsigned long wait = 0;
1018 pr_debug(
"%s enter, out_data:%p, in_data:%p len:%d\n", \
1019 __func__, out_data, in_data, len);
1020 pr_debug(
"tcr1:0x%04x, tcr2:0x%04x, tclkdiv:0x%04x, tfsdiv:0x%04x\n"
1021 "mcmc1:0x%04x, mcmc2:0x%04x\n",
1022 sport->
regs->tcr1, sport->
regs->tcr2,
1023 sport->
regs->tclkdiv, sport->
regs->tfsdiv,
1024 sport->
regs->mcmc1, sport->
regs->mcmc2);
1029 set_dma_start_addr(sport->
dma_tx_chan, (
unsigned long)out_data);
1035 if (in_data !=
NULL) {
1037 (
unsigned)(in_data + len));
1040 set_dma_start_addr(sport->
dma_rx_chan, (
unsigned long)in_data);
1052 status = get_dma_curr_irqstat(sport->
dma_tx_chan);
1055 status = get_dma_curr_irqstat(sport->
dma_tx_chan);
1056 pr_debug(
"DMA status:0x%04x\n", status);
1060 status = sport->
regs->stat;
1063 while (!(status &
TXHRE)) {
1064 pr_debug(
"sport status:0x%04x\n", status);
1066 status = *(
unsigned short *)&sport->
regs->stat;
1072 pr_debug(
"sport status:0x%04x\n", status);
1081 if (in_data !=
NULL) {