Merge branch 'master' into micropeak-logging
[fw/altos] / src / drivers / ao_m25.c
1 /*
2  * Copyright © 2010 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 /* Total bytes of available storage */
21 __pdata uint32_t        ao_storage_total;
22
23 /* Block size - device is erased in these units. At least 256 bytes */
24 __pdata uint32_t        ao_storage_block;
25
26 /* Byte offset of config block. Will be ao_storage_block bytes long */
27 __pdata uint32_t        ao_storage_config;
28
29 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
30 __pdata uint16_t        ao_storage_unit;
31
32 /*
33  * Each flash chip is arranged in 64kB sectors; the
34  * chip cannot erase in units smaller than that.
35  *
36  * Writing happens in units of 256 byte pages and
37  * can only change bits from 1 to 0. So, you can rewrite
38  * the same contents, or append to an existing page easily enough
39  */
40
41 #define M25_WREN        0x06    /* Write Enable */
42 #define M25_WRDI        0x04    /* Write Disable */
43 #define M25_RDID        0x9f    /* Read Identification */
44 #define M25_RDSR        0x05    /* Read Status Register */
45 #define M25_WRSR        0x01    /* Write Status Register */
46 #define M25_READ        0x03    /* Read Data Bytes */
47 #define M25_FAST_READ   0x0b    /* Read Data Bytes at Higher Speed */
48 #define M25_PP          0x02    /* Page Program */
49 #define M25_SE          0xd8    /* Sector Erase */
50 #define M25_BE          0xc7    /* Bulk Erase */
51 #define M25_DP          0xb9    /* Deep Power-down */
52
53 /* RDID response */
54 #define M25_MANUF_OFFSET        0
55 #define M25_MEMORY_TYPE_OFFSET  1
56 #define M25_CAPACITY_OFFSET     2
57 #define M25_UID_OFFSET          3
58 #define M25_CFI_OFFSET          4
59 #define M25_RDID_LEN            4       /* that's all we need */
60
61 #define M25_CAPACITY_128KB      0x11
62 #define M25_CAPACITY_256KB      0x12
63 #define M25_CAPACITY_512KB      0x13
64 #define M25_CAPACITY_1MB        0x14
65 #define M25_CAPACITY_2MB        0x15
66
67 /*
68  * Status register bits
69  */
70
71 #define M25_STATUS_SRWD         (1 << 7)        /* Status register write disable */
72 #define M25_STATUS_BP_MASK      (7 << 2)        /* Block protect bits */
73 #define M25_STATUS_BP_SHIFT     (2)
74 #define M25_STATUS_WEL          (1 << 1)        /* Write enable latch */
75 #define M25_STATUS_WIP          (1 << 0)        /* Write in progress */
76
77 /*
78  * On teleterra, the m25 chip select pins are
79  * wired on P0_0 through P0_3.
80  */
81
82 #if M25_MAX_CHIPS > 1
83 static uint8_t ao_m25_size[M25_MAX_CHIPS];      /* number of sectors in each chip */
84 static uint8_t ao_m25_pin[M25_MAX_CHIPS];       /* chip select pin for each chip */
85 static uint8_t ao_m25_numchips;                 /* number of chips detected */
86 #endif
87 static uint8_t ao_m25_total;                    /* total sectors available */
88 static uint8_t ao_m25_wip;                      /* write in progress */
89
90 static __xdata uint8_t ao_m25_mutex;
91
92 /*
93  * This little array is abused to send and receive data. A particular
94  * caution -- the read and write addresses are written into the last
95  * three bytes of the array by ao_m25_set_page_address and then the
96  * first byte is used by ao_m25_wait_wip and ao_m25_write_enable, neither
97  * of which touch those last three bytes.
98  */
99
100 static __xdata uint8_t  ao_m25_instruction[4];
101
102 #define M25_SELECT(cs)          ao_spi_get_mask(AO_M25_SPI_CS_PORT,cs,AO_M25_SPI_BUS, AO_SPI_SPEED_FAST)
103 #define M25_DESELECT(cs)        ao_spi_put_mask(AO_M25_SPI_CS_PORT,cs,AO_M25_SPI_BUS)
104
105 #define M25_BLOCK_SHIFT                 16
106 #define M25_BLOCK                       65536L
107 #define M25_POS_TO_SECTOR(pos)          ((uint8_t) ((pos) >> M25_BLOCK_SHIFT))
108 #define M25_SECTOR_TO_POS(sector)       (((uint32_t) (sector)) << M25_BLOCK_SHIFT)
109
110 /*
111  * Block until the specified chip is done writing
112  */
113 static void
114 ao_m25_wait_wip(uint8_t cs)
115 {
116         if (ao_m25_wip & cs) {
117                 M25_SELECT(cs);
118                 ao_m25_instruction[0] = M25_RDSR;
119                 ao_spi_send(ao_m25_instruction, 1, AO_M25_SPI_BUS);
120                 do {
121                         ao_spi_recv(ao_m25_instruction, 1, AO_M25_SPI_BUS);
122                 } while (ao_m25_instruction[0] & M25_STATUS_WIP);
123                 M25_DESELECT(cs);
124                 ao_m25_wip &= ~cs;
125         }
126 }
127
128 /*
129  * Set the write enable latch so that page program and sector
130  * erase commands will work. Also mark the chip as busy writing
131  * so that future operations will block until the WIP bit goes off
132  */
133 static void
134 ao_m25_write_enable(uint8_t cs)
135 {
136         M25_SELECT(cs);
137         ao_m25_instruction[0] = M25_WREN;
138         ao_spi_send(&ao_m25_instruction, 1, AO_M25_SPI_BUS);
139         M25_DESELECT(cs);
140         ao_m25_wip |= cs;
141 }
142
143
144 /*
145  * Returns the number of 64kB sectors
146  */
147 static uint8_t
148 ao_m25_read_capacity(uint8_t cs)
149 {
150         uint8_t capacity;
151         M25_SELECT(cs);
152         ao_m25_instruction[0] = M25_RDID;
153         ao_spi_send(ao_m25_instruction, 1, AO_M25_SPI_BUS);
154         ao_spi_recv(ao_m25_instruction, M25_RDID_LEN, AO_M25_SPI_BUS);
155         M25_DESELECT(cs);
156
157         /* Check to see if the chip is present */
158         if (ao_m25_instruction[0] == 0xff)
159                 return 0;
160         capacity = ao_m25_instruction[M25_CAPACITY_OFFSET];
161
162         /* Sanity check capacity number */
163         if (capacity < 0x11 || 0x1f < capacity)
164                 return 0;
165         return 1 << (capacity - 0x10);
166 }
167
168 static uint8_t
169 ao_m25_set_address(uint32_t pos)
170 {
171         uint8_t chip;
172 #if M25_MAX_CHIPS > 1
173         uint8_t size;
174
175         for (chip = 0; chip < ao_m25_numchips; chip++) {
176                 size = ao_m25_size[chip];
177                 if (M25_POS_TO_SECTOR(pos) < size)
178                         break;
179                 pos -= M25_SECTOR_TO_POS(size);
180         }
181         if (chip == ao_m25_numchips)
182                 return 0xff;
183
184         chip = ao_m25_pin[chip];
185 #else
186         chip = AO_M25_SPI_CS_MASK;
187 #endif
188         ao_m25_wait_wip(chip);
189
190         ao_m25_instruction[1] = pos >> 16;
191         ao_m25_instruction[2] = pos >> 8;
192         ao_m25_instruction[3] = pos;
193         return chip;
194 }
195
196 /*
197  * Scan the possible chip select lines
198  * to see which flash chips are connected
199  */
200 static uint8_t
201 ao_m25_scan(void)
202 {
203 #if M25_MAX_CHIPS > 1
204         uint8_t pin, size;
205 #endif
206
207         if (ao_m25_total)
208                 return 1;
209
210 #if M25_MAX_CHIPS > 1
211         ao_m25_numchips = 0;
212         for (pin = 1; pin != 0; pin <<= 1) {
213                 if (AO_M25_SPI_CS_MASK & pin) {
214                         size = ao_m25_read_capacity(pin);
215                         if (size != 0) {
216                                 ao_m25_size[ao_m25_numchips] = size;
217                                 ao_m25_pin[ao_m25_numchips] = pin;
218                                 ao_m25_total += size;
219                                 ao_m25_numchips++;
220                         }
221                 }
222         }
223 #else
224         ao_m25_total = ao_m25_read_capacity(AO_M25_SPI_CS_MASK);
225 #endif
226         if (!ao_m25_total)
227                 return 0;
228         ao_storage_total = M25_SECTOR_TO_POS(ao_m25_total);
229         ao_storage_block = M25_BLOCK;
230         ao_storage_config = ao_storage_total - M25_BLOCK;
231         ao_storage_unit = 256;
232         return 1;
233 }
234
235 /*
236  * Erase the specified sector
237  */
238 uint8_t
239 ao_storage_erase(uint32_t pos) __reentrant
240 {
241         uint8_t cs;
242
243         if (pos >= ao_storage_total || pos + ao_storage_block > ao_storage_total)
244                 return 0;
245
246         ao_mutex_get(&ao_m25_mutex);
247         ao_m25_scan();
248
249         cs = ao_m25_set_address(pos);
250
251         ao_m25_wait_wip(cs);
252         ao_m25_write_enable(cs);
253
254         ao_m25_instruction[0] = M25_SE;
255         M25_SELECT(cs);
256         ao_spi_send(ao_m25_instruction, 4, AO_M25_SPI_BUS);
257         M25_DESELECT(cs);
258         ao_m25_wip |= cs;
259
260         ao_mutex_put(&ao_m25_mutex);
261         return 1;
262 }
263
264 /*
265  * Write to flash
266  */
267 uint8_t
268 ao_storage_device_write(uint32_t pos, __xdata void *d, uint16_t len) __reentrant
269 {
270         uint8_t cs;
271
272         if (pos >= ao_storage_total || pos + len > ao_storage_total)
273                 return 0;
274
275         ao_mutex_get(&ao_m25_mutex);
276         ao_m25_scan();
277
278         cs = ao_m25_set_address(pos);
279         ao_m25_write_enable(cs);
280
281         ao_m25_instruction[0] = M25_PP;
282         M25_SELECT(cs);
283         ao_spi_send(ao_m25_instruction, 4, AO_M25_SPI_BUS);
284         ao_spi_send(d, len, AO_M25_SPI_BUS);
285         M25_DESELECT(cs);
286
287         ao_mutex_put(&ao_m25_mutex);
288         return 1;
289 }
290
291 /*
292  * Read from flash
293  */
294 uint8_t
295 ao_storage_device_read(uint32_t pos, __xdata void *d, uint16_t len) __reentrant
296 {
297         uint8_t cs;
298
299         if (pos >= ao_storage_total || pos + len > ao_storage_total)
300                 return 0;
301         ao_mutex_get(&ao_m25_mutex);
302         ao_m25_scan();
303
304         cs = ao_m25_set_address(pos);
305
306         /* No need to use the FAST_READ as we're running at only 8MHz */
307         ao_m25_instruction[0] = M25_READ;
308         M25_SELECT(cs);
309         ao_spi_send(ao_m25_instruction, 4, AO_M25_SPI_BUS);
310         ao_spi_recv(d, len, AO_M25_SPI_BUS);
311         M25_DESELECT(cs);
312
313         ao_mutex_put(&ao_m25_mutex);
314         return 1;
315 }
316
317 void
318 ao_storage_flush(void) __reentrant
319 {
320 }
321
322 void
323 ao_storage_setup(void)
324 {
325         ao_mutex_get(&ao_m25_mutex);
326         ao_m25_scan();
327         ao_mutex_put(&ao_m25_mutex);
328 }
329
330 void
331 ao_storage_device_info(void) __reentrant
332 {
333         uint8_t cs;
334 #if M25_MAX_CHIPS > 1
335         uint8_t chip;
336 #endif
337
338         ao_mutex_get(&ao_m25_mutex);
339         ao_m25_scan();
340         ao_mutex_put(&ao_m25_mutex);
341
342 #if M25_MAX_CHIPS > 1
343         printf ("Detected chips %d size %d\n", ao_m25_numchips, ao_m25_total);
344         for (chip = 0; chip < ao_m25_numchips; chip++)
345                 printf ("Flash chip %d select %02x size %d\n",
346                         chip, ao_m25_pin[chip], ao_m25_size[chip]);
347 #else
348         printf ("Detected chips 1 size %d\n", ao_m25_total);
349 #endif
350
351         printf ("Available chips:\n");
352         for (cs = 1; cs != 0; cs <<= 1) {
353                 if ((AO_M25_SPI_CS_MASK & cs) == 0)
354                         continue;
355
356                 ao_mutex_get(&ao_m25_mutex);
357                 M25_SELECT(cs);
358                 ao_m25_instruction[0] = M25_RDID;
359                 ao_spi_send(ao_m25_instruction, 1, AO_M25_SPI_BUS);
360                 ao_spi_recv(ao_m25_instruction, M25_RDID_LEN, AO_M25_SPI_BUS);
361                 M25_DESELECT(cs);
362
363                 printf ("Select %02x manf %02x type %02x cap %02x uid %02x\n",
364                         cs,
365                         ao_m25_instruction[M25_MANUF_OFFSET],
366                         ao_m25_instruction[M25_MEMORY_TYPE_OFFSET],
367                         ao_m25_instruction[M25_CAPACITY_OFFSET],
368                         ao_m25_instruction[M25_UID_OFFSET]);
369                 ao_mutex_put(&ao_m25_mutex);
370         }
371 }
372
373 void
374 ao_storage_device_init(void)
375 {
376         ao_spi_init_cs (AO_M25_SPI_CS_PORT, AO_M25_SPI_CS_MASK);
377 }