1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#ifndef SPI_H
23#define SPI_H
24
25#include <config.h>
26#include <common.h>
27#include <asm/errno.h>
28#include <asm/arch/hardware.h>
29
30#define SPIF 0x80
31
32#define spi_lock() disable_interrupts();
33#define spi_unlock() enable_interrupts();
34
35extern unsigned long spi_flags;
36extern unsigned char spi_idle;
37
38int spi_init(void);
39
40static inline unsigned char spi_read(void)
41{
42 unsigned char b;
43
44 PUT8(S0SPDR, spi_idle);
45 while (!(GET8(S0SPSR) & SPIF));
46 b = GET8(S0SPDR);
47
48 return b;
49}
50
51static inline void spi_write(unsigned char b)
52{
53 PUT8(S0SPDR, b);
54 while (!(GET8(S0SPSR) & SPIF));
55 GET8(S0SPDR);
56}
57
58static inline void spi_set_clock(unsigned char clk_value)
59{
60 PUT8(S0SPCCR, clk_value);
61}
62
63static inline void spi_set_cfg(unsigned char phase,
64 unsigned char polarity,
65 unsigned char lsbf)
66{
67 unsigned char v = 0x20;
68
69 if (phase)
70 v |= 0x08;
71 if (polarity) {
72 v |= 0x10;
73 spi_idle = 0xFF;
74 } else {
75 spi_idle = 0x00;
76 }
77 if (lsbf)
78 v |= 0x40;
79
80 PUT8(S0SPCR, v);
81}
82#endif
83