tegra_slink.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * NVIDIA Tegra SPI-SLINK controller
  3. *
  4. * Copyright (c) 2010-2013 NVIDIA Corporation
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  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 <malloc.h>
  25. #include <asm/io.h>
  26. #include <asm/gpio.h>
  27. #include <asm/arch/clock.h>
  28. #include <asm/arch-tegra/clk_rst.h>
  29. #include <asm/arch-tegra/tegra_slink.h>
  30. #include <spi.h>
  31. #include <fdtdec.h>
  32. DECLARE_GLOBAL_DATA_PTR;
  33. struct tegra_spi_ctrl {
  34. struct slink_tegra *regs;
  35. unsigned int freq;
  36. unsigned int mode;
  37. int periph_id;
  38. int valid;
  39. };
  40. struct tegra_spi_slave {
  41. struct spi_slave slave;
  42. struct tegra_spi_ctrl *ctrl;
  43. };
  44. static struct tegra_spi_ctrl spi_ctrls[CONFIG_TEGRA_SLINK_CTRLS];
  45. static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave)
  46. {
  47. return container_of(slave, struct tegra_spi_slave, slave);
  48. }
  49. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  50. {
  51. if (bus >= CONFIG_TEGRA_SLINK_CTRLS || cs > 3 || !spi_ctrls[bus].valid)
  52. return 0;
  53. else
  54. return 1;
  55. }
  56. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  57. unsigned int max_hz, unsigned int mode)
  58. {
  59. struct tegra_spi_slave *spi;
  60. debug("%s: bus: %u, cs: %u, max_hz: %u, mode: %u\n", __func__,
  61. bus, cs, max_hz, mode);
  62. if (!spi_cs_is_valid(bus, cs)) {
  63. printf("SPI error: unsupported bus %d / chip select %d\n",
  64. bus, cs);
  65. return NULL;
  66. }
  67. if (max_hz > TEGRA_SPI_MAX_FREQ) {
  68. printf("SPI error: unsupported frequency %d Hz. Max frequency"
  69. " is %d Hz\n", max_hz, TEGRA_SPI_MAX_FREQ);
  70. return NULL;
  71. }
  72. spi = malloc(sizeof(struct tegra_spi_slave));
  73. if (!spi) {
  74. printf("SPI error: malloc of SPI structure failed\n");
  75. return NULL;
  76. }
  77. spi->slave.bus = bus;
  78. spi->slave.cs = cs;
  79. spi->ctrl = &spi_ctrls[bus];
  80. if (!spi->ctrl) {
  81. printf("SPI error: could not find controller for bus %d\n",
  82. bus);
  83. return NULL;
  84. }
  85. if (max_hz < spi->ctrl->freq) {
  86. debug("%s: limiting frequency from %u to %u\n", __func__,
  87. spi->ctrl->freq, max_hz);
  88. spi->ctrl->freq = max_hz;
  89. }
  90. spi->ctrl->mode = mode;
  91. return &spi->slave;
  92. }
  93. void spi_free_slave(struct spi_slave *slave)
  94. {
  95. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  96. free(spi);
  97. }
  98. void spi_init(void)
  99. {
  100. struct tegra_spi_ctrl *ctrl;
  101. int i;
  102. #ifdef CONFIG_OF_CONTROL
  103. int node = 0;
  104. int count;
  105. int node_list[CONFIG_TEGRA_SLINK_CTRLS];
  106. count = fdtdec_find_aliases_for_id(gd->fdt_blob, "spi",
  107. COMPAT_NVIDIA_TEGRA20_SLINK,
  108. node_list,
  109. CONFIG_TEGRA_SLINK_CTRLS);
  110. for (i = 0; i < count; i++) {
  111. ctrl = &spi_ctrls[i];
  112. node = node_list[i];
  113. ctrl->regs = (struct slink_tegra *)fdtdec_get_addr(gd->fdt_blob,
  114. node, "reg");
  115. if ((fdt_addr_t)ctrl->regs == FDT_ADDR_T_NONE) {
  116. debug("%s: no slink register found\n", __func__);
  117. continue;
  118. }
  119. ctrl->freq = fdtdec_get_int(gd->fdt_blob, node,
  120. "spi-max-frequency", 0);
  121. if (!ctrl->freq) {
  122. debug("%s: no slink max frequency found\n", __func__);
  123. continue;
  124. }
  125. ctrl->periph_id = clock_decode_periph_id(gd->fdt_blob, node);
  126. if (ctrl->periph_id == PERIPH_ID_NONE) {
  127. debug("%s: could not decode periph id\n", __func__);
  128. continue;
  129. }
  130. ctrl->valid = 1;
  131. debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
  132. __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
  133. }
  134. #else
  135. for (i = 0; i < CONFIG_TEGRA_SLINK_CTRLS; i++) {
  136. ctrl = &spi_ctrls[i];
  137. u32 base_regs[] = {
  138. NV_PA_SLINK1_BASE,
  139. NV_PA_SLINK2_BASE,
  140. NV_PA_SLINK3_BASE,
  141. NV_PA_SLINK4_BASE,
  142. NV_PA_SLINK5_BASE,
  143. NV_PA_SLINK6_BASE,
  144. };
  145. int periph_ids[] = {
  146. PERIPH_ID_SBC1,
  147. PERIPH_ID_SBC2,
  148. PERIPH_ID_SBC3,
  149. PERIPH_ID_SBC4,
  150. PERIPH_ID_SBC5,
  151. PERIPH_ID_SBC6,
  152. };
  153. ctrl->regs = (struct slink_tegra *)base_regs[i];
  154. ctrl->freq = TEGRA_SPI_MAX_FREQ;
  155. ctrl->periph_id = periph_ids[i];
  156. ctrl->valid = 1;
  157. debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
  158. __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
  159. }
  160. #endif
  161. }
  162. int spi_claim_bus(struct spi_slave *slave)
  163. {
  164. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  165. struct slink_tegra *regs = spi->ctrl->regs;
  166. u32 reg;
  167. /* Change SPI clock to correct frequency, PLLP_OUT0 source */
  168. clock_start_periph_pll(spi->ctrl->periph_id, CLOCK_ID_PERIPH,
  169. spi->ctrl->freq);
  170. /* Clear stale status here */
  171. reg = SLINK_STAT_RDY | SLINK_STAT_RXF_FLUSH | SLINK_STAT_TXF_FLUSH | \
  172. SLINK_STAT_RXF_UNR | SLINK_STAT_TXF_OVF;
  173. writel(reg, &regs->status);
  174. debug("%s: STATUS = %08x\n", __func__, readl(&regs->status));
  175. /* Set master mode and sw controlled CS */
  176. reg = readl(&regs->command);
  177. reg |= SLINK_CMD_M_S | SLINK_CMD_CS_SOFT;
  178. writel(reg, &regs->command);
  179. debug("%s: COMMAND = %08x\n", __func__, readl(&regs->command));
  180. return 0;
  181. }
  182. void spi_release_bus(struct spi_slave *slave)
  183. {
  184. }
  185. void spi_cs_activate(struct spi_slave *slave)
  186. {
  187. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  188. struct slink_tegra *regs = spi->ctrl->regs;
  189. /* CS is negated on Tegra, so drive a 1 to get a 0 */
  190. setbits_le32(&regs->command, SLINK_CMD_CS_VAL);
  191. }
  192. void spi_cs_deactivate(struct spi_slave *slave)
  193. {
  194. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  195. struct slink_tegra *regs = spi->ctrl->regs;
  196. /* CS is negated on Tegra, so drive a 0 to get a 1 */
  197. clrbits_le32(&regs->command, SLINK_CMD_CS_VAL);
  198. }
  199. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  200. const void *data_out, void *data_in, unsigned long flags)
  201. {
  202. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  203. struct slink_tegra *regs = spi->ctrl->regs;
  204. u32 reg, tmpdout, tmpdin = 0;
  205. const u8 *dout = data_out;
  206. u8 *din = data_in;
  207. int num_bytes;
  208. int ret;
  209. debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
  210. __func__, slave->bus, slave->cs, dout, din, bitlen);
  211. if (bitlen % 8)
  212. return -1;
  213. num_bytes = bitlen / 8;
  214. ret = 0;
  215. reg = readl(&regs->status);
  216. writel(reg, &regs->status); /* Clear all SPI events via R/W */
  217. debug("%s entry: STATUS = %08x\n", __func__, reg);
  218. reg = readl(&regs->status2);
  219. writel(reg, &regs->status2); /* Clear all STATUS2 events via R/W */
  220. debug("%s entry: STATUS2 = %08x\n", __func__, reg);
  221. debug("%s entry: COMMAND = %08x\n", __func__, readl(&regs->command));
  222. clrsetbits_le32(&regs->command2, SLINK_CMD2_SS_EN_MASK,
  223. SLINK_CMD2_TXEN | SLINK_CMD2_RXEN |
  224. (slave->cs << SLINK_CMD2_SS_EN_SHIFT));
  225. debug("%s entry: COMMAND2 = %08x\n", __func__, readl(&regs->command2));
  226. if (flags & SPI_XFER_BEGIN)
  227. spi_cs_activate(slave);
  228. /* handle data in 32-bit chunks */
  229. while (num_bytes > 0) {
  230. int bytes;
  231. int is_read = 0;
  232. int tm, i;
  233. tmpdout = 0;
  234. bytes = (num_bytes > 4) ? 4 : num_bytes;
  235. if (dout != NULL) {
  236. for (i = 0; i < bytes; ++i)
  237. tmpdout = (tmpdout << 8) | dout[i];
  238. dout += bytes;
  239. }
  240. num_bytes -= bytes;
  241. clrsetbits_le32(&regs->command, SLINK_CMD_BIT_LENGTH_MASK,
  242. bytes * 8 - 1);
  243. writel(tmpdout, &regs->tx_fifo);
  244. setbits_le32(&regs->command, SLINK_CMD_GO);
  245. /*
  246. * Wait for SPI transmit FIFO to empty, or to time out.
  247. * The RX FIFO status will be read and cleared last
  248. */
  249. for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
  250. u32 status;
  251. status = readl(&regs->status);
  252. /* We can exit when we've had both RX and TX activity */
  253. if (is_read && (status & SLINK_STAT_TXF_EMPTY))
  254. break;
  255. if ((status & (SLINK_STAT_BSY | SLINK_STAT_RDY)) !=
  256. SLINK_STAT_RDY)
  257. tm++;
  258. else if (!(status & SLINK_STAT_RXF_EMPTY)) {
  259. tmpdin = readl(&regs->rx_fifo);
  260. is_read = 1;
  261. /* swap bytes read in */
  262. if (din != NULL) {
  263. for (i = bytes - 1; i >= 0; --i) {
  264. din[i] = tmpdin & 0xff;
  265. tmpdin >>= 8;
  266. }
  267. din += bytes;
  268. }
  269. }
  270. }
  271. if (tm >= SPI_TIMEOUT)
  272. ret = tm;
  273. /* clear ACK RDY, etc. bits */
  274. writel(readl(&regs->status), &regs->status);
  275. }
  276. if (flags & SPI_XFER_END)
  277. spi_cs_deactivate(slave);
  278. debug("%s: transfer ended. Value=%08x, status = %08x\n",
  279. __func__, tmpdin, readl(&regs->status));
  280. if (ret) {
  281. printf("%s: timeout during SPI transfer, tm %d\n",
  282. __func__, ret);
  283. return -1;
  284. }
  285. return 0;
  286. }