blob: 60b557e00cfbac5c99614c600a3c96c675edc36b [file] [log] [blame]
Graham Moore14062342016-06-04 02:39:34 +02001/*
2 * Driver for Cadence QSPI Controller
3 *
4 * Copyright Altera Corporation (C) 2012-2014. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18#include <linux/clk.h>
19#include <linux/completion.h>
20#include <linux/delay.h>
21#include <linux/err.h>
22#include <linux/errno.h>
23#include <linux/interrupt.h>
24#include <linux/io.h>
25#include <linux/jiffies.h>
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/mtd/mtd.h>
29#include <linux/mtd/partitions.h>
30#include <linux/mtd/spi-nor.h>
31#include <linux/of_device.h>
32#include <linux/of.h>
33#include <linux/platform_device.h>
34#include <linux/sched.h>
35#include <linux/spi/spi.h>
36#include <linux/timer.h>
37
38#define CQSPI_NAME "cadence-qspi"
39#define CQSPI_MAX_CHIPSELECT 16
40
Vignesh R61dc8492017-10-03 10:49:21 +053041/* Quirks */
42#define CQSPI_NEEDS_WR_DELAY BIT(0)
43
Graham Moore14062342016-06-04 02:39:34 +020044struct cqspi_st;
45
46struct cqspi_flash_pdata {
47 struct spi_nor nor;
48 struct cqspi_st *cqspi;
49 u32 clk_rate;
50 u32 read_delay;
51 u32 tshsl_ns;
52 u32 tsd2d_ns;
53 u32 tchsh_ns;
54 u32 tslch_ns;
55 u8 inst_width;
56 u8 addr_width;
57 u8 data_width;
58 u8 cs;
59 bool registered;
60};
61
62struct cqspi_st {
63 struct platform_device *pdev;
64
65 struct clk *clk;
66 unsigned int sclk;
67
68 void __iomem *iobase;
69 void __iomem *ahb_base;
70 struct completion transfer_complete;
71 struct mutex bus_mutex;
72
73 int current_cs;
74 int current_page_size;
75 int current_erase_size;
76 int current_addr_width;
77 unsigned long master_ref_clk_hz;
78 bool is_decoded_cs;
79 u32 fifo_depth;
80 u32 fifo_width;
Vignesh Re2580a42017-10-03 10:49:23 +053081 bool rclk_en;
Graham Moore14062342016-06-04 02:39:34 +020082 u32 trigger_address;
Vignesh R61dc8492017-10-03 10:49:21 +053083 u32 wr_delay;
Graham Moore14062342016-06-04 02:39:34 +020084 struct cqspi_flash_pdata f_pdata[CQSPI_MAX_CHIPSELECT];
85};
86
87/* Operation timeout value */
88#define CQSPI_TIMEOUT_MS 500
89#define CQSPI_READ_TIMEOUT_MS 10
90
91/* Instruction type */
92#define CQSPI_INST_TYPE_SINGLE 0
93#define CQSPI_INST_TYPE_DUAL 1
94#define CQSPI_INST_TYPE_QUAD 2
95
96#define CQSPI_DUMMY_CLKS_PER_BYTE 8
97#define CQSPI_DUMMY_BYTES_MAX 4
98#define CQSPI_DUMMY_CLKS_MAX 31
99
100#define CQSPI_STIG_DATA_LEN_MAX 8
101
102/* Register map */
103#define CQSPI_REG_CONFIG 0x00
104#define CQSPI_REG_CONFIG_ENABLE_MASK BIT(0)
105#define CQSPI_REG_CONFIG_DECODE_MASK BIT(9)
106#define CQSPI_REG_CONFIG_CHIPSELECT_LSB 10
107#define CQSPI_REG_CONFIG_DMA_MASK BIT(15)
108#define CQSPI_REG_CONFIG_BAUD_LSB 19
109#define CQSPI_REG_CONFIG_IDLE_LSB 31
110#define CQSPI_REG_CONFIG_CHIPSELECT_MASK 0xF
111#define CQSPI_REG_CONFIG_BAUD_MASK 0xF
112
113#define CQSPI_REG_RD_INSTR 0x04
114#define CQSPI_REG_RD_INSTR_OPCODE_LSB 0
115#define CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB 8
116#define CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB 12
117#define CQSPI_REG_RD_INSTR_TYPE_DATA_LSB 16
118#define CQSPI_REG_RD_INSTR_MODE_EN_LSB 20
119#define CQSPI_REG_RD_INSTR_DUMMY_LSB 24
120#define CQSPI_REG_RD_INSTR_TYPE_INSTR_MASK 0x3
121#define CQSPI_REG_RD_INSTR_TYPE_ADDR_MASK 0x3
122#define CQSPI_REG_RD_INSTR_TYPE_DATA_MASK 0x3
123#define CQSPI_REG_RD_INSTR_DUMMY_MASK 0x1F
124
125#define CQSPI_REG_WR_INSTR 0x08
126#define CQSPI_REG_WR_INSTR_OPCODE_LSB 0
127#define CQSPI_REG_WR_INSTR_TYPE_ADDR_LSB 12
128#define CQSPI_REG_WR_INSTR_TYPE_DATA_LSB 16
129
130#define CQSPI_REG_DELAY 0x0C
131#define CQSPI_REG_DELAY_TSLCH_LSB 0
132#define CQSPI_REG_DELAY_TCHSH_LSB 8
133#define CQSPI_REG_DELAY_TSD2D_LSB 16
134#define CQSPI_REG_DELAY_TSHSL_LSB 24
135#define CQSPI_REG_DELAY_TSLCH_MASK 0xFF
136#define CQSPI_REG_DELAY_TCHSH_MASK 0xFF
137#define CQSPI_REG_DELAY_TSD2D_MASK 0xFF
138#define CQSPI_REG_DELAY_TSHSL_MASK 0xFF
139
140#define CQSPI_REG_READCAPTURE 0x10
141#define CQSPI_REG_READCAPTURE_BYPASS_LSB 0
142#define CQSPI_REG_READCAPTURE_DELAY_LSB 1
143#define CQSPI_REG_READCAPTURE_DELAY_MASK 0xF
144
145#define CQSPI_REG_SIZE 0x14
146#define CQSPI_REG_SIZE_ADDRESS_LSB 0
147#define CQSPI_REG_SIZE_PAGE_LSB 4
148#define CQSPI_REG_SIZE_BLOCK_LSB 16
149#define CQSPI_REG_SIZE_ADDRESS_MASK 0xF
150#define CQSPI_REG_SIZE_PAGE_MASK 0xFFF
151#define CQSPI_REG_SIZE_BLOCK_MASK 0x3F
152
153#define CQSPI_REG_SRAMPARTITION 0x18
154#define CQSPI_REG_INDIRECTTRIGGER 0x1C
155
156#define CQSPI_REG_DMA 0x20
157#define CQSPI_REG_DMA_SINGLE_LSB 0
158#define CQSPI_REG_DMA_BURST_LSB 8
159#define CQSPI_REG_DMA_SINGLE_MASK 0xFF
160#define CQSPI_REG_DMA_BURST_MASK 0xFF
161
162#define CQSPI_REG_REMAP 0x24
163#define CQSPI_REG_MODE_BIT 0x28
164
165#define CQSPI_REG_SDRAMLEVEL 0x2C
166#define CQSPI_REG_SDRAMLEVEL_RD_LSB 0
167#define CQSPI_REG_SDRAMLEVEL_WR_LSB 16
168#define CQSPI_REG_SDRAMLEVEL_RD_MASK 0xFFFF
169#define CQSPI_REG_SDRAMLEVEL_WR_MASK 0xFFFF
170
171#define CQSPI_REG_IRQSTATUS 0x40
172#define CQSPI_REG_IRQMASK 0x44
173
174#define CQSPI_REG_INDIRECTRD 0x60
175#define CQSPI_REG_INDIRECTRD_START_MASK BIT(0)
176#define CQSPI_REG_INDIRECTRD_CANCEL_MASK BIT(1)
177#define CQSPI_REG_INDIRECTRD_DONE_MASK BIT(5)
178
179#define CQSPI_REG_INDIRECTRDWATERMARK 0x64
180#define CQSPI_REG_INDIRECTRDSTARTADDR 0x68
181#define CQSPI_REG_INDIRECTRDBYTES 0x6C
182
183#define CQSPI_REG_CMDCTRL 0x90
184#define CQSPI_REG_CMDCTRL_EXECUTE_MASK BIT(0)
185#define CQSPI_REG_CMDCTRL_INPROGRESS_MASK BIT(1)
186#define CQSPI_REG_CMDCTRL_WR_BYTES_LSB 12
187#define CQSPI_REG_CMDCTRL_WR_EN_LSB 15
188#define CQSPI_REG_CMDCTRL_ADD_BYTES_LSB 16
189#define CQSPI_REG_CMDCTRL_ADDR_EN_LSB 19
190#define CQSPI_REG_CMDCTRL_RD_BYTES_LSB 20
191#define CQSPI_REG_CMDCTRL_RD_EN_LSB 23
192#define CQSPI_REG_CMDCTRL_OPCODE_LSB 24
193#define CQSPI_REG_CMDCTRL_WR_BYTES_MASK 0x7
194#define CQSPI_REG_CMDCTRL_ADD_BYTES_MASK 0x3
195#define CQSPI_REG_CMDCTRL_RD_BYTES_MASK 0x7
196
197#define CQSPI_REG_INDIRECTWR 0x70
198#define CQSPI_REG_INDIRECTWR_START_MASK BIT(0)
199#define CQSPI_REG_INDIRECTWR_CANCEL_MASK BIT(1)
200#define CQSPI_REG_INDIRECTWR_DONE_MASK BIT(5)
201
202#define CQSPI_REG_INDIRECTWRWATERMARK 0x74
203#define CQSPI_REG_INDIRECTWRSTARTADDR 0x78
204#define CQSPI_REG_INDIRECTWRBYTES 0x7C
205
206#define CQSPI_REG_CMDADDRESS 0x94
207#define CQSPI_REG_CMDREADDATALOWER 0xA0
208#define CQSPI_REG_CMDREADDATAUPPER 0xA4
209#define CQSPI_REG_CMDWRITEDATALOWER 0xA8
210#define CQSPI_REG_CMDWRITEDATAUPPER 0xAC
211
212/* Interrupt status bits */
213#define CQSPI_REG_IRQ_MODE_ERR BIT(0)
214#define CQSPI_REG_IRQ_UNDERFLOW BIT(1)
215#define CQSPI_REG_IRQ_IND_COMP BIT(2)
216#define CQSPI_REG_IRQ_IND_RD_REJECT BIT(3)
217#define CQSPI_REG_IRQ_WR_PROTECTED_ERR BIT(4)
218#define CQSPI_REG_IRQ_ILLEGAL_AHB_ERR BIT(5)
219#define CQSPI_REG_IRQ_WATERMARK BIT(6)
220#define CQSPI_REG_IRQ_IND_SRAM_FULL BIT(12)
221
222#define CQSPI_IRQ_MASK_RD (CQSPI_REG_IRQ_WATERMARK | \
223 CQSPI_REG_IRQ_IND_SRAM_FULL | \
224 CQSPI_REG_IRQ_IND_COMP)
225
226#define CQSPI_IRQ_MASK_WR (CQSPI_REG_IRQ_IND_COMP | \
227 CQSPI_REG_IRQ_WATERMARK | \
228 CQSPI_REG_IRQ_UNDERFLOW)
229
230#define CQSPI_IRQ_STATUS_MASK 0x1FFFF
231
232static int cqspi_wait_for_bit(void __iomem *reg, const u32 mask, bool clear)
233{
234 unsigned long end = jiffies + msecs_to_jiffies(CQSPI_TIMEOUT_MS);
235 u32 val;
236
237 while (1) {
238 val = readl(reg);
239 if (clear)
240 val = ~val;
241 val &= mask;
242
243 if (val == mask)
244 return 0;
245
246 if (time_after(jiffies, end))
247 return -ETIMEDOUT;
248 }
249}
250
251static bool cqspi_is_idle(struct cqspi_st *cqspi)
252{
253 u32 reg = readl(cqspi->iobase + CQSPI_REG_CONFIG);
254
255 return reg & (1 << CQSPI_REG_CONFIG_IDLE_LSB);
256}
257
258static u32 cqspi_get_rd_sram_level(struct cqspi_st *cqspi)
259{
260 u32 reg = readl(cqspi->iobase + CQSPI_REG_SDRAMLEVEL);
261
262 reg >>= CQSPI_REG_SDRAMLEVEL_RD_LSB;
263 return reg & CQSPI_REG_SDRAMLEVEL_RD_MASK;
264}
265
266static irqreturn_t cqspi_irq_handler(int this_irq, void *dev)
267{
268 struct cqspi_st *cqspi = dev;
269 unsigned int irq_status;
270
271 /* Read interrupt status */
272 irq_status = readl(cqspi->iobase + CQSPI_REG_IRQSTATUS);
273
274 /* Clear interrupt */
275 writel(irq_status, cqspi->iobase + CQSPI_REG_IRQSTATUS);
276
277 irq_status &= CQSPI_IRQ_MASK_RD | CQSPI_IRQ_MASK_WR;
278
279 if (irq_status)
280 complete(&cqspi->transfer_complete);
281
282 return IRQ_HANDLED;
283}
284
285static unsigned int cqspi_calc_rdreg(struct spi_nor *nor, const u8 opcode)
286{
287 struct cqspi_flash_pdata *f_pdata = nor->priv;
288 u32 rdreg = 0;
289
290 rdreg |= f_pdata->inst_width << CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB;
291 rdreg |= f_pdata->addr_width << CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB;
292 rdreg |= f_pdata->data_width << CQSPI_REG_RD_INSTR_TYPE_DATA_LSB;
293
294 return rdreg;
295}
296
297static int cqspi_wait_idle(struct cqspi_st *cqspi)
298{
299 const unsigned int poll_idle_retry = 3;
300 unsigned int count = 0;
301 unsigned long timeout;
302
303 timeout = jiffies + msecs_to_jiffies(CQSPI_TIMEOUT_MS);
304 while (1) {
305 /*
306 * Read few times in succession to ensure the controller
307 * is indeed idle, that is, the bit does not transition
308 * low again.
309 */
310 if (cqspi_is_idle(cqspi))
311 count++;
312 else
313 count = 0;
314
315 if (count >= poll_idle_retry)
316 return 0;
317
318 if (time_after(jiffies, timeout)) {
319 /* Timeout, in busy mode. */
320 dev_err(&cqspi->pdev->dev,
321 "QSPI is still busy after %dms timeout.\n",
322 CQSPI_TIMEOUT_MS);
323 return -ETIMEDOUT;
324 }
325
326 cpu_relax();
327 }
328}
329
330static int cqspi_exec_flash_cmd(struct cqspi_st *cqspi, unsigned int reg)
331{
332 void __iomem *reg_base = cqspi->iobase;
333 int ret;
334
335 /* Write the CMDCTRL without start execution. */
336 writel(reg, reg_base + CQSPI_REG_CMDCTRL);
337 /* Start execute */
338 reg |= CQSPI_REG_CMDCTRL_EXECUTE_MASK;
339 writel(reg, reg_base + CQSPI_REG_CMDCTRL);
340
341 /* Polling for completion. */
342 ret = cqspi_wait_for_bit(reg_base + CQSPI_REG_CMDCTRL,
343 CQSPI_REG_CMDCTRL_INPROGRESS_MASK, 1);
344 if (ret) {
345 dev_err(&cqspi->pdev->dev,
346 "Flash command execution timed out.\n");
347 return ret;
348 }
349
350 /* Polling QSPI idle status. */
351 return cqspi_wait_idle(cqspi);
352}
353
354static int cqspi_command_read(struct spi_nor *nor,
355 const u8 *txbuf, const unsigned n_tx,
356 u8 *rxbuf, const unsigned n_rx)
357{
358 struct cqspi_flash_pdata *f_pdata = nor->priv;
359 struct cqspi_st *cqspi = f_pdata->cqspi;
360 void __iomem *reg_base = cqspi->iobase;
361 unsigned int rdreg;
362 unsigned int reg;
363 unsigned int read_len;
364 int status;
365
366 if (!n_rx || n_rx > CQSPI_STIG_DATA_LEN_MAX || !rxbuf) {
367 dev_err(nor->dev, "Invalid input argument, len %d rxbuf 0x%p\n",
368 n_rx, rxbuf);
369 return -EINVAL;
370 }
371
372 reg = txbuf[0] << CQSPI_REG_CMDCTRL_OPCODE_LSB;
373
374 rdreg = cqspi_calc_rdreg(nor, txbuf[0]);
375 writel(rdreg, reg_base + CQSPI_REG_RD_INSTR);
376
377 reg |= (0x1 << CQSPI_REG_CMDCTRL_RD_EN_LSB);
378
379 /* 0 means 1 byte. */
380 reg |= (((n_rx - 1) & CQSPI_REG_CMDCTRL_RD_BYTES_MASK)
381 << CQSPI_REG_CMDCTRL_RD_BYTES_LSB);
382 status = cqspi_exec_flash_cmd(cqspi, reg);
383 if (status)
384 return status;
385
386 reg = readl(reg_base + CQSPI_REG_CMDREADDATALOWER);
387
388 /* Put the read value into rx_buf */
389 read_len = (n_rx > 4) ? 4 : n_rx;
390 memcpy(rxbuf, &reg, read_len);
391 rxbuf += read_len;
392
393 if (n_rx > 4) {
394 reg = readl(reg_base + CQSPI_REG_CMDREADDATAUPPER);
395
396 read_len = n_rx - read_len;
397 memcpy(rxbuf, &reg, read_len);
398 }
399
400 return 0;
401}
402
403static int cqspi_command_write(struct spi_nor *nor, const u8 opcode,
404 const u8 *txbuf, const unsigned n_tx)
405{
406 struct cqspi_flash_pdata *f_pdata = nor->priv;
407 struct cqspi_st *cqspi = f_pdata->cqspi;
408 void __iomem *reg_base = cqspi->iobase;
409 unsigned int reg;
410 unsigned int data;
411 int ret;
412
413 if (n_tx > 4 || (n_tx && !txbuf)) {
414 dev_err(nor->dev,
415 "Invalid input argument, cmdlen %d txbuf 0x%p\n",
416 n_tx, txbuf);
417 return -EINVAL;
418 }
419
420 reg = opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB;
421 if (n_tx) {
422 reg |= (0x1 << CQSPI_REG_CMDCTRL_WR_EN_LSB);
423 reg |= ((n_tx - 1) & CQSPI_REG_CMDCTRL_WR_BYTES_MASK)
424 << CQSPI_REG_CMDCTRL_WR_BYTES_LSB;
425 data = 0;
426 memcpy(&data, txbuf, n_tx);
427 writel(data, reg_base + CQSPI_REG_CMDWRITEDATALOWER);
428 }
429
430 ret = cqspi_exec_flash_cmd(cqspi, reg);
431 return ret;
432}
433
434static int cqspi_command_write_addr(struct spi_nor *nor,
435 const u8 opcode, const unsigned int addr)
436{
437 struct cqspi_flash_pdata *f_pdata = nor->priv;
438 struct cqspi_st *cqspi = f_pdata->cqspi;
439 void __iomem *reg_base = cqspi->iobase;
440 unsigned int reg;
441
442 reg = opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB;
443 reg |= (0x1 << CQSPI_REG_CMDCTRL_ADDR_EN_LSB);
444 reg |= ((nor->addr_width - 1) & CQSPI_REG_CMDCTRL_ADD_BYTES_MASK)
445 << CQSPI_REG_CMDCTRL_ADD_BYTES_LSB;
446
447 writel(addr, reg_base + CQSPI_REG_CMDADDRESS);
448
449 return cqspi_exec_flash_cmd(cqspi, reg);
450}
451
452static int cqspi_indirect_read_setup(struct spi_nor *nor,
453 const unsigned int from_addr)
454{
455 struct cqspi_flash_pdata *f_pdata = nor->priv;
456 struct cqspi_st *cqspi = f_pdata->cqspi;
457 void __iomem *reg_base = cqspi->iobase;
458 unsigned int dummy_clk = 0;
459 unsigned int reg;
460
461 writel(from_addr, reg_base + CQSPI_REG_INDIRECTRDSTARTADDR);
462
463 reg = nor->read_opcode << CQSPI_REG_RD_INSTR_OPCODE_LSB;
464 reg |= cqspi_calc_rdreg(nor, nor->read_opcode);
465
466 /* Setup dummy clock cycles */
467 dummy_clk = nor->read_dummy;
468 if (dummy_clk > CQSPI_DUMMY_CLKS_MAX)
469 dummy_clk = CQSPI_DUMMY_CLKS_MAX;
470
471 if (dummy_clk / 8) {
472 reg |= (1 << CQSPI_REG_RD_INSTR_MODE_EN_LSB);
473 /* Set mode bits high to ensure chip doesn't enter XIP */
474 writel(0xFF, reg_base + CQSPI_REG_MODE_BIT);
475
476 /* Need to subtract the mode byte (8 clocks). */
477 if (f_pdata->inst_width != CQSPI_INST_TYPE_QUAD)
478 dummy_clk -= 8;
479
480 if (dummy_clk)
481 reg |= (dummy_clk & CQSPI_REG_RD_INSTR_DUMMY_MASK)
482 << CQSPI_REG_RD_INSTR_DUMMY_LSB;
483 }
484
485 writel(reg, reg_base + CQSPI_REG_RD_INSTR);
486
487 /* Set address width */
488 reg = readl(reg_base + CQSPI_REG_SIZE);
489 reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
490 reg |= (nor->addr_width - 1);
491 writel(reg, reg_base + CQSPI_REG_SIZE);
492 return 0;
493}
494
495static int cqspi_indirect_read_execute(struct spi_nor *nor,
496 u8 *rxbuf, const unsigned n_rx)
497{
498 struct cqspi_flash_pdata *f_pdata = nor->priv;
499 struct cqspi_st *cqspi = f_pdata->cqspi;
500 void __iomem *reg_base = cqspi->iobase;
501 void __iomem *ahb_base = cqspi->ahb_base;
502 unsigned int remaining = n_rx;
503 unsigned int bytes_to_read = 0;
504 int ret = 0;
505
506 writel(remaining, reg_base + CQSPI_REG_INDIRECTRDBYTES);
507
508 /* Clear all interrupts. */
509 writel(CQSPI_IRQ_STATUS_MASK, reg_base + CQSPI_REG_IRQSTATUS);
510
511 writel(CQSPI_IRQ_MASK_RD, reg_base + CQSPI_REG_IRQMASK);
512
513 reinit_completion(&cqspi->transfer_complete);
514 writel(CQSPI_REG_INDIRECTRD_START_MASK,
515 reg_base + CQSPI_REG_INDIRECTRD);
516
517 while (remaining > 0) {
518 ret = wait_for_completion_timeout(&cqspi->transfer_complete,
519 msecs_to_jiffies
520 (CQSPI_READ_TIMEOUT_MS));
521
522 bytes_to_read = cqspi_get_rd_sram_level(cqspi);
523
524 if (!ret && bytes_to_read == 0) {
525 dev_err(nor->dev, "Indirect read timeout, no bytes\n");
526 ret = -ETIMEDOUT;
527 goto failrd;
528 }
529
530 while (bytes_to_read != 0) {
531 bytes_to_read *= cqspi->fifo_width;
532 bytes_to_read = bytes_to_read > remaining ?
533 remaining : bytes_to_read;
Marek Vasut0cf17252016-08-02 15:10:47 +0200534 ioread32_rep(ahb_base, rxbuf,
535 DIV_ROUND_UP(bytes_to_read, 4));
Graham Moore14062342016-06-04 02:39:34 +0200536 rxbuf += bytes_to_read;
537 remaining -= bytes_to_read;
538 bytes_to_read = cqspi_get_rd_sram_level(cqspi);
539 }
540
541 if (remaining > 0)
542 reinit_completion(&cqspi->transfer_complete);
543 }
544
545 /* Check indirect done status */
546 ret = cqspi_wait_for_bit(reg_base + CQSPI_REG_INDIRECTRD,
547 CQSPI_REG_INDIRECTRD_DONE_MASK, 0);
548 if (ret) {
549 dev_err(nor->dev,
550 "Indirect read completion error (%i)\n", ret);
551 goto failrd;
552 }
553
554 /* Disable interrupt */
555 writel(0, reg_base + CQSPI_REG_IRQMASK);
556
557 /* Clear indirect completion status */
558 writel(CQSPI_REG_INDIRECTRD_DONE_MASK, reg_base + CQSPI_REG_INDIRECTRD);
559
560 return 0;
561
562failrd:
563 /* Disable interrupt */
564 writel(0, reg_base + CQSPI_REG_IRQMASK);
565
566 /* Cancel the indirect read */
567 writel(CQSPI_REG_INDIRECTWR_CANCEL_MASK,
568 reg_base + CQSPI_REG_INDIRECTRD);
569 return ret;
570}
571
572static int cqspi_indirect_write_setup(struct spi_nor *nor,
573 const unsigned int to_addr)
574{
575 unsigned int reg;
576 struct cqspi_flash_pdata *f_pdata = nor->priv;
577 struct cqspi_st *cqspi = f_pdata->cqspi;
578 void __iomem *reg_base = cqspi->iobase;
579
580 /* Set opcode. */
581 reg = nor->program_opcode << CQSPI_REG_WR_INSTR_OPCODE_LSB;
582 writel(reg, reg_base + CQSPI_REG_WR_INSTR);
583 reg = cqspi_calc_rdreg(nor, nor->program_opcode);
584 writel(reg, reg_base + CQSPI_REG_RD_INSTR);
585
586 writel(to_addr, reg_base + CQSPI_REG_INDIRECTWRSTARTADDR);
587
588 reg = readl(reg_base + CQSPI_REG_SIZE);
589 reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
590 reg |= (nor->addr_width - 1);
591 writel(reg, reg_base + CQSPI_REG_SIZE);
592 return 0;
593}
594
595static int cqspi_indirect_write_execute(struct spi_nor *nor,
596 const u8 *txbuf, const unsigned n_tx)
597{
598 const unsigned int page_size = nor->page_size;
599 struct cqspi_flash_pdata *f_pdata = nor->priv;
600 struct cqspi_st *cqspi = f_pdata->cqspi;
601 void __iomem *reg_base = cqspi->iobase;
602 unsigned int remaining = n_tx;
603 unsigned int write_bytes;
604 int ret;
605
606 writel(remaining, reg_base + CQSPI_REG_INDIRECTWRBYTES);
607
608 /* Clear all interrupts. */
609 writel(CQSPI_IRQ_STATUS_MASK, reg_base + CQSPI_REG_IRQSTATUS);
610
611 writel(CQSPI_IRQ_MASK_WR, reg_base + CQSPI_REG_IRQMASK);
612
613 reinit_completion(&cqspi->transfer_complete);
614 writel(CQSPI_REG_INDIRECTWR_START_MASK,
615 reg_base + CQSPI_REG_INDIRECTWR);
Vignesh R61dc8492017-10-03 10:49:21 +0530616 /*
617 * As per 66AK2G02 TRM SPRUHY8F section 11.15.5.3 Indirect Access
618 * Controller programming sequence, couple of cycles of
619 * QSPI_REF_CLK delay is required for the above bit to
620 * be internally synchronized by the QSPI module. Provide 5
621 * cycles of delay.
622 */
623 if (cqspi->wr_delay)
624 ndelay(cqspi->wr_delay);
Graham Moore14062342016-06-04 02:39:34 +0200625
626 while (remaining > 0) {
627 write_bytes = remaining > page_size ? page_size : remaining;
Marek Vasut0cf17252016-08-02 15:10:47 +0200628 iowrite32_rep(cqspi->ahb_base, txbuf,
629 DIV_ROUND_UP(write_bytes, 4));
Graham Moore14062342016-06-04 02:39:34 +0200630
631 ret = wait_for_completion_timeout(&cqspi->transfer_complete,
632 msecs_to_jiffies
633 (CQSPI_TIMEOUT_MS));
634 if (!ret) {
635 dev_err(nor->dev, "Indirect write timeout\n");
636 ret = -ETIMEDOUT;
637 goto failwr;
638 }
639
640 txbuf += write_bytes;
641 remaining -= write_bytes;
642
643 if (remaining > 0)
644 reinit_completion(&cqspi->transfer_complete);
645 }
646
647 /* Check indirect done status */
648 ret = cqspi_wait_for_bit(reg_base + CQSPI_REG_INDIRECTWR,
649 CQSPI_REG_INDIRECTWR_DONE_MASK, 0);
650 if (ret) {
651 dev_err(nor->dev,
652 "Indirect write completion error (%i)\n", ret);
653 goto failwr;
654 }
655
656 /* Disable interrupt. */
657 writel(0, reg_base + CQSPI_REG_IRQMASK);
658
659 /* Clear indirect completion status */
660 writel(CQSPI_REG_INDIRECTWR_DONE_MASK, reg_base + CQSPI_REG_INDIRECTWR);
661
662 cqspi_wait_idle(cqspi);
663
664 return 0;
665
666failwr:
667 /* Disable interrupt. */
668 writel(0, reg_base + CQSPI_REG_IRQMASK);
669
670 /* Cancel the indirect write */
671 writel(CQSPI_REG_INDIRECTWR_CANCEL_MASK,
672 reg_base + CQSPI_REG_INDIRECTWR);
673 return ret;
674}
675
676static void cqspi_chipselect(struct spi_nor *nor)
677{
678 struct cqspi_flash_pdata *f_pdata = nor->priv;
679 struct cqspi_st *cqspi = f_pdata->cqspi;
680 void __iomem *reg_base = cqspi->iobase;
681 unsigned int chip_select = f_pdata->cs;
682 unsigned int reg;
683
684 reg = readl(reg_base + CQSPI_REG_CONFIG);
685 if (cqspi->is_decoded_cs) {
686 reg |= CQSPI_REG_CONFIG_DECODE_MASK;
687 } else {
688 reg &= ~CQSPI_REG_CONFIG_DECODE_MASK;
689
690 /* Convert CS if without decoder.
691 * CS0 to 4b'1110
692 * CS1 to 4b'1101
693 * CS2 to 4b'1011
694 * CS3 to 4b'0111
695 */
696 chip_select = 0xF & ~(1 << chip_select);
697 }
698
699 reg &= ~(CQSPI_REG_CONFIG_CHIPSELECT_MASK
700 << CQSPI_REG_CONFIG_CHIPSELECT_LSB);
701 reg |= (chip_select & CQSPI_REG_CONFIG_CHIPSELECT_MASK)
702 << CQSPI_REG_CONFIG_CHIPSELECT_LSB;
703 writel(reg, reg_base + CQSPI_REG_CONFIG);
704}
705
706static void cqspi_configure_cs_and_sizes(struct spi_nor *nor)
707{
708 struct cqspi_flash_pdata *f_pdata = nor->priv;
709 struct cqspi_st *cqspi = f_pdata->cqspi;
710 void __iomem *iobase = cqspi->iobase;
711 unsigned int reg;
712
713 /* configure page size and block size. */
714 reg = readl(iobase + CQSPI_REG_SIZE);
715 reg &= ~(CQSPI_REG_SIZE_PAGE_MASK << CQSPI_REG_SIZE_PAGE_LSB);
716 reg &= ~(CQSPI_REG_SIZE_BLOCK_MASK << CQSPI_REG_SIZE_BLOCK_LSB);
717 reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
718 reg |= (nor->page_size << CQSPI_REG_SIZE_PAGE_LSB);
719 reg |= (ilog2(nor->mtd.erasesize) << CQSPI_REG_SIZE_BLOCK_LSB);
720 reg |= (nor->addr_width - 1);
721 writel(reg, iobase + CQSPI_REG_SIZE);
722
723 /* configure the chip select */
724 cqspi_chipselect(nor);
725
726 /* Store the new configuration of the controller */
727 cqspi->current_page_size = nor->page_size;
728 cqspi->current_erase_size = nor->mtd.erasesize;
729 cqspi->current_addr_width = nor->addr_width;
730}
731
732static unsigned int calculate_ticks_for_ns(const unsigned int ref_clk_hz,
733 const unsigned int ns_val)
734{
735 unsigned int ticks;
736
737 ticks = ref_clk_hz / 1000; /* kHz */
738 ticks = DIV_ROUND_UP(ticks * ns_val, 1000000);
739
740 return ticks;
741}
742
743static void cqspi_delay(struct spi_nor *nor)
744{
745 struct cqspi_flash_pdata *f_pdata = nor->priv;
746 struct cqspi_st *cqspi = f_pdata->cqspi;
747 void __iomem *iobase = cqspi->iobase;
748 const unsigned int ref_clk_hz = cqspi->master_ref_clk_hz;
749 unsigned int tshsl, tchsh, tslch, tsd2d;
750 unsigned int reg;
751 unsigned int tsclk;
752
753 /* calculate the number of ref ticks for one sclk tick */
754 tsclk = DIV_ROUND_UP(ref_clk_hz, cqspi->sclk);
755
756 tshsl = calculate_ticks_for_ns(ref_clk_hz, f_pdata->tshsl_ns);
757 /* this particular value must be at least one sclk */
758 if (tshsl < tsclk)
759 tshsl = tsclk;
760
761 tchsh = calculate_ticks_for_ns(ref_clk_hz, f_pdata->tchsh_ns);
762 tslch = calculate_ticks_for_ns(ref_clk_hz, f_pdata->tslch_ns);
763 tsd2d = calculate_ticks_for_ns(ref_clk_hz, f_pdata->tsd2d_ns);
764
765 reg = (tshsl & CQSPI_REG_DELAY_TSHSL_MASK)
766 << CQSPI_REG_DELAY_TSHSL_LSB;
767 reg |= (tchsh & CQSPI_REG_DELAY_TCHSH_MASK)
768 << CQSPI_REG_DELAY_TCHSH_LSB;
769 reg |= (tslch & CQSPI_REG_DELAY_TSLCH_MASK)
770 << CQSPI_REG_DELAY_TSLCH_LSB;
771 reg |= (tsd2d & CQSPI_REG_DELAY_TSD2D_MASK)
772 << CQSPI_REG_DELAY_TSD2D_LSB;
773 writel(reg, iobase + CQSPI_REG_DELAY);
774}
775
776static void cqspi_config_baudrate_div(struct cqspi_st *cqspi)
777{
778 const unsigned int ref_clk_hz = cqspi->master_ref_clk_hz;
779 void __iomem *reg_base = cqspi->iobase;
780 u32 reg, div;
781
782 /* Recalculate the baudrate divisor based on QSPI specification. */
783 div = DIV_ROUND_UP(ref_clk_hz, 2 * cqspi->sclk) - 1;
784
785 reg = readl(reg_base + CQSPI_REG_CONFIG);
786 reg &= ~(CQSPI_REG_CONFIG_BAUD_MASK << CQSPI_REG_CONFIG_BAUD_LSB);
787 reg |= (div & CQSPI_REG_CONFIG_BAUD_MASK) << CQSPI_REG_CONFIG_BAUD_LSB;
788 writel(reg, reg_base + CQSPI_REG_CONFIG);
789}
790
791static void cqspi_readdata_capture(struct cqspi_st *cqspi,
Vignesh Re2580a42017-10-03 10:49:23 +0530792 const bool bypass,
Graham Moore14062342016-06-04 02:39:34 +0200793 const unsigned int delay)
794{
795 void __iomem *reg_base = cqspi->iobase;
796 unsigned int reg;
797
798 reg = readl(reg_base + CQSPI_REG_READCAPTURE);
799
800 if (bypass)
801 reg |= (1 << CQSPI_REG_READCAPTURE_BYPASS_LSB);
802 else
803 reg &= ~(1 << CQSPI_REG_READCAPTURE_BYPASS_LSB);
804
805 reg &= ~(CQSPI_REG_READCAPTURE_DELAY_MASK
806 << CQSPI_REG_READCAPTURE_DELAY_LSB);
807
808 reg |= (delay & CQSPI_REG_READCAPTURE_DELAY_MASK)
809 << CQSPI_REG_READCAPTURE_DELAY_LSB;
810
811 writel(reg, reg_base + CQSPI_REG_READCAPTURE);
812}
813
814static void cqspi_controller_enable(struct cqspi_st *cqspi, bool enable)
815{
816 void __iomem *reg_base = cqspi->iobase;
817 unsigned int reg;
818
819 reg = readl(reg_base + CQSPI_REG_CONFIG);
820
821 if (enable)
822 reg |= CQSPI_REG_CONFIG_ENABLE_MASK;
823 else
824 reg &= ~CQSPI_REG_CONFIG_ENABLE_MASK;
825
826 writel(reg, reg_base + CQSPI_REG_CONFIG);
827}
828
829static void cqspi_configure(struct spi_nor *nor)
830{
831 struct cqspi_flash_pdata *f_pdata = nor->priv;
832 struct cqspi_st *cqspi = f_pdata->cqspi;
833 const unsigned int sclk = f_pdata->clk_rate;
834 int switch_cs = (cqspi->current_cs != f_pdata->cs);
835 int switch_ck = (cqspi->sclk != sclk);
836
837 if ((cqspi->current_page_size != nor->page_size) ||
838 (cqspi->current_erase_size != nor->mtd.erasesize) ||
839 (cqspi->current_addr_width != nor->addr_width))
840 switch_cs = 1;
841
842 if (switch_cs || switch_ck)
843 cqspi_controller_enable(cqspi, 0);
844
845 /* Switch chip select. */
846 if (switch_cs) {
847 cqspi->current_cs = f_pdata->cs;
848 cqspi_configure_cs_and_sizes(nor);
849 }
850
851 /* Setup baudrate divisor and delays */
852 if (switch_ck) {
853 cqspi->sclk = sclk;
854 cqspi_config_baudrate_div(cqspi);
855 cqspi_delay(nor);
Vignesh Re2580a42017-10-03 10:49:23 +0530856 cqspi_readdata_capture(cqspi, !cqspi->rclk_en,
857 f_pdata->read_delay);
Graham Moore14062342016-06-04 02:39:34 +0200858 }
859
860 if (switch_cs || switch_ck)
861 cqspi_controller_enable(cqspi, 1);
862}
863
864static int cqspi_set_protocol(struct spi_nor *nor, const int read)
865{
866 struct cqspi_flash_pdata *f_pdata = nor->priv;
867
868 f_pdata->inst_width = CQSPI_INST_TYPE_SINGLE;
869 f_pdata->addr_width = CQSPI_INST_TYPE_SINGLE;
870 f_pdata->data_width = CQSPI_INST_TYPE_SINGLE;
871
872 if (read) {
Cyrille Pitchencfc56042017-04-25 22:08:46 +0200873 switch (nor->read_proto) {
874 case SNOR_PROTO_1_1_1:
Graham Moore14062342016-06-04 02:39:34 +0200875 f_pdata->data_width = CQSPI_INST_TYPE_SINGLE;
876 break;
Cyrille Pitchencfc56042017-04-25 22:08:46 +0200877 case SNOR_PROTO_1_1_2:
Graham Moore14062342016-06-04 02:39:34 +0200878 f_pdata->data_width = CQSPI_INST_TYPE_DUAL;
879 break;
Cyrille Pitchencfc56042017-04-25 22:08:46 +0200880 case SNOR_PROTO_1_1_4:
Graham Moore14062342016-06-04 02:39:34 +0200881 f_pdata->data_width = CQSPI_INST_TYPE_QUAD;
882 break;
883 default:
884 return -EINVAL;
885 }
886 }
887
888 cqspi_configure(nor);
889
890 return 0;
891}
892
893static ssize_t cqspi_write(struct spi_nor *nor, loff_t to,
894 size_t len, const u_char *buf)
895{
896 int ret;
897
898 ret = cqspi_set_protocol(nor, 0);
899 if (ret)
900 return ret;
901
902 ret = cqspi_indirect_write_setup(nor, to);
903 if (ret)
904 return ret;
905
906 ret = cqspi_indirect_write_execute(nor, buf, len);
907 if (ret)
908 return ret;
909
Colin Ian King7fa2c702017-01-31 15:53:17 +0000910 return len;
Graham Moore14062342016-06-04 02:39:34 +0200911}
912
913static ssize_t cqspi_read(struct spi_nor *nor, loff_t from,
914 size_t len, u_char *buf)
915{
916 int ret;
917
918 ret = cqspi_set_protocol(nor, 1);
919 if (ret)
920 return ret;
921
922 ret = cqspi_indirect_read_setup(nor, from);
923 if (ret)
924 return ret;
925
926 ret = cqspi_indirect_read_execute(nor, buf, len);
927 if (ret)
928 return ret;
929
Colin Ian King7fa2c702017-01-31 15:53:17 +0000930 return len;
Graham Moore14062342016-06-04 02:39:34 +0200931}
932
933static int cqspi_erase(struct spi_nor *nor, loff_t offs)
934{
935 int ret;
936
937 ret = cqspi_set_protocol(nor, 0);
938 if (ret)
939 return ret;
940
941 /* Send write enable, then erase commands. */
942 ret = nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
943 if (ret)
944 return ret;
945
946 /* Set up command buffer. */
947 ret = cqspi_command_write_addr(nor, nor->erase_opcode, offs);
948 if (ret)
949 return ret;
950
951 return 0;
952}
953
954static int cqspi_prep(struct spi_nor *nor, enum spi_nor_ops ops)
955{
956 struct cqspi_flash_pdata *f_pdata = nor->priv;
957 struct cqspi_st *cqspi = f_pdata->cqspi;
958
959 mutex_lock(&cqspi->bus_mutex);
960
961 return 0;
962}
963
964static void cqspi_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
965{
966 struct cqspi_flash_pdata *f_pdata = nor->priv;
967 struct cqspi_st *cqspi = f_pdata->cqspi;
968
969 mutex_unlock(&cqspi->bus_mutex);
970}
971
972static int cqspi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
973{
974 int ret;
975
976 ret = cqspi_set_protocol(nor, 0);
977 if (!ret)
978 ret = cqspi_command_read(nor, &opcode, 1, buf, len);
979
980 return ret;
981}
982
983static int cqspi_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
984{
985 int ret;
986
987 ret = cqspi_set_protocol(nor, 0);
988 if (!ret)
989 ret = cqspi_command_write(nor, opcode, buf, len);
990
991 return ret;
992}
993
994static int cqspi_of_get_flash_pdata(struct platform_device *pdev,
995 struct cqspi_flash_pdata *f_pdata,
996 struct device_node *np)
997{
998 if (of_property_read_u32(np, "cdns,read-delay", &f_pdata->read_delay)) {
999 dev_err(&pdev->dev, "couldn't determine read-delay\n");
1000 return -ENXIO;
1001 }
1002
1003 if (of_property_read_u32(np, "cdns,tshsl-ns", &f_pdata->tshsl_ns)) {
1004 dev_err(&pdev->dev, "couldn't determine tshsl-ns\n");
1005 return -ENXIO;
1006 }
1007
1008 if (of_property_read_u32(np, "cdns,tsd2d-ns", &f_pdata->tsd2d_ns)) {
1009 dev_err(&pdev->dev, "couldn't determine tsd2d-ns\n");
1010 return -ENXIO;
1011 }
1012
1013 if (of_property_read_u32(np, "cdns,tchsh-ns", &f_pdata->tchsh_ns)) {
1014 dev_err(&pdev->dev, "couldn't determine tchsh-ns\n");
1015 return -ENXIO;
1016 }
1017
1018 if (of_property_read_u32(np, "cdns,tslch-ns", &f_pdata->tslch_ns)) {
1019 dev_err(&pdev->dev, "couldn't determine tslch-ns\n");
1020 return -ENXIO;
1021 }
1022
1023 if (of_property_read_u32(np, "spi-max-frequency", &f_pdata->clk_rate)) {
1024 dev_err(&pdev->dev, "couldn't determine spi-max-frequency\n");
1025 return -ENXIO;
1026 }
1027
1028 return 0;
1029}
1030
1031static int cqspi_of_get_pdata(struct platform_device *pdev)
1032{
1033 struct device_node *np = pdev->dev.of_node;
1034 struct cqspi_st *cqspi = platform_get_drvdata(pdev);
1035
1036 cqspi->is_decoded_cs = of_property_read_bool(np, "cdns,is-decoded-cs");
1037
1038 if (of_property_read_u32(np, "cdns,fifo-depth", &cqspi->fifo_depth)) {
1039 dev_err(&pdev->dev, "couldn't determine fifo-depth\n");
1040 return -ENXIO;
1041 }
1042
1043 if (of_property_read_u32(np, "cdns,fifo-width", &cqspi->fifo_width)) {
1044 dev_err(&pdev->dev, "couldn't determine fifo-width\n");
1045 return -ENXIO;
1046 }
1047
1048 if (of_property_read_u32(np, "cdns,trigger-address",
1049 &cqspi->trigger_address)) {
1050 dev_err(&pdev->dev, "couldn't determine trigger-address\n");
1051 return -ENXIO;
1052 }
1053
Vignesh Re2580a42017-10-03 10:49:23 +05301054 cqspi->rclk_en = of_property_read_bool(np, "cdns,rclk-en");
1055
Graham Moore14062342016-06-04 02:39:34 +02001056 return 0;
1057}
1058
1059static void cqspi_controller_init(struct cqspi_st *cqspi)
1060{
1061 cqspi_controller_enable(cqspi, 0);
1062
1063 /* Configure the remap address register, no remap */
1064 writel(0, cqspi->iobase + CQSPI_REG_REMAP);
1065
1066 /* Disable all interrupts. */
1067 writel(0, cqspi->iobase + CQSPI_REG_IRQMASK);
1068
1069 /* Configure the SRAM split to 1:1 . */
1070 writel(cqspi->fifo_depth / 2, cqspi->iobase + CQSPI_REG_SRAMPARTITION);
1071
1072 /* Load indirect trigger address. */
1073 writel(cqspi->trigger_address,
1074 cqspi->iobase + CQSPI_REG_INDIRECTTRIGGER);
1075
1076 /* Program read watermark -- 1/2 of the FIFO. */
1077 writel(cqspi->fifo_depth * cqspi->fifo_width / 2,
1078 cqspi->iobase + CQSPI_REG_INDIRECTRDWATERMARK);
1079 /* Program write watermark -- 1/8 of the FIFO. */
1080 writel(cqspi->fifo_depth * cqspi->fifo_width / 8,
1081 cqspi->iobase + CQSPI_REG_INDIRECTWRWATERMARK);
1082
1083 cqspi_controller_enable(cqspi, 1);
1084}
1085
1086static int cqspi_setup_flash(struct cqspi_st *cqspi, struct device_node *np)
1087{
Cyrille Pitchencfc56042017-04-25 22:08:46 +02001088 const struct spi_nor_hwcaps hwcaps = {
1089 .mask = SNOR_HWCAPS_READ |
1090 SNOR_HWCAPS_READ_FAST |
1091 SNOR_HWCAPS_READ_1_1_2 |
1092 SNOR_HWCAPS_READ_1_1_4 |
1093 SNOR_HWCAPS_PP,
1094 };
Graham Moore14062342016-06-04 02:39:34 +02001095 struct platform_device *pdev = cqspi->pdev;
1096 struct device *dev = &pdev->dev;
1097 struct cqspi_flash_pdata *f_pdata;
1098 struct spi_nor *nor;
1099 struct mtd_info *mtd;
1100 unsigned int cs;
1101 int i, ret;
1102
1103 /* Get flash device data */
1104 for_each_available_child_of_node(dev->of_node, np) {
Dan Carpenter10ad1d72016-10-13 11:30:39 +03001105 ret = of_property_read_u32(np, "reg", &cs);
1106 if (ret) {
Graham Moore14062342016-06-04 02:39:34 +02001107 dev_err(dev, "Couldn't determine chip select.\n");
1108 goto err;
1109 }
1110
Dan Carpenter193e87142016-10-13 11:06:47 +03001111 if (cs >= CQSPI_MAX_CHIPSELECT) {
Dan Carpenter10ad1d72016-10-13 11:30:39 +03001112 ret = -EINVAL;
Graham Moore14062342016-06-04 02:39:34 +02001113 dev_err(dev, "Chip select %d out of range.\n", cs);
1114 goto err;
1115 }
1116
1117 f_pdata = &cqspi->f_pdata[cs];
1118 f_pdata->cqspi = cqspi;
1119 f_pdata->cs = cs;
1120
1121 ret = cqspi_of_get_flash_pdata(pdev, f_pdata, np);
1122 if (ret)
1123 goto err;
1124
1125 nor = &f_pdata->nor;
1126 mtd = &nor->mtd;
1127
1128 mtd->priv = nor;
1129
1130 nor->dev = dev;
1131 spi_nor_set_flash_node(nor, np);
1132 nor->priv = f_pdata;
1133
1134 nor->read_reg = cqspi_read_reg;
1135 nor->write_reg = cqspi_write_reg;
1136 nor->read = cqspi_read;
1137 nor->write = cqspi_write;
1138 nor->erase = cqspi_erase;
1139 nor->prepare = cqspi_prep;
1140 nor->unprepare = cqspi_unprep;
1141
1142 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "%s.%d",
1143 dev_name(dev), cs);
1144 if (!mtd->name) {
1145 ret = -ENOMEM;
1146 goto err;
1147 }
1148
Cyrille Pitchencfc56042017-04-25 22:08:46 +02001149 ret = spi_nor_scan(nor, NULL, &hwcaps);
Graham Moore14062342016-06-04 02:39:34 +02001150 if (ret)
1151 goto err;
1152
1153 ret = mtd_device_register(mtd, NULL, 0);
1154 if (ret)
1155 goto err;
1156
1157 f_pdata->registered = true;
1158 }
1159
1160 return 0;
1161
1162err:
1163 for (i = 0; i < CQSPI_MAX_CHIPSELECT; i++)
1164 if (cqspi->f_pdata[i].registered)
1165 mtd_device_unregister(&cqspi->f_pdata[i].nor.mtd);
1166 return ret;
1167}
1168
1169static int cqspi_probe(struct platform_device *pdev)
1170{
1171 struct device_node *np = pdev->dev.of_node;
1172 struct device *dev = &pdev->dev;
1173 struct cqspi_st *cqspi;
1174 struct resource *res;
1175 struct resource *res_ahb;
Vignesh R61dc8492017-10-03 10:49:21 +05301176 unsigned long data;
Graham Moore14062342016-06-04 02:39:34 +02001177 int ret;
1178 int irq;
1179
1180 cqspi = devm_kzalloc(dev, sizeof(*cqspi), GFP_KERNEL);
1181 if (!cqspi)
1182 return -ENOMEM;
1183
1184 mutex_init(&cqspi->bus_mutex);
1185 cqspi->pdev = pdev;
1186 platform_set_drvdata(pdev, cqspi);
1187
1188 /* Obtain configuration from OF. */
1189 ret = cqspi_of_get_pdata(pdev);
1190 if (ret) {
1191 dev_err(dev, "Cannot get mandatory OF data.\n");
1192 return -ENODEV;
1193 }
1194
1195 /* Obtain QSPI clock. */
1196 cqspi->clk = devm_clk_get(dev, NULL);
1197 if (IS_ERR(cqspi->clk)) {
1198 dev_err(dev, "Cannot claim QSPI clock.\n");
1199 return PTR_ERR(cqspi->clk);
1200 }
1201
1202 /* Obtain and remap controller address. */
1203 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1204 cqspi->iobase = devm_ioremap_resource(dev, res);
1205 if (IS_ERR(cqspi->iobase)) {
1206 dev_err(dev, "Cannot remap controller address.\n");
1207 return PTR_ERR(cqspi->iobase);
1208 }
1209
1210 /* Obtain and remap AHB address. */
1211 res_ahb = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1212 cqspi->ahb_base = devm_ioremap_resource(dev, res_ahb);
1213 if (IS_ERR(cqspi->ahb_base)) {
1214 dev_err(dev, "Cannot remap AHB address.\n");
1215 return PTR_ERR(cqspi->ahb_base);
1216 }
1217
1218 init_completion(&cqspi->transfer_complete);
1219
1220 /* Obtain IRQ line. */
1221 irq = platform_get_irq(pdev, 0);
1222 if (irq < 0) {
1223 dev_err(dev, "Cannot obtain IRQ.\n");
1224 return -ENXIO;
1225 }
1226
1227 ret = clk_prepare_enable(cqspi->clk);
1228 if (ret) {
1229 dev_err(dev, "Cannot enable QSPI clock.\n");
1230 return ret;
1231 }
1232
1233 cqspi->master_ref_clk_hz = clk_get_rate(cqspi->clk);
Vignesh R61dc8492017-10-03 10:49:21 +05301234 data = (unsigned long)of_device_get_match_data(dev);
1235 if (data & CQSPI_NEEDS_WR_DELAY)
1236 cqspi->wr_delay = 5 * DIV_ROUND_UP(NSEC_PER_SEC,
1237 cqspi->master_ref_clk_hz);
Graham Moore14062342016-06-04 02:39:34 +02001238
1239 ret = devm_request_irq(dev, irq, cqspi_irq_handler, 0,
1240 pdev->name, cqspi);
1241 if (ret) {
1242 dev_err(dev, "Cannot request IRQ.\n");
1243 goto probe_irq_failed;
1244 }
1245
1246 cqspi_wait_idle(cqspi);
1247 cqspi_controller_init(cqspi);
1248 cqspi->current_cs = -1;
1249 cqspi->sclk = 0;
1250
1251 ret = cqspi_setup_flash(cqspi, np);
1252 if (ret) {
1253 dev_err(dev, "Cadence QSPI NOR probe failed %d\n", ret);
1254 goto probe_setup_failed;
1255 }
1256
1257 return ret;
Graham Moore14062342016-06-04 02:39:34 +02001258probe_setup_failed:
Vignesh R329864d2017-10-03 10:49:24 +05301259 cqspi_controller_enable(cqspi, 0);
1260probe_irq_failed:
Graham Moore14062342016-06-04 02:39:34 +02001261 clk_disable_unprepare(cqspi->clk);
1262 return ret;
1263}
1264
1265static int cqspi_remove(struct platform_device *pdev)
1266{
1267 struct cqspi_st *cqspi = platform_get_drvdata(pdev);
1268 int i;
1269
1270 for (i = 0; i < CQSPI_MAX_CHIPSELECT; i++)
1271 if (cqspi->f_pdata[i].registered)
1272 mtd_device_unregister(&cqspi->f_pdata[i].nor.mtd);
1273
1274 cqspi_controller_enable(cqspi, 0);
1275
1276 clk_disable_unprepare(cqspi->clk);
1277
1278 return 0;
1279}
1280
1281#ifdef CONFIG_PM_SLEEP
1282static int cqspi_suspend(struct device *dev)
1283{
1284 struct cqspi_st *cqspi = dev_get_drvdata(dev);
1285
1286 cqspi_controller_enable(cqspi, 0);
1287 return 0;
1288}
1289
1290static int cqspi_resume(struct device *dev)
1291{
1292 struct cqspi_st *cqspi = dev_get_drvdata(dev);
1293
1294 cqspi_controller_enable(cqspi, 1);
1295 return 0;
1296}
1297
1298static const struct dev_pm_ops cqspi__dev_pm_ops = {
1299 .suspend = cqspi_suspend,
1300 .resume = cqspi_resume,
1301};
1302
1303#define CQSPI_DEV_PM_OPS (&cqspi__dev_pm_ops)
1304#else
1305#define CQSPI_DEV_PM_OPS NULL
1306#endif
1307
Arnd Bergmann315e9c72017-06-27 17:34:19 +02001308static const struct of_device_id cqspi_dt_ids[] = {
Vignesh R61dc8492017-10-03 10:49:21 +05301309 {
1310 .compatible = "cdns,qspi-nor",
1311 .data = (void *)0,
1312 },
1313 {
1314 .compatible = "ti,k2g-qspi",
1315 .data = (void *)CQSPI_NEEDS_WR_DELAY,
1316 },
Graham Moore14062342016-06-04 02:39:34 +02001317 { /* end of table */ }
1318};
1319
1320MODULE_DEVICE_TABLE(of, cqspi_dt_ids);
1321
1322static struct platform_driver cqspi_platform_driver = {
1323 .probe = cqspi_probe,
1324 .remove = cqspi_remove,
1325 .driver = {
1326 .name = CQSPI_NAME,
1327 .pm = CQSPI_DEV_PM_OPS,
1328 .of_match_table = cqspi_dt_ids,
1329 },
1330};
1331
1332module_platform_driver(cqspi_platform_driver);
1333
1334MODULE_DESCRIPTION("Cadence QSPI Controller Driver");
1335MODULE_LICENSE("GPL v2");
1336MODULE_ALIAS("platform:" CQSPI_NAME);
1337MODULE_AUTHOR("Ley Foon Tan <lftan@altera.com>");
1338MODULE_AUTHOR("Graham Moore <grmoore@opensource.altera.com>");