mpc52xx_spi.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * (C) Copyright 2009
  3. * Frank Bodammer <frank.bodammer@gcd-solutions.de>
  4. * (C) Copyright 2009 Semihalf, Grzegorz Bernacki
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <asm/io.h>
  26. #include <malloc.h>
  27. #include <spi.h>
  28. #include <mpc5xxx.h>
  29. void spi_init(void)
  30. {
  31. struct mpc5xxx_spi *spi = (struct mpc5xxx_spi *)MPC5XXX_SPI;
  32. /*
  33. * Its important to use the correct order when initializing the
  34. * registers
  35. */
  36. out_8(&spi->ddr, 0x0F); /* set all SPI pins as output */
  37. out_8(&spi->pdr, 0x00); /* set SS low */
  38. /* SPI is master, SS is general purpose output */
  39. out_8(&spi->cr1, SPI_CR_MSTR | SPI_CR_SPE);
  40. out_8(&spi->cr2, 0x00); /* normal operation */
  41. out_8(&spi->brr, 0x77); /* baud rate: IPB clock / 2048 */
  42. }
  43. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  44. unsigned int max_hz, unsigned int mode)
  45. {
  46. struct spi_slave *slave;
  47. slave = malloc(sizeof(struct spi_slave));
  48. if (!slave)
  49. return NULL;
  50. slave->bus = bus;
  51. slave->cs = cs;
  52. return slave;
  53. }
  54. void spi_free_slave(struct spi_slave *slave)
  55. {
  56. free(slave);
  57. }
  58. int spi_claim_bus(struct spi_slave *slave)
  59. {
  60. return 0;
  61. }
  62. void spi_release_bus(struct spi_slave *slave)
  63. {
  64. return;
  65. }
  66. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  67. void *din, unsigned long flags)
  68. {
  69. struct mpc5xxx_spi *spi = (struct mpc5xxx_spi *)MPC5XXX_SPI;
  70. int i, iter = bitlen >> 3;
  71. const uchar *txp = dout;
  72. uchar *rxp = din;
  73. debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
  74. slave->bus, slave->cs, *(uint *) dout, *(uint *) din, bitlen);
  75. if (flags & SPI_XFER_BEGIN)
  76. setbits_8(&spi->pdr, SPI_PDR_SS);
  77. for (i = 0; i < iter; i++) {
  78. udelay(1000);
  79. debug("spi_xfer: sending %x\n", txp[i]);
  80. out_8(&spi->dr, txp[i]);
  81. while (!(in_8(&spi->sr) & SPI_SR_SPIF)) {
  82. udelay(1000);
  83. if (in_8(&spi->sr) & SPI_SR_WCOL) {
  84. rxp[i] = in_8(&spi->dr);
  85. puts("spi_xfer: write collision\n");
  86. return -1;
  87. }
  88. }
  89. rxp[i] = in_8(&spi->dr);
  90. debug("spi_xfer: received %x\n", rxp[i]);
  91. }
  92. if (flags & SPI_XFER_END)
  93. clrbits_8(&spi->pdr, SPI_PDR_SS);
  94. return 0;
  95. }