1 /*
2  * Copyright 2009 Free Software Foundation, Inc.
3  * Copyright 2009-2012 Ettus Research LLC
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "spi_flash.h"
20 #include "spi_flash_private.h"
21 //#include <stdlib.h>
22 #include <nonstdio.h>
23 
24 uint32_t
spi_flash_rdsr(void)25 spi_flash_rdsr(void)
26 {
27   return spif_transact(SPI_TXRX, SPI_SS_FLASH, RDSR_CMD << 8, 16, FLAGS) & 0xff;
28 }
29 
30 static void
spi_flash_write_enable(void)31 spi_flash_write_enable(void)
32 {
33 //	spif_transact(SPI_TXONLY, SPI_SS_FLASH, (WRSR_CMD << 8) | 0x00, 16, FLAGS); //disable write protection bits
34   spif_transact(SPI_TXONLY, SPI_SS_FLASH, WREN_CMD, 8, FLAGS);
35 }
36 
37 bool
spi_flash_done_p(void)38 spi_flash_done_p(void)
39 {
40   return (spi_flash_rdsr() & SR_WIP) == 0;
41 }
42 
43 void
spi_flash_wait(void)44 spi_flash_wait(void)
45 {
46   while (!spi_flash_done_p())
47     ;
48 }
49 
50 void
spi_flash_erase_sector_start(uint32_t flash_addr)51 spi_flash_erase_sector_start(uint32_t flash_addr)
52 {
53   //printf("spi_flash_erase_sector_start: addr = 0x%x\n", flash_addr);
54   if(flash_addr > spi_flash_memory_size())
55     return;
56 
57   spi_flash_wait();
58   spi_flash_write_enable();
59   spif_transact(SPI_TXONLY, SPI_SS_FLASH,
60 		(SE_CMD << 24) | (flash_addr & 0x00ffffff),
61 		32, FLAGS);
62 }
63 
64 bool
spi_flash_page_program_start(uint32_t flash_addr,size_t nbytes,const void * buf)65 spi_flash_page_program_start(uint32_t flash_addr, size_t nbytes, const void *buf)
66 {
67   if (nbytes == 0 || nbytes > SPI_FLASH_PAGE_SIZE)
68     return false;
69 
70   //please to not be writing past the end of the device
71   if ((flash_addr + nbytes) > spi_flash_memory_size())
72     return false;
73 
74   uint32_t local_buf[SPI_FLASH_PAGE_SIZE / sizeof(uint32_t)];
75   memset(local_buf, 0xff, sizeof(local_buf));	// init to 0xff (nops when programming)
76   memcpy(local_buf, buf, nbytes);
77 
78   spi_flash_wait();
79   spi_flash_write_enable();
80 
81   /*
82    * We explicitly control the slave select here (/S), so that we can
83    * do the entire write operation as a single transaction from
84    * device's point of view.  (The most our SPI peripheral can transfer
85    * in a single shot is 16 bytes.)
86    */
87   spif_wait();
88 
89   spif_regs->ss = 0;
90   spif_regs->ctrl = FLAGS;	// ASS is now clear and no chip select is enabled.
91 
92   /* write PP_CMD, ADDR2, ADDR1, ADDR0 */
93 
94   spif_regs->txrx0 = (PP_CMD << 24) | (flash_addr & 0x00ffffff);
95   spif_regs->ss = SPI_SS_FLASH;		// assert chip select
96   spif_regs->ctrl = FLAGS | LEN(4 * 8);
97   spif_regs->ctrl = FLAGS | LEN(4 * 8) | SPI_CTRL_GO_BSY;
98   spif_wait();
99 
100   /*  send 256 bytes total, 16 at a time */
101   for (size_t i = 0; i < 16; i++){
102     spif_regs->txrx3 = local_buf[i * 4 + 0];
103     spif_regs->txrx2 = local_buf[i * 4 + 1];
104     spif_regs->txrx1 = local_buf[i * 4 + 2];
105     spif_regs->txrx0 = local_buf[i * 4 + 3];
106 
107     spif_regs->ctrl = FLAGS | LEN(16 * 8);	// xfer 16 bytes
108     spif_regs->ctrl = FLAGS | LEN(16 * 8) | SPI_CTRL_GO_BSY;
109     spif_wait();
110   }
111   spif_regs->ss = 0;		// desassert chip select
112 
113   return true;
114 }
115 
116 void
spi_flash_erase(uint32_t flash_addr,size_t nbytes)117 spi_flash_erase(uint32_t flash_addr, size_t nbytes)
118 {
119   if (nbytes == 0)
120     return;
121 
122   uint32_t first = round_down(flash_addr, spi_flash_sector_size());
123   uint32_t last  = round_down(flash_addr + nbytes - 1, spi_flash_sector_size());
124 
125   for (uint32_t s = first; s <= last; s += spi_flash_sector_size()){
126     spi_flash_erase_sector_start(s);
127   }
128   spi_flash_wait();
129 }
130 
131 bool
spi_flash_program(uint32_t flash_addr,size_t nbytes,const void * buf)132 spi_flash_program(uint32_t flash_addr, size_t nbytes, const void *buf)
133 {
134   //uprintf(UART_DEBUG, "\nspi_flash_program: addr = 0x%x, nbytes = %d\n", flash_addr, nbytes);
135 
136   const unsigned char *p = (const unsigned char *) buf;
137   size_t n;
138 
139   if ((nbytes + flash_addr) > spi_flash_memory_size())
140     return false;
141   if (nbytes == 0)
142     return true;
143 
144   uint32_t r = flash_addr % SPI_FLASH_PAGE_SIZE;
145   if (r){	/* do initial non-aligned page */
146     n = min(SPI_FLASH_PAGE_SIZE - r, nbytes);
147     spi_flash_page_program_start(flash_addr, n, p);
148     flash_addr += n;
149     p += n;
150     nbytes -= n;
151   }
152 
153   while (nbytes > 0){
154     n = min(SPI_FLASH_PAGE_SIZE, nbytes);
155     spi_flash_page_program_start(flash_addr, n, p);
156     flash_addr += n;
157     p += n;
158     nbytes -= n;
159   }
160 
161   spi_flash_wait();
162   return true;
163 }
164 
165 void
spi_flash_async_erase_start(spi_flash_async_state_t * s,uint32_t flash_addr,size_t nbytes)166 spi_flash_async_erase_start(spi_flash_async_state_t *s,
167 			    uint32_t flash_addr, size_t nbytes)
168 {
169 
170   //printf("got command to erase %d bytes at 0x%x\n", nbytes, flash_addr);
171 
172   if ((nbytes == 0) || ((flash_addr + nbytes) > spi_flash_memory_size())){
173     s->first = s->last = s->current = 0;
174     return;
175   }
176 
177   uint32_t first = round_down(flash_addr, spi_flash_sector_size());
178   uint32_t last  = round_down(flash_addr + nbytes - 1, spi_flash_sector_size());
179 
180   s->first = first;
181   s->last = last;
182   s->current = first;
183 
184   spi_flash_erase_sector_start(s->current);
185 }
186 
187 bool
spi_flash_async_erase_poll(spi_flash_async_state_t * s)188 spi_flash_async_erase_poll(spi_flash_async_state_t *s)
189 {
190   if (!spi_flash_done_p())
191     return false;
192 
193   //printf("%d/%d\n", s->current, s->last);
194 
195   // The current sector erase has completed.  See if we're finished or
196   // if there's more to do.
197 
198   if (s->current == s->last)	// we're done!
199     return true;
200 
201   s->current += spi_flash_sector_size();
202   spi_flash_erase_sector_start(s->current);
203   return false;
204 }
205 
206