spi-dw-mmio.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Memory-mapped interface driver for DW SPI Core
  4. *
  5. * Copyright (c) 2010, Octasic semiconductor.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/err.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/slab.h>
  12. #include <linux/spi/spi.h>
  13. #include <linux/scatterlist.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/acpi.h>
  19. #include <linux/property.h>
  20. #include <linux/regmap.h>
  21. #include <linux/reset.h>
  22. #include "spi-dw.h"
  23. #define DRIVER_NAME "dw_spi_mmio"
  24. struct dw_spi_mmio {
  25. struct dw_spi dws;
  26. struct clk *clk;
  27. struct clk *pclk;
  28. void *priv;
  29. struct reset_control *rstc;
  30. };
  31. #define MSCC_CPU_SYSTEM_CTRL_GENERAL_CTRL 0x24
  32. #define OCELOT_IF_SI_OWNER_OFFSET 4
  33. #define JAGUAR2_IF_SI_OWNER_OFFSET 6
  34. #define MSCC_IF_SI_OWNER_MASK GENMASK(1, 0)
  35. #define MSCC_IF_SI_OWNER_SISL 0
  36. #define MSCC_IF_SI_OWNER_SIBM 1
  37. #define MSCC_IF_SI_OWNER_SIMC 2
  38. #define MSCC_SPI_MST_SW_MODE 0x14
  39. #define MSCC_SPI_MST_SW_MODE_SW_PIN_CTRL_MODE BIT(13)
  40. #define MSCC_SPI_MST_SW_MODE_SW_SPI_CS(x) (x << 5)
  41. #define SPARX5_FORCE_ENA 0xa4
  42. #define SPARX5_FORCE_VAL 0xa8
  43. struct dw_spi_mscc {
  44. struct regmap *syscon;
  45. void __iomem *spi_mst; /* Not sparx5 */
  46. };
  47. /*
  48. * The Designware SPI controller (referred to as master in the documentation)
  49. * automatically deasserts chip select when the tx fifo is empty. The chip
  50. * selects then needs to be either driven as GPIOs or, for the first 4 using the
  51. * the SPI boot controller registers. the final chip select is an OR gate
  52. * between the Designware SPI controller and the SPI boot controller.
  53. */
  54. static void dw_spi_mscc_set_cs(struct spi_device *spi, bool enable)
  55. {
  56. struct dw_spi *dws = spi_master_get_devdata(spi->master);
  57. struct dw_spi_mmio *dwsmmio = container_of(dws, struct dw_spi_mmio, dws);
  58. struct dw_spi_mscc *dwsmscc = dwsmmio->priv;
  59. u32 cs = spi->chip_select;
  60. if (cs < 4) {
  61. u32 sw_mode = MSCC_SPI_MST_SW_MODE_SW_PIN_CTRL_MODE;
  62. if (!enable)
  63. sw_mode |= MSCC_SPI_MST_SW_MODE_SW_SPI_CS(BIT(cs));
  64. writel(sw_mode, dwsmscc->spi_mst + MSCC_SPI_MST_SW_MODE);
  65. }
  66. dw_spi_set_cs(spi, enable);
  67. }
  68. static int dw_spi_mscc_init(struct platform_device *pdev,
  69. struct dw_spi_mmio *dwsmmio,
  70. const char *cpu_syscon, u32 if_si_owner_offset)
  71. {
  72. struct dw_spi_mscc *dwsmscc;
  73. dwsmscc = devm_kzalloc(&pdev->dev, sizeof(*dwsmscc), GFP_KERNEL);
  74. if (!dwsmscc)
  75. return -ENOMEM;
  76. dwsmscc->spi_mst = devm_platform_ioremap_resource(pdev, 1);
  77. if (IS_ERR(dwsmscc->spi_mst)) {
  78. dev_err(&pdev->dev, "SPI_MST region map failed\n");
  79. return PTR_ERR(dwsmscc->spi_mst);
  80. }
  81. dwsmscc->syscon = syscon_regmap_lookup_by_compatible(cpu_syscon);
  82. if (IS_ERR(dwsmscc->syscon))
  83. return PTR_ERR(dwsmscc->syscon);
  84. /* Deassert all CS */
  85. writel(0, dwsmscc->spi_mst + MSCC_SPI_MST_SW_MODE);
  86. /* Select the owner of the SI interface */
  87. regmap_update_bits(dwsmscc->syscon, MSCC_CPU_SYSTEM_CTRL_GENERAL_CTRL,
  88. MSCC_IF_SI_OWNER_MASK << if_si_owner_offset,
  89. MSCC_IF_SI_OWNER_SIMC << if_si_owner_offset);
  90. dwsmmio->dws.set_cs = dw_spi_mscc_set_cs;
  91. dwsmmio->priv = dwsmscc;
  92. return 0;
  93. }
  94. static int dw_spi_mscc_ocelot_init(struct platform_device *pdev,
  95. struct dw_spi_mmio *dwsmmio)
  96. {
  97. return dw_spi_mscc_init(pdev, dwsmmio, "mscc,ocelot-cpu-syscon",
  98. OCELOT_IF_SI_OWNER_OFFSET);
  99. }
  100. static int dw_spi_mscc_jaguar2_init(struct platform_device *pdev,
  101. struct dw_spi_mmio *dwsmmio)
  102. {
  103. return dw_spi_mscc_init(pdev, dwsmmio, "mscc,jaguar2-cpu-syscon",
  104. JAGUAR2_IF_SI_OWNER_OFFSET);
  105. }
  106. /*
  107. * The Designware SPI controller (referred to as master in the
  108. * documentation) automatically deasserts chip select when the tx fifo
  109. * is empty. The chip selects then needs to be driven by a CS override
  110. * register. enable is an active low signal.
  111. */
  112. static void dw_spi_sparx5_set_cs(struct spi_device *spi, bool enable)
  113. {
  114. struct dw_spi *dws = spi_master_get_devdata(spi->master);
  115. struct dw_spi_mmio *dwsmmio = container_of(dws, struct dw_spi_mmio, dws);
  116. struct dw_spi_mscc *dwsmscc = dwsmmio->priv;
  117. u8 cs = spi->chip_select;
  118. if (!enable) {
  119. /* CS override drive enable */
  120. regmap_write(dwsmscc->syscon, SPARX5_FORCE_ENA, 1);
  121. /* Now set CSx enabled */
  122. regmap_write(dwsmscc->syscon, SPARX5_FORCE_VAL, ~BIT(cs));
  123. /* Allow settle */
  124. usleep_range(1, 5);
  125. } else {
  126. /* CS value */
  127. regmap_write(dwsmscc->syscon, SPARX5_FORCE_VAL, ~0);
  128. /* Allow settle */
  129. usleep_range(1, 5);
  130. /* CS override drive disable */
  131. regmap_write(dwsmscc->syscon, SPARX5_FORCE_ENA, 0);
  132. }
  133. dw_spi_set_cs(spi, enable);
  134. }
  135. static int dw_spi_mscc_sparx5_init(struct platform_device *pdev,
  136. struct dw_spi_mmio *dwsmmio)
  137. {
  138. const char *syscon_name = "microchip,sparx5-cpu-syscon";
  139. struct device *dev = &pdev->dev;
  140. struct dw_spi_mscc *dwsmscc;
  141. if (!IS_ENABLED(CONFIG_SPI_MUX)) {
  142. dev_err(dev, "This driver needs CONFIG_SPI_MUX\n");
  143. return -EOPNOTSUPP;
  144. }
  145. dwsmscc = devm_kzalloc(dev, sizeof(*dwsmscc), GFP_KERNEL);
  146. if (!dwsmscc)
  147. return -ENOMEM;
  148. dwsmscc->syscon =
  149. syscon_regmap_lookup_by_compatible(syscon_name);
  150. if (IS_ERR(dwsmscc->syscon)) {
  151. dev_err(dev, "No syscon map %s\n", syscon_name);
  152. return PTR_ERR(dwsmscc->syscon);
  153. }
  154. dwsmmio->dws.set_cs = dw_spi_sparx5_set_cs;
  155. dwsmmio->priv = dwsmscc;
  156. return 0;
  157. }
  158. static int dw_spi_alpine_init(struct platform_device *pdev,
  159. struct dw_spi_mmio *dwsmmio)
  160. {
  161. dwsmmio->dws.caps = DW_SPI_CAP_CS_OVERRIDE;
  162. return 0;
  163. }
  164. static int dw_spi_dw_apb_init(struct platform_device *pdev,
  165. struct dw_spi_mmio *dwsmmio)
  166. {
  167. dw_spi_dma_setup_generic(&dwsmmio->dws);
  168. return 0;
  169. }
  170. static int dw_spi_dwc_ssi_init(struct platform_device *pdev,
  171. struct dw_spi_mmio *dwsmmio)
  172. {
  173. dwsmmio->dws.caps = DW_SPI_CAP_DWC_SSI;
  174. dw_spi_dma_setup_generic(&dwsmmio->dws);
  175. return 0;
  176. }
  177. static int dw_spi_keembay_init(struct platform_device *pdev,
  178. struct dw_spi_mmio *dwsmmio)
  179. {
  180. dwsmmio->dws.caps = DW_SPI_CAP_KEEMBAY_MST | DW_SPI_CAP_DWC_SSI;
  181. return 0;
  182. }
  183. static int dw_spi_mmio_probe(struct platform_device *pdev)
  184. {
  185. int (*init_func)(struct platform_device *pdev,
  186. struct dw_spi_mmio *dwsmmio);
  187. struct dw_spi_mmio *dwsmmio;
  188. struct resource *mem;
  189. struct dw_spi *dws;
  190. int ret;
  191. int num_cs;
  192. dwsmmio = devm_kzalloc(&pdev->dev, sizeof(struct dw_spi_mmio),
  193. GFP_KERNEL);
  194. if (!dwsmmio)
  195. return -ENOMEM;
  196. dws = &dwsmmio->dws;
  197. /* Get basic io resource and map it */
  198. dws->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &mem);
  199. if (IS_ERR(dws->regs))
  200. return PTR_ERR(dws->regs);
  201. dws->paddr = mem->start;
  202. dws->irq = platform_get_irq(pdev, 0);
  203. if (dws->irq < 0)
  204. return dws->irq; /* -ENXIO */
  205. dwsmmio->clk = devm_clk_get(&pdev->dev, NULL);
  206. if (IS_ERR(dwsmmio->clk))
  207. return PTR_ERR(dwsmmio->clk);
  208. ret = clk_prepare_enable(dwsmmio->clk);
  209. if (ret)
  210. return ret;
  211. /* Optional clock needed to access the registers */
  212. dwsmmio->pclk = devm_clk_get_optional(&pdev->dev, "pclk");
  213. if (IS_ERR(dwsmmio->pclk)) {
  214. ret = PTR_ERR(dwsmmio->pclk);
  215. goto out_clk;
  216. }
  217. ret = clk_prepare_enable(dwsmmio->pclk);
  218. if (ret)
  219. goto out_clk;
  220. /* find an optional reset controller */
  221. dwsmmio->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev, "spi");
  222. if (IS_ERR(dwsmmio->rstc)) {
  223. ret = PTR_ERR(dwsmmio->rstc);
  224. goto out_clk;
  225. }
  226. reset_control_deassert(dwsmmio->rstc);
  227. dws->bus_num = pdev->id;
  228. dws->max_freq = clk_get_rate(dwsmmio->clk);
  229. device_property_read_u32(&pdev->dev, "reg-io-width", &dws->reg_io_width);
  230. num_cs = 4;
  231. device_property_read_u32(&pdev->dev, "num-cs", &num_cs);
  232. dws->num_cs = num_cs;
  233. init_func = device_get_match_data(&pdev->dev);
  234. if (init_func) {
  235. ret = init_func(pdev, dwsmmio);
  236. if (ret)
  237. goto out;
  238. }
  239. pm_runtime_enable(&pdev->dev);
  240. ret = dw_spi_add_host(&pdev->dev, dws);
  241. if (ret)
  242. goto out;
  243. platform_set_drvdata(pdev, dwsmmio);
  244. return 0;
  245. out:
  246. pm_runtime_disable(&pdev->dev);
  247. clk_disable_unprepare(dwsmmio->pclk);
  248. out_clk:
  249. clk_disable_unprepare(dwsmmio->clk);
  250. reset_control_assert(dwsmmio->rstc);
  251. return ret;
  252. }
  253. static int dw_spi_mmio_remove(struct platform_device *pdev)
  254. {
  255. struct dw_spi_mmio *dwsmmio = platform_get_drvdata(pdev);
  256. dw_spi_remove_host(&dwsmmio->dws);
  257. pm_runtime_disable(&pdev->dev);
  258. clk_disable_unprepare(dwsmmio->pclk);
  259. clk_disable_unprepare(dwsmmio->clk);
  260. reset_control_assert(dwsmmio->rstc);
  261. return 0;
  262. }
  263. static const struct of_device_id dw_spi_mmio_of_match[] = {
  264. { .compatible = "snps,dw-apb-ssi", .data = dw_spi_dw_apb_init},
  265. { .compatible = "mscc,ocelot-spi", .data = dw_spi_mscc_ocelot_init},
  266. { .compatible = "mscc,jaguar2-spi", .data = dw_spi_mscc_jaguar2_init},
  267. { .compatible = "amazon,alpine-dw-apb-ssi", .data = dw_spi_alpine_init},
  268. { .compatible = "renesas,rzn1-spi", .data = dw_spi_dw_apb_init},
  269. { .compatible = "snps,dwc-ssi-1.01a", .data = dw_spi_dwc_ssi_init},
  270. { .compatible = "intel,keembay-ssi", .data = dw_spi_keembay_init},
  271. { .compatible = "microchip,sparx5-spi", dw_spi_mscc_sparx5_init},
  272. { /* end of table */}
  273. };
  274. MODULE_DEVICE_TABLE(of, dw_spi_mmio_of_match);
  275. #ifdef CONFIG_ACPI
  276. static const struct acpi_device_id dw_spi_mmio_acpi_match[] = {
  277. {"HISI0173", (kernel_ulong_t)dw_spi_dw_apb_init},
  278. {},
  279. };
  280. MODULE_DEVICE_TABLE(acpi, dw_spi_mmio_acpi_match);
  281. #endif
  282. static struct platform_driver dw_spi_mmio_driver = {
  283. .probe = dw_spi_mmio_probe,
  284. .remove = dw_spi_mmio_remove,
  285. .driver = {
  286. .name = DRIVER_NAME,
  287. .of_match_table = dw_spi_mmio_of_match,
  288. .acpi_match_table = ACPI_PTR(dw_spi_mmio_acpi_match),
  289. },
  290. };
  291. module_platform_driver(dw_spi_mmio_driver);
  292. MODULE_AUTHOR("Jean-Hugues Deschenes <jean-hugues.deschenes@octasic.com>");
  293. MODULE_DESCRIPTION("Memory-mapped I/O interface driver for DW SPI Core");
  294. MODULE_LICENSE("GPL v2");