blob: 8fff43e12242ea40742d5d402ae234cceafeba88 [file] [log] [blame]
Kamal Dasufa236a72016-08-24 18:04:23 -04001/*
2 * Driver for Broadcom BRCMSTB, NSP, NS2, Cygnus SPI Controllers
3 *
4 * Copyright 2016 Broadcom
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2, as
8 * published by the Free Software Foundation (the "GPL").
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 (GPLv2) for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * version 2 (GPLv2) along with this source code.
17 */
18
19#include <linux/clk.h>
20#include <linux/delay.h>
21#include <linux/device.h>
22#include <linux/init.h>
23#include <linux/interrupt.h>
24#include <linux/io.h>
25#include <linux/ioport.h>
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/mtd/cfi.h>
29#include <linux/mtd/spi-nor.h>
30#include <linux/of.h>
31#include <linux/of_irq.h>
32#include <linux/platform_device.h>
33#include <linux/slab.h>
34#include <linux/spi/spi.h>
35#include <linux/sysfs.h>
36#include <linux/types.h>
37#include "spi-bcm-qspi.h"
38
39#define DRIVER_NAME "bcm_qspi"
40
Kamal Dasu4e3b2d22016-08-24 18:04:25 -040041
42/* BSPI register offsets */
43#define BSPI_REVISION_ID 0x000
44#define BSPI_SCRATCH 0x004
45#define BSPI_MAST_N_BOOT_CTRL 0x008
46#define BSPI_BUSY_STATUS 0x00c
47#define BSPI_INTR_STATUS 0x010
48#define BSPI_B0_STATUS 0x014
49#define BSPI_B0_CTRL 0x018
50#define BSPI_B1_STATUS 0x01c
51#define BSPI_B1_CTRL 0x020
52#define BSPI_STRAP_OVERRIDE_CTRL 0x024
53#define BSPI_FLEX_MODE_ENABLE 0x028
54#define BSPI_BITS_PER_CYCLE 0x02c
55#define BSPI_BITS_PER_PHASE 0x030
56#define BSPI_CMD_AND_MODE_BYTE 0x034
57#define BSPI_BSPI_FLASH_UPPER_ADDR_BYTE 0x038
58#define BSPI_BSPI_XOR_VALUE 0x03c
59#define BSPI_BSPI_XOR_ENABLE 0x040
60#define BSPI_BSPI_PIO_MODE_ENABLE 0x044
61#define BSPI_BSPI_PIO_IODIR 0x048
62#define BSPI_BSPI_PIO_DATA 0x04c
63
64/* RAF register offsets */
65#define BSPI_RAF_START_ADDR 0x100
66#define BSPI_RAF_NUM_WORDS 0x104
67#define BSPI_RAF_CTRL 0x108
68#define BSPI_RAF_FULLNESS 0x10c
69#define BSPI_RAF_WATERMARK 0x110
70#define BSPI_RAF_STATUS 0x114
71#define BSPI_RAF_READ_DATA 0x118
72#define BSPI_RAF_WORD_CNT 0x11c
73#define BSPI_RAF_CURR_ADDR 0x120
74
75/* Override mode masks */
76#define BSPI_STRAP_OVERRIDE_CTRL_OVERRIDE BIT(0)
77#define BSPI_STRAP_OVERRIDE_CTRL_DATA_DUAL BIT(1)
78#define BSPI_STRAP_OVERRIDE_CTRL_ADDR_4BYTE BIT(2)
79#define BSPI_STRAP_OVERRIDE_CTRL_DATA_QUAD BIT(3)
80#define BSPI_STRAP_OVERRIDE_CTRL_ENDAIN_MODE BIT(4)
81
82#define BSPI_ADDRLEN_3BYTES 3
83#define BSPI_ADDRLEN_4BYTES 4
84
85#define BSPI_RAF_STATUS_FIFO_EMPTY_MASK BIT(1)
86
87#define BSPI_RAF_CTRL_START_MASK BIT(0)
88#define BSPI_RAF_CTRL_CLEAR_MASK BIT(1)
89
90#define BSPI_BPP_MODE_SELECT_MASK BIT(8)
91#define BSPI_BPP_ADDR_SELECT_MASK BIT(16)
92
93#define BSPI_READ_LENGTH 256
94
Kamal Dasufa236a72016-08-24 18:04:23 -040095/* MSPI register offsets */
96#define MSPI_SPCR0_LSB 0x000
97#define MSPI_SPCR0_MSB 0x004
98#define MSPI_SPCR1_LSB 0x008
99#define MSPI_SPCR1_MSB 0x00c
100#define MSPI_NEWQP 0x010
101#define MSPI_ENDQP 0x014
102#define MSPI_SPCR2 0x018
103#define MSPI_MSPI_STATUS 0x020
104#define MSPI_CPTQP 0x024
105#define MSPI_SPCR3 0x028
106#define MSPI_TXRAM 0x040
107#define MSPI_RXRAM 0x0c0
108#define MSPI_CDRAM 0x140
109#define MSPI_WRITE_LOCK 0x180
110
111#define MSPI_MASTER_BIT BIT(7)
112
113#define MSPI_NUM_CDRAM 16
114#define MSPI_CDRAM_CONT_BIT BIT(7)
115#define MSPI_CDRAM_BITSE_BIT BIT(6)
116#define MSPI_CDRAM_PCS 0xf
117
118#define MSPI_SPCR2_SPE BIT(6)
119#define MSPI_SPCR2_CONT_AFTER_CMD BIT(7)
120
121#define MSPI_MSPI_STATUS_SPIF BIT(0)
122
123#define INTR_BASE_BIT_SHIFT 0x02
124#define INTR_COUNT 0x07
125
126#define NUM_CHIPSELECT 4
127#define QSPI_SPBR_MIN 8U
128#define QSPI_SPBR_MAX 255U
129
130#define OPCODE_DIOR 0xBB
131#define OPCODE_QIOR 0xEB
132#define OPCODE_DIOR_4B 0xBC
133#define OPCODE_QIOR_4B 0xEC
134
135#define MAX_CMD_SIZE 6
136
137#define ADDR_4MB_MASK GENMASK(22, 0)
138
139/* stop at end of transfer, no other reason */
140#define TRANS_STATUS_BREAK_NONE 0
141/* stop at end of spi_message */
142#define TRANS_STATUS_BREAK_EOM 1
143/* stop at end of spi_transfer if delay */
144#define TRANS_STATUS_BREAK_DELAY 2
145/* stop at end of spi_transfer if cs_change */
146#define TRANS_STATUS_BREAK_CS_CHANGE 4
147/* stop if we run out of bytes */
148#define TRANS_STATUS_BREAK_NO_BYTES 8
149
150/* events that make us stop filling TX slots */
151#define TRANS_STATUS_BREAK_TX (TRANS_STATUS_BREAK_EOM | \
152 TRANS_STATUS_BREAK_DELAY | \
153 TRANS_STATUS_BREAK_CS_CHANGE)
154
155/* events that make us deassert CS */
156#define TRANS_STATUS_BREAK_DESELECT (TRANS_STATUS_BREAK_EOM | \
157 TRANS_STATUS_BREAK_CS_CHANGE)
158
159struct bcm_qspi_parms {
160 u32 speed_hz;
161 u8 mode;
162 u8 bits_per_word;
163};
164
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400165struct bcm_xfer_mode {
166 bool flex_mode;
167 unsigned int width;
168 unsigned int addrlen;
169 unsigned int hp;
170};
171
Kamal Dasufa236a72016-08-24 18:04:23 -0400172enum base_type {
173 MSPI,
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400174 BSPI,
Kamal Dasufa236a72016-08-24 18:04:23 -0400175 CHIP_SELECT,
176 BASEMAX,
177};
178
179struct bcm_qspi_irq {
180 const char *irq_name;
181 const irq_handler_t irq_handler;
182 u32 mask;
183};
184
185struct bcm_qspi_dev_id {
186 const struct bcm_qspi_irq *irqp;
187 void *dev;
188};
189
190struct qspi_trans {
191 struct spi_transfer *trans;
192 int byte;
193};
194
195struct bcm_qspi {
196 struct platform_device *pdev;
197 struct spi_master *master;
198 struct clk *clk;
199 u32 base_clk;
200 u32 max_speed_hz;
201 void __iomem *base[BASEMAX];
202 struct bcm_qspi_parms last_parms;
203 struct qspi_trans trans_pos;
204 int curr_cs;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400205 int bspi_maj_rev;
206 int bspi_min_rev;
207 int bspi_enabled;
208 struct spi_flash_read_message *bspi_rf_msg;
209 u32 bspi_rf_msg_idx;
210 u32 bspi_rf_msg_len;
211 u32 bspi_rf_msg_status;
212 struct bcm_xfer_mode xfer_mode;
Kamal Dasufa236a72016-08-24 18:04:23 -0400213 u32 s3_strap_override_ctrl;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400214 bool bspi_mode;
Kamal Dasufa236a72016-08-24 18:04:23 -0400215 bool big_endian;
216 int num_irqs;
217 struct bcm_qspi_dev_id *dev_ids;
218 struct completion mspi_done;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400219 struct completion bspi_done;
Kamal Dasufa236a72016-08-24 18:04:23 -0400220};
221
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400222static inline bool has_bspi(struct bcm_qspi *qspi)
223{
224 return qspi->bspi_mode;
225}
226
Kamal Dasufa236a72016-08-24 18:04:23 -0400227/* Read qspi controller register*/
228static inline u32 bcm_qspi_read(struct bcm_qspi *qspi, enum base_type type,
229 unsigned int offset)
230{
231 return bcm_qspi_readl(qspi->big_endian, qspi->base[type] + offset);
232}
233
234/* Write qspi controller register*/
235static inline void bcm_qspi_write(struct bcm_qspi *qspi, enum base_type type,
236 unsigned int offset, unsigned int data)
237{
238 bcm_qspi_writel(qspi->big_endian, data, qspi->base[type] + offset);
239}
240
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400241/* BSPI helpers */
242static int bcm_qspi_bspi_busy_poll(struct bcm_qspi *qspi)
243{
244 int i;
245
246 /* this should normally finish within 10us */
247 for (i = 0; i < 1000; i++) {
248 if (!(bcm_qspi_read(qspi, BSPI, BSPI_BUSY_STATUS) & 1))
249 return 0;
250 udelay(1);
251 }
252 dev_warn(&qspi->pdev->dev, "timeout waiting for !busy_status\n");
253 return -EIO;
254}
255
256static inline bool bcm_qspi_bspi_ver_three(struct bcm_qspi *qspi)
257{
258 if (qspi->bspi_maj_rev < 4)
259 return true;
260 return false;
261}
262
263static void bcm_qspi_bspi_flush_prefetch_buffers(struct bcm_qspi *qspi)
264{
265 bcm_qspi_bspi_busy_poll(qspi);
266 /* Force rising edge for the b0/b1 'flush' field */
267 bcm_qspi_write(qspi, BSPI, BSPI_B0_CTRL, 1);
268 bcm_qspi_write(qspi, BSPI, BSPI_B1_CTRL, 1);
269 bcm_qspi_write(qspi, BSPI, BSPI_B0_CTRL, 0);
270 bcm_qspi_write(qspi, BSPI, BSPI_B1_CTRL, 0);
271}
272
273static int bcm_qspi_bspi_lr_is_fifo_empty(struct bcm_qspi *qspi)
274{
275 return (bcm_qspi_read(qspi, BSPI, BSPI_RAF_STATUS) &
276 BSPI_RAF_STATUS_FIFO_EMPTY_MASK);
277}
278
279static inline u32 bcm_qspi_bspi_lr_read_fifo(struct bcm_qspi *qspi)
280{
281 u32 data = bcm_qspi_read(qspi, BSPI, BSPI_RAF_READ_DATA);
282
283 /* BSPI v3 LR is LE only, convert data to host endianness */
284 if (bcm_qspi_bspi_ver_three(qspi))
285 data = le32_to_cpu(data);
286
287 return data;
288}
289
290static inline void bcm_qspi_bspi_lr_start(struct bcm_qspi *qspi)
291{
292 bcm_qspi_bspi_busy_poll(qspi);
293 bcm_qspi_write(qspi, BSPI, BSPI_RAF_CTRL,
294 BSPI_RAF_CTRL_START_MASK);
295}
296
297static inline void bcm_qspi_bspi_lr_clear(struct bcm_qspi *qspi)
298{
299 bcm_qspi_write(qspi, BSPI, BSPI_RAF_CTRL,
300 BSPI_RAF_CTRL_CLEAR_MASK);
301 bcm_qspi_bspi_flush_prefetch_buffers(qspi);
302}
303
304static void bcm_qspi_bspi_lr_data_read(struct bcm_qspi *qspi)
305{
306 u32 *buf = (u32 *)qspi->bspi_rf_msg->buf;
307 u32 data = 0;
308
309 dev_dbg(&qspi->pdev->dev, "xfer %p rx %p rxlen %d\n", qspi->bspi_rf_msg,
310 qspi->bspi_rf_msg->buf, qspi->bspi_rf_msg_len);
311 while (!bcm_qspi_bspi_lr_is_fifo_empty(qspi)) {
312 data = bcm_qspi_bspi_lr_read_fifo(qspi);
313 if (likely(qspi->bspi_rf_msg_len >= 4) &&
314 IS_ALIGNED((uintptr_t)buf, 4)) {
315 buf[qspi->bspi_rf_msg_idx++] = data;
316 qspi->bspi_rf_msg_len -= 4;
317 } else {
318 /* Read out remaining bytes, make sure*/
319 u8 *cbuf = (u8 *)&buf[qspi->bspi_rf_msg_idx];
320
321 data = cpu_to_le32(data);
322 while (qspi->bspi_rf_msg_len) {
323 *cbuf++ = (u8)data;
324 data >>= 8;
325 qspi->bspi_rf_msg_len--;
326 }
327 }
328 }
329}
330
331static void bcm_qspi_bspi_set_xfer_params(struct bcm_qspi *qspi, u8 cmd_byte,
332 int bpp, int bpc, int flex_mode)
333{
334 bcm_qspi_write(qspi, BSPI, BSPI_FLEX_MODE_ENABLE, 0);
335 bcm_qspi_write(qspi, BSPI, BSPI_BITS_PER_CYCLE, bpc);
336 bcm_qspi_write(qspi, BSPI, BSPI_BITS_PER_PHASE, bpp);
337 bcm_qspi_write(qspi, BSPI, BSPI_CMD_AND_MODE_BYTE, cmd_byte);
338 bcm_qspi_write(qspi, BSPI, BSPI_FLEX_MODE_ENABLE, flex_mode);
339}
340
341static int bcm_qspi_bspi_set_flex_mode(struct bcm_qspi *qspi, int width,
342 int addrlen, int hp)
343{
344 int bpc = 0, bpp = 0;
345 u8 command = SPINOR_OP_READ_FAST;
346 int flex_mode = 1, rv = 0;
347 bool spans_4byte = false;
348
349 dev_dbg(&qspi->pdev->dev, "set flex mode w %x addrlen %x hp %d\n",
350 width, addrlen, hp);
351
352 if (addrlen == BSPI_ADDRLEN_4BYTES) {
353 bpp = BSPI_BPP_ADDR_SELECT_MASK;
354 spans_4byte = true;
355 }
356
357 bpp |= 8;
358
359 switch (width) {
360 case SPI_NBITS_SINGLE:
361 if (addrlen == BSPI_ADDRLEN_3BYTES)
362 /* default mode, does not need flex_cmd */
363 flex_mode = 0;
364 else
365 command = SPINOR_OP_READ4_FAST;
366 break;
367 case SPI_NBITS_DUAL:
368 bpc = 0x00000001;
369 if (hp) {
370 bpc |= 0x00010100; /* address and mode are 2-bit */
371 bpp = BSPI_BPP_MODE_SELECT_MASK;
372 command = OPCODE_DIOR;
373 if (spans_4byte)
374 command = OPCODE_DIOR_4B;
375 } else {
376 command = SPINOR_OP_READ_1_1_2;
377 if (spans_4byte)
378 command = SPINOR_OP_READ4_1_1_2;
379 }
380 break;
381 case SPI_NBITS_QUAD:
382 bpc = 0x00000002;
383 if (hp) {
384 bpc |= 0x00020200; /* address and mode are 4-bit */
385 bpp = 4; /* dummy cycles */
386 bpp |= BSPI_BPP_ADDR_SELECT_MASK;
387 command = OPCODE_QIOR;
388 if (spans_4byte)
389 command = OPCODE_QIOR_4B;
390 } else {
391 command = SPINOR_OP_READ_1_1_4;
392 if (spans_4byte)
393 command = SPINOR_OP_READ4_1_1_4;
394 }
395 break;
396 default:
397 rv = -EINVAL;
398 break;
399 }
400
401 if (rv == 0)
402 bcm_qspi_bspi_set_xfer_params(qspi, command, bpp, bpc,
403 flex_mode);
404
405 return rv;
406}
407
408static int bcm_qspi_bspi_set_override(struct bcm_qspi *qspi, int width,
409 int addrlen, int hp)
410{
411 u32 data = bcm_qspi_read(qspi, BSPI, BSPI_STRAP_OVERRIDE_CTRL);
412
413 dev_dbg(&qspi->pdev->dev, "set override mode w %x addrlen %x hp %d\n",
414 width, addrlen, hp);
415
416 switch (width) {
417 case SPI_NBITS_SINGLE:
418 /* clear quad/dual mode */
419 data &= ~(BSPI_STRAP_OVERRIDE_CTRL_DATA_QUAD |
420 BSPI_STRAP_OVERRIDE_CTRL_DATA_DUAL);
421 break;
422
423 case SPI_NBITS_QUAD:
424 /* clear dual mode and set quad mode */
425 data &= ~BSPI_STRAP_OVERRIDE_CTRL_DATA_DUAL;
426 data |= BSPI_STRAP_OVERRIDE_CTRL_DATA_QUAD;
427 break;
428 case SPI_NBITS_DUAL:
429 /* clear quad mode set dual mode */
430 data &= ~BSPI_STRAP_OVERRIDE_CTRL_DATA_QUAD;
431 data |= BSPI_STRAP_OVERRIDE_CTRL_DATA_DUAL;
432 break;
433 default:
434 return -EINVAL;
435 }
436
437 if (addrlen == BSPI_ADDRLEN_4BYTES)
438 /* set 4byte mode*/
439 data |= BSPI_STRAP_OVERRIDE_CTRL_ADDR_4BYTE;
440 else
441 /* clear 4 byte mode */
442 data &= ~BSPI_STRAP_OVERRIDE_CTRL_ADDR_4BYTE;
443
444 /* set the override mode */
445 data |= BSPI_STRAP_OVERRIDE_CTRL_OVERRIDE;
446 bcm_qspi_write(qspi, BSPI, BSPI_STRAP_OVERRIDE_CTRL, data);
447 bcm_qspi_bspi_set_xfer_params(qspi, SPINOR_OP_READ_FAST, 0, 0, 0);
448
449 return 0;
450}
451
452static int bcm_qspi_bspi_set_mode(struct bcm_qspi *qspi,
453 int width, int addrlen, int hp)
454{
455 int error = 0;
456
457 /* default mode */
458 qspi->xfer_mode.flex_mode = true;
459
460 if (!bcm_qspi_bspi_ver_three(qspi)) {
461 u32 val, mask;
462
463 val = bcm_qspi_read(qspi, BSPI, BSPI_STRAP_OVERRIDE_CTRL);
464 mask = BSPI_STRAP_OVERRIDE_CTRL_OVERRIDE;
465 if (val & mask || qspi->s3_strap_override_ctrl & mask) {
466 qspi->xfer_mode.flex_mode = false;
467 bcm_qspi_write(qspi, BSPI, BSPI_FLEX_MODE_ENABLE,
468 0);
469
470 if ((val | qspi->s3_strap_override_ctrl) &
471 BSPI_STRAP_OVERRIDE_CTRL_DATA_DUAL)
472 width = SPI_NBITS_DUAL;
473 else if ((val | qspi->s3_strap_override_ctrl) &
474 BSPI_STRAP_OVERRIDE_CTRL_DATA_QUAD)
475 width = SPI_NBITS_QUAD;
476
477 error = bcm_qspi_bspi_set_override(qspi, width, addrlen,
478 hp);
479 }
480 }
481
482 if (qspi->xfer_mode.flex_mode)
483 error = bcm_qspi_bspi_set_flex_mode(qspi, width, addrlen, hp);
484
485 if (error) {
486 dev_warn(&qspi->pdev->dev,
487 "INVALID COMBINATION: width=%d addrlen=%d hp=%d\n",
488 width, addrlen, hp);
489 } else if (qspi->xfer_mode.width != width ||
490 qspi->xfer_mode.addrlen != addrlen ||
491 qspi->xfer_mode.hp != hp) {
492 qspi->xfer_mode.width = width;
493 qspi->xfer_mode.addrlen = addrlen;
494 qspi->xfer_mode.hp = hp;
495 dev_dbg(&qspi->pdev->dev,
496 "cs:%d %d-lane output, %d-byte address%s\n",
497 qspi->curr_cs,
498 qspi->xfer_mode.width,
499 qspi->xfer_mode.addrlen,
500 qspi->xfer_mode.hp != -1 ? ", hp mode" : "");
501 }
502
503 return error;
504}
505
506static void bcm_qspi_enable_bspi(struct bcm_qspi *qspi)
507{
508 if (!has_bspi(qspi) || (qspi->bspi_enabled))
509 return;
510
511 qspi->bspi_enabled = 1;
512 if ((bcm_qspi_read(qspi, BSPI, BSPI_MAST_N_BOOT_CTRL) & 1) == 0)
513 return;
514
515 bcm_qspi_bspi_flush_prefetch_buffers(qspi);
516 udelay(1);
517 bcm_qspi_write(qspi, BSPI, BSPI_MAST_N_BOOT_CTRL, 0);
518 udelay(1);
519}
520
521static void bcm_qspi_disable_bspi(struct bcm_qspi *qspi)
522{
523 if (!has_bspi(qspi) || (!qspi->bspi_enabled))
524 return;
525
526 qspi->bspi_enabled = 0;
527 if ((bcm_qspi_read(qspi, BSPI, BSPI_MAST_N_BOOT_CTRL) & 1))
528 return;
529
530 bcm_qspi_bspi_busy_poll(qspi);
531 bcm_qspi_write(qspi, BSPI, BSPI_MAST_N_BOOT_CTRL, 1);
532 udelay(1);
533}
534
Kamal Dasufa236a72016-08-24 18:04:23 -0400535static void bcm_qspi_chip_select(struct bcm_qspi *qspi, int cs)
536{
537 u32 data = 0;
538
539 if (qspi->curr_cs == cs)
540 return;
541 if (qspi->base[CHIP_SELECT]) {
542 data = bcm_qspi_read(qspi, CHIP_SELECT, 0);
543 data = (data & ~0xff) | (1 << cs);
544 bcm_qspi_write(qspi, CHIP_SELECT, 0, data);
545 usleep_range(10, 20);
546 }
547 qspi->curr_cs = cs;
548}
549
550/* MSPI helpers */
551static void bcm_qspi_hw_set_parms(struct bcm_qspi *qspi,
552 const struct bcm_qspi_parms *xp)
553{
554 u32 spcr, spbr = 0;
555
556 if (xp->speed_hz)
557 spbr = qspi->base_clk / (2 * xp->speed_hz);
558
559 spcr = clamp_val(spbr, QSPI_SPBR_MIN, QSPI_SPBR_MAX);
560 bcm_qspi_write(qspi, MSPI, MSPI_SPCR0_LSB, spcr);
561
562 spcr = MSPI_MASTER_BIT;
563 /* for 16 bit the data should be zero */
564 if (xp->bits_per_word != 16)
565 spcr |= xp->bits_per_word << 2;
566 spcr |= xp->mode & 3;
567 bcm_qspi_write(qspi, MSPI, MSPI_SPCR0_MSB, spcr);
568
569 qspi->last_parms = *xp;
570}
571
572static void bcm_qspi_update_parms(struct bcm_qspi *qspi,
573 struct spi_device *spi,
574 struct spi_transfer *trans)
575{
576 struct bcm_qspi_parms xp;
577
578 xp.speed_hz = trans->speed_hz;
579 xp.bits_per_word = trans->bits_per_word;
580 xp.mode = spi->mode;
581
582 bcm_qspi_hw_set_parms(qspi, &xp);
583}
584
585static int bcm_qspi_setup(struct spi_device *spi)
586{
587 struct bcm_qspi_parms *xp;
588
589 if (spi->bits_per_word > 16)
590 return -EINVAL;
591
592 xp = spi_get_ctldata(spi);
593 if (!xp) {
594 xp = kzalloc(sizeof(*xp), GFP_KERNEL);
595 if (!xp)
596 return -ENOMEM;
597 spi_set_ctldata(spi, xp);
598 }
599 xp->speed_hz = spi->max_speed_hz;
600 xp->mode = spi->mode;
601
602 if (spi->bits_per_word)
603 xp->bits_per_word = spi->bits_per_word;
604 else
605 xp->bits_per_word = 8;
606
607 return 0;
608}
609
610static int update_qspi_trans_byte_count(struct bcm_qspi *qspi,
611 struct qspi_trans *qt, int flags)
612{
613 int ret = TRANS_STATUS_BREAK_NONE;
614
615 /* count the last transferred bytes */
616 if (qt->trans->bits_per_word <= 8)
617 qt->byte++;
618 else
619 qt->byte += 2;
620
621 if (qt->byte >= qt->trans->len) {
622 /* we're at the end of the spi_transfer */
623
624 /* in TX mode, need to pause for a delay or CS change */
625 if (qt->trans->delay_usecs &&
626 (flags & TRANS_STATUS_BREAK_DELAY))
627 ret |= TRANS_STATUS_BREAK_DELAY;
628 if (qt->trans->cs_change &&
629 (flags & TRANS_STATUS_BREAK_CS_CHANGE))
630 ret |= TRANS_STATUS_BREAK_CS_CHANGE;
631 if (ret)
632 goto done;
633
634 dev_dbg(&qspi->pdev->dev, "advance msg exit\n");
635 if (spi_transfer_is_last(qspi->master, qt->trans))
636 ret = TRANS_STATUS_BREAK_EOM;
637 else
638 ret = TRANS_STATUS_BREAK_NO_BYTES;
639
640 qt->trans = NULL;
641 }
642
643done:
644 dev_dbg(&qspi->pdev->dev, "trans %p len %d byte %d ret %x\n",
645 qt->trans, qt->trans ? qt->trans->len : 0, qt->byte, ret);
646 return ret;
647}
648
649static inline u8 read_rxram_slot_u8(struct bcm_qspi *qspi, int slot)
650{
651 u32 slot_offset = MSPI_RXRAM + (slot << 3) + 0x4;
652
653 /* mask out reserved bits */
654 return bcm_qspi_read(qspi, MSPI, slot_offset) & 0xff;
655}
656
657static inline u16 read_rxram_slot_u16(struct bcm_qspi *qspi, int slot)
658{
659 u32 reg_offset = MSPI_RXRAM;
660 u32 lsb_offset = reg_offset + (slot << 3) + 0x4;
661 u32 msb_offset = reg_offset + (slot << 3);
662
663 return (bcm_qspi_read(qspi, MSPI, lsb_offset) & 0xff) |
664 ((bcm_qspi_read(qspi, MSPI, msb_offset) & 0xff) << 8);
665}
666
667static void read_from_hw(struct bcm_qspi *qspi, int slots)
668{
669 struct qspi_trans tp;
670 int slot;
671
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400672 bcm_qspi_disable_bspi(qspi);
673
Kamal Dasufa236a72016-08-24 18:04:23 -0400674 if (slots > MSPI_NUM_CDRAM) {
675 /* should never happen */
676 dev_err(&qspi->pdev->dev, "%s: too many slots!\n", __func__);
677 return;
678 }
679
680 tp = qspi->trans_pos;
681
682 for (slot = 0; slot < slots; slot++) {
683 if (tp.trans->bits_per_word <= 8) {
684 u8 *buf = tp.trans->rx_buf;
685
686 if (buf)
687 buf[tp.byte] = read_rxram_slot_u8(qspi, slot);
688 dev_dbg(&qspi->pdev->dev, "RD %02x\n",
689 buf ? buf[tp.byte] : 0xff);
690 } else {
691 u16 *buf = tp.trans->rx_buf;
692
693 if (buf)
694 buf[tp.byte / 2] = read_rxram_slot_u16(qspi,
695 slot);
696 dev_dbg(&qspi->pdev->dev, "RD %04x\n",
697 buf ? buf[tp.byte] : 0xffff);
698 }
699
700 update_qspi_trans_byte_count(qspi, &tp,
701 TRANS_STATUS_BREAK_NONE);
702 }
703
704 qspi->trans_pos = tp;
705}
706
707static inline void write_txram_slot_u8(struct bcm_qspi *qspi, int slot,
708 u8 val)
709{
710 u32 reg_offset = MSPI_TXRAM + (slot << 3);
711
712 /* mask out reserved bits */
713 bcm_qspi_write(qspi, MSPI, reg_offset, val);
714}
715
716static inline void write_txram_slot_u16(struct bcm_qspi *qspi, int slot,
717 u16 val)
718{
719 u32 reg_offset = MSPI_TXRAM;
720 u32 msb_offset = reg_offset + (slot << 3);
721 u32 lsb_offset = reg_offset + (slot << 3) + 0x4;
722
723 bcm_qspi_write(qspi, MSPI, msb_offset, (val >> 8));
724 bcm_qspi_write(qspi, MSPI, lsb_offset, (val & 0xff));
725}
726
727static inline u32 read_cdram_slot(struct bcm_qspi *qspi, int slot)
728{
729 return bcm_qspi_read(qspi, MSPI, MSPI_CDRAM + (slot << 2));
730}
731
732static inline void write_cdram_slot(struct bcm_qspi *qspi, int slot, u32 val)
733{
734 bcm_qspi_write(qspi, MSPI, (MSPI_CDRAM + (slot << 2)), val);
735}
736
737/* Return number of slots written */
738static int write_to_hw(struct bcm_qspi *qspi, struct spi_device *spi)
739{
740 struct qspi_trans tp;
741 int slot = 0, tstatus = 0;
742 u32 mspi_cdram = 0;
743
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400744 bcm_qspi_disable_bspi(qspi);
Kamal Dasufa236a72016-08-24 18:04:23 -0400745 tp = qspi->trans_pos;
746 bcm_qspi_update_parms(qspi, spi, tp.trans);
747
748 /* Run until end of transfer or reached the max data */
749 while (!tstatus && slot < MSPI_NUM_CDRAM) {
750 if (tp.trans->bits_per_word <= 8) {
751 const u8 *buf = tp.trans->tx_buf;
752 u8 val = buf ? buf[tp.byte] : 0xff;
753
754 write_txram_slot_u8(qspi, slot, val);
755 dev_dbg(&qspi->pdev->dev, "WR %02x\n", val);
756 } else {
757 const u16 *buf = tp.trans->tx_buf;
758 u16 val = buf ? buf[tp.byte / 2] : 0xffff;
759
760 write_txram_slot_u16(qspi, slot, val);
761 dev_dbg(&qspi->pdev->dev, "WR %04x\n", val);
762 }
763 mspi_cdram = MSPI_CDRAM_CONT_BIT;
764 mspi_cdram |= (~(1 << spi->chip_select) &
765 MSPI_CDRAM_PCS);
766 mspi_cdram |= ((tp.trans->bits_per_word <= 8) ? 0 :
767 MSPI_CDRAM_BITSE_BIT);
768
769 write_cdram_slot(qspi, slot, mspi_cdram);
770
771 tstatus = update_qspi_trans_byte_count(qspi, &tp,
772 TRANS_STATUS_BREAK_TX);
773 slot++;
774 }
775
776 if (!slot) {
777 dev_err(&qspi->pdev->dev, "%s: no data to send?", __func__);
778 goto done;
779 }
780
781 dev_dbg(&qspi->pdev->dev, "submitting %d slots\n", slot);
782 bcm_qspi_write(qspi, MSPI, MSPI_NEWQP, 0);
783 bcm_qspi_write(qspi, MSPI, MSPI_ENDQP, slot - 1);
784
785 if (tstatus & TRANS_STATUS_BREAK_DESELECT) {
786 mspi_cdram = read_cdram_slot(qspi, slot - 1) &
787 ~MSPI_CDRAM_CONT_BIT;
788 write_cdram_slot(qspi, slot - 1, mspi_cdram);
789 }
790
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400791 if (has_bspi(qspi))
792 bcm_qspi_write(qspi, MSPI, MSPI_WRITE_LOCK, 1);
793
Kamal Dasufa236a72016-08-24 18:04:23 -0400794 /* Must flush previous writes before starting MSPI operation */
795 mb();
796 /* Set cont | spe | spifie */
797 bcm_qspi_write(qspi, MSPI, MSPI_SPCR2, 0xe0);
798
799done:
800 return slot;
801}
802
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400803static int bcm_qspi_bspi_flash_read(struct spi_device *spi,
804 struct spi_flash_read_message *msg)
805{
806 struct bcm_qspi *qspi = spi_master_get_devdata(spi->master);
807 u32 addr = 0, len, len_words;
808 int ret = 0;
809 unsigned long timeo = msecs_to_jiffies(100);
810
811 if (bcm_qspi_bspi_ver_three(qspi))
812 if (msg->addr_width == BSPI_ADDRLEN_4BYTES)
813 return -EIO;
814
815 bcm_qspi_chip_select(qspi, spi->chip_select);
816 bcm_qspi_write(qspi, MSPI, MSPI_WRITE_LOCK, 0);
817
818 /*
819 * when using flex mode mode we need to send
820 * the upper address byte to bspi
821 */
822 if (bcm_qspi_bspi_ver_three(qspi) == false) {
823 addr = msg->from & 0xff000000;
824 bcm_qspi_write(qspi, BSPI,
825 BSPI_BSPI_FLASH_UPPER_ADDR_BYTE, addr);
826 }
827
828 if (!qspi->xfer_mode.flex_mode)
829 addr = msg->from;
830 else
831 addr = msg->from & 0x00ffffff;
832
833 /* set BSPI RAF buffer max read length */
834 len = msg->len;
835 if (len > BSPI_READ_LENGTH)
836 len = BSPI_READ_LENGTH;
837
838 if (bcm_qspi_bspi_ver_three(qspi) == true)
839 addr = (addr + 0xc00000) & 0xffffff;
840
841 reinit_completion(&qspi->bspi_done);
842 bcm_qspi_enable_bspi(qspi);
843 len_words = (len + 3) >> 2;
844 qspi->bspi_rf_msg = msg;
845 qspi->bspi_rf_msg_status = 0;
846 qspi->bspi_rf_msg_idx = 0;
847 qspi->bspi_rf_msg_len = len;
848 dev_dbg(&qspi->pdev->dev, "bspi xfr addr 0x%x len 0x%x", addr, len);
849
850 bcm_qspi_write(qspi, BSPI, BSPI_RAF_START_ADDR, addr);
851 bcm_qspi_write(qspi, BSPI, BSPI_RAF_NUM_WORDS, len_words);
852 bcm_qspi_write(qspi, BSPI, BSPI_RAF_WATERMARK, 0);
853
854 /* Must flush previous writes before starting BSPI operation */
855 mb();
856
857 bcm_qspi_bspi_lr_start(qspi);
858 if (!wait_for_completion_timeout(&qspi->bspi_done, timeo)) {
859 dev_err(&qspi->pdev->dev, "timeout waiting for BSPI\n");
860 ret = -ETIMEDOUT;
861 } else {
862 /* set the return length for the caller */
863 msg->retlen = len;
864 }
865
866 return ret;
867}
868
869static int bcm_qspi_flash_read(struct spi_device *spi,
870 struct spi_flash_read_message *msg)
871{
872 struct bcm_qspi *qspi = spi_master_get_devdata(spi->master);
873 int ret = 0;
874 bool mspi_read = false;
875 u32 io_width, addrlen, addr, len;
876 u_char *buf;
877
878 buf = msg->buf;
879 addr = msg->from;
880 len = msg->len;
881
882 if (bcm_qspi_bspi_ver_three(qspi) == true) {
883 /*
884 * The address coming into this function is a raw flash offset.
885 * But for BSPI <= V3, we need to convert it to a remapped BSPI
886 * address. If it crosses a 4MB boundary, just revert back to
887 * using MSPI.
888 */
889 addr = (addr + 0xc00000) & 0xffffff;
890
891 if ((~ADDR_4MB_MASK & addr) ^
892 (~ADDR_4MB_MASK & (addr + len - 1)))
893 mspi_read = true;
894 }
895
896 /* non-aligned and very short transfers are handled by MSPI */
897 if (!IS_ALIGNED((uintptr_t)addr, 4) || !IS_ALIGNED((uintptr_t)buf, 4) ||
898 len < 4)
899 mspi_read = true;
900
901 if (mspi_read)
902 /* this will make the m25p80 read to fallback to mspi read */
903 return -EAGAIN;
904
905 io_width = msg->data_nbits ? msg->data_nbits : SPI_NBITS_SINGLE;
906 addrlen = msg->addr_width;
907 ret = bcm_qspi_bspi_set_mode(qspi, io_width, addrlen, -1);
908
909 if (!ret)
910 ret = bcm_qspi_bspi_flash_read(spi, msg);
911
912 return ret;
913}
914
Kamal Dasufa236a72016-08-24 18:04:23 -0400915static int bcm_qspi_transfer_one(struct spi_master *master,
916 struct spi_device *spi,
917 struct spi_transfer *trans)
918{
919 struct bcm_qspi *qspi = spi_master_get_devdata(master);
920 int slots;
921 unsigned long timeo = msecs_to_jiffies(100);
922
923 bcm_qspi_chip_select(qspi, spi->chip_select);
924 qspi->trans_pos.trans = trans;
925 qspi->trans_pos.byte = 0;
926
927 while (qspi->trans_pos.byte < trans->len) {
928 reinit_completion(&qspi->mspi_done);
929
930 slots = write_to_hw(qspi, spi);
931 if (!wait_for_completion_timeout(&qspi->mspi_done, timeo)) {
932 dev_err(&qspi->pdev->dev, "timeout waiting for MSPI\n");
933 return -ETIMEDOUT;
934 }
935
936 read_from_hw(qspi, slots);
937 }
938
939 return 0;
940}
941
942static void bcm_qspi_cleanup(struct spi_device *spi)
943{
944 struct bcm_qspi_parms *xp = spi_get_ctldata(spi);
945
946 kfree(xp);
947}
948
949static irqreturn_t bcm_qspi_mspi_l2_isr(int irq, void *dev_id)
950{
951 struct bcm_qspi_dev_id *qspi_dev_id = dev_id;
952 struct bcm_qspi *qspi = qspi_dev_id->dev;
953 u32 status = bcm_qspi_read(qspi, MSPI, MSPI_MSPI_STATUS);
954
955 if (status & MSPI_MSPI_STATUS_SPIF) {
956 /* clear interrupt */
957 status &= ~MSPI_MSPI_STATUS_SPIF;
958 bcm_qspi_write(qspi, MSPI, MSPI_MSPI_STATUS, status);
959 complete(&qspi->mspi_done);
960 return IRQ_HANDLED;
Kamal Dasufa236a72016-08-24 18:04:23 -0400961 }
Kamal Dasu4e3b2d22016-08-24 18:04:25 -0400962
963 return IRQ_NONE;
964}
965
966static irqreturn_t bcm_qspi_bspi_lr_l2_isr(int irq, void *dev_id)
967{
968 struct bcm_qspi_dev_id *qspi_dev_id = dev_id;
969 struct bcm_qspi *qspi = qspi_dev_id->dev;
970 u32 status;
971
972 if (qspi->bspi_enabled && qspi->bspi_rf_msg) {
973 bcm_qspi_bspi_lr_data_read(qspi);
974 if (qspi->bspi_rf_msg_len == 0) {
975 qspi->bspi_rf_msg = NULL;
976 if (qspi->bspi_rf_msg_status)
977 bcm_qspi_bspi_lr_clear(qspi);
978 else
979 bcm_qspi_bspi_flush_prefetch_buffers(qspi);
980 }
981 }
982
983 status = (qspi_dev_id->irqp->mask & INTR_BSPI_LR_SESSION_DONE_MASK);
984 if (qspi->bspi_enabled && status && qspi->bspi_rf_msg_len == 0)
985 complete(&qspi->bspi_done);
986
987 return IRQ_HANDLED;
988}
989
990static irqreturn_t bcm_qspi_bspi_lr_err_l2_isr(int irq, void *dev_id)
991{
992 struct bcm_qspi_dev_id *qspi_dev_id = dev_id;
993 struct bcm_qspi *qspi = qspi_dev_id->dev;
994
995 dev_err(&qspi->pdev->dev, "BSPI INT error\n");
996 qspi->bspi_rf_msg_status = -EIO;
997 complete(&qspi->bspi_done);
998 return IRQ_HANDLED;
Kamal Dasufa236a72016-08-24 18:04:23 -0400999}
1000
1001static const struct bcm_qspi_irq qspi_irq_tab[] = {
1002 {
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001003 .irq_name = "spi_lr_fullness_reached",
1004 .irq_handler = bcm_qspi_bspi_lr_l2_isr,
1005 .mask = INTR_BSPI_LR_FULLNESS_REACHED_MASK,
1006 },
1007 {
1008 .irq_name = "spi_lr_session_aborted",
1009 .irq_handler = bcm_qspi_bspi_lr_err_l2_isr,
1010 .mask = INTR_BSPI_LR_SESSION_ABORTED_MASK,
1011 },
1012 {
1013 .irq_name = "spi_lr_impatient",
1014 .irq_handler = bcm_qspi_bspi_lr_err_l2_isr,
1015 .mask = INTR_BSPI_LR_IMPATIENT_MASK,
1016 },
1017 {
1018 .irq_name = "spi_lr_session_done",
1019 .irq_handler = bcm_qspi_bspi_lr_l2_isr,
1020 .mask = INTR_BSPI_LR_SESSION_DONE_MASK,
1021 },
1022#ifdef QSPI_INT_DEBUG
1023 /* this interrupt is for debug purposes only, dont request irq */
1024 {
1025 .irq_name = "spi_lr_overread",
1026 .irq_handler = bcm_qspi_bspi_lr_err_l2_isr,
1027 .mask = INTR_BSPI_LR_OVERREAD_MASK,
1028 },
1029#endif
1030 {
Kamal Dasufa236a72016-08-24 18:04:23 -04001031 .irq_name = "mspi_done",
1032 .irq_handler = bcm_qspi_mspi_l2_isr,
1033 .mask = INTR_MSPI_DONE_MASK,
1034 },
1035 {
1036 .irq_name = "mspi_halted",
1037 .irq_handler = bcm_qspi_mspi_l2_isr,
1038 .mask = INTR_MSPI_HALTED_MASK,
1039 },
1040};
1041
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001042static void bcm_qspi_bspi_init(struct bcm_qspi *qspi)
1043{
1044 u32 val = 0;
1045
1046 val = bcm_qspi_read(qspi, BSPI, BSPI_REVISION_ID);
1047 qspi->bspi_maj_rev = (val >> 8) & 0xff;
1048 qspi->bspi_min_rev = val & 0xff;
1049 if (!(bcm_qspi_bspi_ver_three(qspi))) {
1050 /* Force mapping of BSPI address -> flash offset */
1051 bcm_qspi_write(qspi, BSPI, BSPI_BSPI_XOR_VALUE, 0);
1052 bcm_qspi_write(qspi, BSPI, BSPI_BSPI_XOR_ENABLE, 1);
1053 }
1054 qspi->bspi_enabled = 1;
1055 bcm_qspi_disable_bspi(qspi);
1056 bcm_qspi_write(qspi, BSPI, BSPI_B0_CTRL, 0);
1057 bcm_qspi_write(qspi, BSPI, BSPI_B1_CTRL, 0);
1058}
1059
Kamal Dasufa236a72016-08-24 18:04:23 -04001060static void bcm_qspi_hw_init(struct bcm_qspi *qspi)
1061{
1062 struct bcm_qspi_parms parms;
1063
1064 bcm_qspi_write(qspi, MSPI, MSPI_SPCR1_LSB, 0);
1065 bcm_qspi_write(qspi, MSPI, MSPI_SPCR1_MSB, 0);
1066 bcm_qspi_write(qspi, MSPI, MSPI_NEWQP, 0);
1067 bcm_qspi_write(qspi, MSPI, MSPI_ENDQP, 0);
1068 bcm_qspi_write(qspi, MSPI, MSPI_SPCR2, 0x20);
1069
1070 parms.mode = SPI_MODE_3;
1071 parms.bits_per_word = 8;
1072 parms.speed_hz = qspi->max_speed_hz;
1073 bcm_qspi_hw_set_parms(qspi, &parms);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001074
1075 if (has_bspi(qspi))
1076 bcm_qspi_bspi_init(qspi);
Kamal Dasufa236a72016-08-24 18:04:23 -04001077}
1078
1079static void bcm_qspi_hw_uninit(struct bcm_qspi *qspi)
1080{
1081 bcm_qspi_write(qspi, MSPI, MSPI_SPCR2, 0);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001082 if (has_bspi(qspi))
1083 bcm_qspi_write(qspi, MSPI, MSPI_WRITE_LOCK, 0);
1084
Kamal Dasufa236a72016-08-24 18:04:23 -04001085}
1086
1087static const struct of_device_id bcm_qspi_of_match[] = {
1088 { .compatible = "brcm,spi-bcm-qspi" },
1089 {},
1090};
1091MODULE_DEVICE_TABLE(of, bcm_qspi_of_match);
1092
1093int bcm_qspi_probe(struct platform_device *pdev,
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001094 struct bcm_qspi_soc_intc *soc_intc)
Kamal Dasufa236a72016-08-24 18:04:23 -04001095{
1096 struct device *dev = &pdev->dev;
1097 struct bcm_qspi *qspi;
1098 struct spi_master *master;
1099 struct resource *res;
1100 int irq, ret = 0, num_ints = 0;
1101 u32 val;
1102 const char *name = NULL;
1103 int num_irqs = ARRAY_SIZE(qspi_irq_tab);
1104
1105 /* We only support device-tree instantiation */
1106 if (!dev->of_node)
1107 return -ENODEV;
1108
1109 if (!of_match_node(bcm_qspi_of_match, dev->of_node))
1110 return -ENODEV;
1111
1112 master = spi_alloc_master(dev, sizeof(struct bcm_qspi));
1113 if (!master) {
1114 dev_err(dev, "error allocating spi_master\n");
1115 return -ENOMEM;
1116 }
1117
1118 qspi = spi_master_get_devdata(master);
1119 qspi->pdev = pdev;
1120 qspi->trans_pos.trans = NULL;
1121 qspi->trans_pos.byte = 0;
1122 qspi->master = master;
1123
1124 master->bus_num = -1;
1125 master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_RX_DUAL | SPI_RX_QUAD;
1126 master->setup = bcm_qspi_setup;
1127 master->transfer_one = bcm_qspi_transfer_one;
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001128 master->spi_flash_read = bcm_qspi_flash_read;
Kamal Dasufa236a72016-08-24 18:04:23 -04001129 master->cleanup = bcm_qspi_cleanup;
1130 master->dev.of_node = dev->of_node;
1131 master->num_chipselect = NUM_CHIPSELECT;
1132
1133 qspi->big_endian = of_device_is_big_endian(dev->of_node);
1134
1135 if (!of_property_read_u32(dev->of_node, "num-cs", &val))
1136 master->num_chipselect = val;
1137
1138 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hif_mspi");
1139 if (!res)
1140 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1141 "mspi");
1142
1143 if (res) {
1144 qspi->base[MSPI] = devm_ioremap_resource(dev, res);
1145 if (IS_ERR(qspi->base[MSPI])) {
1146 ret = PTR_ERR(qspi->base[MSPI]);
1147 goto qspi_probe_err;
1148 }
1149 } else {
1150 goto qspi_probe_err;
1151 }
1152
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001153 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "bspi");
1154 if (res) {
1155 qspi->base[BSPI] = devm_ioremap_resource(dev, res);
1156 if (IS_ERR(qspi->base[BSPI])) {
1157 ret = PTR_ERR(qspi->base[BSPI]);
1158 goto qspi_probe_err;
1159 }
1160 qspi->bspi_mode = true;
1161 } else {
1162 qspi->bspi_mode = false;
1163 }
1164
1165 dev_info(dev, "using %smspi mode\n", qspi->bspi_mode ? "bspi-" : "");
1166
Kamal Dasufa236a72016-08-24 18:04:23 -04001167 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs_reg");
1168 if (res) {
1169 qspi->base[CHIP_SELECT] = devm_ioremap_resource(dev, res);
1170 if (IS_ERR(qspi->base[CHIP_SELECT])) {
1171 ret = PTR_ERR(qspi->base[CHIP_SELECT]);
1172 goto qspi_probe_err;
1173 }
1174 }
1175
1176 qspi->dev_ids = kcalloc(num_irqs, sizeof(struct bcm_qspi_dev_id),
1177 GFP_KERNEL);
1178 if (IS_ERR(qspi->dev_ids)) {
1179 ret = PTR_ERR(qspi->dev_ids);
1180 goto qspi_probe_err;
1181 }
1182
1183 for (val = 0; val < num_irqs; val++) {
1184 irq = -1;
1185 name = qspi_irq_tab[val].irq_name;
1186 irq = platform_get_irq_byname(pdev, name);
1187
1188 if (irq >= 0) {
1189 ret = devm_request_irq(&pdev->dev, irq,
1190 qspi_irq_tab[val].irq_handler, 0,
1191 name,
1192 &qspi->dev_ids[val]);
1193 if (ret < 0) {
1194 dev_err(&pdev->dev, "IRQ %s not found\n", name);
1195 goto qspi_probe_err;
1196 }
1197
1198 qspi->dev_ids[val].dev = qspi;
1199 qspi->dev_ids[val].irqp = &qspi_irq_tab[val];
1200 num_ints++;
1201 dev_dbg(&pdev->dev, "registered IRQ %s %d\n",
1202 qspi_irq_tab[val].irq_name,
1203 irq);
1204 }
1205 }
1206
1207 if (!num_ints) {
1208 dev_err(&pdev->dev, "no IRQs registered, cannot init driver\n");
1209 goto qspi_probe_err;
1210 }
1211
1212 qspi->clk = devm_clk_get(&pdev->dev, NULL);
1213 if (IS_ERR(qspi->clk)) {
1214 dev_warn(dev, "unable to get clock\n");
1215 goto qspi_probe_err;
1216 }
1217
1218 ret = clk_prepare_enable(qspi->clk);
1219 if (ret) {
1220 dev_err(dev, "failed to prepare clock\n");
1221 goto qspi_probe_err;
1222 }
1223
1224 qspi->base_clk = clk_get_rate(qspi->clk);
1225 qspi->max_speed_hz = qspi->base_clk / (QSPI_SPBR_MIN * 2);
1226
1227 bcm_qspi_hw_init(qspi);
1228 init_completion(&qspi->mspi_done);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001229 init_completion(&qspi->bspi_done);
Kamal Dasufa236a72016-08-24 18:04:23 -04001230 qspi->curr_cs = -1;
1231
1232 platform_set_drvdata(pdev, qspi);
Kamal Dasu4e3b2d22016-08-24 18:04:25 -04001233
1234 qspi->xfer_mode.width = -1;
1235 qspi->xfer_mode.addrlen = -1;
1236 qspi->xfer_mode.hp = -1;
1237
Kamal Dasufa236a72016-08-24 18:04:23 -04001238 ret = devm_spi_register_master(&pdev->dev, master);
1239 if (ret < 0) {
1240 dev_err(dev, "can't register master\n");
1241 goto qspi_reg_err;
1242 }
1243
1244 return 0;
1245
1246qspi_reg_err:
1247 bcm_qspi_hw_uninit(qspi);
1248 clk_disable_unprepare(qspi->clk);
1249qspi_probe_err:
1250 spi_master_put(master);
1251 kfree(qspi->dev_ids);
1252 return ret;
1253}
1254/* probe function to be called by SoC specific platform driver probe */
1255EXPORT_SYMBOL_GPL(bcm_qspi_probe);
1256
1257int bcm_qspi_remove(struct platform_device *pdev)
1258{
1259 struct bcm_qspi *qspi = platform_get_drvdata(pdev);
1260
1261 platform_set_drvdata(pdev, NULL);
1262 bcm_qspi_hw_uninit(qspi);
1263 clk_disable_unprepare(qspi->clk);
1264 kfree(qspi->dev_ids);
1265 spi_unregister_master(qspi->master);
1266
1267 return 0;
1268}
1269/* function to be called by SoC specific platform driver remove() */
1270EXPORT_SYMBOL_GPL(bcm_qspi_remove);
1271
1272#ifdef CONFIG_PM_SLEEP
1273static int bcm_qspi_suspend(struct device *dev)
1274{
1275 struct bcm_qspi *qspi = dev_get_drvdata(dev);
1276
1277 spi_master_suspend(qspi->master);
1278 clk_disable(qspi->clk);
1279 bcm_qspi_hw_uninit(qspi);
1280
1281 return 0;
1282};
1283
1284static int bcm_qspi_resume(struct device *dev)
1285{
1286 struct bcm_qspi *qspi = dev_get_drvdata(dev);
1287 int ret = 0;
1288
1289 bcm_qspi_hw_init(qspi);
1290 bcm_qspi_chip_select(qspi, qspi->curr_cs);
1291 ret = clk_enable(qspi->clk);
1292 if (!ret)
1293 spi_master_resume(qspi->master);
1294
1295 return ret;
1296}
1297#endif /* CONFIG_PM_SLEEP */
1298
1299const struct dev_pm_ops bcm_qspi_pm_ops = {
1300 .suspend = bcm_qspi_suspend,
1301 .resume = bcm_qspi_resume,
1302};
1303/* pm_ops to be called by SoC specific platform driver */
1304EXPORT_SYMBOL_GPL(bcm_qspi_pm_ops);
1305
1306MODULE_AUTHOR("Kamal Dasu");
1307MODULE_DESCRIPTION("Broadcom QSPI driver");
1308MODULE_LICENSE("GPL v2");
1309MODULE_ALIAS("platform:" DRIVER_NAME);