spi-cavium-thunderx.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Cavium ThunderX SPI driver.
  3. *
  4. * Copyright (C) 2016 Cavium Inc.
  5. * Authors: Jan Glauber <jglauber@cavium.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/pci.h>
  9. #include <linux/spi/spi.h>
  10. #include "spi-cavium.h"
  11. #define DRV_NAME "spi-thunderx"
  12. #define SYS_FREQ_DEFAULT 700000000 /* 700 Mhz */
  13. static int thunderx_spi_probe(struct pci_dev *pdev,
  14. const struct pci_device_id *ent)
  15. {
  16. struct device *dev = &pdev->dev;
  17. struct spi_master *master;
  18. struct octeon_spi *p;
  19. int ret;
  20. master = spi_alloc_master(dev, sizeof(struct octeon_spi));
  21. if (!master)
  22. return -ENOMEM;
  23. p = spi_master_get_devdata(master);
  24. ret = pcim_enable_device(pdev);
  25. if (ret)
  26. goto error;
  27. ret = pci_request_regions(pdev, DRV_NAME);
  28. if (ret)
  29. goto error;
  30. p->register_base = pcim_iomap(pdev, 0, pci_resource_len(pdev, 0));
  31. if (!p->register_base) {
  32. ret = -EINVAL;
  33. goto error;
  34. }
  35. p->regs.config = 0x1000;
  36. p->regs.status = 0x1008;
  37. p->regs.tx = 0x1010;
  38. p->regs.data = 0x1080;
  39. p->clk = devm_clk_get(dev, NULL);
  40. if (IS_ERR(p->clk)) {
  41. ret = PTR_ERR(p->clk);
  42. goto error;
  43. }
  44. ret = clk_prepare_enable(p->clk);
  45. if (ret)
  46. goto error;
  47. p->sys_freq = clk_get_rate(p->clk);
  48. if (!p->sys_freq)
  49. p->sys_freq = SYS_FREQ_DEFAULT;
  50. dev_info(dev, "Set system clock to %u\n", p->sys_freq);
  51. master->num_chipselect = 4;
  52. master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_CS_HIGH |
  53. SPI_LSB_FIRST | SPI_3WIRE;
  54. master->transfer_one_message = octeon_spi_transfer_one_message;
  55. master->bits_per_word_mask = SPI_BPW_MASK(8);
  56. master->max_speed_hz = OCTEON_SPI_MAX_CLOCK_HZ;
  57. master->dev.of_node = pdev->dev.of_node;
  58. pci_set_drvdata(pdev, master);
  59. ret = devm_spi_register_master(dev, master);
  60. if (ret)
  61. goto error;
  62. return 0;
  63. error:
  64. clk_disable_unprepare(p->clk);
  65. spi_master_put(master);
  66. return ret;
  67. }
  68. static void thunderx_spi_remove(struct pci_dev *pdev)
  69. {
  70. struct spi_master *master = pci_get_drvdata(pdev);
  71. struct octeon_spi *p;
  72. p = spi_master_get_devdata(master);
  73. if (!p)
  74. return;
  75. clk_disable_unprepare(p->clk);
  76. /* Put everything in a known state. */
  77. writeq(0, p->register_base + OCTEON_SPI_CFG(p));
  78. }
  79. static const struct pci_device_id thunderx_spi_pci_id_table[] = {
  80. { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, 0xa00b) },
  81. { 0, }
  82. };
  83. MODULE_DEVICE_TABLE(pci, thunderx_spi_pci_id_table);
  84. static struct pci_driver thunderx_spi_driver = {
  85. .name = DRV_NAME,
  86. .id_table = thunderx_spi_pci_id_table,
  87. .probe = thunderx_spi_probe,
  88. .remove = thunderx_spi_remove,
  89. };
  90. module_pci_driver(thunderx_spi_driver);
  91. MODULE_DESCRIPTION("Cavium, Inc. ThunderX SPI bus driver");
  92. MODULE_AUTHOR("Jan Glauber");
  93. MODULE_LICENSE("GPL");