altos: Get SAMD21 SPI driver working in non-DMA mode
[fw/altos] / src / samd21 / ao_arch_funcs.h
1 /*
2  * Copyright © 2019 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #ifndef _AO_ARCH_FUNCS_H_
20 #define _AO_ARCH_FUNCS_H_
21
22 #define AO_MODE_PULL_NONE       0
23 #define AO_MODE_PULL_UP         1
24 #define AO_MODE_PULL_DOWN       2
25
26 static inline void ao_enable_port(struct samd21_port *port)
27 {
28         (void) port;
29         samd21_pm.apbbmask |= (1UL << SAMD21_PM_APBBMASK_PORT);
30 }
31
32 static inline void ao_disable_port(struct samd21_port *port)
33 {
34         (void) port;
35         samd21_pm.apbbmask &= ~(1UL << SAMD21_PM_APBBMASK_PORT);
36 }
37
38 static inline void
39 ao_gpio_set(struct samd21_port *port, uint8_t bit, uint8_t v)
40 {
41         if (v)
42                 port->outset = (1 << bit);
43         else
44                 port->outclr = (1 << bit);
45 }
46
47 static inline uint8_t
48 ao_gpio_get(struct samd21_port *port, uint8_t bit)
49 {
50         return (port->in >> bit) & 1;
51 }
52
53 static inline void
54 ao_gpio_dir_set(struct samd21_port *port, uint8_t bit, bool output)
55 {
56         if (output)
57                 port->dirset = (1 << bit);
58         else
59                 port->dirclr = (1 << bit);
60 }
61
62 static inline void
63 ao_gpio_set_mode(struct samd21_port *port, uint8_t bit, uint32_t mode)
64 {
65         uint8_t pincfg = 0;
66
67         if (mode != AO_MODE_PULL_NONE) {
68                 pincfg |= (1 << SAMD21_PORT_PINCFG_PULLEN);
69                 ao_gpio_set(port, bit, mode == AO_MODE_PULL_UP);
70         }
71
72         samd21_port_pincfg_set(port, bit,
73                                (0 << SAMD21_PORT_PINCFG_DRVSTR) |
74                                (1 << SAMD21_PORT_PINCFG_PULLEN) |
75                                (0 << SAMD21_PORT_PINCFG_INEN) |
76                                (0 << SAMD21_PORT_PINCFG_PMUXEN),
77                                pincfg);
78 }
79
80 static inline void
81 ao_enable_output(struct samd21_port *port, uint8_t pin, uint8_t v)
82 {
83         ao_enable_port(port);
84         ao_gpio_set(port, pin, v);
85         samd21_port_dir_set(port, pin, SAMD21_PORT_DIR_OUT);
86         samd21_port_pincfg_set(port, pin,
87                                (1 << SAMD21_PORT_PINCFG_DRVSTR) |
88                                (1 << SAMD21_PORT_PINCFG_PULLEN) |
89                                (1 << SAMD21_PORT_PINCFG_INEN),
90                                (0 << SAMD21_PORT_PINCFG_DRVSTR) |
91                                (0 << SAMD21_PORT_PINCFG_PULLEN) |
92                                (0 << SAMD21_PORT_PINCFG_INEN));
93 }
94
95 static inline void
96 ao_enable_input(struct samd21_port *port, uint8_t pin, uint32_t mode)
97 {
98         ao_enable_port(port);
99         samd21_port_dir_set(port, pin, SAMD21_PORT_DIR_IN);
100         uint8_t pincfg;
101
102         pincfg = ((0 << SAMD21_PORT_PINCFG_DRVSTR) |
103                   (0 << SAMD21_PORT_PINCFG_PULLEN) |
104                   (1 << SAMD21_PORT_PINCFG_INEN) |
105                   (0 << SAMD21_PORT_PINCFG_PMUXEN));
106
107         if (mode != AO_MODE_PULL_NONE) {
108                 pincfg |= (1 << SAMD21_PORT_PINCFG_PULLEN);
109                 ao_gpio_set(port, pin, mode == AO_MODE_PULL_UP);
110         }
111
112         samd21_port_pincfg_set(port, pin,
113                                (1 << SAMD21_PORT_PINCFG_DRVSTR) |
114                                (1 << SAMD21_PORT_PINCFG_PULLEN) |
115                                (1 << SAMD21_PORT_PINCFG_INEN) |
116                                (1 << SAMD21_PORT_PINCFG_PMUXEN),
117                                pincfg);
118 }
119
120 static inline void
121 ao_enable_cs(struct samd21_port *port, uint8_t pin)
122 {
123         ao_enable_output(port, pin, 1);
124 }
125
126 /* ao_spi_samd21.c */
127
128 #define AO_SPI_CPOL_BIT         6
129 #define AO_SPI_CPHA_BIT         7
130
131 #define AO_SPI_CONFIG_1         0x00
132 /*
133  * PA08 SERCOM0.0 -> MOSI       (DOPO 0)
134  * PA09 SERCOM0.1 -> SCLK       (DOPO 0)
135  * PA10 SERCOM0.2 -> MISO       (DIPO 2)
136  */
137 #define AO_SPI_0_CONFIG_PA08_PA09_PA10  AO_SPI_CONFIG_1
138
139 #define AO_SPI_CONFIG_2         0x08
140 /*
141  * PA04 SERCOM0.0 -> MOSI       (DOPO 0)
142  * PA05 SERCOM0.1 -> SCLK       (DOPO 0)
143  * PA16 SERCOM0.2 -> MISO       (DIPO 2)
144  */
145 #define AO_SPI_0_CONFIG_PA04_PA05_PA06  AO_SPI_CONFIG_2
146
147 #define AO_SPI_CONFIG_NONE      0x0c
148
149 #define AO_SPI_INDEX_MASK       0x07
150 #define AO_SPI_CONFIG_MASK      0x18
151
152 #define AO_SPI_INDEX(id)        ((id) & AO_SPI_INDEX_MASK)
153 #define AO_SPI_CONFIG(id)       ((id) & AO_SPI_CONFIG_MASK)
154 #define AO_SPI_PIN_CONFIG(id)   ((id) & (AO_SPI_INDEX_MASK | AO_SPI_CONFIG_MASK))
155 #define AO_SPI_CPOL(id)         ((uint32_t) (((id) >> AO_SPI_CPOL_BIT) & 1))
156 #define AO_SPI_CPHA(id)         ((uint32_t) (((id) >> AO_SPI_CPHA_BIT) & 1))
157
158 /*
159  * We're not going to do any fancy SPI pin remapping, just use the first
160  * three PAD pins, which means:
161  *
162  * MOSI: PAD.0
163  * SCK:  PAD.1
164  * MISO: PAD.2
165  */
166
167 #define AO_SPI_0_PA08_PA09_PA10 (0 | AO_SPI_0_CONFIG_PA08_PA09_PA10)
168 #define AO_SPI_0_PA04_PA05_PA06 (0 | AO_SPI_0_CONFIG_PA04_PA05_PA06)
169
170 void
171 ao_spi_send(const void *block, uint16_t len, uint8_t spi_index);
172
173 void
174 ao_spi_recv(void *block, uint16_t len, uint8_t spi_index);
175
176 void
177 ao_spi_duplex(const void *out, void *in, uint16_t len, uint8_t spi_index);
178
179 void
180 ao_spi_get(uint8_t spi_index, uint32_t speed);
181
182 void
183 ao_spi_put(uint8_t spi_index);
184
185 void
186 ao_spi_init(void);
187
188 #define ao_spi_get_mask(reg,mask,bus, speed) do {               \
189                 ao_spi_get(bus, speed);                         \
190                 ao_spi_set_cs(reg,mask);                        \
191         } while (0)
192
193 #define ao_spi_put_mask(reg,mask,bus) do {      \
194                 ao_spi_clr_cs(reg,mask);        \
195                 ao_spi_put(bus);                \
196         } while (0)
197
198 static inline void
199 ao_spi_get_bit(struct samd21_port *port, uint8_t bit, uint8_t bus, uint32_t speed)
200 {
201         ao_spi_get(bus, speed);
202         ao_gpio_set(port, bit, 0);
203 }
204
205 static inline void
206 ao_spi_put_bit(struct samd21_port *port, uint8_t bit, uint8_t bus)
207 {
208         ao_gpio_set(port, bit, 1);
209         ao_spi_put(bus);
210 }
211
212 static inline uint8_t
213 ao_spi_speed(uint32_t hz)
214 {
215         int32_t baud = (int32_t) (AO_SYSCLK / (2 * hz)) - 1;
216
217         if (baud < 0)
218                 baud = 0;
219         if (baud > 255)
220                 baud = 255;
221         return (uint8_t) baud;
222 }
223
224 #define ao_spi_init_cs(port, mask) do {                                 \
225                 uint8_t __bit__;                                        \
226                 for (__bit__ = 0; __bit__ < 32; __bit__++) {            \
227                         if (mask & (1 << __bit__))                      \
228                                 ao_enable_output(port, __bit__, 1); \
229                 }                                                       \
230         } while (0)
231
232 #define ARM_PUSH32(stack, val)  (*(--(stack)) = (val))
233
234 typedef uint32_t        ao_arch_irq_t;
235
236 static inline uint32_t
237 ao_arch_irqsave(void) {
238         uint32_t        primask;
239         asm("mrs %0,primask" : "=&r" (primask));
240         ao_arch_block_interrupts();
241         return primask;
242 }
243
244 static inline void
245 ao_arch_irqrestore(uint32_t primask) {
246         asm("msr primask,%0" : : "r" (primask));
247 }
248
249 static inline void
250 ao_arch_memory_barrier(void) {
251         asm volatile("" ::: "memory");
252 }
253
254 #if HAS_TASK
255 static inline void
256 ao_arch_init_stack(struct ao_task *task, uint32_t *sp, void *start)
257 {
258         uint32_t        a = (uint32_t) start;
259         int             i;
260
261         /* Return address (goes into LR) */
262         ARM_PUSH32(sp, a);
263
264         /* Clear register values r0-r7 */
265         i = 8;
266         while (i--)
267                 ARM_PUSH32(sp, 0);
268
269         /* APSR */
270         ARM_PUSH32(sp, 0);
271
272         /* PRIMASK with interrupts enabled */
273         ARM_PUSH32(sp, 0);
274
275         task->sp32 = sp;
276 }
277
278 static inline void ao_arch_save_regs(void) {
279         /* Save general registers */
280         asm("push {r0-r7,lr}\n");
281
282         /* Save APSR */
283         asm("mrs r0,apsr");
284         asm("push {r0}");
285
286         /* Save PRIMASK */
287         asm("mrs r0,primask");
288         asm("push {r0}");
289 }
290
291 static inline void ao_arch_save_stack(void) {
292         uint32_t        *sp;
293         asm("mov %0,sp" : "=&r" (sp) );
294         ao_cur_task->sp32 = (sp);
295         if (sp < &ao_cur_task->stack32[0])
296                 ao_panic (AO_PANIC_STACK);
297 }
298
299 static inline void ao_arch_restore_stack(void) {
300         /* Switch stacks */
301         asm("mov sp, %0" : : "r" (ao_cur_task->sp32) );
302
303         /* Restore PRIMASK */
304         asm("pop {r0}");
305         asm("msr primask,r0");
306
307         /* Restore APSR */
308         asm("pop {r0}");
309         asm("msr apsr_nczvq,r0");
310
311         /* Restore general registers */
312         asm("pop {r0-r7,pc}\n");
313 }
314
315 #ifndef HAS_SAMPLE_PROFILE
316 #define HAS_SAMPLE_PROFILE 0
317 #endif
318
319 #if !HAS_SAMPLE_PROFILE
320 #define HAS_ARCH_START_SCHEDULER        1
321
322 static inline void ao_arch_start_scheduler(void) {
323         uint32_t        sp;
324         uint32_t        control;
325
326         asm("mrs %0,msp" : "=&r" (sp));
327         asm("msr psp,%0" : : "r" (sp));
328         asm("mrs %0,control" : "=&r" (control));
329         control |= (1 << 1);
330         asm("msr control,%0" : : "r" (control));
331         asm("isb");
332 }
333 #endif
334
335 #define ao_arch_isr_stack()
336
337 #endif
338
339 #define ao_arch_wait_interrupt() do {                           \
340                 asm("\twfi\n");                                 \
341                 ao_arch_release_interrupts();                   \
342                 asm(".global ao_idle_loc\nao_idle_loc:");       \
343                 ao_arch_block_interrupts();                     \
344         } while (0)
345
346 #define ao_arch_critical(b) do {                        \
347                 uint32_t __mask = ao_arch_irqsave();    \
348                 do { b } while (0);                     \
349                 ao_arch_irqrestore(__mask);             \
350         } while (0)
351
352 /* ao_serial_samd21.c */
353
354 #if USE_SERIAL_0_FLOW && USE_SERIAL_0_SW_FLOW || USE_SERIAL_1_FLOW && USE_SERIAL_1_SW_FLOW
355 #define HAS_SERIAL_SW_FLOW      1
356 #else
357 #define HAS_SERIAL_SW_FLOW      0
358 #endif
359
360 #if USE_SERIAL_1_FLOW && !USE_SERIAL_1_SW_FLOW
361 #define USE_SERIAL_1_HW_FLOW    1
362 #endif
363
364 #if USE_SERIAL_0_FLOW && !USE_SERIAL_0_SW_FLOW
365 #define USE_SERIAL_0_HW_FLOW    1
366 #endif
367
368 #if USE_SERIAL_0_HW_FLOW || USE_SERIAL_1_HW_FLOW
369 #define HAS_SERIAL_HW_FLOW      1
370 #else
371 #define HAS_SERIAL_HW_FLOW      0
372 #endif
373
374 struct ao_samd21_usart {
375         struct ao_fifo          rx_fifo;
376         struct ao_fifo          tx_fifo;
377         struct samd21_sercom    *reg;
378         uint8_t                 tx_running;
379         uint8_t                 draining;
380 #if HAS_SERIAL_SW_FLOW
381         /* RTS - 0 if we have FIFO space, 1 if not
382          * CTS - 0 if we can send, 0 if not
383          */
384         struct samd21_port      *gpio_rts;
385         struct samd21_port      *gpio_cts;
386         uint8_t                 pin_rts;
387         uint8_t                 pin_cts;
388         uint8_t                 rts;
389 #endif
390 };
391
392 #if HAS_USART_0
393 extern struct ao_samd21_usart   ao_samd21_usart0;
394 #endif
395
396 void
397 ao_serial_init(void);
398
399 /* ao_usb_samd21.c */
400
401 #if AO_USB_OUT_HOOK
402 void
403 ao_usb_out_hook(uint8_t *buffer, uint16_t count);
404 #endif
405
406 void start(void);
407
408 #endif /* _AO_ARCH_FUNCS_H_ */