exynos_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 <fdtdec.h>
  23. #include <asm/arch/clk.h>
  24. #include <asm/arch/clock.h>
  25. #include <asm/arch/cpu.h>
  26. #include <asm/arch/gpio.h>
  27. #include <asm/arch/pinmux.h>
  28. #include <asm/arch-exynos/spi.h>
  29. #include <asm/io.h>
  30. DECLARE_GLOBAL_DATA_PTR;
  31. /* Information about each SPI controller */
  32. struct spi_bus {
  33. enum periph_id periph_id;
  34. s32 frequency; /* Default clock frequency, -1 for none */
  35. struct exynos_spi *regs;
  36. int inited; /* 1 if this bus is ready for use */
  37. int node;
  38. };
  39. /* A list of spi buses that we know about */
  40. static struct spi_bus spi_bus[EXYNOS5_SPI_NUM_CONTROLLERS];
  41. static unsigned int bus_count;
  42. struct exynos_spi_slave {
  43. struct spi_slave slave;
  44. struct exynos_spi *regs;
  45. unsigned int freq; /* Default frequency */
  46. unsigned int mode;
  47. enum periph_id periph_id; /* Peripheral ID for this device */
  48. unsigned int fifo_size;
  49. };
  50. static struct spi_bus *spi_get_bus(unsigned dev_index)
  51. {
  52. if (dev_index < bus_count)
  53. return &spi_bus[dev_index];
  54. debug("%s: invalid bus %d", __func__, dev_index);
  55. return NULL;
  56. }
  57. static inline struct exynos_spi_slave *to_exynos_spi(struct spi_slave *slave)
  58. {
  59. return container_of(slave, struct exynos_spi_slave, slave);
  60. }
  61. /**
  62. * Setup the driver private data
  63. *
  64. * @param bus ID of the bus that the slave is attached to
  65. * @param cs ID of the chip select connected to the slave
  66. * @param max_hz Required spi frequency
  67. * @param mode Required spi mode (clk polarity, clk phase and
  68. * master or slave)
  69. * @return new device or NULL
  70. */
  71. struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
  72. unsigned int max_hz, unsigned int mode)
  73. {
  74. struct exynos_spi_slave *spi_slave;
  75. struct spi_bus *bus;
  76. if (!spi_cs_is_valid(busnum, cs)) {
  77. debug("%s: Invalid bus/chip select %d, %d\n", __func__,
  78. busnum, cs);
  79. return NULL;
  80. }
  81. spi_slave = malloc(sizeof(*spi_slave));
  82. if (!spi_slave) {
  83. debug("%s: Could not allocate spi_slave\n", __func__);
  84. return NULL;
  85. }
  86. bus = &spi_bus[busnum];
  87. spi_slave->slave.bus = busnum;
  88. spi_slave->slave.cs = cs;
  89. spi_slave->regs = bus->regs;
  90. spi_slave->mode = mode;
  91. spi_slave->periph_id = bus->periph_id;
  92. if (bus->periph_id == PERIPH_ID_SPI1 ||
  93. bus->periph_id == PERIPH_ID_SPI2)
  94. spi_slave->fifo_size = 64;
  95. else
  96. spi_slave->fifo_size = 256;
  97. spi_slave->freq = bus->frequency;
  98. if (max_hz)
  99. spi_slave->freq = min(max_hz, spi_slave->freq);
  100. return &spi_slave->slave;
  101. }
  102. /**
  103. * Free spi controller
  104. *
  105. * @param slave Pointer to spi_slave to which controller has to
  106. * communicate with
  107. */
  108. void spi_free_slave(struct spi_slave *slave)
  109. {
  110. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  111. free(spi_slave);
  112. }
  113. /**
  114. * Flush spi tx, rx fifos and reset the SPI controller
  115. *
  116. * @param slave Pointer to spi_slave to which controller has to
  117. * communicate with
  118. */
  119. static void spi_flush_fifo(struct spi_slave *slave)
  120. {
  121. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  122. struct exynos_spi *regs = spi_slave->regs;
  123. clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
  124. clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
  125. setbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
  126. }
  127. /**
  128. * Initialize the spi base registers, set the required clock frequency and
  129. * initialize the gpios
  130. *
  131. * @param slave Pointer to spi_slave to which controller has to
  132. * communicate with
  133. * @return zero on success else a negative value
  134. */
  135. int spi_claim_bus(struct spi_slave *slave)
  136. {
  137. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  138. struct exynos_spi *regs = spi_slave->regs;
  139. u32 reg = 0;
  140. int ret;
  141. ret = set_spi_clk(spi_slave->periph_id,
  142. spi_slave->freq);
  143. if (ret < 0) {
  144. debug("%s: Failed to setup spi clock\n", __func__);
  145. return ret;
  146. }
  147. exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
  148. spi_flush_fifo(slave);
  149. reg = readl(&regs->ch_cfg);
  150. reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
  151. if (spi_slave->mode & SPI_CPHA)
  152. reg |= SPI_CH_CPHA_B;
  153. if (spi_slave->mode & SPI_CPOL)
  154. reg |= SPI_CH_CPOL_L;
  155. writel(reg, &regs->ch_cfg);
  156. writel(SPI_FB_DELAY_180, &regs->fb_clk);
  157. return 0;
  158. }
  159. /**
  160. * Reset the spi H/W and flush the tx and rx fifos
  161. *
  162. * @param slave Pointer to spi_slave to which controller has to
  163. * communicate with
  164. */
  165. void spi_release_bus(struct spi_slave *slave)
  166. {
  167. spi_flush_fifo(slave);
  168. }
  169. static void spi_get_fifo_levels(struct exynos_spi *regs,
  170. int *rx_lvl, int *tx_lvl)
  171. {
  172. uint32_t spi_sts = readl(&regs->spi_sts);
  173. *rx_lvl = (spi_sts >> SPI_RX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
  174. *tx_lvl = (spi_sts >> SPI_TX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
  175. }
  176. /**
  177. * If there's something to transfer, do a software reset and set a
  178. * transaction size.
  179. *
  180. * @param regs SPI peripheral registers
  181. * @param count Number of bytes to transfer
  182. */
  183. static void spi_request_bytes(struct exynos_spi *regs, int count)
  184. {
  185. assert(count && count < (1 << 16));
  186. setbits_le32(&regs->ch_cfg, SPI_CH_RST);
  187. clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
  188. writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
  189. }
  190. static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
  191. void **dinp, void const **doutp)
  192. {
  193. struct exynos_spi *regs = spi_slave->regs;
  194. uchar *rxp = *dinp;
  195. const uchar *txp = *doutp;
  196. int rx_lvl, tx_lvl;
  197. uint out_bytes, in_bytes;
  198. out_bytes = in_bytes = todo;
  199. /*
  200. * If there's something to send, do a software reset and set a
  201. * transaction size.
  202. */
  203. spi_request_bytes(regs, todo);
  204. /*
  205. * Bytes are transmitted/received in pairs. Wait to receive all the
  206. * data because then transmission will be done as well.
  207. */
  208. while (in_bytes) {
  209. int temp;
  210. /* Keep the fifos full/empty. */
  211. spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl);
  212. if (tx_lvl < spi_slave->fifo_size && out_bytes) {
  213. temp = txp ? *txp++ : 0xff;
  214. writel(temp, &regs->tx_data);
  215. out_bytes--;
  216. }
  217. if (rx_lvl > 0 && in_bytes) {
  218. temp = readl(&regs->rx_data);
  219. if (rxp)
  220. *rxp++ = temp;
  221. in_bytes--;
  222. }
  223. }
  224. *dinp = rxp;
  225. *doutp = txp;
  226. }
  227. /**
  228. * Transfer and receive data
  229. *
  230. * @param slave Pointer to spi_slave to which controller has to
  231. * communicate with
  232. * @param bitlen No of bits to tranfer or receive
  233. * @param dout Pointer to transfer buffer
  234. * @param din Pointer to receive buffer
  235. * @param flags Flags for transfer begin and end
  236. * @return zero on success else a negative value
  237. */
  238. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  239. void *din, unsigned long flags)
  240. {
  241. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  242. int upto, todo;
  243. int bytelen;
  244. /* spi core configured to do 8 bit transfers */
  245. if (bitlen % 8) {
  246. debug("Non byte aligned SPI transfer.\n");
  247. return -1;
  248. }
  249. /* Start the transaction, if necessary. */
  250. if ((flags & SPI_XFER_BEGIN))
  251. spi_cs_activate(slave);
  252. /* Exynos SPI limits each transfer to 65535 bytes */
  253. bytelen = bitlen / 8;
  254. for (upto = 0; upto < bytelen; upto += todo) {
  255. todo = min(bytelen - upto, (1 << 16) - 1);
  256. spi_rx_tx(spi_slave, todo, &din, &dout);
  257. }
  258. /* Stop the transaction, if necessary. */
  259. if ((flags & SPI_XFER_END))
  260. spi_cs_deactivate(slave);
  261. return 0;
  262. }
  263. /**
  264. * Validates the bus and chip select numbers
  265. *
  266. * @param bus ID of the bus that the slave is attached to
  267. * @param cs ID of the chip select connected to the slave
  268. * @return one on success else zero
  269. */
  270. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  271. {
  272. return spi_get_bus(bus) && cs == 0;
  273. }
  274. /**
  275. * Activate the CS by driving it LOW
  276. *
  277. * @param slave Pointer to spi_slave to which controller has to
  278. * communicate with
  279. */
  280. void spi_cs_activate(struct spi_slave *slave)
  281. {
  282. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  283. clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  284. debug("Activate CS, bus %d\n", spi_slave->slave.bus);
  285. }
  286. /**
  287. * Deactivate the CS by driving it HIGH
  288. *
  289. * @param slave Pointer to spi_slave to which controller has to
  290. * communicate with
  291. */
  292. void spi_cs_deactivate(struct spi_slave *slave)
  293. {
  294. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  295. setbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  296. debug("Deactivate CS, bus %d\n", spi_slave->slave.bus);
  297. }
  298. static inline struct exynos_spi *get_spi_base(int dev_index)
  299. {
  300. if (dev_index < 3)
  301. return (struct exynos_spi *)samsung_get_base_spi() + dev_index;
  302. else
  303. return (struct exynos_spi *)samsung_get_base_spi_isp() +
  304. (dev_index - 3);
  305. }
  306. /*
  307. * Read the SPI config from the device tree node.
  308. *
  309. * @param blob FDT blob to read from
  310. * @param node Node offset to read from
  311. * @param bus SPI bus structure to fill with information
  312. * @return 0 if ok, or -FDT_ERR_NOTFOUND if something was missing
  313. */
  314. static int spi_get_config(const void *blob, int node, struct spi_bus *bus)
  315. {
  316. bus->node = node;
  317. bus->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
  318. bus->periph_id = pinmux_decode_periph_id(blob, node);
  319. if (bus->periph_id == PERIPH_ID_NONE) {
  320. debug("%s: Invalid peripheral ID %d\n", __func__,
  321. bus->periph_id);
  322. return -FDT_ERR_NOTFOUND;
  323. }
  324. /* Use 500KHz as a suitable default */
  325. bus->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
  326. 500000);
  327. return 0;
  328. }
  329. /*
  330. * Process a list of nodes, adding them to our list of SPI ports.
  331. *
  332. * @param blob fdt blob
  333. * @param node_list list of nodes to process (any <=0 are ignored)
  334. * @param count number of nodes to process
  335. * @param is_dvc 1 if these are DVC ports, 0 if standard I2C
  336. * @return 0 if ok, -1 on error
  337. */
  338. static int process_nodes(const void *blob, int node_list[], int count)
  339. {
  340. int i;
  341. /* build the i2c_controllers[] for each controller */
  342. for (i = 0; i < count; i++) {
  343. int node = node_list[i];
  344. struct spi_bus *bus;
  345. if (node <= 0)
  346. continue;
  347. bus = &spi_bus[i];
  348. if (spi_get_config(blob, node, bus)) {
  349. printf("exynos spi_init: failed to decode bus %d\n",
  350. i);
  351. return -1;
  352. }
  353. debug("spi: controller bus %d at %p, periph_id %d\n",
  354. i, bus->regs, bus->periph_id);
  355. bus->inited = 1;
  356. bus_count++;
  357. }
  358. return 0;
  359. }
  360. /* Sadly there is no error return from this function */
  361. void spi_init(void)
  362. {
  363. int count;
  364. #ifdef CONFIG_OF_CONTROL
  365. int node_list[EXYNOS5_SPI_NUM_CONTROLLERS];
  366. const void *blob = gd->fdt_blob;
  367. count = fdtdec_find_aliases_for_id(blob, "spi",
  368. COMPAT_SAMSUNG_EXYNOS_SPI, node_list,
  369. EXYNOS5_SPI_NUM_CONTROLLERS);
  370. if (process_nodes(blob, node_list, count))
  371. return;
  372. #else
  373. struct spi_bus *bus;
  374. for (count = 0; count < EXYNOS5_SPI_NUM_CONTROLLERS; count++) {
  375. bus = &spi_bus[count];
  376. bus->regs = get_spi_base(count);
  377. bus->periph_id = PERIPH_ID_SPI0 + count;
  378. /* Although Exynos5 supports upto 50Mhz speed,
  379. * we are setting it to 10Mhz for safe side
  380. */
  381. bus->frequency = 10000000;
  382. bus->inited = 1;
  383. bus->node = 0;
  384. bus_count = EXYNOS5_SPI_NUM_CONTROLLERS;
  385. }
  386. #endif
  387. }