Debian Codegraph @

drivers/dahdi/wcxb_flash.c

raw
1 /*
2 * wcxb SPI library
3 *
4 * Copyright (C) 2013 Digium, Inc.
5 *
6 * All rights reserved.
7 *
8 */
9
10 /*
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
16 *
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2 as published by the
19 * Free Software Foundation. See the LICENSE file included with
20 * this program for more details.
21 */
22
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/device.h>
26 #include <linux/sched.h>
27
28 #include "wcxb_spi.h"
29 #include "wcxb_flash.h"
30
31 #define FLASH_PAGE_PROGRAM 0x02
32 #define FLASH_READ 0x03
33 #define FLASH_READ_STATUS 0x05
34 #define FLASH_WRITE_ENABLE 0x06
35 #define FLASH_SECTOR_ERASE 0xd8
36
37 static int wcxb_flash_read_status_register(struct wcxb_spi_device *spi,
38 u8 *status)
39 {
40 u8 command[] = {
41 FLASH_READ_STATUS,
42 };
43 struct wcxb_spi_transfer t_cmd = {
44 .tx_buf = command,
45 .len = sizeof(command),
46 };
47 struct wcxb_spi_transfer t_serial = {
48 .rx_buf = status,
49 .len = 1,
50 };
51 struct wcxb_spi_message m;
52 wcxb_spi_message_init(&m);
53 wcxb_spi_message_add_tail(&t_cmd, &m);
54 wcxb_spi_message_add_tail(&t_serial, &m);
55 return wcxb_spi_sync(spi, &m);
56 }
57
58 int wcxb_flash_read(struct wcxb_spi_device *spi, unsigned int address,
59 void *data, size_t len)
60 {
61 u8 command[] = {
62 FLASH_READ,
63 (address & 0xff0000) >> 16,
64 (address & 0xff00) >> 8,
65 (address & 0xff)
66 };
67 struct wcxb_spi_transfer t_cmd = {
68 .tx_buf = command,
69 .len = sizeof(command),
70 };
71 struct wcxb_spi_transfer t_serial = {
72 .rx_buf = data,
73 .len = len,
74 };
75 struct wcxb_spi_message m;
76 wcxb_spi_message_init(&m);
77 wcxb_spi_message_add_tail(&t_cmd, &m);
78 wcxb_spi_message_add_tail(&t_serial, &m);
79 return wcxb_spi_sync(spi, &m);
80 }
81
82 static int wcxb_flash_wait_until_not_busy(struct wcxb_spi_device *spi)
83 {
84 int res;
85 u8 status;
86 unsigned long stop = jiffies + 5*HZ;
87 do {
88 res = wcxb_flash_read_status_register(spi, &status);
89 } while (!res && (status & 0x1) && time_before(jiffies, stop));
90 if (!res)
91 return res;
92 if (time_after_eq(jiffies, stop))
93 return -EIO;
94 return 0;
95 }
96
97 static int wcxb_flash_write_enable(struct wcxb_spi_device *spi)
98 {
99 u8 command = FLASH_WRITE_ENABLE;
100 return wcxb_spi_write(spi, &command, 1);
101 }
102
103 int wcxb_flash_sector_erase(struct wcxb_spi_device *spi,
104 unsigned int address)
105 {
106 int res;
107 u8 command[] = {FLASH_SECTOR_ERASE, (address >> 16)&0xff, 0x00, 0x00};
108 /* Sector must be on 64KB boundary. */
109 if (address & 0xffff)
110 return -EINVAL;
111 /* Start the erase. */
112 res = wcxb_flash_write_enable(spi);
113 if (res)
114 return res;
115 res = wcxb_spi_write(spi, &command, sizeof(command));
116 if (res)
117 return res;
118 return wcxb_flash_wait_until_not_busy(spi);
119 }
120
121 int wcxb_flash_write(struct wcxb_spi_device *spi, unsigned int address,
122 const void *data, size_t len)
123 {
124 int res;
125 const size_t FLASH_PAGE_SIZE = 256;
126 u8 command[] = {
127 FLASH_PAGE_PROGRAM,
128 (address & 0xff0000) >> 16,
129 (address & 0xff00) >> 8,
130 0x00,
131 };
132 struct wcxb_spi_transfer t_cmd = {
133 .tx_buf = command,
134 .len = sizeof(command),
135 };
136 struct wcxb_spi_transfer t_data = {
137 .tx_buf = data,
138 .len = len,
139 };
140 struct wcxb_spi_message m;
141
142 /* We need to write on page size boundaries */
143 WARN_ON(address & 0xff);
144
145 wcxb_spi_message_init(&m);
146 wcxb_spi_message_add_tail(&t_cmd, &m);
147 wcxb_spi_message_add_tail(&t_data, &m);
148
149 while (len) {
150 res = wcxb_flash_write_enable(spi);
151 if (res)
152 return res;
153 command[1] = (address >> 16) & 0xff;
154 command[2] = (address >> 8) & 0xff;
155 t_data.tx_buf = data;
156 t_data.len = min(len, FLASH_PAGE_SIZE);
157 res = wcxb_spi_sync(spi, &m);
158 WARN_ON(res);
159 if (res)
160 return res;
161 res = wcxb_flash_wait_until_not_busy(spi);
162 WARN_ON(res);
163 if (res)
164 return res;
165 len -= t_data.len;
166 address += t_data.len;
167 data += t_data.len;
168 }
169 return 0;
170 }