spi.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Common SPI Interface: Controller-specific definitions
  4. *
  5. * (C) Copyright 2001
  6. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
  7. */
  8. #ifndef _SPI_H_
  9. #define _SPI_H_
  10. #include <common.h>
  11. #include <linux/bitops.h>
  12. /* SPI mode flags */
  13. #define SPI_CPHA BIT(0) /* clock phase (1 = SPI_CLOCK_PHASE_SECOND) */
  14. #define SPI_CPOL BIT(1) /* clock polarity (1 = SPI_POLARITY_HIGH) */
  15. #define SPI_MODE_0 (0|0) /* (original MicroWire) */
  16. #define SPI_MODE_1 (0|SPI_CPHA)
  17. #define SPI_MODE_2 (SPI_CPOL|0)
  18. #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
  19. #define SPI_CS_HIGH BIT(2) /* CS active high */
  20. #define SPI_LSB_FIRST BIT(3) /* per-word bits-on-wire */
  21. #define SPI_3WIRE BIT(4) /* SI/SO signals shared */
  22. #define SPI_LOOP BIT(5) /* loopback mode */
  23. #define SPI_SLAVE BIT(6) /* slave mode */
  24. #define SPI_PREAMBLE BIT(7) /* Skip preamble bytes */
  25. #define SPI_TX_BYTE BIT(8) /* transmit with 1 wire byte */
  26. #define SPI_TX_DUAL BIT(9) /* transmit with 2 wires */
  27. #define SPI_TX_QUAD BIT(10) /* transmit with 4 wires */
  28. #define SPI_RX_SLOW BIT(11) /* receive with 1 wire slow */
  29. #define SPI_RX_DUAL BIT(12) /* receive with 2 wires */
  30. #define SPI_RX_QUAD BIT(13) /* receive with 4 wires */
  31. #define SPI_TX_OCTAL BIT(14) /* transmit with 8 wires */
  32. #define SPI_RX_OCTAL BIT(15) /* receive with 8 wires */
  33. /* Header byte that marks the start of the message */
  34. #define SPI_PREAMBLE_END_BYTE 0xec
  35. #define SPI_DEFAULT_WORDLEN 8
  36. /* TODO(sjg@chromium.org): Remove this and use max_hz from struct spi_slave */
  37. struct dm_spi_bus {
  38. uint max_hz;
  39. };
  40. /**
  41. * struct dm_spi_platdata - platform data for all SPI slaves
  42. *
  43. * This describes a SPI slave, a child device of the SPI bus. To obtain this
  44. * struct from a spi_slave, use dev_get_parent_platdata(dev) or
  45. * dev_get_parent_platdata(slave->dev).
  46. *
  47. * This data is immutable. Each time the device is probed, @max_hz and @mode
  48. * will be copied to struct spi_slave.
  49. *
  50. * @cs: Chip select number (0..n-1)
  51. * @max_hz: Maximum bus speed that this slave can tolerate
  52. * @mode: SPI mode to use for this device (see SPI mode flags)
  53. */
  54. struct dm_spi_slave_platdata {
  55. unsigned int cs;
  56. uint max_hz;
  57. uint mode;
  58. };
  59. /**
  60. * enum spi_clock_phase - indicates the clock phase to use for SPI (CPHA)
  61. *
  62. * @SPI_CLOCK_PHASE_FIRST: Data sampled on the first phase
  63. * @SPI_CLOCK_PHASE_SECOND: Data sampled on the second phase
  64. */
  65. enum spi_clock_phase {
  66. SPI_CLOCK_PHASE_FIRST,
  67. SPI_CLOCK_PHASE_SECOND,
  68. };
  69. /**
  70. * enum spi_wire_mode - indicates the number of wires used for SPI
  71. *
  72. * @SPI_4_WIRE_MODE: Normal bidirectional mode with MOSI and MISO
  73. * @SPI_3_WIRE_MODE: Unidirectional version with a single data line SISO
  74. */
  75. enum spi_wire_mode {
  76. SPI_4_WIRE_MODE,
  77. SPI_3_WIRE_MODE,
  78. };
  79. /**
  80. * enum spi_polarity - indicates the polarity of the SPI bus (CPOL)
  81. *
  82. * @SPI_POLARITY_LOW: Clock is low in idle state
  83. * @SPI_POLARITY_HIGH: Clock is high in idle state
  84. */
  85. enum spi_polarity {
  86. SPI_POLARITY_LOW,
  87. SPI_POLARITY_HIGH,
  88. };
  89. /**
  90. * struct spi_slave - Representation of a SPI slave
  91. *
  92. * For driver model this is the per-child data used by the SPI bus. It can
  93. * be accessed using dev_get_parent_priv() on the slave device. The SPI uclass
  94. * sets uip per_child_auto_alloc_size to sizeof(struct spi_slave), and the
  95. * driver should not override it. Two platform data fields (max_hz and mode)
  96. * are copied into this structure to provide an initial value. This allows
  97. * them to be changed, since we should never change platform data in drivers.
  98. *
  99. * If not using driver model, drivers are expected to extend this with
  100. * controller-specific data.
  101. *
  102. * @dev: SPI slave device
  103. * @max_hz: Maximum speed for this slave
  104. * @speed: Current bus speed. This is 0 until the bus is first
  105. * claimed.
  106. * @bus: ID of the bus that the slave is attached to. For
  107. * driver model this is the sequence number of the SPI
  108. * bus (bus->seq) so does not need to be stored
  109. * @cs: ID of the chip select connected to the slave.
  110. * @mode: SPI mode to use for this slave (see SPI mode flags)
  111. * @wordlen: Size of SPI word in number of bits
  112. * @max_read_size: If non-zero, the maximum number of bytes which can
  113. * be read at once.
  114. * @max_write_size: If non-zero, the maximum number of bytes which can
  115. * be written at once.
  116. * @memory_map: Address of read-only SPI flash access.
  117. * @flags: Indication of SPI flags.
  118. */
  119. struct spi_slave {
  120. #if CONFIG_IS_ENABLED(DM_SPI)
  121. struct udevice *dev; /* struct spi_slave is dev->parentdata */
  122. uint max_hz;
  123. uint speed;
  124. #else
  125. unsigned int bus;
  126. unsigned int cs;
  127. #endif
  128. uint mode;
  129. unsigned int wordlen;
  130. unsigned int max_read_size;
  131. unsigned int max_write_size;
  132. void *memory_map;
  133. u8 flags;
  134. #define SPI_XFER_BEGIN BIT(0) /* Assert CS before transfer */
  135. #define SPI_XFER_END BIT(1) /* Deassert CS after transfer */
  136. #define SPI_XFER_ONCE (SPI_XFER_BEGIN | SPI_XFER_END)
  137. };
  138. /**
  139. * spi_do_alloc_slave - Allocate a new SPI slave (internal)
  140. *
  141. * Allocate and zero all fields in the spi slave, and set the bus/chip
  142. * select. Use the helper macro spi_alloc_slave() to call this.
  143. *
  144. * @offset: Offset of struct spi_slave within slave structure.
  145. * @size: Size of slave structure.
  146. * @bus: Bus ID of the slave chip.
  147. * @cs: Chip select ID of the slave chip on the specified bus.
  148. */
  149. void *spi_do_alloc_slave(int offset, int size, unsigned int bus,
  150. unsigned int cs);
  151. /**
  152. * spi_alloc_slave - Allocate a new SPI slave
  153. *
  154. * Allocate and zero all fields in the spi slave, and set the bus/chip
  155. * select.
  156. *
  157. * @_struct: Name of structure to allocate (e.g. struct tegra_spi).
  158. * This structure must contain a member 'struct spi_slave *slave'.
  159. * @bus: Bus ID of the slave chip.
  160. * @cs: Chip select ID of the slave chip on the specified bus.
  161. */
  162. #define spi_alloc_slave(_struct, bus, cs) \
  163. spi_do_alloc_slave(offsetof(_struct, slave), \
  164. sizeof(_struct), bus, cs)
  165. /**
  166. * spi_alloc_slave_base - Allocate a new SPI slave with no private data
  167. *
  168. * Allocate and zero all fields in the spi slave, and set the bus/chip
  169. * select.
  170. *
  171. * @bus: Bus ID of the slave chip.
  172. * @cs: Chip select ID of the slave chip on the specified bus.
  173. */
  174. #define spi_alloc_slave_base(bus, cs) \
  175. spi_do_alloc_slave(0, sizeof(struct spi_slave), bus, cs)
  176. /**
  177. * Set up communications parameters for a SPI slave.
  178. *
  179. * This must be called once for each slave. Note that this function
  180. * usually doesn't touch any actual hardware, it only initializes the
  181. * contents of spi_slave so that the hardware can be easily
  182. * initialized later.
  183. *
  184. * @bus: Bus ID of the slave chip.
  185. * @cs: Chip select ID of the slave chip on the specified bus.
  186. * @max_hz: Maximum SCK rate in Hz.
  187. * @mode: Clock polarity, clock phase and other parameters.
  188. *
  189. * Returns: A spi_slave reference that can be used in subsequent SPI
  190. * calls, or NULL if one or more of the parameters are not supported.
  191. */
  192. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  193. unsigned int max_hz, unsigned int mode);
  194. /**
  195. * Free any memory associated with a SPI slave.
  196. *
  197. * @slave: The SPI slave
  198. */
  199. void spi_free_slave(struct spi_slave *slave);
  200. /**
  201. * Claim the bus and prepare it for communication with a given slave.
  202. *
  203. * This must be called before doing any transfers with a SPI slave. It
  204. * will enable and initialize any SPI hardware as necessary, and make
  205. * sure that the SCK line is in the correct idle state. It is not
  206. * allowed to claim the same bus for several slaves without releasing
  207. * the bus in between.
  208. *
  209. * @slave: The SPI slave
  210. *
  211. * Returns: 0 if the bus was claimed successfully, or a negative value
  212. * if it wasn't.
  213. */
  214. int spi_claim_bus(struct spi_slave *slave);
  215. /**
  216. * Release the SPI bus
  217. *
  218. * This must be called once for every call to spi_claim_bus() after
  219. * all transfers have finished. It may disable any SPI hardware as
  220. * appropriate.
  221. *
  222. * @slave: The SPI slave
  223. */
  224. void spi_release_bus(struct spi_slave *slave);
  225. /**
  226. * Set the word length for SPI transactions
  227. *
  228. * Set the word length (number of bits per word) for SPI transactions.
  229. *
  230. * @slave: The SPI slave
  231. * @wordlen: The number of bits in a word
  232. *
  233. * Returns: 0 on success, -1 on failure.
  234. */
  235. int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen);
  236. /**
  237. * SPI transfer (optional if mem_ops is used)
  238. *
  239. * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
  240. * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
  241. *
  242. * The source of the outgoing bits is the "dout" parameter and the
  243. * destination of the input bits is the "din" parameter. Note that "dout"
  244. * and "din" can point to the same memory location, in which case the
  245. * input data overwrites the output data (since both are buffered by
  246. * temporary variables, this is OK).
  247. *
  248. * spi_xfer() interface:
  249. * @slave: The SPI slave which will be sending/receiving the data.
  250. * @bitlen: How many bits to write and read.
  251. * @dout: Pointer to a string of bits to send out. The bits are
  252. * held in a byte array and are sent MSB first.
  253. * @din: Pointer to a string of bits that will be filled in.
  254. * @flags: A bitwise combination of SPI_XFER_* flags.
  255. *
  256. * Returns: 0 on success, not 0 on failure
  257. */
  258. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  259. void *din, unsigned long flags);
  260. /**
  261. * spi_write_then_read - SPI synchronous write followed by read
  262. *
  263. * This performs a half duplex transaction in which the first transaction
  264. * is to send the opcode and if the length of buf is non-zero then it start
  265. * the second transaction as tx or rx based on the need from respective slave.
  266. *
  267. * @slave: The SPI slave device with which opcode/data will be exchanged
  268. * @opcode: opcode used for specific transfer
  269. * @n_opcode: size of opcode, in bytes
  270. * @txbuf: buffer into which data to be written
  271. * @rxbuf: buffer into which data will be read
  272. * @n_buf: size of buf (whether it's [tx|rx]buf), in bytes
  273. *
  274. * Returns: 0 on success, not 0 on failure
  275. */
  276. int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
  277. size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
  278. size_t n_buf);
  279. /* Copy memory mapped data */
  280. void spi_flash_copy_mmap(void *data, void *offset, size_t len);
  281. /**
  282. * Determine if a SPI chipselect is valid.
  283. * This function is provided by the board if the low-level SPI driver
  284. * needs it to determine if a given chipselect is actually valid.
  285. *
  286. * Returns: 1 if bus:cs identifies a valid chip on this board, 0
  287. * otherwise.
  288. */
  289. int spi_cs_is_valid(unsigned int bus, unsigned int cs);
  290. /*
  291. * These names are used in several drivers and these declarations will be
  292. * removed soon as part of the SPI DM migration. Drop them if driver model is
  293. * enabled for SPI.
  294. */
  295. #if !CONFIG_IS_ENABLED(DM_SPI)
  296. /**
  297. * Activate a SPI chipselect.
  298. * This function is provided by the board code when using a driver
  299. * that can't control its chipselects automatically (e.g.
  300. * common/soft_spi.c). When called, it should activate the chip select
  301. * to the device identified by "slave".
  302. */
  303. void spi_cs_activate(struct spi_slave *slave);
  304. /**
  305. * Deactivate a SPI chipselect.
  306. * This function is provided by the board code when using a driver
  307. * that can't control its chipselects automatically (e.g.
  308. * common/soft_spi.c). When called, it should deactivate the chip
  309. * select to the device identified by "slave".
  310. */
  311. void spi_cs_deactivate(struct spi_slave *slave);
  312. #endif
  313. /**
  314. * Set transfer speed.
  315. * This sets a new speed to be applied for next spi_xfer().
  316. * @slave: The SPI slave
  317. * @hz: The transfer speed
  318. */
  319. void spi_set_speed(struct spi_slave *slave, uint hz);
  320. /**
  321. * Write 8 bits, then read 8 bits.
  322. * @slave: The SPI slave we're communicating with
  323. * @byte: Byte to be written
  324. *
  325. * Returns: The value that was read, or a negative value on error.
  326. *
  327. * TODO: This function probably shouldn't be inlined.
  328. */
  329. static inline int spi_w8r8(struct spi_slave *slave, unsigned char byte)
  330. {
  331. unsigned char dout[2];
  332. unsigned char din[2];
  333. int ret;
  334. dout[0] = byte;
  335. dout[1] = 0;
  336. ret = spi_xfer(slave, 16, dout, din, SPI_XFER_BEGIN | SPI_XFER_END);
  337. return ret < 0 ? ret : din[1];
  338. }
  339. /**
  340. * struct spi_cs_info - Information about a bus chip select
  341. *
  342. * @dev: Connected device, or NULL if none
  343. */
  344. struct spi_cs_info {
  345. struct udevice *dev;
  346. };
  347. /**
  348. * struct struct dm_spi_ops - Driver model SPI operations
  349. *
  350. * The uclass interface is implemented by all SPI devices which use
  351. * driver model.
  352. */
  353. struct dm_spi_ops {
  354. /**
  355. * Claim the bus and prepare it for communication.
  356. *
  357. * The device provided is the slave device. It's parent controller
  358. * will be used to provide the communication.
  359. *
  360. * This must be called before doing any transfers with a SPI slave. It
  361. * will enable and initialize any SPI hardware as necessary, and make
  362. * sure that the SCK line is in the correct idle state. It is not
  363. * allowed to claim the same bus for several slaves without releasing
  364. * the bus in between.
  365. *
  366. * @dev: The SPI slave
  367. *
  368. * Returns: 0 if the bus was claimed successfully, or a negative value
  369. * if it wasn't.
  370. */
  371. int (*claim_bus)(struct udevice *dev);
  372. /**
  373. * Release the SPI bus
  374. *
  375. * This must be called once for every call to spi_claim_bus() after
  376. * all transfers have finished. It may disable any SPI hardware as
  377. * appropriate.
  378. *
  379. * @dev: The SPI slave
  380. */
  381. int (*release_bus)(struct udevice *dev);
  382. /**
  383. * Set the word length for SPI transactions
  384. *
  385. * Set the word length (number of bits per word) for SPI transactions.
  386. *
  387. * @bus: The SPI slave
  388. * @wordlen: The number of bits in a word
  389. *
  390. * Returns: 0 on success, -ve on failure.
  391. */
  392. int (*set_wordlen)(struct udevice *dev, unsigned int wordlen);
  393. /**
  394. * SPI transfer
  395. *
  396. * This writes "bitlen" bits out the SPI MOSI port and simultaneously
  397. * clocks "bitlen" bits in the SPI MISO port. That's just the way SPI
  398. * works.
  399. *
  400. * The source of the outgoing bits is the "dout" parameter and the
  401. * destination of the input bits is the "din" parameter. Note that
  402. * "dout" and "din" can point to the same memory location, in which
  403. * case the input data overwrites the output data (since both are
  404. * buffered by temporary variables, this is OK).
  405. *
  406. * spi_xfer() interface:
  407. * @dev: The slave device to communicate with
  408. * @bitlen: How many bits to write and read.
  409. * @dout: Pointer to a string of bits to send out. The bits are
  410. * held in a byte array and are sent MSB first.
  411. * @din: Pointer to a string of bits that will be filled in.
  412. * @flags: A bitwise combination of SPI_XFER_* flags.
  413. *
  414. * Returns: 0 on success, not -1 on failure
  415. */
  416. int (*xfer)(struct udevice *dev, unsigned int bitlen, const void *dout,
  417. void *din, unsigned long flags);
  418. /**
  419. * Optimized handlers for SPI memory-like operations.
  420. *
  421. * Optimized/dedicated operations for interactions with SPI memory. This
  422. * field is optional and should only be implemented if the controller
  423. * has native support for memory like operations.
  424. */
  425. const struct spi_controller_mem_ops *mem_ops;
  426. /**
  427. * Set transfer speed.
  428. * This sets a new speed to be applied for next spi_xfer().
  429. * @bus: The SPI bus
  430. * @hz: The transfer speed
  431. * @return 0 if OK, -ve on error
  432. */
  433. int (*set_speed)(struct udevice *bus, uint hz);
  434. /**
  435. * Set the SPI mode/flags
  436. *
  437. * It is unclear if we want to set speed and mode together instead
  438. * of separately.
  439. *
  440. * @bus: The SPI bus
  441. * @mode: Requested SPI mode (SPI_... flags)
  442. * @return 0 if OK, -ve on error
  443. */
  444. int (*set_mode)(struct udevice *bus, uint mode);
  445. /**
  446. * Get information on a chip select
  447. *
  448. * This is only called when the SPI uclass does not know about a
  449. * chip select, i.e. it has no attached device. It gives the driver
  450. * a chance to allow activity on that chip select even so.
  451. *
  452. * @bus: The SPI bus
  453. * @cs: The chip select (0..n-1)
  454. * @info: Returns information about the chip select, if valid.
  455. * On entry info->dev is NULL
  456. * @return 0 if OK (and @info is set up), -EINVAL if the chip select
  457. * is invalid, other -ve value on error
  458. */
  459. int (*cs_info)(struct udevice *bus, uint cs, struct spi_cs_info *info);
  460. /**
  461. * get_mmap() - Get memory-mapped SPI
  462. *
  463. * @dev: The SPI flash slave device
  464. * @map_basep: Returns base memory address for mapped SPI
  465. * @map_sizep: Returns size of mapped SPI
  466. * @offsetp: Returns start offset of SPI flash where the map works
  467. * correctly (offsets before this are not visible)
  468. * @return 0 if OK, -EFAULT if memory mapping is not available
  469. */
  470. int (*get_mmap)(struct udevice *dev, ulong *map_basep,
  471. uint *map_sizep, uint *offsetp);
  472. };
  473. struct dm_spi_emul_ops {
  474. /**
  475. * SPI transfer
  476. *
  477. * This writes "bitlen" bits out the SPI MOSI port and simultaneously
  478. * clocks "bitlen" bits in the SPI MISO port. That's just the way SPI
  479. * works. Here the device is a slave.
  480. *
  481. * The source of the outgoing bits is the "dout" parameter and the
  482. * destination of the input bits is the "din" parameter. Note that
  483. * "dout" and "din" can point to the same memory location, in which
  484. * case the input data overwrites the output data (since both are
  485. * buffered by temporary variables, this is OK).
  486. *
  487. * spi_xfer() interface:
  488. * @slave: The SPI slave which will be sending/receiving the data.
  489. * @bitlen: How many bits to write and read.
  490. * @dout: Pointer to a string of bits sent to the device. The
  491. * bits are held in a byte array and are sent MSB first.
  492. * @din: Pointer to a string of bits that will be sent back to
  493. * the master.
  494. * @flags: A bitwise combination of SPI_XFER_* flags.
  495. *
  496. * Returns: 0 on success, not -1 on failure
  497. */
  498. int (*xfer)(struct udevice *slave, unsigned int bitlen,
  499. const void *dout, void *din, unsigned long flags);
  500. };
  501. /**
  502. * spi_find_bus_and_cs() - Find bus and slave devices by number
  503. *
  504. * Given a bus number and chip select, this finds the corresponding bus
  505. * device and slave device. Neither device is activated by this function,
  506. * although they may have been activated previously.
  507. *
  508. * @busnum: SPI bus number
  509. * @cs: Chip select to look for
  510. * @busp: Returns bus device
  511. * @devp: Return slave device
  512. * @return 0 if found, -ENODEV on error
  513. */
  514. int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
  515. struct udevice **devp);
  516. /**
  517. * spi_get_bus_and_cs() - Find and activate bus and slave devices by number
  518. *
  519. * Given a bus number and chip select, this finds the corresponding bus
  520. * device and slave device.
  521. *
  522. * If no such slave exists, and drv_name is not NULL, then a new slave device
  523. * is automatically bound on this chip select with requested speed and mode.
  524. *
  525. * Ths new slave device is probed ready for use with the speed and mode
  526. * from platdata when available or the requested values.
  527. *
  528. * @busnum: SPI bus number
  529. * @cs: Chip select to look for
  530. * @speed: SPI speed to use for this slave when not available in platdata
  531. * @mode: SPI mode to use for this slave when not available in platdata
  532. * @drv_name: Name of driver to attach to this chip select
  533. * @dev_name: Name of the new device thus created
  534. * @busp: Returns bus device
  535. * @devp: Return slave device
  536. * @return 0 if found, -ve on error
  537. */
  538. int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
  539. const char *drv_name, const char *dev_name,
  540. struct udevice **busp, struct spi_slave **devp);
  541. /**
  542. * spi_chip_select() - Get the chip select for a slave
  543. *
  544. * @return the chip select this slave is attached to
  545. */
  546. int spi_chip_select(struct udevice *slave);
  547. /**
  548. * spi_find_chip_select() - Find the slave attached to chip select
  549. *
  550. * @bus: SPI bus to search
  551. * @cs: Chip select to look for
  552. * @devp: Returns the slave device if found
  553. * @return 0 if found, -EINVAL if cs is invalid, -ENODEV if no device attached,
  554. * other -ve value on error
  555. */
  556. int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp);
  557. /**
  558. * spi_slave_ofdata_to_platdata() - decode standard SPI platform data
  559. *
  560. * This decodes the speed and mode for a slave from a device tree node
  561. *
  562. * @blob: Device tree blob
  563. * @node: Node offset to read from
  564. * @plat: Place to put the decoded information
  565. */
  566. int spi_slave_ofdata_to_platdata(struct udevice *dev,
  567. struct dm_spi_slave_platdata *plat);
  568. /**
  569. * spi_cs_info() - Check information on a chip select
  570. *
  571. * This checks a particular chip select on a bus to see if it has a device
  572. * attached, or is even valid.
  573. *
  574. * @bus: The SPI bus
  575. * @cs: The chip select (0..n-1)
  576. * @info: Returns information about the chip select, if valid
  577. * @return 0 if OK (and @info is set up), -ENODEV if the chip select
  578. * is invalid, other -ve value on error
  579. */
  580. int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info);
  581. struct sandbox_state;
  582. /**
  583. * sandbox_spi_get_emul() - get an emulator for a SPI slave
  584. *
  585. * This provides a way to attach an emulated SPI device to a particular SPI
  586. * slave, so that xfer() operations on the slave will be handled by the
  587. * emulator. If a emulator already exists on that chip select it is returned.
  588. * Otherwise one is created.
  589. *
  590. * @state: Sandbox state
  591. * @bus: SPI bus requesting the emulator
  592. * @slave: SPI slave device requesting the emulator
  593. * @emuip: Returns pointer to emulator
  594. * @return 0 if OK, -ve on error
  595. */
  596. int sandbox_spi_get_emul(struct sandbox_state *state,
  597. struct udevice *bus, struct udevice *slave,
  598. struct udevice **emulp);
  599. /**
  600. * Claim the bus and prepare it for communication with a given slave.
  601. *
  602. * This must be called before doing any transfers with a SPI slave. It
  603. * will enable and initialize any SPI hardware as necessary, and make
  604. * sure that the SCK line is in the correct idle state. It is not
  605. * allowed to claim the same bus for several slaves without releasing
  606. * the bus in between.
  607. *
  608. * @dev: The SPI slave device
  609. *
  610. * Returns: 0 if the bus was claimed successfully, or a negative value
  611. * if it wasn't.
  612. */
  613. int dm_spi_claim_bus(struct udevice *dev);
  614. /**
  615. * Release the SPI bus
  616. *
  617. * This must be called once for every call to dm_spi_claim_bus() after
  618. * all transfers have finished. It may disable any SPI hardware as
  619. * appropriate.
  620. *
  621. * @slave: The SPI slave device
  622. */
  623. void dm_spi_release_bus(struct udevice *dev);
  624. /**
  625. * SPI transfer
  626. *
  627. * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
  628. * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
  629. *
  630. * The source of the outgoing bits is the "dout" parameter and the
  631. * destination of the input bits is the "din" parameter. Note that "dout"
  632. * and "din" can point to the same memory location, in which case the
  633. * input data overwrites the output data (since both are buffered by
  634. * temporary variables, this is OK).
  635. *
  636. * dm_spi_xfer() interface:
  637. * @dev: The SPI slave device which will be sending/receiving the data.
  638. * @bitlen: How many bits to write and read.
  639. * @dout: Pointer to a string of bits to send out. The bits are
  640. * held in a byte array and are sent MSB first.
  641. * @din: Pointer to a string of bits that will be filled in.
  642. * @flags: A bitwise combination of SPI_XFER_* flags.
  643. *
  644. * Returns: 0 on success, not 0 on failure
  645. */
  646. int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
  647. const void *dout, void *din, unsigned long flags);
  648. /**
  649. * spi_get_mmap() - Get memory-mapped SPI
  650. *
  651. * @dev: SPI slave device to check
  652. * @map_basep: Returns base memory address for mapped SPI
  653. * @map_sizep: Returns size of mapped SPI
  654. * @offsetp: Returns start offset of SPI flash where the map works
  655. * correctly (offsets before this are not visible)
  656. * @return 0 if OK, -ENOSYS if no operation, -EFAULT if memory mapping is not
  657. * available
  658. */
  659. int dm_spi_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep,
  660. uint *offsetp);
  661. /* Access the operations for a SPI device */
  662. #define spi_get_ops(dev) ((struct dm_spi_ops *)(dev)->driver->ops)
  663. #define spi_emul_get_ops(dev) ((struct dm_spi_emul_ops *)(dev)->driver->ops)
  664. #endif /* _SPI_H_ */