| 273 |
theseven |
1 |
//
|
|
|
2 |
//
|
|
|
3 |
// Copyright 2010 TheSeven
|
|
|
4 |
//
|
|
|
5 |
//
|
|
|
6 |
// This file is part of emBIOS.
|
|
|
7 |
//
|
|
|
8 |
// emBIOS is free software: you can redistribute it and/or
|
|
|
9 |
// modify it under the terms of the GNU General Public License as
|
|
|
10 |
// published by the Free Software Foundation, either version 2 of the
|
|
|
11 |
// License, or (at your option) any later version.
|
|
|
12 |
//
|
|
|
13 |
// emBIOS is distributed in the hope that it will be useful,
|
|
|
14 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
15 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
16 |
// See the GNU General Public License for more details.
|
|
|
17 |
//
|
|
|
18 |
// You should have received a copy of the GNU General Public License along
|
|
|
19 |
// with emBIOS. If not, see <http://www.gnu.org/licenses/>.
|
|
|
20 |
//
|
|
|
21 |
//
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
#include "global.h"
|
|
|
25 |
#include "spi.h"
|
|
|
26 |
#include "s5l8702.h"
|
|
|
27 |
#include "thread.h"
|
|
|
28 |
#include "clockgates.h"
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
static struct mutex spimutex[3];
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
void spi_prepare(int port)
|
|
|
35 |
{
|
|
|
36 |
mutex_lock(&spimutex[port], TIMEOUT_BLOCK);
|
|
|
37 |
clockgate_enable(SPICLKGATE(port), true);
|
|
|
38 |
SPISTATUS(port) = 0xf;
|
|
|
39 |
SPICTRL(port) |= 0xc;
|
|
|
40 |
SPICLKDIV(port) = 4;
|
|
|
41 |
SPIUNKREG1(port) = 6;
|
|
|
42 |
SPISETUP(port) = 0x10618;
|
|
|
43 |
SPICTRL(port) |= 0xc;
|
|
|
44 |
SPICTRL(port) = 1;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
void spi_release(int port)
|
|
|
48 |
{
|
|
|
49 |
clockgate_enable(SPICLKGATE(port), false);
|
|
|
50 |
mutex_unlock(&spimutex[port]);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
uint32_t spi_write(int port, uint32_t data)
|
|
|
54 |
{
|
|
|
55 |
SPIRXLIMIT(port) = 1;
|
|
|
56 |
while ((SPISTATUS(port) & 0x1f0) == 0x100) yield();
|
|
|
57 |
SPITXDATA(port) = data;
|
|
|
58 |
while (!(SPISTATUS(port) & 0x3e00)) yield();
|
|
|
59 |
return SPIRXDATA(port);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
void spi_read(int port, uint32_t size, void* buf)
|
|
|
63 |
{
|
|
|
64 |
uint8_t* buffer = (uint8_t*)buf;
|
|
|
65 |
SPIRXLIMIT(port) = size;
|
|
|
66 |
SPISETUP(port) |= 1;
|
|
|
67 |
while (size--)
|
|
|
68 |
{
|
|
|
69 |
while (!(SPISTATUS(port) & 0x3e00)) yield();
|
|
|
70 |
*buffer++ = SPIRXDATA(port);
|
|
|
71 |
}
|
|
|
72 |
SPISETUP(port) &= ~1;
|
|
|
73 |
}
|
|
|
74 |
|