spi.h 24 KB

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