[543] | 1 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 2 | // File : spi_driver.h |
---|
| 3 | // Date : 31/08/2012 |
---|
| 4 | // Author : cesar fuguet |
---|
| 5 | // Copyright (c) UPMC-LIP6 |
---|
| 6 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 7 | #ifndef _GIET_SPI_DRIVER_H_ |
---|
| 8 | #define _GIET_SPI_DRIVER_H_ |
---|
| 9 | |
---|
| 10 | //#include <mapping_info.h> |
---|
| 11 | #include <hal_kernel_types.h> |
---|
| 12 | |
---|
| 13 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 14 | // SPI structure definition |
---|
| 15 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 16 | struct spi_dev |
---|
| 17 | { |
---|
| 18 | // RX/TX registers of the SPI controller |
---|
| 19 | unsigned int rx_tx[4]; |
---|
| 20 | |
---|
| 21 | // control register of the SPI controller |
---|
| 22 | unsigned int ctrl; |
---|
| 23 | |
---|
| 24 | // divider register for the SPI controller generated clock signal |
---|
| 25 | unsigned int divider; |
---|
| 26 | |
---|
| 27 | // slave select register of the SPI controller |
---|
| 28 | unsigned int ss; |
---|
| 29 | |
---|
| 30 | // SPI-DMA registers |
---|
| 31 | unsigned int dma_base; |
---|
| 32 | unsigned int dma_baseh; |
---|
| 33 | unsigned int dma_count; |
---|
| 34 | }; |
---|
| 35 | |
---|
| 36 | void spi_put_tx(struct spi_dev * spi, unsigned char byte, int index); |
---|
| 37 | |
---|
| 38 | inline volatile unsigned char spi_get_rx(struct spi_dev * spi, int index); |
---|
| 39 | |
---|
| 40 | unsigned int spi_get_data(struct spi_dev * spi, paddr_t buffer, unsigned int count); |
---|
| 41 | |
---|
| 42 | inline void spi_ss_assert(struct spi_dev * spi, int index); |
---|
| 43 | |
---|
| 44 | inline void spi_ss_deassert(struct spi_dev * spi, int index); |
---|
| 45 | |
---|
| 46 | void _spi_init ( struct spi_dev * spi, |
---|
| 47 | int spi_freq , |
---|
| 48 | int sys_freq , |
---|
| 49 | int char_len , |
---|
| 50 | int tx_edge , |
---|
| 51 | int rx_edge ); |
---|
| 52 | |
---|
| 53 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 54 | // SPI macros and constants |
---|
| 55 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 56 | #define SPI_TX_POSEDGE 1 // MOSI is changed on neg edge |
---|
| 57 | #define SPI_TX_NEGEDGE 0 // MOSI is changed on pos edge |
---|
| 58 | #define SPI_RX_POSEDGE 1 // MISO is latched on pos edge |
---|
| 59 | #define SPI_RX_NEGEDGE 0 // MISO is latched on neg edge |
---|
| 60 | |
---|
| 61 | #define SPI_CTRL_ASS_EN ( 1 << 13 ) // Auto Slave Sel Assertion |
---|
| 62 | #define SPI_CTRL_IE_EN ( 1 << 12 ) // Interrupt Enable |
---|
| 63 | #define SPI_CTRL_LSB_EN ( 1 << 11 ) // LSB are sent first |
---|
| 64 | #define SPI_CTRL_TXN_EN ( 1 << 10 ) // MOSI is changed on neg edge |
---|
| 65 | #define SPI_CTRL_RXN_EN ( 1 << 9 ) // MISO is latched on neg edge |
---|
| 66 | #define SPI_CTRL_GO_BSY ( 1 << 8 ) // Start the transfer |
---|
| 67 | #define SPI_CTRL_DMA_BSY ( 1 << 16 ) // DMA in progress |
---|
| 68 | #define SPI_CTRL_CHAR_LEN_MASK ( 0xFF ) // Bits transmited in 1 transfer |
---|
| 69 | #define SPI_RXTX_MASK ( 0xFF ) // Mask for the an RX/TX value |
---|
| 70 | |
---|
| 71 | #define SPI_DMA_COUNT_READ ( 1 << 0 ) // operation is a read (else write) |
---|
| 72 | |
---|
| 73 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 74 | // SPI_IS_BUSY() |
---|
| 75 | // This macro checks the GO_BSY and DMA_BSY bits of the SPI controller which |
---|
| 76 | // indicates an ongoing transfer. |
---|
| 77 | // |
---|
| 78 | // Returns 1 if there is an unfinished transfer |
---|
| 79 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 80 | #define SPI_IS_BUSY(x) \ |
---|
| 81 | ((ioread32(&x->ctrl) & (SPI_CTRL_GO_BSY|SPI_CTRL_DMA_BSY)) != 0) ? 1 : 0 |
---|
| 82 | |
---|
| 83 | #endif |
---|
| 84 | |
---|
| 85 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 86 | // Read an 32 bits memory mapped hardware register |
---|
| 87 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 88 | static inline unsigned int ioread32(void * addr) |
---|
| 89 | { |
---|
| 90 | return *(volatile unsigned int *) addr; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 94 | // Read an 16 bits memory mapped hardware register |
---|
| 95 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 96 | static inline unsigned short ioread16(void * addr) |
---|
| 97 | { |
---|
| 98 | return *(volatile unsigned short *) addr; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 102 | // Read an 8 bits memory mapped hardware register |
---|
| 103 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 104 | static inline unsigned char ioread8(void * addr) |
---|
| 105 | { |
---|
| 106 | return *(volatile unsigned char *) addr; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 110 | // Write an 32 bits memory mapped hardware register |
---|
| 111 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 112 | static inline void iowrite32(void * addr, unsigned int value) |
---|
| 113 | { |
---|
| 114 | *(volatile unsigned int *) addr = value; |
---|
| 115 | asm volatile("sync" ::: "memory"); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 119 | // Write an 16 bits memory mapped hardware register |
---|
| 120 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 121 | static inline void iowrite16(void * addr, unsigned short value) |
---|
| 122 | { |
---|
| 123 | *(volatile unsigned short *) addr = value; |
---|
| 124 | asm volatile("sync" ::: "memory"); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 128 | // Write an 8 bits memory mapped hardware register |
---|
| 129 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 130 | static inline void iowrite8(void * addr, unsigned char value) |
---|
| 131 | { |
---|
| 132 | *(volatile unsigned char *) addr = value; |
---|
| 133 | asm volatile("sync" ::: "memory"); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | // Local Variables: |
---|
| 137 | // tab-width: 4 |
---|
| 138 | // c-basic-offset: 4 |
---|
| 139 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 140 | // indent-tabs-mode: nil |
---|
| 141 | // End: |
---|
[619] | 142 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|