spi-dw-mmio.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Memory-mapped interface driver for DW SPI Core
  3. *
  4. * Copyright (c) 2010, Octasic semiconductor.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/err.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/property.h>
  23. #include <linux/regmap.h>
  24. #include "spi-dw.h"
  25. #define DRIVER_NAME "dw_spi_mmio"
  26. struct dw_spi_mmio {
  27. struct dw_spi dws;
  28. struct clk *clk;
  29. void *priv;
  30. };
  31. #define MSCC_CPU_SYSTEM_CTRL_GENERAL_CTRL 0x24
  32. #define OCELOT_IF_SI_OWNER_MASK GENMASK(5, 4)
  33. #define OCELOT_IF_SI_OWNER_OFFSET 4
  34. #define MSCC_IF_SI_OWNER_SISL 0
  35. #define MSCC_IF_SI_OWNER_SIBM 1
  36. #define MSCC_IF_SI_OWNER_SIMC 2
  37. #define MSCC_SPI_MST_SW_MODE 0x14
  38. #define MSCC_SPI_MST_SW_MODE_SW_PIN_CTRL_MODE BIT(13)
  39. #define MSCC_SPI_MST_SW_MODE_SW_SPI_CS(x) (x << 5)
  40. struct dw_spi_mscc {
  41. struct regmap *syscon;
  42. void __iomem *spi_mst;
  43. };
  44. /*
  45. * The Designware SPI controller (referred to as master in the documentation)
  46. * automatically deasserts chip select when the tx fifo is empty. The chip
  47. * selects then needs to be either driven as GPIOs or, for the first 4 using the
  48. * the SPI boot controller registers. the final chip select is an OR gate
  49. * between the Designware SPI controller and the SPI boot controller.
  50. */
  51. static void dw_spi_mscc_set_cs(struct spi_device *spi, bool enable)
  52. {
  53. struct dw_spi *dws = spi_master_get_devdata(spi->master);
  54. struct dw_spi_mmio *dwsmmio = container_of(dws, struct dw_spi_mmio, dws);
  55. struct dw_spi_mscc *dwsmscc = dwsmmio->priv;
  56. u32 cs = spi->chip_select;
  57. if (cs < 4) {
  58. u32 sw_mode = MSCC_SPI_MST_SW_MODE_SW_PIN_CTRL_MODE;
  59. if (!enable)
  60. sw_mode |= MSCC_SPI_MST_SW_MODE_SW_SPI_CS(BIT(cs));
  61. writel(sw_mode, dwsmscc->spi_mst + MSCC_SPI_MST_SW_MODE);
  62. }
  63. dw_spi_set_cs(spi, enable);
  64. }
  65. static int dw_spi_mscc_init(struct platform_device *pdev,
  66. struct dw_spi_mmio *dwsmmio)
  67. {
  68. struct dw_spi_mscc *dwsmscc;
  69. struct resource *res;
  70. dwsmscc = devm_kzalloc(&pdev->dev, sizeof(*dwsmscc), GFP_KERNEL);
  71. if (!dwsmscc)
  72. return -ENOMEM;
  73. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  74. dwsmscc->spi_mst = devm_ioremap_resource(&pdev->dev, res);
  75. if (IS_ERR(dwsmscc->spi_mst)) {
  76. dev_err(&pdev->dev, "SPI_MST region map failed\n");
  77. return PTR_ERR(dwsmscc->spi_mst);
  78. }
  79. dwsmscc->syscon = syscon_regmap_lookup_by_compatible("mscc,ocelot-cpu-syscon");
  80. if (IS_ERR(dwsmscc->syscon))
  81. return PTR_ERR(dwsmscc->syscon);
  82. /* Deassert all CS */
  83. writel(0, dwsmscc->spi_mst + MSCC_SPI_MST_SW_MODE);
  84. /* Select the owner of the SI interface */
  85. regmap_update_bits(dwsmscc->syscon, MSCC_CPU_SYSTEM_CTRL_GENERAL_CTRL,
  86. OCELOT_IF_SI_OWNER_MASK,
  87. MSCC_IF_SI_OWNER_SIMC << OCELOT_IF_SI_OWNER_OFFSET);
  88. dwsmmio->dws.set_cs = dw_spi_mscc_set_cs;
  89. dwsmmio->priv = dwsmscc;
  90. return 0;
  91. }
  92. static int dw_spi_mmio_probe(struct platform_device *pdev)
  93. {
  94. int (*init_func)(struct platform_device *pdev,
  95. struct dw_spi_mmio *dwsmmio);
  96. struct dw_spi_mmio *dwsmmio;
  97. struct dw_spi *dws;
  98. struct resource *mem;
  99. int ret;
  100. int num_cs;
  101. dwsmmio = devm_kzalloc(&pdev->dev, sizeof(struct dw_spi_mmio),
  102. GFP_KERNEL);
  103. if (!dwsmmio)
  104. return -ENOMEM;
  105. dws = &dwsmmio->dws;
  106. /* Get basic io resource and map it */
  107. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  108. dws->regs = devm_ioremap_resource(&pdev->dev, mem);
  109. if (IS_ERR(dws->regs)) {
  110. dev_err(&pdev->dev, "SPI region map failed\n");
  111. return PTR_ERR(dws->regs);
  112. }
  113. dws->irq = platform_get_irq(pdev, 0);
  114. if (dws->irq < 0) {
  115. dev_err(&pdev->dev, "no irq resource?\n");
  116. return dws->irq; /* -ENXIO */
  117. }
  118. dwsmmio->clk = devm_clk_get(&pdev->dev, NULL);
  119. if (IS_ERR(dwsmmio->clk))
  120. return PTR_ERR(dwsmmio->clk);
  121. ret = clk_prepare_enable(dwsmmio->clk);
  122. if (ret)
  123. return ret;
  124. dws->bus_num = pdev->id;
  125. dws->max_freq = clk_get_rate(dwsmmio->clk);
  126. device_property_read_u32(&pdev->dev, "reg-io-width", &dws->reg_io_width);
  127. num_cs = 4;
  128. device_property_read_u32(&pdev->dev, "num-cs", &num_cs);
  129. dws->num_cs = num_cs;
  130. if (pdev->dev.of_node) {
  131. int i;
  132. for (i = 0; i < dws->num_cs; i++) {
  133. int cs_gpio = of_get_named_gpio(pdev->dev.of_node,
  134. "cs-gpios", i);
  135. if (cs_gpio == -EPROBE_DEFER) {
  136. ret = cs_gpio;
  137. goto out;
  138. }
  139. if (gpio_is_valid(cs_gpio)) {
  140. ret = devm_gpio_request(&pdev->dev, cs_gpio,
  141. dev_name(&pdev->dev));
  142. if (ret)
  143. goto out;
  144. }
  145. }
  146. }
  147. init_func = device_get_match_data(&pdev->dev);
  148. if (init_func) {
  149. ret = init_func(pdev, dwsmmio);
  150. if (ret)
  151. goto out;
  152. }
  153. ret = dw_spi_add_host(&pdev->dev, dws);
  154. if (ret)
  155. goto out;
  156. platform_set_drvdata(pdev, dwsmmio);
  157. return 0;
  158. out:
  159. clk_disable_unprepare(dwsmmio->clk);
  160. return ret;
  161. }
  162. static int dw_spi_mmio_remove(struct platform_device *pdev)
  163. {
  164. struct dw_spi_mmio *dwsmmio = platform_get_drvdata(pdev);
  165. dw_spi_remove_host(&dwsmmio->dws);
  166. clk_disable_unprepare(dwsmmio->clk);
  167. return 0;
  168. }
  169. static const struct of_device_id dw_spi_mmio_of_match[] = {
  170. { .compatible = "snps,dw-apb-ssi", },
  171. { .compatible = "mscc,ocelot-spi", .data = dw_spi_mscc_init},
  172. { /* end of table */}
  173. };
  174. MODULE_DEVICE_TABLE(of, dw_spi_mmio_of_match);
  175. static struct platform_driver dw_spi_mmio_driver = {
  176. .probe = dw_spi_mmio_probe,
  177. .remove = dw_spi_mmio_remove,
  178. .driver = {
  179. .name = DRIVER_NAME,
  180. .of_match_table = dw_spi_mmio_of_match,
  181. },
  182. };
  183. module_platform_driver(dw_spi_mmio_driver);
  184. MODULE_AUTHOR("Jean-Hugues Deschenes <jean-hugues.deschenes@octasic.com>");
  185. MODULE_DESCRIPTION("Memory-mapped I/O interface driver for DW SPI Core");
  186. MODULE_LICENSE("GPL v2");