qca_7k.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. *
  3. * Copyright (c) 2011, 2012, Qualcomm Atheros Communications Inc.
  4. * Copyright (c) 2014, I2SE GmbH
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software
  7. * for any purpose with or without fee is hereby granted, provided
  8. * that the above copyright notice and this permission notice appear
  9. * in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  12. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  13. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  14. * THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  15. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  16. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  17. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  18. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. *
  20. */
  21. /* This module implements the Qualcomm Atheros SPI protocol for
  22. * kernel-based SPI device.
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/spi/spi.h>
  27. #include "qca_7k.h"
  28. void
  29. qcaspi_spi_error(struct qcaspi *qca)
  30. {
  31. if (qca->sync != QCASPI_SYNC_READY)
  32. return;
  33. netdev_err(qca->net_dev, "spi error\n");
  34. qca->sync = QCASPI_SYNC_UNKNOWN;
  35. qca->stats.spi_err++;
  36. }
  37. int
  38. qcaspi_read_register(struct qcaspi *qca, u16 reg, u16 *result)
  39. {
  40. __be16 rx_data;
  41. __be16 tx_data;
  42. struct spi_transfer *transfer;
  43. struct spi_message *msg;
  44. int ret;
  45. tx_data = cpu_to_be16(QCA7K_SPI_READ | QCA7K_SPI_INTERNAL | reg);
  46. if (qca->legacy_mode) {
  47. msg = &qca->spi_msg1;
  48. transfer = &qca->spi_xfer1;
  49. transfer->tx_buf = &tx_data;
  50. transfer->rx_buf = NULL;
  51. transfer->len = QCASPI_CMD_LEN;
  52. spi_sync(qca->spi_dev, msg);
  53. } else {
  54. msg = &qca->spi_msg2;
  55. transfer = &qca->spi_xfer2[0];
  56. transfer->tx_buf = &tx_data;
  57. transfer->rx_buf = NULL;
  58. transfer->len = QCASPI_CMD_LEN;
  59. transfer = &qca->spi_xfer2[1];
  60. }
  61. transfer->tx_buf = NULL;
  62. transfer->rx_buf = &rx_data;
  63. transfer->len = QCASPI_CMD_LEN;
  64. ret = spi_sync(qca->spi_dev, msg);
  65. if (!ret)
  66. ret = msg->status;
  67. if (ret)
  68. qcaspi_spi_error(qca);
  69. else
  70. *result = be16_to_cpu(rx_data);
  71. return ret;
  72. }
  73. int
  74. qcaspi_write_register(struct qcaspi *qca, u16 reg, u16 value)
  75. {
  76. __be16 tx_data[2];
  77. struct spi_transfer *transfer;
  78. struct spi_message *msg;
  79. int ret;
  80. tx_data[0] = cpu_to_be16(QCA7K_SPI_WRITE | QCA7K_SPI_INTERNAL | reg);
  81. tx_data[1] = cpu_to_be16(value);
  82. if (qca->legacy_mode) {
  83. msg = &qca->spi_msg1;
  84. transfer = &qca->spi_xfer1;
  85. transfer->tx_buf = &tx_data[0];
  86. transfer->rx_buf = NULL;
  87. transfer->len = QCASPI_CMD_LEN;
  88. spi_sync(qca->spi_dev, msg);
  89. } else {
  90. msg = &qca->spi_msg2;
  91. transfer = &qca->spi_xfer2[0];
  92. transfer->tx_buf = &tx_data[0];
  93. transfer->rx_buf = NULL;
  94. transfer->len = QCASPI_CMD_LEN;
  95. transfer = &qca->spi_xfer2[1];
  96. }
  97. transfer->tx_buf = &tx_data[1];
  98. transfer->rx_buf = NULL;
  99. transfer->len = QCASPI_CMD_LEN;
  100. ret = spi_sync(qca->spi_dev, msg);
  101. if (!ret)
  102. ret = msg->status;
  103. if (ret)
  104. qcaspi_spi_error(qca);
  105. return ret;
  106. }