2424#include <linux/mtd/partitions.h>
2525
2626#include <linux/spi/spi.h>
27+ #include <linux/spi/spi-mem.h>
2728#include <linux/spi/flash.h>
2829#include <linux/mtd/spi-nor.h>
2930
3031#define MAX_CMD_SIZE 6
3132struct m25p {
32- struct spi_device * spi ;
33+ struct spi_mem * spimem ;
3334 struct spi_nor spi_nor ;
3435 u8 command [MAX_CMD_SIZE ];
3536};
3637
3738static int m25p80_read_reg (struct spi_nor * nor , u8 code , u8 * val , int len )
3839{
3940 struct m25p * flash = nor -> priv ;
40- struct spi_device * spi = flash -> spi ;
41+ struct spi_mem_op op = SPI_MEM_OP (SPI_MEM_OP_CMD (code , 1 ),
42+ SPI_MEM_OP_NO_ADDR ,
43+ SPI_MEM_OP_NO_DUMMY ,
44+ SPI_MEM_OP_DATA_IN (len , val , 1 ));
4145 int ret ;
4246
43- ret = spi_write_then_read ( spi , & code , 1 , val , len );
47+ ret = spi_mem_exec_op ( flash -> spimem , & op );
4448 if (ret < 0 )
45- dev_err (& spi -> dev , "error %d reading %x\n" , ret , code );
49+ dev_err (& flash -> spimem -> spi -> dev , "error %d reading %x\n" , ret ,
50+ code );
4651
4752 return ret ;
4853}
4954
50- static void m25p_addr2cmd (struct spi_nor * nor , unsigned int addr , u8 * cmd )
51- {
52- /* opcode is in cmd[0] */
53- cmd [1 ] = addr >> (nor -> addr_width * 8 - 8 );
54- cmd [2 ] = addr >> (nor -> addr_width * 8 - 16 );
55- cmd [3 ] = addr >> (nor -> addr_width * 8 - 24 );
56- cmd [4 ] = addr >> (nor -> addr_width * 8 - 32 );
57- }
58-
59- static int m25p_cmdsz (struct spi_nor * nor )
60- {
61- return 1 + nor -> addr_width ;
62- }
63-
6455static int m25p80_write_reg (struct spi_nor * nor , u8 opcode , u8 * buf , int len )
6556{
6657 struct m25p * flash = nor -> priv ;
67- struct spi_device * spi = flash -> spi ;
68-
69- flash -> command [0 ] = opcode ;
70- if (buf )
71- memcpy (& flash -> command [1 ], buf , len );
58+ struct spi_mem_op op = SPI_MEM_OP (SPI_MEM_OP_CMD (opcode , 1 ),
59+ SPI_MEM_OP_NO_ADDR ,
60+ SPI_MEM_OP_NO_DUMMY ,
61+ SPI_MEM_OP_DATA_OUT (len , buf , 1 ));
7262
73- return spi_write ( spi , flash -> command , len + 1 );
63+ return spi_mem_exec_op ( flash -> spimem , & op );
7464}
7565
7666static ssize_t m25p80_write (struct spi_nor * nor , loff_t to , size_t len ,
7767 const u_char * buf )
7868{
7969 struct m25p * flash = nor -> priv ;
80- struct spi_device * spi = flash -> spi ;
81- unsigned int inst_nbits , addr_nbits , data_nbits , data_idx ;
82- struct spi_transfer t [3 ] = {};
83- struct spi_message m ;
84- int cmd_sz = m25p_cmdsz (nor );
85- ssize_t ret ;
70+ struct spi_mem_op op =
71+ SPI_MEM_OP (SPI_MEM_OP_CMD (nor -> program_opcode , 1 ),
72+ SPI_MEM_OP_ADDR (nor -> addr_width , to , 1 ),
73+ SPI_MEM_OP_DUMMY (0 , 1 ),
74+ SPI_MEM_OP_DATA_OUT (len , buf , 1 ));
75+ size_t remaining = len ;
76+ int ret ;
8677
8778 /* get transfer protocols. */
88- inst_nbits = spi_nor_get_protocol_inst_nbits (nor -> write_proto );
89- addr_nbits = spi_nor_get_protocol_addr_nbits (nor -> write_proto );
90- data_nbits = spi_nor_get_protocol_data_nbits (nor -> write_proto );
91-
92- spi_message_init (& m );
79+ op .cmd .buswidth = spi_nor_get_protocol_inst_nbits (nor -> write_proto );
80+ op .addr .buswidth = spi_nor_get_protocol_addr_nbits (nor -> write_proto );
81+ op .dummy .buswidth = op .addr .buswidth ;
82+ op .data .buswidth = spi_nor_get_protocol_data_nbits (nor -> write_proto );
9383
9484 if (nor -> program_opcode == SPINOR_OP_AAI_WP && nor -> sst_write_second )
95- cmd_sz = 1 ;
96-
97- flash -> command [0 ] = nor -> program_opcode ;
98- m25p_addr2cmd (nor , to , flash -> command );
85+ op .addr .nbytes = 0 ;
9986
100- t [0 ].tx_buf = flash -> command ;
101- t [0 ].tx_nbits = inst_nbits ;
102- t [0 ].len = cmd_sz ;
103- spi_message_add_tail (& t [0 ], & m );
104-
105- /* split the op code and address bytes into two transfers if needed. */
106- data_idx = 1 ;
107- if (addr_nbits != inst_nbits ) {
108- t [0 ].len = 1 ;
87+ while (remaining ) {
88+ op .data .nbytes = remaining < UINT_MAX ? remaining : UINT_MAX ;
89+ ret = spi_mem_adjust_op_size (flash -> spimem , & op );
90+ if (ret )
91+ return ret ;
10992
110- t [1 ].tx_buf = & flash -> command [1 ];
111- t [1 ].tx_nbits = addr_nbits ;
112- t [1 ].len = cmd_sz - 1 ;
113- spi_message_add_tail (& t [1 ], & m );
93+ ret = spi_mem_exec_op (flash -> spimem , & op );
94+ if (ret )
95+ return ret ;
11496
115- data_idx = 2 ;
97+ op .addr .val += op .data .nbytes ;
98+ remaining -= op .data .nbytes ;
99+ op .data .buf .out += op .data .nbytes ;
116100 }
117101
118- t [data_idx ].tx_buf = buf ;
119- t [data_idx ].tx_nbits = data_nbits ;
120- t [data_idx ].len = len ;
121- spi_message_add_tail (& t [data_idx ], & m );
122-
123- ret = spi_sync (spi , & m );
124- if (ret )
125- return ret ;
126-
127- ret = m .actual_length - cmd_sz ;
128- if (ret < 0 )
129- return - EIO ;
130- return ret ;
102+ return len ;
131103}
132104
133105/*
@@ -138,101 +110,49 @@ static ssize_t m25p80_read(struct spi_nor *nor, loff_t from, size_t len,
138110 u_char * buf )
139111{
140112 struct m25p * flash = nor -> priv ;
141- struct spi_device * spi = flash -> spi ;
142- unsigned int inst_nbits , addr_nbits , data_nbits , data_idx ;
143- struct spi_transfer t [ 3 ];
144- struct spi_message m ;
145- unsigned int dummy = nor -> read_dummy ;
146- ssize_t ret ;
147- int cmd_sz ;
113+ struct spi_mem_op op =
114+ SPI_MEM_OP ( SPI_MEM_OP_CMD ( nor -> read_opcode , 1 ),
115+ SPI_MEM_OP_ADDR ( nor -> addr_width , from , 1 ),
116+ SPI_MEM_OP_DUMMY ( nor -> read_dummy , 1 ),
117+ SPI_MEM_OP_DATA_IN ( len , buf , 1 )) ;
118+ size_t remaining = len ;
119+ int ret ;
148120
149121 /* get transfer protocols. */
150- inst_nbits = spi_nor_get_protocol_inst_nbits (nor -> read_proto );
151- addr_nbits = spi_nor_get_protocol_addr_nbits (nor -> read_proto );
152- data_nbits = spi_nor_get_protocol_data_nbits (nor -> read_proto );
122+ op .cmd .buswidth = spi_nor_get_protocol_inst_nbits (nor -> read_proto );
123+ op .addr .buswidth = spi_nor_get_protocol_addr_nbits (nor -> read_proto );
124+ op .dummy .buswidth = op .addr .buswidth ;
125+ op .data .buswidth = spi_nor_get_protocol_data_nbits (nor -> read_proto );
153126
154127 /* convert the dummy cycles to the number of bytes */
155- dummy = (dummy * addr_nbits ) / 8 ;
156-
157- if (spi_flash_read_supported (spi )) {
158- struct spi_flash_read_message msg ;
159-
160- memset (& msg , 0 , sizeof (msg ));
128+ op .dummy .nbytes = (nor -> read_dummy * op .dummy .buswidth ) / 8 ;
161129
162- msg .buf = buf ;
163- msg .from = from ;
164- msg .len = len ;
165- msg .read_opcode = nor -> read_opcode ;
166- msg .addr_width = nor -> addr_width ;
167- msg .dummy_bytes = dummy ;
168- msg .opcode_nbits = inst_nbits ;
169- msg .addr_nbits = addr_nbits ;
170- msg .data_nbits = data_nbits ;
171-
172- ret = spi_flash_read (spi , & msg );
173- if (ret < 0 )
130+ while (remaining ) {
131+ op .data .nbytes = remaining < UINT_MAX ? remaining : UINT_MAX ;
132+ ret = spi_mem_adjust_op_size (flash -> spimem , & op );
133+ if (ret )
174134 return ret ;
175- return msg .retlen ;
176- }
177135
178- spi_message_init (& m );
179- memset (t , 0 , (sizeof t ));
180-
181- flash -> command [0 ] = nor -> read_opcode ;
182- m25p_addr2cmd (nor , from , flash -> command );
183-
184- t [0 ].tx_buf = flash -> command ;
185- t [0 ].tx_nbits = inst_nbits ;
186- t [0 ].len = m25p_cmdsz (nor ) + dummy ;
187- spi_message_add_tail (& t [0 ], & m );
188-
189- /*
190- * Set all dummy/mode cycle bits to avoid sending some manufacturer
191- * specific pattern, which might make the memory enter its Continuous
192- * Read mode by mistake.
193- * Based on the different mode cycle bit patterns listed and described
194- * in the JESD216B specification, the 0xff value works for all memories
195- * and all manufacturers.
196- */
197- cmd_sz = t [0 ].len ;
198- memset (flash -> command + cmd_sz - dummy , 0xff , dummy );
199-
200- /* split the op code and address bytes into two transfers if needed. */
201- data_idx = 1 ;
202- if (addr_nbits != inst_nbits ) {
203- t [0 ].len = 1 ;
204-
205- t [1 ].tx_buf = & flash -> command [1 ];
206- t [1 ].tx_nbits = addr_nbits ;
207- t [1 ].len = cmd_sz - 1 ;
208- spi_message_add_tail (& t [1 ], & m );
136+ ret = spi_mem_exec_op (flash -> spimem , & op );
137+ if (ret )
138+ return ret ;
209139
210- data_idx = 2 ;
140+ op .addr .val += op .data .nbytes ;
141+ remaining -= op .data .nbytes ;
142+ op .data .buf .in += op .data .nbytes ;
211143 }
212144
213- t [data_idx ].rx_buf = buf ;
214- t [data_idx ].rx_nbits = data_nbits ;
215- t [data_idx ].len = min3 (len , spi_max_transfer_size (spi ),
216- spi_max_message_size (spi ) - cmd_sz );
217- spi_message_add_tail (& t [data_idx ], & m );
218-
219- ret = spi_sync (spi , & m );
220- if (ret )
221- return ret ;
222-
223- ret = m .actual_length - cmd_sz ;
224- if (ret < 0 )
225- return - EIO ;
226- return ret ;
145+ return len ;
227146}
228147
229148/*
230149 * board specific setup should have ensured the SPI clock used here
231150 * matches what the READ command supports, at least until this driver
232151 * understands FAST_READ (for clocks over 25 MHz).
233152 */
234- static int m25p_probe (struct spi_device * spi )
153+ static int m25p_probe (struct spi_mem * spimem )
235154{
155+ struct spi_device * spi = spimem -> spi ;
236156 struct flash_platform_data * data ;
237157 struct m25p * flash ;
238158 struct spi_nor * nor ;
@@ -244,9 +164,9 @@ static int m25p_probe(struct spi_device *spi)
244164 char * flash_name ;
245165 int ret ;
246166
247- data = dev_get_platdata (& spi -> dev );
167+ data = dev_get_platdata (& spimem -> spi -> dev );
248168
249- flash = devm_kzalloc (& spi -> dev , sizeof (* flash ), GFP_KERNEL );
169+ flash = devm_kzalloc (& spimem -> spi -> dev , sizeof (* flash ), GFP_KERNEL );
250170 if (!flash )
251171 return - ENOMEM ;
252172
@@ -258,12 +178,12 @@ static int m25p_probe(struct spi_device *spi)
258178 nor -> write_reg = m25p80_write_reg ;
259179 nor -> read_reg = m25p80_read_reg ;
260180
261- nor -> dev = & spi -> dev ;
181+ nor -> dev = & spimem -> spi -> dev ;
262182 spi_nor_set_flash_node (nor , spi -> dev .of_node );
263183 nor -> priv = flash ;
264184
265- spi_set_drvdata ( spi , flash );
266- flash -> spi = spi ;
185+ spi_mem_set_drvdata ( spimem , flash );
186+ flash -> spimem = spimem ;
267187
268188 if (spi -> mode & SPI_RX_QUAD ) {
269189 hwcaps .mask |= SNOR_HWCAPS_READ_1_1_4 ;
@@ -303,19 +223,19 @@ static int m25p_probe(struct spi_device *spi)
303223}
304224
305225
306- static int m25p_remove (struct spi_device * spi )
226+ static int m25p_remove (struct spi_mem * spimem )
307227{
308- struct m25p * flash = spi_get_drvdata ( spi );
228+ struct m25p * flash = spi_mem_get_drvdata ( spimem );
309229
310230 spi_nor_restore (& flash -> spi_nor );
311231
312232 /* Clean up MTD stuff. */
313233 return mtd_device_unregister (& flash -> spi_nor .mtd );
314234}
315235
316- static void m25p_shutdown (struct spi_device * spi )
236+ static void m25p_shutdown (struct spi_mem * spimem )
317237{
318- struct m25p * flash = spi_get_drvdata ( spi );
238+ struct m25p * flash = spi_mem_get_drvdata ( spimem );
319239
320240 spi_nor_restore (& flash -> spi_nor );
321241}
@@ -386,12 +306,14 @@ static const struct of_device_id m25p_of_table[] = {
386306};
387307MODULE_DEVICE_TABLE (of , m25p_of_table );
388308
389- static struct spi_driver m25p80_driver = {
390- .driver = {
391- .name = "m25p80" ,
392- .of_match_table = m25p_of_table ,
309+ static struct spi_mem_driver m25p80_driver = {
310+ .spidrv = {
311+ .driver = {
312+ .name = "m25p80" ,
313+ .of_match_table = m25p_of_table ,
314+ },
315+ .id_table = m25p_ids ,
393316 },
394- .id_table = m25p_ids ,
395317 .probe = m25p_probe ,
396318 .remove = m25p_remove ,
397319 .shutdown = m25p_shutdown ,
@@ -402,7 +324,7 @@ static struct spi_driver m25p80_driver = {
402324 */
403325};
404326
405- module_spi_driver (m25p80_driver );
327+ module_spi_mem_driver (m25p80_driver );
406328
407329MODULE_LICENSE ("GPL" );
408330MODULE_AUTHOR ("Mike Lavender" );
0 commit comments