blob: c2f52aed80b9bba4bf4485078b01f4236d4448d8 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * mchp23k256.c
3 *
4 * Driver for Microchip 23k256 SPI RAM chips
5 *
6 * Copyright © 2016 Andrew Lunn <andrew@lunn.ch>
7 *
8 * This code is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13#include <linux/device.h>
14#include <linux/module.h>
15#include <linux/mtd/mtd.h>
16#include <linux/mtd/partitions.h>
17#include <linux/mutex.h>
18#include <linux/sched.h>
19#include <linux/sizes.h>
20#include <linux/spi/flash.h>
21#include <linux/spi/spi.h>
22#include <linux/of_device.h>
23
24#define MAX_CMD_SIZE 4
25
26struct mchp23_caps {
27 u8 addr_width;
28 unsigned int size;
29};
30
31struct mchp23k256_flash {
32 struct spi_device *spi;
33 struct mutex lock;
34 struct mtd_info mtd;
35 const struct mchp23_caps *caps;
36};
37
38#define MCHP23K256_CMD_WRITE_STATUS 0x01
39#define MCHP23K256_CMD_WRITE 0x02
40#define MCHP23K256_CMD_READ 0x03
41#define MCHP23K256_MODE_SEQ BIT(6)
42
43#define to_mchp23k256_flash(x) container_of(x, struct mchp23k256_flash, mtd)
44
45static void mchp23k256_addr2cmd(struct mchp23k256_flash *flash,
46 unsigned int addr, u8 *cmd)
47{
48 int i;
49
50 /*
51 * Address is sent in big endian (MSB first) and we skip
52 * the first entry of the cmd array which contains the cmd
53 * opcode.
54 */
55 for (i = flash->caps->addr_width; i > 0; i--, addr >>= 8)
56 cmd[i] = addr;
57}
58
59static int mchp23k256_cmdsz(struct mchp23k256_flash *flash)
60{
61 return 1 + flash->caps->addr_width;
62}
63
64static int mchp23k256_write(struct mtd_info *mtd, loff_t to, size_t len,
65 size_t *retlen, const unsigned char *buf)
66{
67 struct mchp23k256_flash *flash = to_mchp23k256_flash(mtd);
68 struct spi_transfer transfer[2] = {};
69 struct spi_message message;
70 unsigned char command[MAX_CMD_SIZE];
71 int ret, cmd_len;
72
73 spi_message_init(&message);
74
75 cmd_len = mchp23k256_cmdsz(flash);
76
77 command[0] = MCHP23K256_CMD_WRITE;
78 mchp23k256_addr2cmd(flash, to, command);
79
80 transfer[0].tx_buf = command;
81 transfer[0].len = cmd_len;
82 spi_message_add_tail(&transfer[0], &message);
83
84 transfer[1].tx_buf = buf;
85 transfer[1].len = len;
86 spi_message_add_tail(&transfer[1], &message);
87
88 mutex_lock(&flash->lock);
89
90 ret = spi_sync(flash->spi, &message);
91
92 mutex_unlock(&flash->lock);
93
94 if (ret)
95 return ret;
96
97 if (retlen && message.actual_length > cmd_len)
98 *retlen += message.actual_length - cmd_len;
99
100 return 0;
101}
102
103static int mchp23k256_read(struct mtd_info *mtd, loff_t from, size_t len,
104 size_t *retlen, unsigned char *buf)
105{
106 struct mchp23k256_flash *flash = to_mchp23k256_flash(mtd);
107 struct spi_transfer transfer[2] = {};
108 struct spi_message message;
109 unsigned char command[MAX_CMD_SIZE];
110 int ret, cmd_len;
111
112 spi_message_init(&message);
113
114 cmd_len = mchp23k256_cmdsz(flash);
115
116 memset(&transfer, 0, sizeof(transfer));
117 command[0] = MCHP23K256_CMD_READ;
118 mchp23k256_addr2cmd(flash, from, command);
119
120 transfer[0].tx_buf = command;
121 transfer[0].len = cmd_len;
122 spi_message_add_tail(&transfer[0], &message);
123
124 transfer[1].rx_buf = buf;
125 transfer[1].len = len;
126 spi_message_add_tail(&transfer[1], &message);
127
128 mutex_lock(&flash->lock);
129
130 ret = spi_sync(flash->spi, &message);
131
132 mutex_unlock(&flash->lock);
133
134 if (ret)
135 return ret;
136
137 if (retlen && message.actual_length > cmd_len)
138 *retlen += message.actual_length - cmd_len;
139
140 return 0;
141}
142
143/*
144 * Set the device into sequential mode. This allows read/writes to the
145 * entire SRAM in a single operation
146 */
147static int mchp23k256_set_mode(struct spi_device *spi)
148{
149 struct spi_transfer transfer = {};
150 struct spi_message message;
151 unsigned char command[2];
152
153 spi_message_init(&message);
154
155 command[0] = MCHP23K256_CMD_WRITE_STATUS;
156 command[1] = MCHP23K256_MODE_SEQ;
157
158 transfer.tx_buf = command;
159 transfer.len = sizeof(command);
160 spi_message_add_tail(&transfer, &message);
161
162 return spi_sync(spi, &message);
163}
164
165static const struct mchp23_caps mchp23k256_caps = {
166 .size = SZ_32K,
167 .addr_width = 2,
168};
169
170static const struct mchp23_caps mchp23lcv1024_caps = {
171 .size = SZ_128K,
172 .addr_width = 3,
173};
174
175static int mchp23k256_probe(struct spi_device *spi)
176{
177 struct mchp23k256_flash *flash;
178 struct flash_platform_data *data;
179 int err;
180
181 flash = devm_kzalloc(&spi->dev, sizeof(*flash), GFP_KERNEL);
182 if (!flash)
183 return -ENOMEM;
184
185 flash->spi = spi;
186 mutex_init(&flash->lock);
187 spi_set_drvdata(spi, flash);
188
189 err = mchp23k256_set_mode(spi);
190 if (err)
191 return err;
192
193 data = dev_get_platdata(&spi->dev);
194
195 flash->caps = of_device_get_match_data(&spi->dev);
196 if (!flash->caps)
197 flash->caps = &mchp23k256_caps;
198
199 mtd_set_of_node(&flash->mtd, spi->dev.of_node);
200 flash->mtd.dev.parent = &spi->dev;
201 flash->mtd.type = MTD_RAM;
202 flash->mtd.flags = MTD_CAP_RAM;
203 flash->mtd.writesize = 1;
204 flash->mtd.size = flash->caps->size;
205 flash->mtd._read = mchp23k256_read;
206 flash->mtd._write = mchp23k256_write;
207
208 err = mtd_device_register(&flash->mtd, data ? data->parts : NULL,
209 data ? data->nr_parts : 0);
210 if (err)
211 return err;
212
213 return 0;
214}
215
216static int mchp23k256_remove(struct spi_device *spi)
217{
218 struct mchp23k256_flash *flash = spi_get_drvdata(spi);
219
220 return mtd_device_unregister(&flash->mtd);
221}
222
223static const struct of_device_id mchp23k256_of_table[] = {
224 {
225 .compatible = "microchip,mchp23k256",
226 .data = &mchp23k256_caps,
227 },
228 {
229 .compatible = "microchip,mchp23lcv1024",
230 .data = &mchp23lcv1024_caps,
231 },
232 {}
233};
234MODULE_DEVICE_TABLE(of, mchp23k256_of_table);
235
236static struct spi_driver mchp23k256_driver = {
237 .driver = {
238 .name = "mchp23k256",
239 .of_match_table = of_match_ptr(mchp23k256_of_table),
240 },
241 .probe = mchp23k256_probe,
242 .remove = mchp23k256_remove,
243};
244
245module_spi_driver(mchp23k256_driver);
246
247MODULE_DESCRIPTION("MTD SPI driver for MCHP23K256 RAM chips");
248MODULE_AUTHOR("Andrew Lunn <andre@lunn.ch>");
249MODULE_LICENSE("GPL v2");
250MODULE_ALIAS("spi:mchp23k256");