exynos_spi.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * (C) Copyright 2012 SAMSUNG Electronics
  3. * Padmavathi Venna <padma.v@samsung.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <common.h>
  20. #include <malloc.h>
  21. #include <spi.h>
  22. #include <asm/arch/clk.h>
  23. #include <asm/arch/clock.h>
  24. #include <asm/arch/cpu.h>
  25. #include <asm/arch/gpio.h>
  26. #include <asm/arch/pinmux.h>
  27. #include <asm/arch-exynos/spi.h>
  28. #include <asm/io.h>
  29. /* Information about each SPI controller */
  30. struct spi_bus {
  31. enum periph_id periph_id;
  32. s32 frequency; /* Default clock frequency, -1 for none */
  33. struct exynos_spi *regs;
  34. int inited; /* 1 if this bus is ready for use */
  35. };
  36. /* A list of spi buses that we know about */
  37. static struct spi_bus spi_bus[EXYNOS5_SPI_NUM_CONTROLLERS];
  38. struct exynos_spi_slave {
  39. struct spi_slave slave;
  40. struct exynos_spi *regs;
  41. unsigned int freq; /* Default frequency */
  42. unsigned int mode;
  43. enum periph_id periph_id; /* Peripheral ID for this device */
  44. unsigned int fifo_size;
  45. };
  46. static struct spi_bus *spi_get_bus(unsigned dev_index)
  47. {
  48. if (dev_index < EXYNOS5_SPI_NUM_CONTROLLERS)
  49. return &spi_bus[dev_index];
  50. debug("%s: invalid bus %d", __func__, dev_index);
  51. return NULL;
  52. }
  53. static inline struct exynos_spi_slave *to_exynos_spi(struct spi_slave *slave)
  54. {
  55. return container_of(slave, struct exynos_spi_slave, slave);
  56. }
  57. /**
  58. * Setup the driver private data
  59. *
  60. * @param bus ID of the bus that the slave is attached to
  61. * @param cs ID of the chip select connected to the slave
  62. * @param max_hz Required spi frequency
  63. * @param mode Required spi mode (clk polarity, clk phase and
  64. * master or slave)
  65. * @return new device or NULL
  66. */
  67. struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
  68. unsigned int max_hz, unsigned int mode)
  69. {
  70. struct exynos_spi_slave *spi_slave;
  71. struct spi_bus *bus;
  72. if (!spi_cs_is_valid(busnum, cs)) {
  73. debug("%s: Invalid bus/chip select %d, %d\n", __func__,
  74. busnum, cs);
  75. return NULL;
  76. }
  77. spi_slave = malloc(sizeof(*spi_slave));
  78. if (!spi_slave) {
  79. debug("%s: Could not allocate spi_slave\n", __func__);
  80. return NULL;
  81. }
  82. bus = &spi_bus[busnum];
  83. spi_slave->slave.bus = busnum;
  84. spi_slave->slave.cs = cs;
  85. spi_slave->regs = bus->regs;
  86. spi_slave->mode = mode;
  87. spi_slave->periph_id = bus->periph_id;
  88. if (bus->periph_id == PERIPH_ID_SPI1 ||
  89. bus->periph_id == PERIPH_ID_SPI2)
  90. spi_slave->fifo_size = 64;
  91. else
  92. spi_slave->fifo_size = 256;
  93. spi_slave->freq = bus->frequency;
  94. if (max_hz)
  95. spi_slave->freq = min(max_hz, spi_slave->freq);
  96. return &spi_slave->slave;
  97. }
  98. /**
  99. * Free spi controller
  100. *
  101. * @param slave Pointer to spi_slave to which controller has to
  102. * communicate with
  103. */
  104. void spi_free_slave(struct spi_slave *slave)
  105. {
  106. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  107. free(spi_slave);
  108. }
  109. /**
  110. * Flush spi tx, rx fifos and reset the SPI controller
  111. *
  112. * @param slave Pointer to spi_slave to which controller has to
  113. * communicate with
  114. */
  115. static void spi_flush_fifo(struct spi_slave *slave)
  116. {
  117. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  118. struct exynos_spi *regs = spi_slave->regs;
  119. clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
  120. clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
  121. setbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
  122. }
  123. /**
  124. * Initialize the spi base registers, set the required clock frequency and
  125. * initialize the gpios
  126. *
  127. * @param slave Pointer to spi_slave to which controller has to
  128. * communicate with
  129. * @return zero on success else a negative value
  130. */
  131. int spi_claim_bus(struct spi_slave *slave)
  132. {
  133. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  134. struct exynos_spi *regs = spi_slave->regs;
  135. u32 reg = 0;
  136. int ret;
  137. ret = set_spi_clk(spi_slave->periph_id,
  138. spi_slave->freq);
  139. if (ret < 0) {
  140. debug("%s: Failed to setup spi clock\n", __func__);
  141. return ret;
  142. }
  143. exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
  144. spi_flush_fifo(slave);
  145. reg = readl(&regs->ch_cfg);
  146. reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
  147. if (spi_slave->mode & SPI_CPHA)
  148. reg |= SPI_CH_CPHA_B;
  149. if (spi_slave->mode & SPI_CPOL)
  150. reg |= SPI_CH_CPOL_L;
  151. writel(reg, &regs->ch_cfg);
  152. writel(SPI_FB_DELAY_180, &regs->fb_clk);
  153. return 0;
  154. }
  155. /**
  156. * Reset the spi H/W and flush the tx and rx fifos
  157. *
  158. * @param slave Pointer to spi_slave to which controller has to
  159. * communicate with
  160. */
  161. void spi_release_bus(struct spi_slave *slave)
  162. {
  163. spi_flush_fifo(slave);
  164. }
  165. static void spi_get_fifo_levels(struct exynos_spi *regs,
  166. int *rx_lvl, int *tx_lvl)
  167. {
  168. uint32_t spi_sts = readl(&regs->spi_sts);
  169. *rx_lvl = (spi_sts >> SPI_RX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
  170. *tx_lvl = (spi_sts >> SPI_TX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
  171. }
  172. /**
  173. * If there's something to transfer, do a software reset and set a
  174. * transaction size.
  175. *
  176. * @param regs SPI peripheral registers
  177. * @param count Number of bytes to transfer
  178. */
  179. static void spi_request_bytes(struct exynos_spi *regs, int count)
  180. {
  181. assert(count && count < (1 << 16));
  182. setbits_le32(&regs->ch_cfg, SPI_CH_RST);
  183. clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
  184. writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
  185. }
  186. static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
  187. void **dinp, void const **doutp)
  188. {
  189. struct exynos_spi *regs = spi_slave->regs;
  190. uchar *rxp = *dinp;
  191. const uchar *txp = *doutp;
  192. int rx_lvl, tx_lvl;
  193. uint out_bytes, in_bytes;
  194. out_bytes = in_bytes = todo;
  195. /*
  196. * If there's something to send, do a software reset and set a
  197. * transaction size.
  198. */
  199. spi_request_bytes(regs, todo);
  200. /*
  201. * Bytes are transmitted/received in pairs. Wait to receive all the
  202. * data because then transmission will be done as well.
  203. */
  204. while (in_bytes) {
  205. int temp;
  206. /* Keep the fifos full/empty. */
  207. spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl);
  208. if (tx_lvl < spi_slave->fifo_size && out_bytes) {
  209. temp = txp ? *txp++ : 0xff;
  210. writel(temp, &regs->tx_data);
  211. out_bytes--;
  212. }
  213. if (rx_lvl > 0 && in_bytes) {
  214. temp = readl(&regs->rx_data);
  215. if (rxp)
  216. *rxp++ = temp;
  217. in_bytes--;
  218. }
  219. }
  220. *dinp = rxp;
  221. *doutp = txp;
  222. }
  223. /**
  224. * Transfer and receive data
  225. *
  226. * @param slave Pointer to spi_slave to which controller has to
  227. * communicate with
  228. * @param bitlen No of bits to tranfer or receive
  229. * @param dout Pointer to transfer buffer
  230. * @param din Pointer to receive buffer
  231. * @param flags Flags for transfer begin and end
  232. * @return zero on success else a negative value
  233. */
  234. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  235. void *din, unsigned long flags)
  236. {
  237. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  238. int upto, todo;
  239. int bytelen;
  240. /* spi core configured to do 8 bit transfers */
  241. if (bitlen % 8) {
  242. debug("Non byte aligned SPI transfer.\n");
  243. return -1;
  244. }
  245. /* Start the transaction, if necessary. */
  246. if ((flags & SPI_XFER_BEGIN))
  247. spi_cs_activate(slave);
  248. /* Exynos SPI limits each transfer to 65535 bytes */
  249. bytelen = bitlen / 8;
  250. for (upto = 0; upto < bytelen; upto += todo) {
  251. todo = min(bytelen - upto, (1 << 16) - 1);
  252. spi_rx_tx(spi_slave, todo, &din, &dout);
  253. }
  254. /* Stop the transaction, if necessary. */
  255. if ((flags & SPI_XFER_END))
  256. spi_cs_deactivate(slave);
  257. return 0;
  258. }
  259. /**
  260. * Validates the bus and chip select numbers
  261. *
  262. * @param bus ID of the bus that the slave is attached to
  263. * @param cs ID of the chip select connected to the slave
  264. * @return one on success else zero
  265. */
  266. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  267. {
  268. return spi_get_bus(bus) && cs == 0;
  269. }
  270. /**
  271. * Activate the CS by driving it LOW
  272. *
  273. * @param slave Pointer to spi_slave to which controller has to
  274. * communicate with
  275. */
  276. void spi_cs_activate(struct spi_slave *slave)
  277. {
  278. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  279. clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  280. debug("Activate CS, bus %d\n", spi_slave->slave.bus);
  281. }
  282. /**
  283. * Deactivate the CS by driving it HIGH
  284. *
  285. * @param slave Pointer to spi_slave to which controller has to
  286. * communicate with
  287. */
  288. void spi_cs_deactivate(struct spi_slave *slave)
  289. {
  290. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  291. setbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  292. debug("Deactivate CS, bus %d\n", spi_slave->slave.bus);
  293. }
  294. static inline struct exynos_spi *get_spi_base(int dev_index)
  295. {
  296. if (dev_index < 3)
  297. return (struct exynos_spi *)samsung_get_base_spi() + dev_index;
  298. else
  299. return (struct exynos_spi *)samsung_get_base_spi_isp() +
  300. (dev_index - 3);
  301. }
  302. /* Sadly there is no error return from this function */
  303. void spi_init(void)
  304. {
  305. int i;
  306. struct spi_bus *bus;
  307. for (i = 0; i < EXYNOS5_SPI_NUM_CONTROLLERS; i++) {
  308. bus = &spi_bus[i];
  309. bus->regs = get_spi_base(i);
  310. bus->periph_id = PERIPH_ID_SPI0 + i;
  311. /* Although Exynos5 supports upto 50Mhz speed,
  312. * we are setting it to 10Mhz for safe side
  313. */
  314. bus->frequency = 10000000;
  315. bus->inited = 1;
  316. }
  317. }