1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Microchip 48L640 64 Kb SPI Serial EERAM
5 * Copyright Heiko Schocher <hs@denx.de>
7 * datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/20006055B.pdf
9 * we set continuous mode but reading/writing more bytes than
10 * pagesize seems to bring chip into state where readden values
11 * are wrong ... no idea why.
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/jiffies.h>
17 #include <linux/module.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/partitions.h>
20 #include <linux/mutex.h>
21 #include <linux/sched.h>
22 #include <linux/sizes.h>
23 #include <linux/spi/flash.h>
24 #include <linux/spi/spi.h>
29 unsigned int page_size
;
32 struct mchp48l640_flash
{
33 struct spi_device
*spi
;
36 const struct mchp48_caps
*caps
;
39 #define MCHP48L640_CMD_WREN 0x06
40 #define MCHP48L640_CMD_WRDI 0x04
41 #define MCHP48L640_CMD_WRITE 0x02
42 #define MCHP48L640_CMD_READ 0x03
43 #define MCHP48L640_CMD_WRSR 0x01
44 #define MCHP48L640_CMD_RDSR 0x05
46 #define MCHP48L640_STATUS_RDY 0x01
47 #define MCHP48L640_STATUS_WEL 0x02
48 #define MCHP48L640_STATUS_BP0 0x04
49 #define MCHP48L640_STATUS_BP1 0x08
50 #define MCHP48L640_STATUS_SWM 0x10
51 #define MCHP48L640_STATUS_PRO 0x20
52 #define MCHP48L640_STATUS_ASE 0x40
54 #define MCHP48L640_TIMEOUT 100
56 #define MAX_CMD_SIZE 0x10
58 #define to_mchp48l640_flash(x) container_of(x, struct mchp48l640_flash, mtd)
60 static int mchp48l640_mkcmd(struct mchp48l640_flash
*flash
, u8 cmd
, loff_t addr
, char *buf
)
69 static int mchp48l640_read_status(struct mchp48l640_flash
*flash
, int *status
)
74 cmd
[0] = MCHP48L640_CMD_RDSR
;
76 mutex_lock(&flash
->lock
);
77 ret
= spi_write_then_read(flash
->spi
, &cmd
[0], 1, &cmd
[1], 1);
78 mutex_unlock(&flash
->lock
);
81 dev_dbg(&flash
->spi
->dev
, "read status ret: %d status: %x", ret
, *status
);
86 static int mchp48l640_waitforbit(struct mchp48l640_flash
*flash
, int bit
, bool set
)
89 unsigned long deadline
;
91 deadline
= jiffies
+ msecs_to_jiffies(MCHP48L640_TIMEOUT
);
93 ret
= mchp48l640_read_status(flash
, &status
);
94 dev_dbg(&flash
->spi
->dev
, "read status ret: %d bit: %x %sset status: %x",
95 ret
, bit
, (set
? "" : "not"), status
);
100 if ((status
& bit
) == bit
)
103 if ((status
& bit
) == 0)
107 usleep_range(1000, 2000);
108 } while (!time_after_eq(jiffies
, deadline
));
110 dev_err(&flash
->spi
->dev
, "Timeout waiting for bit %x %s set in status register.",
111 bit
, (set
? "" : "not"));
115 static int mchp48l640_write_prepare(struct mchp48l640_flash
*flash
, bool enable
)
117 unsigned char cmd
[2];
121 cmd
[0] = MCHP48L640_CMD_WREN
;
123 cmd
[0] = MCHP48L640_CMD_WRDI
;
125 mutex_lock(&flash
->lock
);
126 ret
= spi_write(flash
->spi
, cmd
, 1);
127 mutex_unlock(&flash
->lock
);
130 dev_err(&flash
->spi
->dev
, "write %sable failed ret: %d",
131 (enable
? "en" : "dis"), ret
);
133 dev_dbg(&flash
->spi
->dev
, "write %sable success ret: %d",
134 (enable
? "en" : "dis"), ret
);
136 return mchp48l640_waitforbit(flash
, MCHP48L640_STATUS_WEL
, true);
141 static int mchp48l640_set_mode(struct mchp48l640_flash
*flash
)
143 unsigned char cmd
[2];
146 ret
= mchp48l640_write_prepare(flash
, true);
150 cmd
[0] = MCHP48L640_CMD_WRSR
;
151 cmd
[1] = MCHP48L640_STATUS_PRO
;
153 mutex_lock(&flash
->lock
);
154 ret
= spi_write(flash
->spi
, cmd
, 2);
155 mutex_unlock(&flash
->lock
);
157 dev_err(&flash
->spi
->dev
, "Could not set continuous mode ret: %d", ret
);
159 return mchp48l640_waitforbit(flash
, MCHP48L640_STATUS_PRO
, true);
162 static int mchp48l640_wait_rdy(struct mchp48l640_flash
*flash
)
164 return mchp48l640_waitforbit(flash
, MCHP48L640_STATUS_RDY
, false);
167 static int mchp48l640_write_page(struct mtd_info
*mtd
, loff_t to
, size_t len
,
168 size_t *retlen
, const unsigned char *buf
)
170 struct mchp48l640_flash
*flash
= to_mchp48l640_flash(mtd
);
175 cmd
= kmalloc((3 + len
), GFP_KERNEL
| GFP_DMA
);
179 ret
= mchp48l640_wait_rdy(flash
);
183 ret
= mchp48l640_write_prepare(flash
, true);
187 mutex_lock(&flash
->lock
);
188 cmdlen
= mchp48l640_mkcmd(flash
, MCHP48L640_CMD_WRITE
, to
, cmd
);
189 memcpy(&cmd
[cmdlen
], buf
, len
);
190 ret
= spi_write(flash
->spi
, cmd
, cmdlen
+ len
);
191 mutex_unlock(&flash
->lock
);
197 ret
= mchp48l640_waitforbit(flash
, MCHP48L640_STATUS_WEL
, false);
205 dev_err(&flash
->spi
->dev
, "write fail with: %d", ret
);
209 static int mchp48l640_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
210 size_t *retlen
, const unsigned char *buf
)
212 struct mchp48l640_flash
*flash
= to_mchp48l640_flash(mtd
);
217 size_t page_sz
= flash
->caps
->page_size
;
220 * we set PRO bit (page rollover), but writing length > page size
221 * does result in total chaos, so write in 32 byte chunks.
224 ws
= min((len
- wlen
), page_sz
);
225 ret
= mchp48l640_write_page(mtd
, woff
, ws
, retlen
, &buf
[wlen
]);
235 static int mchp48l640_read_page(struct mtd_info
*mtd
, loff_t from
, size_t len
,
236 size_t *retlen
, unsigned char *buf
)
238 struct mchp48l640_flash
*flash
= to_mchp48l640_flash(mtd
);
243 cmd
= kmalloc((3 + len
), GFP_KERNEL
| GFP_DMA
);
247 ret
= mchp48l640_wait_rdy(flash
);
251 mutex_lock(&flash
->lock
);
252 cmdlen
= mchp48l640_mkcmd(flash
, MCHP48L640_CMD_READ
, from
, cmd
);
253 ret
= spi_write_then_read(flash
->spi
, cmd
, cmdlen
, buf
, len
);
254 mutex_unlock(&flash
->lock
);
263 dev_err(&flash
->spi
->dev
, "read fail with: %d", ret
);
267 static int mchp48l640_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
268 size_t *retlen
, unsigned char *buf
)
270 struct mchp48l640_flash
*flash
= to_mchp48l640_flash(mtd
);
275 size_t page_sz
= flash
->caps
->page_size
;
278 * we set PRO bit (page rollover), but if read length > page size
279 * does result in total chaos in result ...
282 ws
= min((len
- wlen
), page_sz
);
283 ret
= mchp48l640_read_page(mtd
, woff
, ws
, retlen
, &buf
[wlen
]);
293 static const struct mchp48_caps mchp48l640_caps
= {
298 static int mchp48l640_probe(struct spi_device
*spi
)
300 struct mchp48l640_flash
*flash
;
301 struct flash_platform_data
*data
;
305 flash
= devm_kzalloc(&spi
->dev
, sizeof(*flash
), GFP_KERNEL
);
310 mutex_init(&flash
->lock
);
311 spi_set_drvdata(spi
, flash
);
313 err
= mchp48l640_read_status(flash
, &status
);
317 err
= mchp48l640_set_mode(flash
);
321 data
= dev_get_platdata(&spi
->dev
);
323 flash
->caps
= of_device_get_match_data(&spi
->dev
);
325 flash
->caps
= &mchp48l640_caps
;
327 mtd_set_of_node(&flash
->mtd
, spi
->dev
.of_node
);
328 flash
->mtd
.dev
.parent
= &spi
->dev
;
329 flash
->mtd
.type
= MTD_RAM
;
330 flash
->mtd
.flags
= MTD_CAP_RAM
;
331 flash
->mtd
.writesize
= flash
->caps
->page_size
;
332 flash
->mtd
.size
= flash
->caps
->size
;
333 flash
->mtd
._read
= mchp48l640_read
;
334 flash
->mtd
._write
= mchp48l640_write
;
336 err
= mtd_device_register(&flash
->mtd
, data
? data
->parts
: NULL
,
337 data
? data
->nr_parts
: 0);
344 static void mchp48l640_remove(struct spi_device
*spi
)
346 struct mchp48l640_flash
*flash
= spi_get_drvdata(spi
);
348 WARN_ON(mtd_device_unregister(&flash
->mtd
));
351 static const struct of_device_id mchp48l640_of_table
[] = {
353 .compatible
= "microchip,48l640",
354 .data
= &mchp48l640_caps
,
358 MODULE_DEVICE_TABLE(of
, mchp48l640_of_table
);
360 static const struct spi_device_id mchp48l640_spi_ids
[] = {
363 .driver_data
= (kernel_ulong_t
)&mchp48l640_caps
,
367 MODULE_DEVICE_TABLE(spi
, mchp48l640_spi_ids
);
369 static struct spi_driver mchp48l640_driver
= {
371 .name
= "mchp48l640",
372 .of_match_table
= mchp48l640_of_table
,
374 .probe
= mchp48l640_probe
,
375 .remove
= mchp48l640_remove
,
376 .id_table
= mchp48l640_spi_ids
,
379 module_spi_driver(mchp48l640_driver
);
381 MODULE_DESCRIPTION("MTD SPI driver for Microchip 48l640 EERAM chips");
382 MODULE_AUTHOR("Heiko Schocher <hs@denx.de>");
383 MODULE_LICENSE("GPL v2");
384 MODULE_ALIAS("spi:mchp48l640");