.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * |
---|
3 | 4 | * Copyright (C) STMicroelectronics SA 2017 |
---|
4 | 5 | * Author(s): M'boumba Cedric Madianga <cedric.madianga@gmail.com> |
---|
5 | 6 | * Pierre-Yves Mordret <pierre-yves.mordret@st.com> |
---|
6 | 7 | * |
---|
7 | | - * License terms: GPL V2.0. |
---|
8 | | - * |
---|
9 | | - * This program is free software; you can redistribute it and/or modify it |
---|
10 | | - * under the terms of the GNU General Public License version 2 as published by |
---|
11 | | - * the Free Software Foundation. |
---|
12 | | - * |
---|
13 | | - * This program is distributed in the hope that it will be useful, but |
---|
14 | | - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
15 | | - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
16 | | - * details. |
---|
17 | | - * |
---|
18 | 8 | * Driver for STM32 MDMA controller |
---|
19 | 9 | * |
---|
20 | 10 | * Inspired by stm32-dma.c and dma-jz4780.c |
---|
21 | | - * |
---|
22 | 11 | */ |
---|
23 | 12 | |
---|
24 | 13 | #include <linux/clk.h> |
---|
.. | .. |
---|
37 | 26 | #include <linux/of_device.h> |
---|
38 | 27 | #include <linux/of_dma.h> |
---|
39 | 28 | #include <linux/platform_device.h> |
---|
| 29 | +#include <linux/pm_runtime.h> |
---|
40 | 30 | #include <linux/reset.h> |
---|
41 | 31 | #include <linux/slab.h> |
---|
42 | 32 | |
---|
.. | .. |
---|
50 | 40 | STM32_MDMA_SHIFT(mask)) |
---|
51 | 41 | |
---|
52 | 42 | #define STM32_MDMA_GISR0 0x0000 /* MDMA Int Status Reg 1 */ |
---|
53 | | -#define STM32_MDMA_GISR1 0x0004 /* MDMA Int Status Reg 2 */ |
---|
54 | 43 | |
---|
55 | 44 | /* MDMA Channel x interrupt/status register */ |
---|
56 | 45 | #define STM32_MDMA_CISR(x) (0x40 + 0x40 * (x)) /* x = 0..62 */ |
---|
.. | .. |
---|
206 | 195 | |
---|
207 | 196 | #define STM32_MDMA_MAX_BUF_LEN 128 |
---|
208 | 197 | #define STM32_MDMA_MAX_BLOCK_LEN 65536 |
---|
209 | | -#define STM32_MDMA_MAX_CHANNELS 63 |
---|
| 198 | +#define STM32_MDMA_MAX_CHANNELS 32 |
---|
210 | 199 | #define STM32_MDMA_MAX_REQUESTS 256 |
---|
211 | 200 | #define STM32_MDMA_MAX_BURST 128 |
---|
212 | 201 | #define STM32_MDMA_VERY_HIGH_PRIORITY 0x11 |
---|
.. | .. |
---|
283 | 272 | void __iomem *base; |
---|
284 | 273 | struct clk *clk; |
---|
285 | 274 | int irq; |
---|
286 | | - struct reset_control *rst; |
---|
287 | 275 | u32 nr_channels; |
---|
288 | 276 | u32 nr_requests; |
---|
289 | 277 | u32 nr_ahb_addr_masks; |
---|
.. | .. |
---|
1356 | 1344 | static irqreturn_t stm32_mdma_irq_handler(int irq, void *devid) |
---|
1357 | 1345 | { |
---|
1358 | 1346 | struct stm32_mdma_device *dmadev = devid; |
---|
1359 | | - struct stm32_mdma_chan *chan = devid; |
---|
1360 | | - u32 reg, id, ien, status, flag; |
---|
| 1347 | + struct stm32_mdma_chan *chan; |
---|
| 1348 | + u32 reg, id, ccr, ien, status; |
---|
1361 | 1349 | |
---|
1362 | 1350 | /* Find out which channel generates the interrupt */ |
---|
1363 | 1351 | status = readl_relaxed(dmadev->base + STM32_MDMA_GISR0); |
---|
1364 | | - if (status) { |
---|
1365 | | - id = __ffs(status); |
---|
1366 | | - } else { |
---|
1367 | | - status = readl_relaxed(dmadev->base + STM32_MDMA_GISR1); |
---|
1368 | | - if (!status) { |
---|
1369 | | - dev_dbg(mdma2dev(dmadev), "spurious it\n"); |
---|
1370 | | - return IRQ_NONE; |
---|
1371 | | - } |
---|
1372 | | - id = __ffs(status); |
---|
1373 | | - /* |
---|
1374 | | - * As GISR0 provides status for channel id from 0 to 31, |
---|
1375 | | - * so GISR1 provides status for channel id from 32 to 62 |
---|
1376 | | - */ |
---|
1377 | | - id += 32; |
---|
| 1352 | + if (!status) { |
---|
| 1353 | + dev_dbg(mdma2dev(dmadev), "spurious it\n"); |
---|
| 1354 | + return IRQ_NONE; |
---|
1378 | 1355 | } |
---|
| 1356 | + id = __ffs(status); |
---|
1379 | 1357 | |
---|
1380 | 1358 | chan = &dmadev->chan[id]; |
---|
1381 | 1359 | if (!chan) { |
---|
1382 | | - dev_dbg(mdma2dev(dmadev), "MDMA channel not initialized\n"); |
---|
1383 | | - goto exit; |
---|
| 1360 | + dev_warn(mdma2dev(dmadev), "MDMA channel not initialized\n"); |
---|
| 1361 | + return IRQ_NONE; |
---|
1384 | 1362 | } |
---|
1385 | 1363 | |
---|
1386 | 1364 | /* Handle interrupt for the channel */ |
---|
1387 | 1365 | spin_lock(&chan->vchan.lock); |
---|
1388 | | - status = stm32_mdma_read(dmadev, STM32_MDMA_CISR(chan->id)); |
---|
1389 | | - ien = stm32_mdma_read(dmadev, STM32_MDMA_CCR(chan->id)); |
---|
1390 | | - ien &= STM32_MDMA_CCR_IRQ_MASK; |
---|
1391 | | - ien >>= 1; |
---|
| 1366 | + status = stm32_mdma_read(dmadev, STM32_MDMA_CISR(id)); |
---|
| 1367 | + /* Mask Channel ReQuest Active bit which can be set in case of MEM2MEM */ |
---|
| 1368 | + status &= ~STM32_MDMA_CISR_CRQA; |
---|
| 1369 | + ccr = stm32_mdma_read(dmadev, STM32_MDMA_CCR(id)); |
---|
| 1370 | + ien = (ccr & STM32_MDMA_CCR_IRQ_MASK) >> 1; |
---|
1392 | 1371 | |
---|
1393 | 1372 | if (!(status & ien)) { |
---|
1394 | 1373 | spin_unlock(&chan->vchan.lock); |
---|
1395 | | - dev_dbg(chan2dev(chan), |
---|
1396 | | - "spurious it (status=0x%04x, ien=0x%04x)\n", |
---|
1397 | | - status, ien); |
---|
| 1374 | + dev_warn(chan2dev(chan), |
---|
| 1375 | + "spurious it (status=0x%04x, ien=0x%04x)\n", |
---|
| 1376 | + status, ien); |
---|
1398 | 1377 | return IRQ_NONE; |
---|
1399 | 1378 | } |
---|
1400 | 1379 | |
---|
1401 | | - flag = __ffs(status & ien); |
---|
1402 | | - reg = STM32_MDMA_CIFCR(chan->id); |
---|
| 1380 | + reg = STM32_MDMA_CIFCR(id); |
---|
1403 | 1381 | |
---|
1404 | | - switch (1 << flag) { |
---|
1405 | | - case STM32_MDMA_CISR_TEIF: |
---|
1406 | | - id = chan->id; |
---|
1407 | | - status = readl_relaxed(dmadev->base + STM32_MDMA_CESR(id)); |
---|
1408 | | - dev_err(chan2dev(chan), "Transfer Err: stat=0x%08x\n", status); |
---|
| 1382 | + if (status & STM32_MDMA_CISR_TEIF) { |
---|
| 1383 | + dev_err(chan2dev(chan), "Transfer Err: stat=0x%08x\n", |
---|
| 1384 | + readl_relaxed(dmadev->base + STM32_MDMA_CESR(id))); |
---|
1409 | 1385 | stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CTEIF); |
---|
1410 | | - break; |
---|
| 1386 | + status &= ~STM32_MDMA_CISR_TEIF; |
---|
| 1387 | + } |
---|
1411 | 1388 | |
---|
1412 | | - case STM32_MDMA_CISR_CTCIF: |
---|
| 1389 | + if (status & STM32_MDMA_CISR_CTCIF) { |
---|
1413 | 1390 | stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CCTCIF); |
---|
| 1391 | + status &= ~STM32_MDMA_CISR_CTCIF; |
---|
1414 | 1392 | stm32_mdma_xfer_end(chan); |
---|
1415 | | - break; |
---|
| 1393 | + } |
---|
1416 | 1394 | |
---|
1417 | | - case STM32_MDMA_CISR_BRTIF: |
---|
| 1395 | + if (status & STM32_MDMA_CISR_BRTIF) { |
---|
1418 | 1396 | stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CBRTIF); |
---|
1419 | | - break; |
---|
| 1397 | + status &= ~STM32_MDMA_CISR_BRTIF; |
---|
| 1398 | + } |
---|
1420 | 1399 | |
---|
1421 | | - case STM32_MDMA_CISR_BTIF: |
---|
| 1400 | + if (status & STM32_MDMA_CISR_BTIF) { |
---|
1422 | 1401 | stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CBTIF); |
---|
| 1402 | + status &= ~STM32_MDMA_CISR_BTIF; |
---|
1423 | 1403 | chan->curr_hwdesc++; |
---|
1424 | 1404 | if (chan->desc && chan->desc->cyclic) { |
---|
1425 | 1405 | if (chan->curr_hwdesc == chan->desc->count) |
---|
1426 | 1406 | chan->curr_hwdesc = 0; |
---|
1427 | 1407 | vchan_cyclic_callback(&chan->desc->vdesc); |
---|
1428 | 1408 | } |
---|
1429 | | - break; |
---|
| 1409 | + } |
---|
1430 | 1410 | |
---|
1431 | | - case STM32_MDMA_CISR_TCIF: |
---|
| 1411 | + if (status & STM32_MDMA_CISR_TCIF) { |
---|
1432 | 1412 | stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CLTCIF); |
---|
1433 | | - break; |
---|
| 1413 | + status &= ~STM32_MDMA_CISR_TCIF; |
---|
| 1414 | + } |
---|
1434 | 1415 | |
---|
1435 | | - default: |
---|
1436 | | - dev_err(chan2dev(chan), "it %d unhandled (status=0x%04x)\n", |
---|
1437 | | - 1 << flag, status); |
---|
| 1416 | + if (status) { |
---|
| 1417 | + stm32_mdma_set_bits(dmadev, reg, status); |
---|
| 1418 | + dev_err(chan2dev(chan), "DMA error: status=0x%08x\n", status); |
---|
| 1419 | + if (!(ccr & STM32_MDMA_CCR_EN)) |
---|
| 1420 | + dev_err(chan2dev(chan), "chan disabled by HW\n"); |
---|
1438 | 1421 | } |
---|
1439 | 1422 | |
---|
1440 | 1423 | spin_unlock(&chan->vchan.lock); |
---|
1441 | 1424 | |
---|
1442 | | -exit: |
---|
1443 | 1425 | return IRQ_HANDLED; |
---|
1444 | 1426 | } |
---|
1445 | 1427 | |
---|
.. | .. |
---|
1459 | 1441 | return -ENOMEM; |
---|
1460 | 1442 | } |
---|
1461 | 1443 | |
---|
1462 | | - ret = clk_prepare_enable(dmadev->clk); |
---|
1463 | | - if (ret < 0) { |
---|
1464 | | - dev_err(chan2dev(chan), "clk_prepare_enable failed: %d\n", ret); |
---|
| 1444 | + ret = pm_runtime_resume_and_get(dmadev->ddev.dev); |
---|
| 1445 | + if (ret < 0) |
---|
1465 | 1446 | return ret; |
---|
1466 | | - } |
---|
1467 | 1447 | |
---|
1468 | 1448 | ret = stm32_mdma_disable_chan(chan); |
---|
1469 | 1449 | if (ret < 0) |
---|
1470 | | - clk_disable_unprepare(dmadev->clk); |
---|
| 1450 | + pm_runtime_put(dmadev->ddev.dev); |
---|
1471 | 1451 | |
---|
1472 | 1452 | return ret; |
---|
1473 | 1453 | } |
---|
.. | .. |
---|
1487 | 1467 | spin_unlock_irqrestore(&chan->vchan.lock, flags); |
---|
1488 | 1468 | } |
---|
1489 | 1469 | |
---|
1490 | | - clk_disable_unprepare(dmadev->clk); |
---|
| 1470 | + pm_runtime_put(dmadev->ddev.dev); |
---|
1491 | 1471 | vchan_free_chan_resources(to_virt_chan(c)); |
---|
1492 | 1472 | dmam_pool_destroy(chan->desc_pool); |
---|
1493 | 1473 | chan->desc_pool = NULL; |
---|
.. | .. |
---|
1547 | 1527 | struct dma_device *dd; |
---|
1548 | 1528 | struct device_node *of_node; |
---|
1549 | 1529 | struct resource *res; |
---|
| 1530 | + struct reset_control *rst; |
---|
1550 | 1531 | u32 nr_channels, nr_requests; |
---|
1551 | 1532 | int i, count, ret; |
---|
1552 | 1533 | |
---|
.. | .. |
---|
1570 | 1551 | nr_requests); |
---|
1571 | 1552 | } |
---|
1572 | 1553 | |
---|
1573 | | - count = device_property_read_u32_array(&pdev->dev, "st,ahb-addr-masks", |
---|
1574 | | - NULL, 0); |
---|
| 1554 | + count = device_property_count_u32(&pdev->dev, "st,ahb-addr-masks"); |
---|
1575 | 1555 | if (count < 0) |
---|
1576 | 1556 | count = 0; |
---|
1577 | 1557 | |
---|
.. | .. |
---|
1593 | 1573 | return PTR_ERR(dmadev->base); |
---|
1594 | 1574 | |
---|
1595 | 1575 | dmadev->clk = devm_clk_get(&pdev->dev, NULL); |
---|
1596 | | - if (IS_ERR(dmadev->clk)) { |
---|
1597 | | - ret = PTR_ERR(dmadev->clk); |
---|
1598 | | - if (ret == -EPROBE_DEFER) |
---|
1599 | | - dev_info(&pdev->dev, "Missing controller clock\n"); |
---|
| 1576 | + if (IS_ERR(dmadev->clk)) |
---|
| 1577 | + return dev_err_probe(&pdev->dev, PTR_ERR(dmadev->clk), |
---|
| 1578 | + "Missing clock controller\n"); |
---|
| 1579 | + |
---|
| 1580 | + ret = clk_prepare_enable(dmadev->clk); |
---|
| 1581 | + if (ret < 0) { |
---|
| 1582 | + dev_err(&pdev->dev, "clk_prep_enable error: %d\n", ret); |
---|
1600 | 1583 | return ret; |
---|
1601 | 1584 | } |
---|
1602 | 1585 | |
---|
1603 | | - dmadev->rst = devm_reset_control_get(&pdev->dev, NULL); |
---|
1604 | | - if (!IS_ERR(dmadev->rst)) { |
---|
1605 | | - reset_control_assert(dmadev->rst); |
---|
| 1586 | + rst = devm_reset_control_get(&pdev->dev, NULL); |
---|
| 1587 | + if (IS_ERR(rst)) { |
---|
| 1588 | + ret = PTR_ERR(rst); |
---|
| 1589 | + if (ret == -EPROBE_DEFER) |
---|
| 1590 | + goto err_clk; |
---|
| 1591 | + } else { |
---|
| 1592 | + reset_control_assert(rst); |
---|
1606 | 1593 | udelay(2); |
---|
1607 | | - reset_control_deassert(dmadev->rst); |
---|
| 1594 | + reset_control_deassert(rst); |
---|
1608 | 1595 | } |
---|
1609 | 1596 | |
---|
1610 | 1597 | dd = &dmadev->ddev; |
---|
.. | .. |
---|
1624 | 1611 | dd->device_resume = stm32_mdma_resume; |
---|
1625 | 1612 | dd->device_terminate_all = stm32_mdma_terminate_all; |
---|
1626 | 1613 | dd->device_synchronize = stm32_mdma_synchronize; |
---|
| 1614 | + dd->descriptor_reuse = true; |
---|
| 1615 | + |
---|
1627 | 1616 | dd->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | |
---|
1628 | 1617 | BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | |
---|
1629 | 1618 | BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | |
---|
.. | .. |
---|
1648 | 1637 | |
---|
1649 | 1638 | dmadev->irq = platform_get_irq(pdev, 0); |
---|
1650 | 1639 | if (dmadev->irq < 0) { |
---|
1651 | | - dev_err(&pdev->dev, "failed to get IRQ\n"); |
---|
1652 | | - return dmadev->irq; |
---|
| 1640 | + ret = dmadev->irq; |
---|
| 1641 | + goto err_clk; |
---|
1653 | 1642 | } |
---|
1654 | 1643 | |
---|
1655 | 1644 | ret = devm_request_irq(&pdev->dev, dmadev->irq, stm32_mdma_irq_handler, |
---|
1656 | 1645 | 0, dev_name(&pdev->dev), dmadev); |
---|
1657 | 1646 | if (ret) { |
---|
1658 | 1647 | dev_err(&pdev->dev, "failed to request IRQ\n"); |
---|
1659 | | - return ret; |
---|
| 1648 | + goto err_clk; |
---|
1660 | 1649 | } |
---|
1661 | 1650 | |
---|
1662 | | - ret = dma_async_device_register(dd); |
---|
| 1651 | + ret = dmaenginem_async_device_register(dd); |
---|
1663 | 1652 | if (ret) |
---|
1664 | | - return ret; |
---|
| 1653 | + goto err_clk; |
---|
1665 | 1654 | |
---|
1666 | 1655 | ret = of_dma_controller_register(of_node, stm32_mdma_of_xlate, dmadev); |
---|
1667 | 1656 | if (ret < 0) { |
---|
1668 | 1657 | dev_err(&pdev->dev, |
---|
1669 | 1658 | "STM32 MDMA DMA OF registration failed %d\n", ret); |
---|
1670 | | - goto err_unregister; |
---|
| 1659 | + goto err_clk; |
---|
1671 | 1660 | } |
---|
1672 | 1661 | |
---|
1673 | 1662 | platform_set_drvdata(pdev, dmadev); |
---|
| 1663 | + pm_runtime_set_active(&pdev->dev); |
---|
| 1664 | + pm_runtime_enable(&pdev->dev); |
---|
| 1665 | + pm_runtime_get_noresume(&pdev->dev); |
---|
| 1666 | + pm_runtime_put(&pdev->dev); |
---|
1674 | 1667 | |
---|
1675 | 1668 | dev_info(&pdev->dev, "STM32 MDMA driver registered\n"); |
---|
1676 | 1669 | |
---|
1677 | 1670 | return 0; |
---|
1678 | 1671 | |
---|
1679 | | -err_unregister: |
---|
1680 | | - dma_async_device_unregister(dd); |
---|
| 1672 | +err_clk: |
---|
| 1673 | + clk_disable_unprepare(dmadev->clk); |
---|
1681 | 1674 | |
---|
1682 | 1675 | return ret; |
---|
1683 | 1676 | } |
---|
| 1677 | + |
---|
| 1678 | +#ifdef CONFIG_PM |
---|
| 1679 | +static int stm32_mdma_runtime_suspend(struct device *dev) |
---|
| 1680 | +{ |
---|
| 1681 | + struct stm32_mdma_device *dmadev = dev_get_drvdata(dev); |
---|
| 1682 | + |
---|
| 1683 | + clk_disable_unprepare(dmadev->clk); |
---|
| 1684 | + |
---|
| 1685 | + return 0; |
---|
| 1686 | +} |
---|
| 1687 | + |
---|
| 1688 | +static int stm32_mdma_runtime_resume(struct device *dev) |
---|
| 1689 | +{ |
---|
| 1690 | + struct stm32_mdma_device *dmadev = dev_get_drvdata(dev); |
---|
| 1691 | + int ret; |
---|
| 1692 | + |
---|
| 1693 | + ret = clk_prepare_enable(dmadev->clk); |
---|
| 1694 | + if (ret) { |
---|
| 1695 | + dev_err(dev, "failed to prepare_enable clock\n"); |
---|
| 1696 | + return ret; |
---|
| 1697 | + } |
---|
| 1698 | + |
---|
| 1699 | + return 0; |
---|
| 1700 | +} |
---|
| 1701 | +#endif |
---|
| 1702 | + |
---|
| 1703 | +#ifdef CONFIG_PM_SLEEP |
---|
| 1704 | +static int stm32_mdma_pm_suspend(struct device *dev) |
---|
| 1705 | +{ |
---|
| 1706 | + struct stm32_mdma_device *dmadev = dev_get_drvdata(dev); |
---|
| 1707 | + u32 ccr, id; |
---|
| 1708 | + int ret; |
---|
| 1709 | + |
---|
| 1710 | + ret = pm_runtime_resume_and_get(dev); |
---|
| 1711 | + if (ret < 0) |
---|
| 1712 | + return ret; |
---|
| 1713 | + |
---|
| 1714 | + for (id = 0; id < dmadev->nr_channels; id++) { |
---|
| 1715 | + ccr = stm32_mdma_read(dmadev, STM32_MDMA_CCR(id)); |
---|
| 1716 | + if (ccr & STM32_MDMA_CCR_EN) { |
---|
| 1717 | + dev_warn(dev, "Suspend is prevented by Chan %i\n", id); |
---|
| 1718 | + return -EBUSY; |
---|
| 1719 | + } |
---|
| 1720 | + } |
---|
| 1721 | + |
---|
| 1722 | + pm_runtime_put_sync(dev); |
---|
| 1723 | + |
---|
| 1724 | + pm_runtime_force_suspend(dev); |
---|
| 1725 | + |
---|
| 1726 | + return 0; |
---|
| 1727 | +} |
---|
| 1728 | + |
---|
| 1729 | +static int stm32_mdma_pm_resume(struct device *dev) |
---|
| 1730 | +{ |
---|
| 1731 | + return pm_runtime_force_resume(dev); |
---|
| 1732 | +} |
---|
| 1733 | +#endif |
---|
| 1734 | + |
---|
| 1735 | +static const struct dev_pm_ops stm32_mdma_pm_ops = { |
---|
| 1736 | + SET_SYSTEM_SLEEP_PM_OPS(stm32_mdma_pm_suspend, stm32_mdma_pm_resume) |
---|
| 1737 | + SET_RUNTIME_PM_OPS(stm32_mdma_runtime_suspend, |
---|
| 1738 | + stm32_mdma_runtime_resume, NULL) |
---|
| 1739 | +}; |
---|
1684 | 1740 | |
---|
1685 | 1741 | static struct platform_driver stm32_mdma_driver = { |
---|
1686 | 1742 | .probe = stm32_mdma_probe, |
---|
1687 | 1743 | .driver = { |
---|
1688 | 1744 | .name = "stm32-mdma", |
---|
1689 | 1745 | .of_match_table = stm32_mdma_of_match, |
---|
| 1746 | + .pm = &stm32_mdma_pm_ops, |
---|
1690 | 1747 | }, |
---|
1691 | 1748 | }; |
---|
1692 | 1749 | |
---|