mclink.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * (C) Copyright 2012
  3. * Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/io.h>
  25. #include <errno.h>
  26. #include <gdsys_fpga.h>
  27. enum {
  28. MCINT_SLAVE_LINK_CHANGED_EV = 1 << 7,
  29. MCINT_TX_ERROR_EV = 1 << 9,
  30. MCINT_TX_BUFFER_FREE = 1 << 10,
  31. MCINT_TX_PACKET_TRANSMITTED_EV = 1 << 11,
  32. MCINT_RX_ERROR_EV = 1 << 13,
  33. MCINT_RX_CONTENT_AVAILABLE = 1 << 14,
  34. MCINT_RX_PACKET_RECEIVED_EV = 1 << 15,
  35. };
  36. int mclink_probe(void)
  37. {
  38. unsigned int k;
  39. int slaves = 0;
  40. for (k = 0; k < CONFIG_SYS_MCLINK_MAX; ++k) {
  41. int timeout = 0;
  42. unsigned int ctr = 0;
  43. u16 mc_status;
  44. FPGA_GET_REG(k, mc_status, &mc_status);
  45. if (!(mc_status & (1 << 15)))
  46. break;
  47. FPGA_SET_REG(k, mc_control, 0x8000);
  48. FPGA_GET_REG(k, mc_status, &mc_status);
  49. while (!(mc_status & (1 << 14))) {
  50. udelay(100);
  51. if (ctr++ > 500) {
  52. timeout = 1;
  53. break;
  54. }
  55. FPGA_GET_REG(k, mc_status, &mc_status);
  56. }
  57. if (timeout)
  58. break;
  59. printf("waited %d us for mclink %d to come up\n", ctr * 100, k);
  60. slaves++;
  61. }
  62. return slaves;
  63. }
  64. int mclink_send(u8 slave, u16 addr, u16 data)
  65. {
  66. unsigned int ctr = 0;
  67. u16 int_status;
  68. u16 rx_cmd_status;
  69. u16 rx_cmd;
  70. /* reset interrupt status */
  71. FPGA_GET_REG(0, mc_int, &int_status);
  72. FPGA_SET_REG(0, mc_int, int_status);
  73. /* send */
  74. FPGA_SET_REG(0, mc_tx_address, addr);
  75. FPGA_SET_REG(0, mc_tx_data, data);
  76. FPGA_SET_REG(0, mc_tx_cmd, (slave & 0x03) << 14);
  77. FPGA_SET_REG(0, mc_control, 0x8001);
  78. /* wait for reply */
  79. FPGA_GET_REG(0, mc_int, &int_status);
  80. while (!(int_status & MCINT_RX_PACKET_RECEIVED_EV)) {
  81. udelay(100);
  82. if (ctr++ > 3)
  83. return -ETIMEDOUT;
  84. FPGA_GET_REG(0, mc_int, &int_status);
  85. }
  86. FPGA_GET_REG(0, mc_rx_cmd_status, &rx_cmd_status);
  87. rx_cmd = (rx_cmd_status >> 12) & 0x03;
  88. if (rx_cmd != 0)
  89. printf("mclink_send: received cmd %d, expected %d\n", rx_cmd,
  90. 0);
  91. return 0;
  92. }
  93. int mclink_receive(u8 slave, u16 addr, u16 *data)
  94. {
  95. u16 rx_cmd_status;
  96. u16 rx_cmd;
  97. u16 int_status;
  98. unsigned int ctr = 0;
  99. /* send read request */
  100. FPGA_SET_REG(0, mc_tx_address, addr);
  101. FPGA_SET_REG(0, mc_tx_cmd,
  102. ((slave & 0x03) << 14) | (1 << 12) | (1 << 0));
  103. FPGA_SET_REG(0, mc_control, 0x8001);
  104. /* wait for reply */
  105. FPGA_GET_REG(0, mc_int, &int_status);
  106. while (!(int_status & MCINT_RX_CONTENT_AVAILABLE)) {
  107. udelay(100);
  108. if (ctr++ > 3)
  109. return -ETIMEDOUT;
  110. FPGA_GET_REG(0, mc_int, &int_status);
  111. }
  112. /* check reply */
  113. FPGA_GET_REG(0, mc_rx_cmd_status, &rx_cmd_status);
  114. if ((rx_cmd_status >> 14) != slave) {
  115. printf("mclink_receive: reply from slave %d, expected %d\n",
  116. rx_cmd_status >> 14, slave);
  117. return -EINVAL;
  118. }
  119. rx_cmd = (rx_cmd_status >> 12) & 0x03;
  120. if (rx_cmd != 1) {
  121. printf("mclink_send: received cmd %d, expected %d\n",
  122. rx_cmd, 1);
  123. return -EIO;
  124. }
  125. FPGA_GET_REG(0, mc_rx_data, data);
  126. return 0;
  127. }