exynos_spi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. int skip_preamble;
  50. };
  51. static struct spi_bus *spi_get_bus(unsigned dev_index)
  52. {
  53. if (dev_index < bus_count)
  54. return &spi_bus[dev_index];
  55. debug("%s: invalid bus %d", __func__, dev_index);
  56. return NULL;
  57. }
  58. static inline struct exynos_spi_slave *to_exynos_spi(struct spi_slave *slave)
  59. {
  60. return container_of(slave, struct exynos_spi_slave, slave);
  61. }
  62. /**
  63. * Setup the driver private data
  64. *
  65. * @param bus ID of the bus that the slave is attached to
  66. * @param cs ID of the chip select connected to the slave
  67. * @param max_hz Required spi frequency
  68. * @param mode Required spi mode (clk polarity, clk phase and
  69. * master or slave)
  70. * @return new device or NULL
  71. */
  72. struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
  73. unsigned int max_hz, unsigned int mode)
  74. {
  75. struct exynos_spi_slave *spi_slave;
  76. struct spi_bus *bus;
  77. if (!spi_cs_is_valid(busnum, cs)) {
  78. debug("%s: Invalid bus/chip select %d, %d\n", __func__,
  79. busnum, cs);
  80. return NULL;
  81. }
  82. spi_slave = spi_alloc_slave(struct exynos_spi_slave, busnum, cs);
  83. if (!spi_slave) {
  84. debug("%s: Could not allocate spi_slave\n", __func__);
  85. return NULL;
  86. }
  87. bus = &spi_bus[busnum];
  88. spi_slave->regs = bus->regs;
  89. spi_slave->mode = mode;
  90. spi_slave->periph_id = bus->periph_id;
  91. if (bus->periph_id == PERIPH_ID_SPI1 ||
  92. bus->periph_id == PERIPH_ID_SPI2)
  93. spi_slave->fifo_size = 64;
  94. else
  95. spi_slave->fifo_size = 256;
  96. spi_slave->skip_preamble = 0;
  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 int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
  191. void **dinp, void const **doutp, unsigned long flags)
  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. int toread;
  199. unsigned start = get_timer(0);
  200. int stopping;
  201. out_bytes = in_bytes = todo;
  202. stopping = spi_slave->skip_preamble && (flags & SPI_XFER_END) &&
  203. !(spi_slave->mode & SPI_SLAVE);
  204. /*
  205. * If there's something to send, do a software reset and set a
  206. * transaction size.
  207. */
  208. spi_request_bytes(regs, todo);
  209. /*
  210. * Bytes are transmitted/received in pairs. Wait to receive all the
  211. * data because then transmission will be done as well.
  212. */
  213. toread = in_bytes;
  214. while (in_bytes) {
  215. int temp;
  216. /* Keep the fifos full/empty. */
  217. spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl);
  218. if (tx_lvl < spi_slave->fifo_size && out_bytes) {
  219. temp = txp ? *txp++ : 0xff;
  220. writel(temp, &regs->tx_data);
  221. out_bytes--;
  222. }
  223. if (rx_lvl > 0) {
  224. temp = readl(&regs->rx_data);
  225. if (spi_slave->skip_preamble) {
  226. if (temp == SPI_PREAMBLE_END_BYTE) {
  227. spi_slave->skip_preamble = 0;
  228. stopping = 0;
  229. }
  230. } else {
  231. if (rxp || stopping)
  232. *rxp++ = temp;
  233. in_bytes--;
  234. }
  235. toread--;
  236. } else if (!toread) {
  237. /*
  238. * We have run out of input data, but haven't read
  239. * enough bytes after the preamble yet. Read some more,
  240. * and make sure that we transmit dummy bytes too, to
  241. * keep things going.
  242. */
  243. assert(!out_bytes);
  244. out_bytes = in_bytes;
  245. toread = in_bytes;
  246. txp = NULL;
  247. spi_request_bytes(regs, toread);
  248. }
  249. if (spi_slave->skip_preamble && get_timer(start) > 100) {
  250. printf("SPI timeout: in_bytes=%d, out_bytes=%d, ",
  251. in_bytes, out_bytes);
  252. return -1;
  253. }
  254. }
  255. *dinp = rxp;
  256. *doutp = txp;
  257. return 0;
  258. }
  259. /**
  260. * Transfer and receive data
  261. *
  262. * @param slave Pointer to spi_slave to which controller has to
  263. * communicate with
  264. * @param bitlen No of bits to tranfer or receive
  265. * @param dout Pointer to transfer buffer
  266. * @param din Pointer to receive buffer
  267. * @param flags Flags for transfer begin and end
  268. * @return zero on success else a negative value
  269. */
  270. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  271. void *din, unsigned long flags)
  272. {
  273. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  274. int upto, todo;
  275. int bytelen;
  276. int ret = 0;
  277. /* spi core configured to do 8 bit transfers */
  278. if (bitlen % 8) {
  279. debug("Non byte aligned SPI transfer.\n");
  280. return -1;
  281. }
  282. /* Start the transaction, if necessary. */
  283. if ((flags & SPI_XFER_BEGIN))
  284. spi_cs_activate(slave);
  285. /* Exynos SPI limits each transfer to 65535 bytes */
  286. bytelen = bitlen / 8;
  287. for (upto = 0; !ret && upto < bytelen; upto += todo) {
  288. todo = min(bytelen - upto, (1 << 16) - 1);
  289. ret = spi_rx_tx(spi_slave, todo, &din, &dout, flags);
  290. if (ret)
  291. break;
  292. }
  293. /* Stop the transaction, if necessary. */
  294. if ((flags & SPI_XFER_END) && !(spi_slave->mode & SPI_SLAVE)) {
  295. spi_cs_deactivate(slave);
  296. if (spi_slave->skip_preamble) {
  297. assert(!spi_slave->skip_preamble);
  298. debug("Failed to complete premable transaction\n");
  299. ret = -1;
  300. }
  301. }
  302. return ret;
  303. }
  304. /**
  305. * Validates the bus and chip select numbers
  306. *
  307. * @param bus ID of the bus that the slave is attached to
  308. * @param cs ID of the chip select connected to the slave
  309. * @return one on success else zero
  310. */
  311. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  312. {
  313. return spi_get_bus(bus) && cs == 0;
  314. }
  315. /**
  316. * Activate the CS by driving it LOW
  317. *
  318. * @param slave Pointer to spi_slave to which controller has to
  319. * communicate with
  320. */
  321. void spi_cs_activate(struct spi_slave *slave)
  322. {
  323. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  324. clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  325. debug("Activate CS, bus %d\n", spi_slave->slave.bus);
  326. spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
  327. }
  328. /**
  329. * Deactivate the CS by driving it HIGH
  330. *
  331. * @param slave Pointer to spi_slave to which controller has to
  332. * communicate with
  333. */
  334. void spi_cs_deactivate(struct spi_slave *slave)
  335. {
  336. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  337. setbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  338. debug("Deactivate CS, bus %d\n", spi_slave->slave.bus);
  339. }
  340. static inline struct exynos_spi *get_spi_base(int dev_index)
  341. {
  342. if (dev_index < 3)
  343. return (struct exynos_spi *)samsung_get_base_spi() + dev_index;
  344. else
  345. return (struct exynos_spi *)samsung_get_base_spi_isp() +
  346. (dev_index - 3);
  347. }
  348. /*
  349. * Read the SPI config from the device tree node.
  350. *
  351. * @param blob FDT blob to read from
  352. * @param node Node offset to read from
  353. * @param bus SPI bus structure to fill with information
  354. * @return 0 if ok, or -FDT_ERR_NOTFOUND if something was missing
  355. */
  356. #ifdef CONFIG_OF_CONTROL
  357. static int spi_get_config(const void *blob, int node, struct spi_bus *bus)
  358. {
  359. bus->node = node;
  360. bus->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
  361. bus->periph_id = pinmux_decode_periph_id(blob, node);
  362. if (bus->periph_id == PERIPH_ID_NONE) {
  363. debug("%s: Invalid peripheral ID %d\n", __func__,
  364. bus->periph_id);
  365. return -FDT_ERR_NOTFOUND;
  366. }
  367. /* Use 500KHz as a suitable default */
  368. bus->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
  369. 500000);
  370. return 0;
  371. }
  372. /*
  373. * Process a list of nodes, adding them to our list of SPI ports.
  374. *
  375. * @param blob fdt blob
  376. * @param node_list list of nodes to process (any <=0 are ignored)
  377. * @param count number of nodes to process
  378. * @param is_dvc 1 if these are DVC ports, 0 if standard I2C
  379. * @return 0 if ok, -1 on error
  380. */
  381. static int process_nodes(const void *blob, int node_list[], int count)
  382. {
  383. int i;
  384. /* build the i2c_controllers[] for each controller */
  385. for (i = 0; i < count; i++) {
  386. int node = node_list[i];
  387. struct spi_bus *bus;
  388. if (node <= 0)
  389. continue;
  390. bus = &spi_bus[i];
  391. if (spi_get_config(blob, node, bus)) {
  392. printf("exynos spi_init: failed to decode bus %d\n",
  393. i);
  394. return -1;
  395. }
  396. debug("spi: controller bus %d at %p, periph_id %d\n",
  397. i, bus->regs, bus->periph_id);
  398. bus->inited = 1;
  399. bus_count++;
  400. }
  401. return 0;
  402. }
  403. #endif
  404. /* Sadly there is no error return from this function */
  405. void spi_init(void)
  406. {
  407. int count;
  408. #ifdef CONFIG_OF_CONTROL
  409. int node_list[EXYNOS5_SPI_NUM_CONTROLLERS];
  410. const void *blob = gd->fdt_blob;
  411. count = fdtdec_find_aliases_for_id(blob, "spi",
  412. COMPAT_SAMSUNG_EXYNOS_SPI, node_list,
  413. EXYNOS5_SPI_NUM_CONTROLLERS);
  414. if (process_nodes(blob, node_list, count))
  415. return;
  416. #else
  417. struct spi_bus *bus;
  418. for (count = 0; count < EXYNOS5_SPI_NUM_CONTROLLERS; count++) {
  419. bus = &spi_bus[count];
  420. bus->regs = get_spi_base(count);
  421. bus->periph_id = PERIPH_ID_SPI0 + count;
  422. /* Although Exynos5 supports upto 50Mhz speed,
  423. * we are setting it to 10Mhz for safe side
  424. */
  425. bus->frequency = 10000000;
  426. bus->inited = 1;
  427. bus->node = 0;
  428. bus_count = EXYNOS5_SPI_NUM_CONTROLLERS;
  429. }
  430. #endif
  431. }