lm3s1968: generic KFile-compliant SSI bus implementation.
[bertos.git] / bertos / cpu / cortex-m3 / drv / ssi_lm3s.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief LM3S1968 Synchronous Serial Interface (SSI) driver.
34  *
35  * \author Andrea Righi <arighi@develer.com>
36  */
37
38 #include <cfg/compiler.h>
39 #include <cfg/debug.h>
40 #include <string.h> /* memset() */
41 #include "ssi_lm3s.h"
42
43 /* SSI clocking informations (CPSDVSR + SCR) */
44 struct SSIClock
45 {
46         unsigned int cpsdvsr;
47         unsigned int scr;
48 };
49
50 /*
51  * Evaluate the SSI clock prescale (SSICPSR) and SSI serial clock rate (SCR).
52  */
53 INLINE struct SSIClock
54 lm3s_ssiPrescale(unsigned int bitrate)
55 {
56         struct SSIClock ret;
57
58         for (ret.cpsdvsr = 2, ret.scr = CPU_FREQ / bitrate / ret.cpsdvsr - 1;
59                         ret.scr > 255; ret.cpsdvsr += 2);
60         ASSERT(ret.cpsdvsr < 255);
61
62         return ret;
63 }
64
65 /*
66  * Initialize the SSI interface.
67  *
68  * Return 0 in case of success, a negative value otherwise.
69  */
70 int lm3s_ssiOpen(uint32_t addr, uint32_t frame, int mode,
71                         int bitrate, uint32_t data_width)
72 {
73         struct SSIClock ssi_clock;
74
75         ASSERT(addr == SSI0_BASE || addr == SSI1_BASE);
76         /* Configure the SSI operating mode */
77         switch (mode)
78         {
79                 /* SSI Slave Mode Output Disable */
80                 case SSI_MODE_SLAVE_OD:
81                         HWREG(addr + SSI_O_CR1) = SSI_CR1_SOD;
82                         break;
83                 /* SSI Slave */
84                 case SSI_MODE_SLAVE:
85                         HWREG(addr + SSI_O_CR1) = SSI_CR1_MS;
86                         break;
87                 /* SSI Master */
88                 case SSI_MODE_MASTER:
89                         HWREG(addr + SSI_O_CR1) = 0;
90                         break;
91                 default:
92                         ASSERT(0);
93                         return -1;
94         }
95         /* Configure the peripheral clock and frame format */
96         ssi_clock = lm3s_ssiPrescale(bitrate);
97         HWREG(addr + SSI_O_CPSR) = ssi_clock.cpsdvsr;
98         HWREG(addr + SSI_O_CR0) =
99                         (ssi_clock.scr << 8)            |
100                         ((frame & 3) << 6)              |
101                         (frame & SSI_CR0_FRF_M)         |
102                         (data_width - 1);
103         /* Enable the SSI interface */
104         HWREG(addr + SSI_O_CR1) |= SSI_CR1_SSE;
105
106         return 0;
107 }
108
109 /*
110  * Write data to the SSI bus.
111  *
112  * Return the number of bytes written to the bus.
113  */
114 static size_t lm3s_ssiWrite(struct KFile *fd, const void *buf, size_t size)
115 {
116         LM3SSSI *fds = LM3SSSI_CAST(fd);
117         const char *p = (const char *)buf;
118         uint32_t frame;
119         size_t count = 0;
120
121         while (count < size)
122         {
123                 frame = p[count];
124                 if (fds->flags & LM3S_SSI_NONBLOCK)
125                 {
126                         if (!lm3s_ssiWriteFrameNonBlocking(fds->addr,
127                                                                 frame))
128                                 break;
129                 }
130                 else
131                         lm3s_ssiWriteFrame(fds->addr, frame);
132                 count++;
133         }
134         return count;
135 }
136
137 /*
138  * Read data from the SSI bus.
139  *
140  * Return the number of bytes read from the bus.
141  */
142 static size_t lm3s_ssiRead(struct KFile *fd, void *buf, size_t size)
143 {
144         LM3SSSI *fds = LM3SSSI_CAST(fd);
145
146         uint8_t *p = (uint8_t *)buf;
147         uint32_t frame;
148         size_t count = 0;
149
150         while (count < size)
151         {
152                 if (fds->flags & LM3S_SSI_NONBLOCK)
153                 {
154                         if (!lm3s_ssiReadFrameNonBlocking(fds->addr, &frame))
155                                 break;
156                 }
157                 else
158                         lm3s_ssiReadFrame(fds->addr, &frame);
159                 *p++ = (uint8_t)frame;
160                 count++;
161         }
162         return count;
163 }
164
165
166 /* Wait for data in the TX FIFO being actually transmitted */
167 static int lm3s_ssiFlush(struct KFile *fd)
168 {
169         LM3SSSI *fds = LM3SSSI_CAST(fd);
170
171         while (!lm3s_ssiTxDone(fds->addr))
172                 cpu_relax();
173         return 0;
174 }
175
176 /* Disable the SSI interface */
177 static int lm3s_ssiClose(struct KFile *fd)
178 {
179         LM3SSSI *fds = LM3SSSI_CAST(fd);
180
181         lm3s_ssiFlush(fd);
182         HWREG(fds->addr + SSI_O_CR1) &= ~SSI_CR1_SSE;
183         return 0;
184 }
185
186 /**
187  * Initialize a LM3S SSI driver.
188  */
189 void lm3s_ssiInit(struct LM3SSSI *fds, uint32_t addr, uint32_t frame, int mode,
190                         int bitrate, uint32_t data_width)
191 {
192         memset(fds, 0, sizeof(*fds));
193         DB(fds->fd._type = KFT_LM3SSSI);
194
195         /* TODO: only 8-bit frame size is supported */
196         ASSERT(data_width == 8);
197
198         fds->fd.write = lm3s_ssiWrite;
199         fds->fd.read = lm3s_ssiRead;
200         fds->fd.close = lm3s_ssiClose;
201         fds->fd.flush = lm3s_ssiFlush;
202
203         fds->addr = addr;
204         lm3s_ssiOpen(addr, frame, mode, bitrate, data_width);
205 }