817-spi-0004-MLK-21960-2-spi-fspi-dynamically-alloc-AHB-memory.patch 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. From 6e46fe6d9428a41543bd19644de3c9f2611b73bf Mon Sep 17 00:00:00 2001
  2. From: Han Xu <[email protected]>
  3. Date: Wed, 5 Jun 2019 16:43:30 -0500
  4. Subject: [PATCH] MLK-21960-2: spi: fspi: dynamically alloc AHB memory
  5. dynamically allocate AHB memory as needed.
  6. Signed-off-by: Han Xu <[email protected]>
  7. ---
  8. drivers/spi/spi-nxp-fspi.c | 40 ++++++++++++++++++++++++++++++++++------
  9. 1 file changed, 34 insertions(+), 6 deletions(-)
  10. --- a/drivers/spi/spi-nxp-fspi.c
  11. +++ b/drivers/spi/spi-nxp-fspi.c
  12. @@ -307,6 +307,7 @@
  13. #define POLL_TOUT 5000
  14. #define NXP_FSPI_MAX_CHIPSELECT 4
  15. +#define NXP_FSPI_MIN_IOMAP SZ_4M
  16. struct nxp_fspi_devtype_data {
  17. unsigned int rxfifo;
  18. @@ -345,6 +346,8 @@ struct nxp_fspi {
  19. void __iomem *ahb_addr;
  20. u32 memmap_phy;
  21. u32 memmap_phy_size;
  22. + u32 memmap_start;
  23. + u32 memmap_len;
  24. struct clk *clk, *clk_en;
  25. struct device *dev;
  26. struct completion c;
  27. @@ -657,12 +660,35 @@ static void nxp_fspi_select_mem(struct n
  28. f->selected = spi->chip_select;
  29. }
  30. -static void nxp_fspi_read_ahb(struct nxp_fspi *f, const struct spi_mem_op *op)
  31. +static int nxp_fspi_read_ahb(struct nxp_fspi *f, const struct spi_mem_op *op)
  32. {
  33. + u32 start = op->addr.val;
  34. u32 len = op->data.nbytes;
  35. + /* if necessary, ioremap before AHB read */
  36. + if ((!f->ahb_addr) || start < f->memmap_start ||
  37. + start + len > f->memmap_start + f->memmap_len) {
  38. + if (f->ahb_addr)
  39. + iounmap(f->ahb_addr);
  40. +
  41. + f->memmap_start = start;
  42. + f->memmap_len = len > NXP_FSPI_MIN_IOMAP ?
  43. + len : NXP_FSPI_MIN_IOMAP;
  44. +
  45. + f->ahb_addr = ioremap_wc(f->memmap_phy + f->memmap_start,
  46. + f->memmap_len);
  47. +
  48. + if (!f->ahb_addr) {
  49. + dev_err(f->dev, "failed to alloc memory\n");
  50. + return -ENOMEM;
  51. + }
  52. + }
  53. +
  54. /* Read out the data directly from the AHB buffer. */
  55. - memcpy_fromio(op->data.buf.in, (f->ahb_addr + op->addr.val), len);
  56. + memcpy_fromio(op->data.buf.in,
  57. + f->ahb_addr + start - f->memmap_start, len);
  58. +
  59. + return 0;
  60. }
  61. static void nxp_fspi_fill_txfifo(struct nxp_fspi *f,
  62. @@ -822,7 +848,7 @@ static int nxp_fspi_exec_op(struct spi_m
  63. */
  64. if (op->data.nbytes > (f->devtype_data->rxfifo - 4) &&
  65. op->data.dir == SPI_MEM_DATA_IN) {
  66. - nxp_fspi_read_ahb(f, op);
  67. + err = nxp_fspi_read_ahb(f, op);
  68. } else {
  69. if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT)
  70. nxp_fspi_fill_txfifo(f, op);
  71. @@ -999,9 +1025,8 @@ static int nxp_fspi_probe(struct platfor
  72. /* find the resources - controller memory mapped space */
  73. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fspi_mmap");
  74. - f->ahb_addr = devm_ioremap_resource(dev, res);
  75. - if (IS_ERR(f->ahb_addr)) {
  76. - ret = PTR_ERR(f->ahb_addr);
  77. + if (IS_ERR(res)) {
  78. + ret = PTR_ERR(res);
  79. goto err_put_ctrl;
  80. }
  81. @@ -1080,6 +1105,9 @@ static int nxp_fspi_remove(struct platfo
  82. mutex_destroy(&f->lock);
  83. + if (f->ahb_addr)
  84. + iounmap(f->ahb_addr);
  85. +
  86. return 0;
  87. }