76 #include <linux/device.h>
82 #include <linux/module.h>
85 #include <linux/slab.h>
91 #define DRIVER_NAME "pl08xdmac"
273 # define PL08X_LLI_TSFR_SIZE 0x2000
276 #define MAX_NUM_TSFR_LLIS (PL08X_LLI_TSFR_SIZE/sizeof(struct pl08x_lli))
277 #define PL08X_ALIGN 8
318 if (plchan->
signal >= 0) {
361 while (pl08x_phy_channel_busy(phychan))
367 "WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
368 "clli=0x%08x, cctl=0x%08x, ccfg=0x%08x\n",
412 for (timeout = 1000; timeout; timeout--) {
413 if (!pl08x_phy_channel_busy(ch))
417 if (pl08x_phy_channel_busy(ch))
418 pr_err(
"pl08x: channel%u timeout waiting for pause\n", ch->
id);
494 BUG_ON(clli < llis_bus || clli >= llis_bus +
501 index = (clli - llis_bus) /
sizeof(
struct pl08x_lli);
504 bytes += get_bytes_in_cctl(llis_va[index].cctl);
509 if (!llis_va[index].lli)
533 for (i = 0; i < pl08x->
vd->channels; i++) {
540 spin_unlock_irqrestore(&ch->
lock, flags);
544 spin_unlock_irqrestore(&ch->
lock, flags);
547 if (i == pl08x->
vd->channels) {
567 static void pl08x_phy_alloc_and_start(
struct pl08x_dma_chan *plchan)
572 ch = pl08x_get_phy_channel(pl08x, plchan);
574 dev_dbg(&pl08x->
adev->dev,
"no physical channel available for xfer on %s\n", plchan->
name);
579 dev_dbg(&pl08x->
adev->dev,
"allocated physical channel %d for xfer on %s\n",
584 pl08x_start_next_txd(plchan);
592 dev_dbg(&pl08x->
adev->dev,
"reassigned physical channel %d for xfer on %s\n",
603 pl08x_start_next_txd(plchan);
634 pl08x_terminate_phy_chan(pl08x, plchan->
phychan);
643 spin_lock(&next->
vc.lock);
647 pl08x_phy_reassign_start(plchan->
phychan, next);
648 spin_unlock(&next->
vc.lock);
655 pl08x_put_phy_channel(pl08x, plchan->
phychan);
666 static inline unsigned int pl08x_get_bytes_for_cctl(
unsigned int coded)
682 static inline u32 pl08x_cctl_bits(
u32 cctl,
u8 srcwidth,
u8 dstwidth,
690 retbits &= ~PL080_CONTROL_TRANSFER_SIZE_MASK;
768 int num_llis,
int len,
u32 cctl)
778 llis_va[num_llis].
lli = llis_bus + (num_llis + 1) *
782 if (cctl & PL080_CONTROL_SRC_INCR)
784 if (cctl & PL080_CONTROL_DST_INCR)
795 *cctl = pl08x_cctl_bits(*cctl, 1, 1, len);
796 pl08x_fill_lli_for_desc(bd, num_llis, len, *cctl);
797 (*total_bytes) += len;
811 u32 cctl, early_bytes = 0;
818 dev_err(&pl08x->
adev->dev,
"%s no memory for llis\n", __func__);
846 pl08x_choose_master_bus(&bd, &mbus, &sbus, cctl);
848 dev_vdbg(&pl08x->
adev->dev,
"src=0x%08x%s/%u dst=0x%08x%s/%u len=%zu\n",
849 bd.
srcbus.addr, cctl & PL080_CONTROL_SRC_INCR ?
"+" :
"",
851 bd.
dstbus.addr, cctl & PL080_CONTROL_DST_INCR ?
"+" :
"",
855 mbus == &bd.
srcbus ?
"src" :
"dst",
856 sbus == &bd.
srcbus ?
"src" :
"dst");
884 dev_err(&pl08x->
adev->dev,
"%s sg len can't be zero",
892 "%s src & dst address must be aligned to src"
893 " & dst width if peripheral is flow controller",
898 cctl = pl08x_cctl_bits(cctl, bd.
srcbus.buswidth,
900 pl08x_fill_lli_for_desc(&bd, num_llis++, 0, cctl);
920 "%s byte width LLIs (remain 0x%08x)\n",
922 prep_byte_width_lli(&bd, &cctl, early_bytes, num_llis++,
933 "%s set down bus width to one byte\n",
943 max_bytes_per_lli = bd.
srcbus.buswidth *
946 "%s max bytes per lli = %zu\n",
947 __func__, max_bytes_per_lli);
954 size_t lli_len, tsize,
width;
969 lli_len = (lli_len /
width) * width;
970 tsize = lli_len / bd.
srcbus.buswidth;
973 "%s fill lli with single lli chunk of "
974 "size 0x%08zx (remainder 0x%08zx)\n",
977 cctl = pl08x_cctl_bits(cctl, bd.
srcbus.buswidth,
978 bd.
dstbus.buswidth, tsize);
979 pl08x_fill_lli_for_desc(&bd, num_llis++,
981 total_bytes += lli_len;
989 "%s align with boundary, send odd bytes (remain %zu)\n",
991 prep_byte_width_lli(&bd, &cctl, bd.
remainder,
992 num_llis++, &total_bytes);
996 if (total_bytes != dsg->
len) {
998 "%s size of encoded lli:s don't match total txd, transferred 0x%08zx from size 0x%08zx\n",
999 __func__, total_bytes, dsg->
len);
1005 "%s need to increase MAX_NUM_TSFR_LLIS from 0x%08x\n",
1013 llis_va[num_llis - 1].
lli = 0;
1017 #ifdef VERBOSE_DEBUG
1022 "%-3s %-9s %-10s %-10s %-10s %s\n",
1023 "lli",
"",
"csrc",
"cdst",
"clli",
"cctl");
1024 for (i = 0; i < num_llis; i++) {
1026 "%3d @%p: 0x%08x 0x%08x 0x%08x 0x%08x\n",
1027 i, &llis_va[i], llis_va[i].
src,
1028 llis_va[i].
dst, llis_va[i].
lli, llis_va[i].
cctl
1053 static void pl08x_unmap_buffers(
struct pl08x_txd *txd)
1055 struct device *
dev = txd->
vd.tx.chan->device->dev;
1083 struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
1087 pl08x_unmap_buffers(txd);
1090 pl08x_release_mux(plchan);
1092 pl08x_free_txd(plchan->
host, txd);
1101 vchan_get_all_descriptors(&plchan->
vc, &
head);
1103 while (!list_empty(&
head)) {
1106 pl08x_desc_free(&txd->
vd);
1113 static int pl08x_alloc_chan_resources(
struct dma_chan *
chan)
1118 static void pl08x_free_chan_resources(
struct dma_chan *
chan)
1121 vchan_free_chan_resources(to_virt_chan(chan));
1125 struct dma_chan *chan,
unsigned long flags)
1142 unsigned long flags;
1146 ret = dma_cookie_status(chan, cookie, txstate);
1161 ret = dma_cookie_status(chan, cookie, txstate);
1172 bytes = pl08x_getbytes_chan(plchan);
1175 spin_unlock_irqrestore(&plchan->
vc.lock, flags);
1181 dma_set_residue(txstate, bytes);
1242 if (!(src & PL08X_AHB1) || ((src & PL08X_AHB2) && !(dst & PL08X_AHB2)))
1248 static u32 pl08x_cctl(
u32 cctl)
1251 PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR |
1272 static u32 pl08x_burst(
u32 maxburst)
1276 for (i = 0; i <
ARRAY_SIZE(burst_sizes); i++)
1280 return burst_sizes[
i].
reg;
1288 width = pl08x_width(addr_width);
1300 if (plchan->
cd->single)
1303 burst = pl08x_burst(maxburst);
1307 return pl08x_cctl(cctl);
1310 static int dma_set_runtime_config(
struct dma_chan *chan,
1332 static void pl08x_issue_pending(
struct dma_chan *chan)
1335 unsigned long flags;
1338 if (vchan_issue_pending(&plchan->
vc)) {
1340 pl08x_phy_alloc_and_start(plchan);
1342 spin_unlock_irqrestore(&plchan->
vc.lock, flags);
1364 size_t len,
unsigned long flags)
1372 txd = pl08x_get_txd(plchan);
1375 "%s no memory for descriptor\n", __func__);
1381 pl08x_free_txd(pl08x, txd);
1382 dev_err(&pl08x->
adev->dev,
"%s no memory for pl080 sg\n",
1394 txd->
cctl = pl08x->
pd->memcpy_channel.cctl_memcpy &
1400 if (pl08x->
vd->dualmaster)
1404 ret = pl08x_fill_llis_for_desc(plchan->
host, txd);
1406 pl08x_free_txd(pl08x, txd);
1410 return vchan_tx_prep(&plchan->
vc, &txd->
vd, flags);
1416 unsigned long flags,
void *
context)
1426 u8 src_buses, dst_buses;
1429 dev_dbg(&pl08x->
adev->dev,
"%s prepare transaction of %d bytes from %s\n",
1432 txd = pl08x_get_txd(plchan);
1434 dev_err(&pl08x->
adev->dev,
"%s no txd\n", __func__);
1445 slave_addr = plchan->
cfg.dst_addr;
1446 addr_width = plchan->
cfg.dst_addr_width;
1447 maxburst = plchan->
cfg.dst_maxburst;
1449 dst_buses = plchan->
cd->periph_buses;
1452 slave_addr = plchan->
cfg.src_addr;
1453 addr_width = plchan->
cfg.src_addr_width;
1454 maxburst = plchan->
cfg.src_maxburst;
1455 src_buses = plchan->
cd->periph_buses;
1458 pl08x_free_txd(pl08x, txd);
1460 "%s direction unsupported\n", __func__);
1464 cctl |= pl08x_get_cctl(plchan, addr_width, maxburst);
1466 pl08x_free_txd(pl08x, txd);
1468 "DMA slave configuration botched?\n");
1472 txd->
cctl = cctl | pl08x_select_bus(src_buses, dst_buses);
1474 if (plchan->
cfg.device_fc)
1483 ret = pl08x_request_mux(plchan);
1485 pl08x_free_txd(pl08x, txd);
1487 "unable to mux for transfer on %s due to platform restrictions\n",
1492 dev_dbg(&pl08x->
adev->dev,
"allocated DMA request signal %d for xfer on %s\n",
1504 pl08x_release_mux(plchan);
1505 pl08x_free_txd(pl08x, txd);
1506 dev_err(&pl08x->
adev->dev,
"%s no mem for pl080 sg\n",
1522 ret = pl08x_fill_llis_for_desc(plchan->
host, txd);
1524 pl08x_release_mux(plchan);
1525 pl08x_free_txd(pl08x, txd);
1529 return vchan_tx_prep(&plchan->
vc, &txd->
vd, flags);
1537 unsigned long flags;
1542 return dma_set_runtime_config(chan,
1552 spin_unlock_irqrestore(&plchan->
vc.lock, flags);
1565 pl08x_phy_free(plchan);
1569 pl08x_desc_free(&plchan->
at->vd);
1573 pl08x_free_txd_list(pl08x, plchan);
1576 pl08x_pause_phy_chan(plchan->
phychan);
1580 pl08x_resume_phy_chan(plchan->
phychan);
1589 spin_unlock_irqrestore(&plchan->
vc.lock, flags);
1597 char *
name = chan_id;
1600 if (chan->
device->dev->driver != &pl08x_amba_driver.drv)
1603 plchan = to_pl08x_chan(chan);
1621 if (pl08x->
vd->nomadik)
1634 dev_err(&pl08x->
adev->dev,
"%s error interrupt, register value 0x%08x\n",
1645 for (i = 0; i < pl08x->
vd->channels; i++) {
1646 if (((1 << i) &
err) || ((1 << i) & tc)) {
1654 "%s Error TC interrupt on unused channel: 0x%08x\n",
1659 spin_lock(&plchan->
vc.lock);
1667 pl08x_release_mux(plchan);
1669 vchan_cookie_complete(&tx->
vd);
1675 if (vchan_next_desc(&plchan->
vc))
1676 pl08x_start_next_txd(plchan);
1678 pl08x_phy_free(plchan);
1680 spin_unlock(&plchan->
vc.lock);
1692 chan->
name = chan->
cd->bus_id;
1693 chan->
cfg.src_addr = chan->
cd->addr;
1694 chan->
cfg.dst_addr = chan->
cd->addr;
1718 "%s no memory for channel\n", __func__);
1727 chan->
cd = &pl08x->
pd->slave_channels[
i];
1728 pl08x_dma_slave_init(chan);
1730 chan->
cd = &pl08x->
pd->memcpy_channel;
1738 "initialize virtual channel \"%s\"\n",
1741 chan->
vc.desc_free = pl08x_desc_free;
1744 dev_info(&pl08x->
adev->dev,
"initialized %d virtual %s channels\n",
1745 i, slave ?
"slave" :
"memcpy");
1749 static void pl08x_free_virtual_channels(
struct dma_device *dmadev)
1755 next, &dmadev->
channels,
vc.chan.device_node) {
1761 #ifdef CONFIG_DEBUG_FS
1776 return "UNKNOWN STATE";
1779 static int pl08x_debugfs_show(
struct seq_file *
s,
void *
data)
1784 unsigned long flags;
1790 for (i = 0; i < pl08x->
vd->channels; i++) {
1800 virt_chan ? virt_chan->
name :
"(none)",
1801 ch->
locked ?
" LOCKED" :
"");
1803 spin_unlock_irqrestore(&ch->
lock, flags);
1806 seq_printf(s,
"\nPL08x virtual memcpy channels:\n");
1811 pl08x_state_str(chan->
state));
1814 seq_printf(s,
"\nPL08x virtual slave channels:\n");
1819 pl08x_state_str(chan->
state));
1831 .
open = pl08x_debugfs_open,
1842 &pl08x_debugfs_operations);
1872 pl08x->
memcpy.device_alloc_chan_resources = pl08x_alloc_chan_resources;
1873 pl08x->
memcpy.device_free_chan_resources = pl08x_free_chan_resources;
1874 pl08x->
memcpy.device_prep_dma_memcpy = pl08x_prep_dma_memcpy;
1875 pl08x->
memcpy.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
1876 pl08x->
memcpy.device_tx_status = pl08x_dma_tx_status;
1877 pl08x->
memcpy.device_issue_pending = pl08x_issue_pending;
1878 pl08x->
memcpy.device_control = pl08x_control;
1883 pl08x->
slave.device_alloc_chan_resources = pl08x_alloc_chan_resources;
1884 pl08x->
slave.device_free_chan_resources = pl08x_free_chan_resources;
1885 pl08x->
slave.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
1886 pl08x->
slave.device_tx_status = pl08x_dma_tx_status;
1887 pl08x->
slave.device_issue_pending = pl08x_issue_pending;
1888 pl08x->
slave.device_prep_slave_sg = pl08x_prep_slave_sg;
1889 pl08x->
slave.device_control = pl08x_control;
1892 pl08x->
pd = dev_get_platdata(&adev->
dev);
1894 dev_err(&adev->
dev,
"no platform data supplied\n");
1896 goto out_no_platdata;
1906 if (pl08x->
vd->dualmaster) {
1916 goto out_no_lli_pool;
1922 goto out_no_ioremap;
1926 pl08x_ensure_on(pl08x);
1935 dev_err(&adev->
dev,
"%s failed to request interrupt %d\n",
1936 __func__, adev->
irq[0]);
1945 "physical channel holders\n",
1948 goto out_no_phychans;
1951 for (i = 0; i < vd->
channels; i++) {
1968 dev_info(&adev->
dev,
"physical channel %d reserved for secure access only\n", i);
1973 dev_dbg(&adev->
dev,
"physical channel %d is %s\n",
1974 i, pl08x_phy_channel_busy(ch) ?
"BUSY" :
"FREE");
1978 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->
memcpy,
1979 pl08x->
vd->channels,
false);
1982 "%s failed to enumerate memcpy channels - %d\n",
1989 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->
slave,
1990 pl08x->
pd->num_slave_channels,
true);
1993 "%s failed to enumerate slave channels - %d\n",
2002 "%s failed to register memcpy as an async device - %d\n",
2004 goto out_no_memcpy_reg;
2010 "%s failed to register slave as an async device - %d\n",
2012 goto out_no_slave_reg;
2016 init_pl08x_debugfs(pl08x);
2017 dev_info(&pl08x->
adev->dev,
"DMA: PL%03x rev%u at 0x%08llx irq %d\n",
2019 (
unsigned long long)adev->
res.start, adev->
irq[0]);
2026 pl08x_free_virtual_channels(&pl08x->
slave);
2028 pl08x_free_virtual_channels(&pl08x->
memcpy);
2059 .dualmaster =
false,
2062 static struct amba_id pl08x_ids[] = {
2067 .data = &vendor_pl080,
2073 .data = &vendor_pl081,
2079 .data = &vendor_nomadik,
2088 .id_table = pl08x_ids,
2089 .probe = pl08x_probe,
2092 static int __init pl08x_init(
void)
2098 "failed to register as an AMBA device (%d)\n",