exynos_spi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * (C) Copyright 2012 SAMSUNG Electronics
  3. * Padmavathi Venna <padma.v@samsung.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <malloc.h>
  9. #include <spi.h>
  10. #include <fdtdec.h>
  11. #include <asm/arch/clk.h>
  12. #include <asm/arch/clock.h>
  13. #include <asm/arch/cpu.h>
  14. #include <asm/arch/gpio.h>
  15. #include <asm/arch/pinmux.h>
  16. #include <asm/arch-exynos/spi.h>
  17. #include <asm/io.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. /* Information about each SPI controller */
  20. struct spi_bus {
  21. enum periph_id periph_id;
  22. s32 frequency; /* Default clock frequency, -1 for none */
  23. struct exynos_spi *regs;
  24. int inited; /* 1 if this bus is ready for use */
  25. int node;
  26. uint deactivate_delay_us; /* Delay to wait after deactivate */
  27. };
  28. /* A list of spi buses that we know about */
  29. static struct spi_bus spi_bus[EXYNOS5_SPI_NUM_CONTROLLERS];
  30. static unsigned int bus_count;
  31. struct exynos_spi_slave {
  32. struct spi_slave slave;
  33. struct exynos_spi *regs;
  34. unsigned int freq; /* Default frequency */
  35. unsigned int mode;
  36. enum periph_id periph_id; /* Peripheral ID for this device */
  37. unsigned int fifo_size;
  38. int skip_preamble;
  39. struct spi_bus *bus; /* Pointer to our SPI bus info */
  40. ulong last_transaction_us; /* Time of last transaction end */
  41. };
  42. static struct spi_bus *spi_get_bus(unsigned dev_index)
  43. {
  44. if (dev_index < bus_count)
  45. return &spi_bus[dev_index];
  46. debug("%s: invalid bus %d", __func__, dev_index);
  47. return NULL;
  48. }
  49. static inline struct exynos_spi_slave *to_exynos_spi(struct spi_slave *slave)
  50. {
  51. return container_of(slave, struct exynos_spi_slave, slave);
  52. }
  53. /**
  54. * Setup the driver private data
  55. *
  56. * @param bus ID of the bus that the slave is attached to
  57. * @param cs ID of the chip select connected to the slave
  58. * @param max_hz Required spi frequency
  59. * @param mode Required spi mode (clk polarity, clk phase and
  60. * master or slave)
  61. * @return new device or NULL
  62. */
  63. struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
  64. unsigned int max_hz, unsigned int mode)
  65. {
  66. struct exynos_spi_slave *spi_slave;
  67. struct spi_bus *bus;
  68. if (!spi_cs_is_valid(busnum, cs)) {
  69. debug("%s: Invalid bus/chip select %d, %d\n", __func__,
  70. busnum, cs);
  71. return NULL;
  72. }
  73. spi_slave = spi_alloc_slave(struct exynos_spi_slave, busnum, cs);
  74. if (!spi_slave) {
  75. debug("%s: Could not allocate spi_slave\n", __func__);
  76. return NULL;
  77. }
  78. bus = &spi_bus[busnum];
  79. spi_slave->bus = bus;
  80. spi_slave->regs = bus->regs;
  81. spi_slave->mode = mode;
  82. spi_slave->periph_id = bus->periph_id;
  83. if (bus->periph_id == PERIPH_ID_SPI1 ||
  84. bus->periph_id == PERIPH_ID_SPI2)
  85. spi_slave->fifo_size = 64;
  86. else
  87. spi_slave->fifo_size = 256;
  88. spi_slave->skip_preamble = 0;
  89. spi_slave->last_transaction_us = timer_get_us();
  90. spi_slave->freq = bus->frequency;
  91. if (max_hz)
  92. spi_slave->freq = min(max_hz, spi_slave->freq);
  93. return &spi_slave->slave;
  94. }
  95. /**
  96. * Free spi controller
  97. *
  98. * @param slave Pointer to spi_slave to which controller has to
  99. * communicate with
  100. */
  101. void spi_free_slave(struct spi_slave *slave)
  102. {
  103. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  104. free(spi_slave);
  105. }
  106. /**
  107. * Flush spi tx, rx fifos and reset the SPI controller
  108. *
  109. * @param slave Pointer to spi_slave to which controller has to
  110. * communicate with
  111. */
  112. static void spi_flush_fifo(struct spi_slave *slave)
  113. {
  114. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  115. struct exynos_spi *regs = spi_slave->regs;
  116. clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
  117. clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
  118. setbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
  119. }
  120. /**
  121. * Initialize the spi base registers, set the required clock frequency and
  122. * initialize the gpios
  123. *
  124. * @param slave Pointer to spi_slave to which controller has to
  125. * communicate with
  126. * @return zero on success else a negative value
  127. */
  128. int spi_claim_bus(struct spi_slave *slave)
  129. {
  130. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  131. struct exynos_spi *regs = spi_slave->regs;
  132. u32 reg = 0;
  133. int ret;
  134. ret = set_spi_clk(spi_slave->periph_id,
  135. spi_slave->freq);
  136. if (ret < 0) {
  137. debug("%s: Failed to setup spi clock\n", __func__);
  138. return ret;
  139. }
  140. exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
  141. spi_flush_fifo(slave);
  142. reg = readl(&regs->ch_cfg);
  143. reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
  144. if (spi_slave->mode & SPI_CPHA)
  145. reg |= SPI_CH_CPHA_B;
  146. if (spi_slave->mode & SPI_CPOL)
  147. reg |= SPI_CH_CPOL_L;
  148. writel(reg, &regs->ch_cfg);
  149. writel(SPI_FB_DELAY_180, &regs->fb_clk);
  150. return 0;
  151. }
  152. /**
  153. * Reset the spi H/W and flush the tx and rx fifos
  154. *
  155. * @param slave Pointer to spi_slave to which controller has to
  156. * communicate with
  157. */
  158. void spi_release_bus(struct spi_slave *slave)
  159. {
  160. spi_flush_fifo(slave);
  161. }
  162. static void spi_get_fifo_levels(struct exynos_spi *regs,
  163. int *rx_lvl, int *tx_lvl)
  164. {
  165. uint32_t spi_sts = readl(&regs->spi_sts);
  166. *rx_lvl = (spi_sts >> SPI_RX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
  167. *tx_lvl = (spi_sts >> SPI_TX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
  168. }
  169. /**
  170. * If there's something to transfer, do a software reset and set a
  171. * transaction size.
  172. *
  173. * @param regs SPI peripheral registers
  174. * @param count Number of bytes to transfer
  175. * @param step Number of bytes to transfer in each packet (1 or 4)
  176. */
  177. static void spi_request_bytes(struct exynos_spi *regs, int count, int step)
  178. {
  179. /* For word address we need to swap bytes */
  180. if (step == 4) {
  181. setbits_le32(&regs->mode_cfg,
  182. SPI_MODE_CH_WIDTH_WORD | SPI_MODE_BUS_WIDTH_WORD);
  183. count /= 4;
  184. setbits_le32(&regs->swap_cfg, SPI_TX_SWAP_EN | SPI_RX_SWAP_EN |
  185. SPI_TX_BYTE_SWAP | SPI_RX_BYTE_SWAP |
  186. SPI_TX_HWORD_SWAP | SPI_RX_HWORD_SWAP);
  187. } else {
  188. /* Select byte access and clear the swap configuration */
  189. clrbits_le32(&regs->mode_cfg,
  190. SPI_MODE_CH_WIDTH_WORD | SPI_MODE_BUS_WIDTH_WORD);
  191. writel(0, &regs->swap_cfg);
  192. }
  193. assert(count && count < (1 << 16));
  194. setbits_le32(&regs->ch_cfg, SPI_CH_RST);
  195. clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
  196. writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
  197. }
  198. static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
  199. void **dinp, void const **doutp, unsigned long flags)
  200. {
  201. struct exynos_spi *regs = spi_slave->regs;
  202. uchar *rxp = *dinp;
  203. const uchar *txp = *doutp;
  204. int rx_lvl, tx_lvl;
  205. uint out_bytes, in_bytes;
  206. int toread;
  207. unsigned start = get_timer(0);
  208. int stopping;
  209. int step;
  210. out_bytes = in_bytes = todo;
  211. stopping = spi_slave->skip_preamble && (flags & SPI_XFER_END) &&
  212. !(spi_slave->mode & SPI_SLAVE);
  213. /*
  214. * Try to transfer words if we can. This helps read performance at
  215. * SPI clock speeds above about 20MHz.
  216. */
  217. step = 1;
  218. if (!((todo | (uintptr_t)rxp | (uintptr_t)txp) & 3) &&
  219. !spi_slave->skip_preamble)
  220. step = 4;
  221. /*
  222. * If there's something to send, do a software reset and set a
  223. * transaction size.
  224. */
  225. spi_request_bytes(regs, todo, step);
  226. /*
  227. * Bytes are transmitted/received in pairs. Wait to receive all the
  228. * data because then transmission will be done as well.
  229. */
  230. toread = in_bytes;
  231. while (in_bytes) {
  232. int temp;
  233. /* Keep the fifos full/empty. */
  234. spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl);
  235. /*
  236. * Don't completely fill the txfifo, since we don't want our
  237. * rxfifo to overflow, and it may already contain data.
  238. */
  239. while (tx_lvl < spi_slave->fifo_size/2 && out_bytes) {
  240. if (!txp)
  241. temp = -1;
  242. else if (step == 4)
  243. temp = *(uint32_t *)txp;
  244. else
  245. temp = *txp;
  246. writel(temp, &regs->tx_data);
  247. out_bytes -= step;
  248. if (txp)
  249. txp += step;
  250. tx_lvl += step;
  251. }
  252. if (rx_lvl >= step) {
  253. while (rx_lvl >= step) {
  254. temp = readl(&regs->rx_data);
  255. if (spi_slave->skip_preamble) {
  256. if (temp == SPI_PREAMBLE_END_BYTE) {
  257. spi_slave->skip_preamble = 0;
  258. stopping = 0;
  259. }
  260. } else {
  261. if (rxp || stopping) {
  262. *rxp = temp;
  263. rxp += step;
  264. }
  265. in_bytes -= step;
  266. }
  267. toread -= step;
  268. rx_lvl -= step;
  269. }
  270. } else if (!toread) {
  271. /*
  272. * We have run out of input data, but haven't read
  273. * enough bytes after the preamble yet. Read some more,
  274. * and make sure that we transmit dummy bytes too, to
  275. * keep things going.
  276. */
  277. assert(!out_bytes);
  278. out_bytes = in_bytes;
  279. toread = in_bytes;
  280. txp = NULL;
  281. spi_request_bytes(regs, toread, step);
  282. }
  283. if (spi_slave->skip_preamble && get_timer(start) > 100) {
  284. printf("SPI timeout: in_bytes=%d, out_bytes=%d, ",
  285. in_bytes, out_bytes);
  286. return -1;
  287. }
  288. }
  289. *dinp = rxp;
  290. *doutp = txp;
  291. return 0;
  292. }
  293. /**
  294. * Transfer and receive data
  295. *
  296. * @param slave Pointer to spi_slave to which controller has to
  297. * communicate with
  298. * @param bitlen No of bits to tranfer or receive
  299. * @param dout Pointer to transfer buffer
  300. * @param din Pointer to receive buffer
  301. * @param flags Flags for transfer begin and end
  302. * @return zero on success else a negative value
  303. */
  304. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  305. void *din, unsigned long flags)
  306. {
  307. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  308. int upto, todo;
  309. int bytelen;
  310. int ret = 0;
  311. /* spi core configured to do 8 bit transfers */
  312. if (bitlen % 8) {
  313. debug("Non byte aligned SPI transfer.\n");
  314. return -1;
  315. }
  316. /* Start the transaction, if necessary. */
  317. if ((flags & SPI_XFER_BEGIN))
  318. spi_cs_activate(slave);
  319. /*
  320. * Exynos SPI limits each transfer to 65535 transfers. To keep
  321. * things simple, allow a maximum of 65532 bytes. We could allow
  322. * more in word mode, but the performance difference is small.
  323. */
  324. bytelen = bitlen / 8;
  325. for (upto = 0; !ret && upto < bytelen; upto += todo) {
  326. todo = min(bytelen - upto, (1 << 16) - 4);
  327. ret = spi_rx_tx(spi_slave, todo, &din, &dout, flags);
  328. if (ret)
  329. break;
  330. }
  331. /* Stop the transaction, if necessary. */
  332. if ((flags & SPI_XFER_END) && !(spi_slave->mode & SPI_SLAVE)) {
  333. spi_cs_deactivate(slave);
  334. if (spi_slave->skip_preamble) {
  335. assert(!spi_slave->skip_preamble);
  336. debug("Failed to complete premable transaction\n");
  337. ret = -1;
  338. }
  339. }
  340. return ret;
  341. }
  342. /**
  343. * Validates the bus and chip select numbers
  344. *
  345. * @param bus ID of the bus that the slave is attached to
  346. * @param cs ID of the chip select connected to the slave
  347. * @return one on success else zero
  348. */
  349. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  350. {
  351. return spi_get_bus(bus) && cs == 0;
  352. }
  353. /**
  354. * Activate the CS by driving it LOW
  355. *
  356. * @param slave Pointer to spi_slave to which controller has to
  357. * communicate with
  358. */
  359. void spi_cs_activate(struct spi_slave *slave)
  360. {
  361. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  362. /* If it's too soon to do another transaction, wait */
  363. if (spi_slave->bus->deactivate_delay_us &&
  364. spi_slave->last_transaction_us) {
  365. ulong delay_us; /* The delay completed so far */
  366. delay_us = timer_get_us() - spi_slave->last_transaction_us;
  367. if (delay_us < spi_slave->bus->deactivate_delay_us)
  368. udelay(spi_slave->bus->deactivate_delay_us - delay_us);
  369. }
  370. clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  371. debug("Activate CS, bus %d\n", spi_slave->slave.bus);
  372. spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
  373. /* Remember time of this transaction so we can honour the bus delay */
  374. if (spi_slave->bus->deactivate_delay_us)
  375. spi_slave->last_transaction_us = timer_get_us();
  376. }
  377. /**
  378. * Deactivate the CS by driving it HIGH
  379. *
  380. * @param slave Pointer to spi_slave to which controller has to
  381. * communicate with
  382. */
  383. void spi_cs_deactivate(struct spi_slave *slave)
  384. {
  385. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  386. setbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  387. debug("Deactivate CS, bus %d\n", spi_slave->slave.bus);
  388. }
  389. static inline struct exynos_spi *get_spi_base(int dev_index)
  390. {
  391. if (dev_index < 3)
  392. return (struct exynos_spi *)samsung_get_base_spi() + dev_index;
  393. else
  394. return (struct exynos_spi *)samsung_get_base_spi_isp() +
  395. (dev_index - 3);
  396. }
  397. /*
  398. * Read the SPI config from the device tree node.
  399. *
  400. * @param blob FDT blob to read from
  401. * @param node Node offset to read from
  402. * @param bus SPI bus structure to fill with information
  403. * @return 0 if ok, or -FDT_ERR_NOTFOUND if something was missing
  404. */
  405. #ifdef CONFIG_OF_CONTROL
  406. static int spi_get_config(const void *blob, int node, struct spi_bus *bus)
  407. {
  408. bus->node = node;
  409. bus->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
  410. bus->periph_id = pinmux_decode_periph_id(blob, node);
  411. if (bus->periph_id == PERIPH_ID_NONE) {
  412. debug("%s: Invalid peripheral ID %d\n", __func__,
  413. bus->periph_id);
  414. return -FDT_ERR_NOTFOUND;
  415. }
  416. /* Use 500KHz as a suitable default */
  417. bus->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
  418. 500000);
  419. bus->deactivate_delay_us = fdtdec_get_int(blob, node,
  420. "spi-deactivate-delay", 0);
  421. return 0;
  422. }
  423. /*
  424. * Process a list of nodes, adding them to our list of SPI ports.
  425. *
  426. * @param blob fdt blob
  427. * @param node_list list of nodes to process (any <=0 are ignored)
  428. * @param count number of nodes to process
  429. * @param is_dvc 1 if these are DVC ports, 0 if standard I2C
  430. * @return 0 if ok, -1 on error
  431. */
  432. static int process_nodes(const void *blob, int node_list[], int count)
  433. {
  434. int i;
  435. /* build the i2c_controllers[] for each controller */
  436. for (i = 0; i < count; i++) {
  437. int node = node_list[i];
  438. struct spi_bus *bus;
  439. if (node <= 0)
  440. continue;
  441. bus = &spi_bus[i];
  442. if (spi_get_config(blob, node, bus)) {
  443. printf("exynos spi_init: failed to decode bus %d\n",
  444. i);
  445. return -1;
  446. }
  447. debug("spi: controller bus %d at %p, periph_id %d\n",
  448. i, bus->regs, bus->periph_id);
  449. bus->inited = 1;
  450. bus_count++;
  451. }
  452. return 0;
  453. }
  454. #endif
  455. /**
  456. * Set up a new SPI slave for an fdt node
  457. *
  458. * @param blob Device tree blob
  459. * @param node SPI peripheral node to use
  460. * @return 0 if ok, -1 on error
  461. */
  462. struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
  463. unsigned int cs, unsigned int max_hz, unsigned int mode)
  464. {
  465. struct spi_bus *bus;
  466. unsigned int i;
  467. for (i = 0, bus = spi_bus; i < bus_count; i++, bus++) {
  468. if (bus->node == node)
  469. return spi_setup_slave(i, cs, max_hz, mode);
  470. }
  471. debug("%s: Failed to find bus node %d\n", __func__, node);
  472. return NULL;
  473. }
  474. /* Sadly there is no error return from this function */
  475. void spi_init(void)
  476. {
  477. int count;
  478. #ifdef CONFIG_OF_CONTROL
  479. int node_list[EXYNOS5_SPI_NUM_CONTROLLERS];
  480. const void *blob = gd->fdt_blob;
  481. count = fdtdec_find_aliases_for_id(blob, "spi",
  482. COMPAT_SAMSUNG_EXYNOS_SPI, node_list,
  483. EXYNOS5_SPI_NUM_CONTROLLERS);
  484. if (process_nodes(blob, node_list, count))
  485. return;
  486. #else
  487. struct spi_bus *bus;
  488. for (count = 0; count < EXYNOS5_SPI_NUM_CONTROLLERS; count++) {
  489. bus = &spi_bus[count];
  490. bus->regs = get_spi_base(count);
  491. bus->periph_id = PERIPH_ID_SPI0 + count;
  492. /* Although Exynos5 supports upto 50Mhz speed,
  493. * we are setting it to 10Mhz for safe side
  494. */
  495. bus->frequency = 10000000;
  496. bus->inited = 1;
  497. bus->node = 0;
  498. bus_count = EXYNOS5_SPI_NUM_CONTROLLERS;
  499. }
  500. #endif
  501. }