spi.h 22 KB

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