blob: 9ad1dd0896c02344339a9456993cfee57a5236ce [file] [log] [blame]
Joachim Eastwoodf617b952015-08-13 19:19:40 +02001/*
2 * SPI-NOR driver for NXP SPI Flash Interface (SPIFI)
3 *
4 * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
5 *
6 * Based on Freescale QuadSPI driver:
7 * Copyright (C) 2013 Freescale Semiconductor, Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 */
14
15#include <linux/clk.h>
16#include <linux/err.h>
17#include <linux/io.h>
18#include <linux/iopoll.h>
19#include <linux/module.h>
20#include <linux/mtd/mtd.h>
21#include <linux/mtd/partitions.h>
22#include <linux/mtd/spi-nor.h>
23#include <linux/of.h>
24#include <linux/of_device.h>
25#include <linux/platform_device.h>
26#include <linux/spi/spi.h>
27
28/* NXP SPIFI registers, bits and macros */
29#define SPIFI_CTRL 0x000
30#define SPIFI_CTRL_TIMEOUT(timeout) (timeout)
31#define SPIFI_CTRL_CSHIGH(cshigh) ((cshigh) << 16)
32#define SPIFI_CTRL_MODE3 BIT(23)
33#define SPIFI_CTRL_DUAL BIT(28)
34#define SPIFI_CTRL_FBCLK BIT(30)
35#define SPIFI_CMD 0x004
36#define SPIFI_CMD_DATALEN(dlen) ((dlen) & 0x3fff)
37#define SPIFI_CMD_DOUT BIT(15)
38#define SPIFI_CMD_INTLEN(ilen) ((ilen) << 16)
39#define SPIFI_CMD_FIELDFORM(field) ((field) << 19)
40#define SPIFI_CMD_FIELDFORM_ALL_SERIAL SPIFI_CMD_FIELDFORM(0x0)
41#define SPIFI_CMD_FIELDFORM_QUAD_DUAL_DATA SPIFI_CMD_FIELDFORM(0x1)
42#define SPIFI_CMD_FRAMEFORM(frame) ((frame) << 21)
43#define SPIFI_CMD_FRAMEFORM_OPCODE_ONLY SPIFI_CMD_FRAMEFORM(0x1)
44#define SPIFI_CMD_OPCODE(op) ((op) << 24)
45#define SPIFI_ADDR 0x008
46#define SPIFI_IDATA 0x00c
47#define SPIFI_CLIMIT 0x010
48#define SPIFI_DATA 0x014
49#define SPIFI_MCMD 0x018
50#define SPIFI_STAT 0x01c
51#define SPIFI_STAT_MCINIT BIT(0)
52#define SPIFI_STAT_CMD BIT(1)
53#define SPIFI_STAT_RESET BIT(4)
54
55#define SPI_NOR_MAX_ID_LEN 6
56
57struct nxp_spifi {
58 struct device *dev;
59 struct clk *clk_spifi;
60 struct clk *clk_reg;
61 void __iomem *io_base;
62 void __iomem *flash_base;
63 struct mtd_info mtd;
64 struct spi_nor nor;
65 bool memory_mode;
66 u32 mcmd;
67};
68
69static int nxp_spifi_wait_for_cmd(struct nxp_spifi *spifi)
70{
71 u8 stat;
72 int ret;
73
74 ret = readb_poll_timeout(spifi->io_base + SPIFI_STAT, stat,
75 !(stat & SPIFI_STAT_CMD), 10, 30);
76 if (ret)
77 dev_warn(spifi->dev, "command timed out\n");
78
79 return ret;
80}
81
82static int nxp_spifi_reset(struct nxp_spifi *spifi)
83{
84 u8 stat;
85 int ret;
86
87 writel(SPIFI_STAT_RESET, spifi->io_base + SPIFI_STAT);
88 ret = readb_poll_timeout(spifi->io_base + SPIFI_STAT, stat,
89 !(stat & SPIFI_STAT_RESET), 10, 30);
90 if (ret)
91 dev_warn(spifi->dev, "state reset timed out\n");
92
93 return ret;
94}
95
96static int nxp_spifi_set_memory_mode_off(struct nxp_spifi *spifi)
97{
98 int ret;
99
100 if (!spifi->memory_mode)
101 return 0;
102
103 ret = nxp_spifi_reset(spifi);
104 if (ret)
105 dev_err(spifi->dev, "unable to enter command mode\n");
106 else
107 spifi->memory_mode = false;
108
109 return ret;
110}
111
112static int nxp_spifi_set_memory_mode_on(struct nxp_spifi *spifi)
113{
114 u8 stat;
115 int ret;
116
117 if (spifi->memory_mode)
118 return 0;
119
120 writel(spifi->mcmd, spifi->io_base + SPIFI_MCMD);
121 ret = readb_poll_timeout(spifi->io_base + SPIFI_STAT, stat,
122 stat & SPIFI_STAT_MCINIT, 10, 30);
123 if (ret)
124 dev_err(spifi->dev, "unable to enter memory mode\n");
125 else
126 spifi->memory_mode = true;
127
128 return ret;
129}
130
131static int nxp_spifi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
132{
133 struct nxp_spifi *spifi = nor->priv;
134 u32 cmd;
135 int ret;
136
137 ret = nxp_spifi_set_memory_mode_off(spifi);
138 if (ret)
139 return ret;
140
141 cmd = SPIFI_CMD_DATALEN(len) |
142 SPIFI_CMD_OPCODE(opcode) |
143 SPIFI_CMD_FIELDFORM_ALL_SERIAL |
144 SPIFI_CMD_FRAMEFORM_OPCODE_ONLY;
145 writel(cmd, spifi->io_base + SPIFI_CMD);
146
147 while (len--)
148 *buf++ = readb(spifi->io_base + SPIFI_DATA);
149
150 return nxp_spifi_wait_for_cmd(spifi);
151}
152
153static int nxp_spifi_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf,
154 int len, int write_enable)
155{
156 struct nxp_spifi *spifi = nor->priv;
157 u32 cmd;
158 int ret;
159
160 ret = nxp_spifi_set_memory_mode_off(spifi);
161 if (ret)
162 return ret;
163
164 cmd = SPIFI_CMD_DOUT |
165 SPIFI_CMD_DATALEN(len) |
166 SPIFI_CMD_OPCODE(opcode) |
167 SPIFI_CMD_FIELDFORM_ALL_SERIAL |
168 SPIFI_CMD_FRAMEFORM_OPCODE_ONLY;
169 writel(cmd, spifi->io_base + SPIFI_CMD);
170
171 while (len--)
172 writeb(*buf++, spifi->io_base + SPIFI_DATA);
173
174 return nxp_spifi_wait_for_cmd(spifi);
175}
176
177static int nxp_spifi_read(struct spi_nor *nor, loff_t from, size_t len,
178 size_t *retlen, u_char *buf)
179{
180 struct nxp_spifi *spifi = nor->priv;
181 int ret;
182
183 ret = nxp_spifi_set_memory_mode_on(spifi);
184 if (ret)
185 return ret;
186
187 memcpy_fromio(buf, spifi->flash_base + from, len);
188 *retlen += len;
189
190 return 0;
191}
192
193static void nxp_spifi_write(struct spi_nor *nor, loff_t to, size_t len,
194 size_t *retlen, const u_char *buf)
195{
196 struct nxp_spifi *spifi = nor->priv;
197 u32 cmd;
198 int ret;
199
200 ret = nxp_spifi_set_memory_mode_off(spifi);
201 if (ret)
202 return;
203
204 writel(to, spifi->io_base + SPIFI_ADDR);
205 *retlen += len;
206
207 cmd = SPIFI_CMD_DOUT |
208 SPIFI_CMD_DATALEN(len) |
209 SPIFI_CMD_FIELDFORM_ALL_SERIAL |
210 SPIFI_CMD_OPCODE(nor->program_opcode) |
211 SPIFI_CMD_FRAMEFORM(spifi->nor.addr_width + 1);
212 writel(cmd, spifi->io_base + SPIFI_CMD);
213
214 while (len--)
215 writeb(*buf++, spifi->io_base + SPIFI_DATA);
216
217 nxp_spifi_wait_for_cmd(spifi);
218}
219
220static int nxp_spifi_erase(struct spi_nor *nor, loff_t offs)
221{
222 struct nxp_spifi *spifi = nor->priv;
223 u32 cmd;
224 int ret;
225
226 ret = nxp_spifi_set_memory_mode_off(spifi);
227 if (ret)
228 return ret;
229
230 writel(offs, spifi->io_base + SPIFI_ADDR);
231
232 cmd = SPIFI_CMD_FIELDFORM_ALL_SERIAL |
233 SPIFI_CMD_OPCODE(nor->erase_opcode) |
234 SPIFI_CMD_FRAMEFORM(spifi->nor.addr_width + 1);
235 writel(cmd, spifi->io_base + SPIFI_CMD);
236
237 return nxp_spifi_wait_for_cmd(spifi);
238}
239
240static int nxp_spifi_setup_memory_cmd(struct nxp_spifi *spifi)
241{
242 switch (spifi->nor.flash_read) {
243 case SPI_NOR_NORMAL:
244 case SPI_NOR_FAST:
245 spifi->mcmd = SPIFI_CMD_FIELDFORM_ALL_SERIAL;
246 break;
247 case SPI_NOR_DUAL:
248 case SPI_NOR_QUAD:
249 spifi->mcmd = SPIFI_CMD_FIELDFORM_QUAD_DUAL_DATA;
250 break;
251 default:
252 dev_err(spifi->dev, "unsupported SPI read mode\n");
253 return -EINVAL;
254 }
255
256 /* Memory mode supports address length between 1 and 4 */
257 if (spifi->nor.addr_width < 1 || spifi->nor.addr_width > 4)
258 return -EINVAL;
259
260 spifi->mcmd |= SPIFI_CMD_OPCODE(spifi->nor.read_opcode) |
261 SPIFI_CMD_INTLEN(spifi->nor.read_dummy / 8) |
262 SPIFI_CMD_FRAMEFORM(spifi->nor.addr_width + 1);
263
264 return 0;
265}
266
267static void nxp_spifi_dummy_id_read(struct spi_nor *nor)
268{
269 u8 id[SPI_NOR_MAX_ID_LEN];
270 nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
271}
272
273static int nxp_spifi_setup_flash(struct nxp_spifi *spifi,
274 struct device_node *np)
275{
276 struct mtd_part_parser_data ppdata;
277 enum read_mode flash_read;
278 u32 ctrl, property;
279 u16 mode = 0;
280 int ret;
281
282 if (!of_property_read_u32(np, "spi-rx-bus-width", &property)) {
283 switch (property) {
284 case 1:
285 break;
286 case 2:
287 mode |= SPI_RX_DUAL;
288 break;
289 case 4:
290 mode |= SPI_RX_QUAD;
291 break;
292 default:
293 dev_err(spifi->dev, "unsupported rx-bus-width\n");
294 return -EINVAL;
295 }
296 }
297
298 if (of_find_property(np, "spi-cpha", NULL))
299 mode |= SPI_CPHA;
300
301 if (of_find_property(np, "spi-cpol", NULL))
302 mode |= SPI_CPOL;
303
304 /* Setup control register defaults */
305 ctrl = SPIFI_CTRL_TIMEOUT(1000) |
306 SPIFI_CTRL_CSHIGH(15) |
307 SPIFI_CTRL_FBCLK;
308
309 if (mode & SPI_RX_DUAL) {
310 ctrl |= SPIFI_CTRL_DUAL;
311 flash_read = SPI_NOR_DUAL;
312 } else if (mode & SPI_RX_QUAD) {
313 ctrl &= ~SPIFI_CTRL_DUAL;
314 flash_read = SPI_NOR_QUAD;
315 } else {
316 ctrl |= SPIFI_CTRL_DUAL;
317 flash_read = SPI_NOR_NORMAL;
318 }
319
320 switch (mode & (SPI_CPHA | SPI_CPOL)) {
321 case SPI_MODE_0:
322 ctrl &= ~SPIFI_CTRL_MODE3;
323 break;
324 case SPI_MODE_3:
325 ctrl |= SPIFI_CTRL_MODE3;
326 break;
327 default:
328 dev_err(spifi->dev, "only mode 0 and 3 supported\n");
329 return -EINVAL;
330 }
331
332 writel(ctrl, spifi->io_base + SPIFI_CTRL);
333
334 spifi->mtd.priv = &spifi->nor;
335 spifi->nor.mtd = &spifi->mtd;
336 spifi->nor.dev = spifi->dev;
337 spifi->nor.priv = spifi;
338 spifi->nor.read = nxp_spifi_read;
339 spifi->nor.write = nxp_spifi_write;
340 spifi->nor.erase = nxp_spifi_erase;
341 spifi->nor.read_reg = nxp_spifi_read_reg;
342 spifi->nor.write_reg = nxp_spifi_write_reg;
343
344 /*
345 * The first read on a hard reset isn't reliable so do a
346 * dummy read of the id before calling spi_nor_scan().
347 * The reason for this problem is unknown.
348 *
349 * The official NXP spifilib uses more or less the same
350 * workaround that is applied here by reading the device
351 * id multiple times.
352 */
353 nxp_spifi_dummy_id_read(&spifi->nor);
354
355 ret = spi_nor_scan(&spifi->nor, NULL, flash_read);
356 if (ret) {
357 dev_err(spifi->dev, "device scan failed\n");
358 return ret;
359 }
360
361 ret = nxp_spifi_setup_memory_cmd(spifi);
362 if (ret) {
363 dev_err(spifi->dev, "memory command setup failed\n");
364 return ret;
365 }
366
367 ppdata.of_node = np;
368 ret = mtd_device_parse_register(&spifi->mtd, NULL, &ppdata, NULL, 0);
369 if (ret) {
370 dev_err(spifi->dev, "mtd device parse failed\n");
371 return ret;
372 }
373
374 return 0;
375}
376
377static int nxp_spifi_probe(struct platform_device *pdev)
378{
379 struct device_node *flash_np;
380 struct nxp_spifi *spifi;
381 struct resource *res;
382 int ret;
383
384 spifi = devm_kzalloc(&pdev->dev, sizeof(*spifi), GFP_KERNEL);
385 if (!spifi)
386 return -ENOMEM;
387
388 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spifi");
389 spifi->io_base = devm_ioremap_resource(&pdev->dev, res);
390 if (IS_ERR(spifi->io_base))
391 return PTR_ERR(spifi->io_base);
392
393 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "flash");
394 spifi->flash_base = devm_ioremap_resource(&pdev->dev, res);
395 if (IS_ERR(spifi->flash_base))
396 return PTR_ERR(spifi->flash_base);
397
398 spifi->clk_spifi = devm_clk_get(&pdev->dev, "spifi");
399 if (IS_ERR(spifi->clk_spifi)) {
400 dev_err(&pdev->dev, "spifi clock not found\n");
401 return PTR_ERR(spifi->clk_spifi);
402 }
403
404 spifi->clk_reg = devm_clk_get(&pdev->dev, "reg");
405 if (IS_ERR(spifi->clk_reg)) {
406 dev_err(&pdev->dev, "reg clock not found\n");
407 return PTR_ERR(spifi->clk_reg);
408 }
409
410 ret = clk_prepare_enable(spifi->clk_reg);
411 if (ret) {
412 dev_err(&pdev->dev, "unable to enable reg clock\n");
413 return ret;
414 }
415
416 ret = clk_prepare_enable(spifi->clk_spifi);
417 if (ret) {
418 dev_err(&pdev->dev, "unable to enable spifi clock\n");
419 goto dis_clk_reg;
420 }
421
422 spifi->dev = &pdev->dev;
423 platform_set_drvdata(pdev, spifi);
424
425 /* Initialize and reset device */
426 nxp_spifi_reset(spifi);
427 writel(0, spifi->io_base + SPIFI_IDATA);
428 writel(0, spifi->io_base + SPIFI_MCMD);
429 nxp_spifi_reset(spifi);
430
431 flash_np = of_get_next_available_child(pdev->dev.of_node, NULL);
432 if (!flash_np) {
433 dev_err(&pdev->dev, "no SPI flash device to configure\n");
434 ret = -ENODEV;
435 goto dis_clks;
436 }
437
438 ret = nxp_spifi_setup_flash(spifi, flash_np);
439 if (ret) {
440 dev_err(&pdev->dev, "unable to setup flash chip\n");
441 goto dis_clks;
442 }
443
444 return 0;
445
446dis_clks:
447 clk_disable_unprepare(spifi->clk_spifi);
448dis_clk_reg:
449 clk_disable_unprepare(spifi->clk_reg);
450 return ret;
451}
452
453static int nxp_spifi_remove(struct platform_device *pdev)
454{
455 struct nxp_spifi *spifi = platform_get_drvdata(pdev);
456
457 mtd_device_unregister(&spifi->mtd);
458 clk_disable_unprepare(spifi->clk_spifi);
459 clk_disable_unprepare(spifi->clk_reg);
460
461 return 0;
462}
463
464static const struct of_device_id nxp_spifi_match[] = {
465 {.compatible = "nxp,lpc1773-spifi"},
466 { /* sentinel */ }
467};
468MODULE_DEVICE_TABLE(of, nxp_spifi_match);
469
470static struct platform_driver nxp_spifi_driver = {
471 .probe = nxp_spifi_probe,
472 .remove = nxp_spifi_remove,
473 .driver = {
474 .name = "nxp-spifi",
475 .of_match_table = nxp_spifi_match,
476 },
477};
478module_platform_driver(nxp_spifi_driver);
479
480MODULE_DESCRIPTION("NXP SPI Flash Interface driver");
481MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
482MODULE_LICENSE("GPL v2");