altos/lpc: Create TX/RX busy macros for SPI driver
[fw/altos] / src / lpc / ao_spi_lpc.c
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao.h>
19
20 static uint8_t          ao_spi_mutex[LPC_NUM_SPI];
21
22 static struct lpc_ssp * const ao_lpc_ssp[LPC_NUM_SPI] = { &lpc_ssp0, &lpc_ssp1 };
23
24 static uint8_t  spi_dev_null;
25
26 #define tx_busy(lpc_ssp) (lpc_ssp->sr & ((1 << LPC_SSP_SR_BSY) | (1 << LPC_SSP_SR_TNF))) != (1 << LPC_SSP_SR_TNF)
27 #define rx_busy(lpc_ssp) (lpc_ssp->sr & ((1 << LPC_SSP_SR_BSY) | (1 << LPC_SSP_SR_RNE))) != (1 << LPC_SSP_SR_RNE)
28
29 #define spi_loop(len, put, get) do {                                    \
30                 while (len--) {                                         \
31                         /* Wait for space in the fifo */                \
32                         while (tx_busy(lpc_ssp))                        \
33                                 ;                                       \
34                                                                         \
35                         /* send a byte */                               \
36                         lpc_ssp->dr = put;                              \
37                                                                         \
38                         /* Wait for byte to appear in the fifo */       \
39                         while (rx_busy(lpc_ssp))                        \
40                                 ;                                       \
41                                                                         \
42                         /* recv a byte */                               \
43                         get lpc_ssp->dr;                                \
44                 }                                                       \
45         } while (0);
46
47 void
48 ao_spi_send(void *block, uint16_t len, uint8_t id)
49 {
50         uint8_t *b = block;
51         struct lpc_ssp *lpc_ssp = ao_lpc_ssp[id];
52
53         spi_loop(len, *b++, (void));
54 }
55
56 void
57 ao_spi_send_fixed(uint8_t value, uint16_t len, uint8_t id)
58 {
59         struct lpc_ssp *lpc_ssp = ao_lpc_ssp[id];
60
61         spi_loop(len, value, (void));
62 }
63
64 void
65 ao_spi_recv(void *block, uint16_t len, uint8_t id)
66 {
67         uint8_t *b = block;
68         struct lpc_ssp *lpc_ssp = ao_lpc_ssp[id];
69
70         spi_loop(len, 0xff, *b++ =);
71 }
72
73 void
74 ao_spi_duplex(void *out, void *in, uint16_t len, uint8_t id)
75 {
76         uint8_t *o = out;
77         uint8_t *i = in;
78         struct lpc_ssp *lpc_ssp = ao_lpc_ssp[id];
79
80         spi_loop(len, *o++, *i++ =);
81 }
82
83 void
84 ao_spi_get(uint8_t id, uint32_t speed)
85 {
86         struct lpc_ssp  *lpc_ssp = ao_lpc_ssp[id];
87
88         ao_mutex_get(&ao_spi_mutex[id]);
89         
90         /* Set the clock prescale */
91         lpc_ssp->cpsr = speed;
92
93         /* Enable the device */
94         lpc_ssp->cr1 = ((0 << LPC_SSP_CR1_LBM) |
95                         (1 << LPC_SSP_CR1_SSE) |
96                         (LPC_SSP_CR1_MS_MASTER << LPC_SSP_CR1_MS) |
97                         (0 << LPC_SSP_CR1_SOD));
98 }
99
100 void
101 ao_spi_put(uint8_t id)
102 {
103         struct lpc_ssp  *lpc_ssp = ao_lpc_ssp[id];
104
105         /* Disable the device */
106         lpc_ssp->cr1 = ((0 << LPC_SSP_CR1_LBM) |
107                         (0 << LPC_SSP_CR1_SSE) |
108                         (LPC_SSP_CR1_MS_MASTER << LPC_SSP_CR1_MS) |
109                         (0 << LPC_SSP_CR1_SOD));
110         ao_mutex_put(&ao_spi_mutex[id]);
111 }
112
113 static void
114 ao_spi_channel_init(uint8_t id)
115 {
116         struct lpc_ssp  *lpc_ssp = ao_lpc_ssp[id];
117         uint8_t d;
118
119         lpc_ssp->cr0 = ((LPC_SSP_CR0_DSS_8 << LPC_SSP_CR0_DSS) |
120                         (LPC_SSP_CR0_FRF_SPI << LPC_SSP_CR0_FRF) |
121                         (0 << LPC_SSP_CR0_CPOL) |
122                         (0 << LPC_SSP_CR0_CPHA) |
123                         (0 << LPC_SSP_CR0_SCR));
124         /* Drain the receive fifo */
125         for (d = 0; d < LPC_SSP_FIFOSIZE; d++)
126                 (void) lpc_ssp->dr;
127 }
128
129 void
130 ao_spi_init(void)
131 {
132 #if HAS_SPI_0
133         /* Configure pins */
134         lpc_ioconf.pio0_6 = ao_lpc_alternate(LPC_IOCONF_FUNC_SCK0);
135         lpc_ioconf.pio0_8 = ao_lpc_alternate(LPC_IOCONF_FUNC_MISO0);
136         lpc_ioconf.pio0_9 = ao_lpc_alternate(LPC_IOCONF_FUNC_MOSI0);
137
138         /* Enable the device */
139         lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_SSP0);
140
141         /* Turn on the clock */
142         lpc_scb.ssp0clkdiv = 1;
143
144         /* Reset the device */
145         lpc_scb.presetctrl &= ~(1 << LPC_SCB_PRESETCTRL_SSP0_RST_N);
146         lpc_scb.presetctrl |= (1 << LPC_SCB_PRESETCTRL_SSP0_RST_N);
147         ao_spi_channel_init(0);
148 #endif                     
149
150 #if HAS_SPI_1
151
152 #if SPI_SCK1_P1_15
153         lpc_ioconf.pio1_15 = ao_lpc_alternate(LPC_IOCONF_FUNC_PIO1_15_SCK1);
154 #define HAS_SCK1
155 #endif
156 #if SPI_SCK1_P1_20
157         lpc_ioconf.pio1_20 = ao_lpc_alternate(LPC_IOCONF_FUNC_PIO1_20_SCK1);
158 #define HAS_SCK1
159 #endif
160 #ifndef HAS_SCK1
161 #error "No pin specified for SCK1"
162 #endif
163
164 #if SPI_MISO1_P0_22
165         lpc_ioconf.pio0_22 = ao_lpc_alternate(LPC_IOCONF_FUNC_PIO0_22_MISO1);
166 #define HAS_MISO1
167 #endif
168 #if SPI_MISO1_P1_21
169         lpc_ioconf.pio1_21 = ao_lpc_alternate(LPC_IOCONF_FUNC_PIO1_21_MISO1);
170 #define HAS_MISO1
171 #endif
172 #ifndef HAS_MISO1
173 #error "No pin specified for MISO1"
174 #endif
175
176 #if SPI_MOSI1_P0_21
177         lpc_ioconf.pio1_21 = ao_lpc_alternate(LPC_IOCONF_FUNC_PIO0_21_MOSI1);
178 #define HAS_MOSI1
179 #endif
180 #if SPI_MOSI1_P1_22
181         lpc_ioconf.pio1_22 = ao_lpc_alternate(LPC_IOCONF_FUNC_PIO1_22_MOSI1);
182 #define HAS_MOSI1
183 #endif
184 #ifndef HAS_MOSI1
185 #error "No pin specified for MOSI1"
186 #endif
187                 
188         /* Enable the device */
189         lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_SSP1);
190
191         /* Turn on the clock */
192         lpc_scb.ssp1clkdiv = 1;
193
194         /* De-assert reset */
195         lpc_scb.presetctrl |= (1 << LPC_SCB_PRESETCTRL_SSP1_RST_N);
196         ao_spi_channel_init(1);
197 #endif /* HAS_SPI_1 */
198 }