blob: d2428a8809c1c6a880573a32b643b3d1a5755bf7 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Designware SPI core controller driver (refer pxa2xx_spi.c)
3 *
4 * Copyright (c) 2009, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * 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
16#include <linux/dma-mapping.h>
17#include <linux/interrupt.h>
18#include <linux/module.h>
19#include <linux/highmem.h>
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/spi/spi.h>
23#include <linux/gpio.h>
24
25#include "spi-dw.h"
26
27#ifdef CONFIG_DEBUG_FS
28#include <linux/debugfs.h>
29#endif
30
31/* Slave spi_dev related */
32struct chip_data {
33 u8 cs; /* chip select pin */
34 u8 tmode; /* TR/TO/RO/EEPROM */
35 u8 type; /* SPI/SSP/MicroWire */
36
37 u8 poll_mode; /* 1 means use poll mode */
38
39 u8 enable_dma;
40 u16 clk_div; /* baud rate divider */
41 u32 speed_hz; /* baud rate */
42 void (*cs_control)(u32 command);
43};
44
45#ifdef CONFIG_DEBUG_FS
46#define SPI_REGS_BUFSIZE 1024
47static ssize_t dw_spi_show_regs(struct file *file, char __user *user_buf,
48 size_t count, loff_t *ppos)
49{
50 struct dw_spi *dws = file->private_data;
51 char *buf;
52 u32 len = 0;
53 ssize_t ret;
54
55 buf = kzalloc(SPI_REGS_BUFSIZE, GFP_KERNEL);
56 if (!buf)
57 return 0;
58
59 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
60 "%s registers:\n", dev_name(&dws->master->dev));
61 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
62 "=================================\n");
63 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
64 "CTRL0: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL0));
65 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
66 "CTRL1: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL1));
67 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
68 "SSIENR: \t0x%08x\n", dw_readl(dws, DW_SPI_SSIENR));
69 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
70 "SER: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SER));
71 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
72 "BAUDR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_BAUDR));
73 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
74 "TXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_TXFLTR));
75 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
76 "RXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_RXFLTR));
77 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
78 "TXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_TXFLR));
79 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
80 "RXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_RXFLR));
81 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
82 "SR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SR));
83 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
84 "IMR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_IMR));
85 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
86 "ISR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_ISR));
87 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
88 "DMACR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_DMACR));
89 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
90 "DMATDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMATDLR));
91 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
92 "DMARDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMARDLR));
93 len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
94 "=================================\n");
95
96 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
97 kfree(buf);
98 return ret;
99}
100
101static const struct file_operations dw_spi_regs_ops = {
102 .owner = THIS_MODULE,
103 .open = simple_open,
104 .read = dw_spi_show_regs,
105 .llseek = default_llseek,
106};
107
108static int dw_spi_debugfs_init(struct dw_spi *dws)
109{
110 char name[32];
111
112 snprintf(name, 32, "dw_spi%d", dws->master->bus_num);
113 dws->debugfs = debugfs_create_dir(name, NULL);
114 if (!dws->debugfs)
115 return -ENOMEM;
116
117 debugfs_create_file("registers", S_IFREG | S_IRUGO,
118 dws->debugfs, (void *)dws, &dw_spi_regs_ops);
119 return 0;
120}
121
122static void dw_spi_debugfs_remove(struct dw_spi *dws)
123{
124 debugfs_remove_recursive(dws->debugfs);
125}
126
127#else
128static inline int dw_spi_debugfs_init(struct dw_spi *dws)
129{
130 return 0;
131}
132
133static inline void dw_spi_debugfs_remove(struct dw_spi *dws)
134{
135}
136#endif /* CONFIG_DEBUG_FS */
137
138static void dw_spi_set_cs(struct spi_device *spi, bool enable)
139{
140 struct dw_spi *dws = spi_master_get_devdata(spi->master);
141 struct chip_data *chip = spi_get_ctldata(spi);
142
143 /* Chip select logic is inverted from spi_set_cs() */
144 if (chip && chip->cs_control)
145 chip->cs_control(!enable);
146
147 if (!enable)
148 dw_writel(dws, DW_SPI_SER, BIT(spi->chip_select));
149}
150
151/* Return the max entries we can fill into tx fifo */
152static inline u32 tx_max(struct dw_spi *dws)
153{
154 u32 tx_left, tx_room, rxtx_gap;
155
156 tx_left = (dws->tx_end - dws->tx) / dws->n_bytes;
157 tx_room = dws->fifo_len - dw_readl(dws, DW_SPI_TXFLR);
158
159 /*
160 * Another concern is about the tx/rx mismatch, we
161 * though to use (dws->fifo_len - rxflr - txflr) as
162 * one maximum value for tx, but it doesn't cover the
163 * data which is out of tx/rx fifo and inside the
164 * shift registers. So a control from sw point of
165 * view is taken.
166 */
167 rxtx_gap = ((dws->rx_end - dws->rx) - (dws->tx_end - dws->tx))
168 / dws->n_bytes;
169
170 return min3(tx_left, tx_room, (u32) (dws->fifo_len - rxtx_gap));
171}
172
173/* Return the max entries we should read out of rx fifo */
174static inline u32 rx_max(struct dw_spi *dws)
175{
176 u32 rx_left = (dws->rx_end - dws->rx) / dws->n_bytes;
177
178 return min_t(u32, rx_left, dw_readl(dws, DW_SPI_RXFLR));
179}
180
181static void dw_writer(struct dw_spi *dws)
182{
183 u32 max;
184 u16 txw = 0;
185
186 spin_lock(&dws->buf_lock);
187 max = tx_max(dws);
188 while (max--) {
189 /* Set the tx word if the transfer's original "tx" is not null */
190 if (dws->tx_end - dws->len) {
191 if (dws->n_bytes == 1)
192 txw = *(u8 *)(dws->tx);
193 else
194 txw = *(u16 *)(dws->tx);
195 }
196 dw_write_io_reg(dws, DW_SPI_DR, txw);
197 dws->tx += dws->n_bytes;
198 }
199 spin_unlock(&dws->buf_lock);
200}
201
202static void dw_reader(struct dw_spi *dws)
203{
204 u32 max;
205 u16 rxw;
206
207 spin_lock(&dws->buf_lock);
208 max = rx_max(dws);
209 while (max--) {
210 rxw = dw_read_io_reg(dws, DW_SPI_DR);
211 /* Care rx only if the transfer's original "rx" is not null */
212 if (dws->rx_end - dws->len) {
213 if (dws->n_bytes == 1)
214 *(u8 *)(dws->rx) = rxw;
215 else
216 *(u16 *)(dws->rx) = rxw;
217 }
218 dws->rx += dws->n_bytes;
219 }
220 spin_unlock(&dws->buf_lock);
221}
222
223static void int_error_stop(struct dw_spi *dws, const char *msg)
224{
225 spi_reset_chip(dws);
226
227 dev_err(&dws->master->dev, "%s\n", msg);
228 dws->master->cur_msg->status = -EIO;
229 spi_finalize_current_transfer(dws->master);
230}
231
232static irqreturn_t interrupt_transfer(struct dw_spi *dws)
233{
234 u16 irq_status = dw_readl(dws, DW_SPI_ISR);
235
236 /* Error handling */
237 if (irq_status & (SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI)) {
238 dw_readl(dws, DW_SPI_ICR);
239 int_error_stop(dws, "interrupt_transfer: fifo overrun/underrun");
240 return IRQ_HANDLED;
241 }
242
243 dw_reader(dws);
244 if (dws->rx_end == dws->rx) {
245 spi_mask_intr(dws, SPI_INT_TXEI);
246 spi_finalize_current_transfer(dws->master);
247 return IRQ_HANDLED;
248 }
249 if (irq_status & SPI_INT_TXEI) {
250 spi_mask_intr(dws, SPI_INT_TXEI);
251 dw_writer(dws);
252 /* Enable TX irq always, it will be disabled when RX finished */
253 spi_umask_intr(dws, SPI_INT_TXEI);
254 }
255
256 return IRQ_HANDLED;
257}
258
259static irqreturn_t dw_spi_irq(int irq, void *dev_id)
260{
261 struct spi_master *master = dev_id;
262 struct dw_spi *dws = spi_master_get_devdata(master);
263 u16 irq_status = dw_readl(dws, DW_SPI_ISR) & 0x3f;
264
265 if (!irq_status)
266 return IRQ_NONE;
267
268 if (!master->cur_msg) {
269 spi_mask_intr(dws, SPI_INT_TXEI);
270 return IRQ_HANDLED;
271 }
272
273 return dws->transfer_handler(dws);
274}
275
276/* Must be called inside pump_transfers() */
277static int poll_transfer(struct dw_spi *dws)
278{
279 do {
280 dw_writer(dws);
281 dw_reader(dws);
282 cpu_relax();
283 } while (dws->rx_end > dws->rx);
284
285 return 0;
286}
287
288static int dw_spi_transfer_one(struct spi_master *master,
289 struct spi_device *spi, struct spi_transfer *transfer)
290{
291 struct dw_spi *dws = spi_master_get_devdata(master);
292 struct chip_data *chip = spi_get_ctldata(spi);
293 unsigned long flags;
294 u8 imask = 0;
295 u16 txlevel = 0;
296 u32 cr0;
297 int ret;
298
299 dws->dma_mapped = 0;
300 spin_lock_irqsave(&dws->buf_lock, flags);
301 dws->tx = (void *)transfer->tx_buf;
302 dws->tx_end = dws->tx + transfer->len;
303 dws->rx = transfer->rx_buf;
304 dws->rx_end = dws->rx + transfer->len;
305 dws->len = transfer->len;
306 spin_unlock_irqrestore(&dws->buf_lock, flags);
307
308 /* Ensure dw->rx and dw->rx_end are visible */
309 smp_mb();
310
311 spi_enable_chip(dws, 0);
312
313 /* Handle per transfer options for bpw and speed */
314 if (transfer->speed_hz != dws->current_freq) {
315 if (transfer->speed_hz != chip->speed_hz) {
316 /* clk_div doesn't support odd number */
317 chip->clk_div = (DIV_ROUND_UP(dws->max_freq, transfer->speed_hz) + 1) & 0xfffe;
318 chip->speed_hz = transfer->speed_hz;
319 }
320 dws->current_freq = transfer->speed_hz;
321 spi_set_clk(dws, chip->clk_div);
322 }
323 if (transfer->bits_per_word == 8) {
324 dws->n_bytes = 1;
325 dws->dma_width = 1;
326 } else if (transfer->bits_per_word == 16) {
327 dws->n_bytes = 2;
328 dws->dma_width = 2;
329 } else {
330 return -EINVAL;
331 }
332 /* Default SPI mode is SCPOL = 0, SCPH = 0 */
333 cr0 = (transfer->bits_per_word - 1)
334 | (chip->type << SPI_FRF_OFFSET)
335 | (spi->mode << SPI_MODE_OFFSET)
336 | (chip->tmode << SPI_TMOD_OFFSET);
337
338 /*
339 * Adjust transfer mode if necessary. Requires platform dependent
340 * chipselect mechanism.
341 */
342 if (chip->cs_control) {
343 if (dws->rx && dws->tx)
344 chip->tmode = SPI_TMOD_TR;
345 else if (dws->rx)
346 chip->tmode = SPI_TMOD_RO;
347 else
348 chip->tmode = SPI_TMOD_TO;
349
350 cr0 &= ~SPI_TMOD_MASK;
351 cr0 |= (chip->tmode << SPI_TMOD_OFFSET);
352 }
353
354 dw_writel(dws, DW_SPI_CTRL0, cr0);
355
356 /* Check if current transfer is a DMA transaction */
357 if (master->can_dma && master->can_dma(master, spi, transfer))
358 dws->dma_mapped = master->cur_msg_mapped;
359
360 /* For poll mode just disable all interrupts */
361 spi_mask_intr(dws, 0xff);
362
363 /*
364 * Interrupt mode
365 * we only need set the TXEI IRQ, as TX/RX always happen syncronizely
366 */
367 if (dws->dma_mapped) {
368 ret = dws->dma_ops->dma_setup(dws, transfer);
369 if (ret < 0) {
370 spi_enable_chip(dws, 1);
371 return ret;
372 }
373 } else if (!chip->poll_mode) {
374 txlevel = min_t(u16, dws->fifo_len / 2, dws->len / dws->n_bytes);
375 dw_writel(dws, DW_SPI_TXFLTR, txlevel);
376
377 /* Set the interrupt mask */
378 imask |= SPI_INT_TXEI | SPI_INT_TXOI |
379 SPI_INT_RXUI | SPI_INT_RXOI;
380 spi_umask_intr(dws, imask);
381
382 dws->transfer_handler = interrupt_transfer;
383 }
384
385 spi_enable_chip(dws, 1);
386
387 if (dws->dma_mapped)
388 return dws->dma_ops->dma_transfer(dws, transfer);
389
390 if (chip->poll_mode)
391 return poll_transfer(dws);
392
393 return 1;
394}
395
396static void dw_spi_handle_err(struct spi_master *master,
397 struct spi_message *msg)
398{
399 struct dw_spi *dws = spi_master_get_devdata(master);
400
401 if (dws->dma_mapped)
402 dws->dma_ops->dma_stop(dws);
403
404 spi_reset_chip(dws);
405}
406
407/* This may be called twice for each spi dev */
408static int dw_spi_setup(struct spi_device *spi)
409{
410 struct dw_spi_chip *chip_info = NULL;
411 struct chip_data *chip;
412 int ret;
413
414 /* Only alloc on first setup */
415 chip = spi_get_ctldata(spi);
416 if (!chip) {
417 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
418 if (!chip)
419 return -ENOMEM;
420 spi_set_ctldata(spi, chip);
421 }
422
423 /*
424 * Protocol drivers may change the chip settings, so...
425 * if chip_info exists, use it
426 */
427 chip_info = spi->controller_data;
428
429 /* chip_info doesn't always exist */
430 if (chip_info) {
431 if (chip_info->cs_control)
432 chip->cs_control = chip_info->cs_control;
433
434 chip->poll_mode = chip_info->poll_mode;
435 chip->type = chip_info->type;
436 }
437
438 chip->tmode = SPI_TMOD_TR;
439
440 if (gpio_is_valid(spi->cs_gpio)) {
441 ret = gpio_direction_output(spi->cs_gpio,
442 !(spi->mode & SPI_CS_HIGH));
443 if (ret)
444 return ret;
445 }
446
447 return 0;
448}
449
450static void dw_spi_cleanup(struct spi_device *spi)
451{
452 struct chip_data *chip = spi_get_ctldata(spi);
453
454 kfree(chip);
455 spi_set_ctldata(spi, NULL);
456}
457
458/* Restart the controller, disable all interrupts, clean rx fifo */
459static void spi_hw_init(struct device *dev, struct dw_spi *dws)
460{
461 spi_reset_chip(dws);
462
463 /*
464 * Try to detect the FIFO depth if not set by interface driver,
465 * the depth could be from 2 to 256 from HW spec
466 */
467 if (!dws->fifo_len) {
468 u32 fifo;
469
470 for (fifo = 1; fifo < 256; fifo++) {
471 dw_writel(dws, DW_SPI_TXFLTR, fifo);
472 if (fifo != dw_readl(dws, DW_SPI_TXFLTR))
473 break;
474 }
475 dw_writel(dws, DW_SPI_TXFLTR, 0);
476
477 dws->fifo_len = (fifo == 1) ? 0 : fifo;
478 dev_dbg(dev, "Detected FIFO size: %u bytes\n", dws->fifo_len);
479 }
480}
481
482int dw_spi_add_host(struct device *dev, struct dw_spi *dws)
483{
484 struct spi_master *master;
485 int ret;
486
487 BUG_ON(dws == NULL);
488
489 master = spi_alloc_master(dev, 0);
490 if (!master)
491 return -ENOMEM;
492
493 dws->master = master;
494 dws->type = SSI_MOTO_SPI;
495 dws->dma_inited = 0;
496 dws->dma_addr = (dma_addr_t)(dws->paddr + DW_SPI_DR);
497 spin_lock_init(&dws->buf_lock);
498
499 spi_master_set_devdata(master, dws);
500
501 ret = request_irq(dws->irq, dw_spi_irq, IRQF_SHARED, dev_name(dev),
502 master);
503 if (ret < 0) {
504 dev_err(dev, "can not get IRQ\n");
505 goto err_free_master;
506 }
507
508 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
509 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
510 master->bus_num = dws->bus_num;
511 master->num_chipselect = dws->num_cs;
512 master->setup = dw_spi_setup;
513 master->cleanup = dw_spi_cleanup;
514 master->set_cs = dw_spi_set_cs;
515 master->transfer_one = dw_spi_transfer_one;
516 master->handle_err = dw_spi_handle_err;
517 master->max_speed_hz = dws->max_freq;
518 master->dev.of_node = dev->of_node;
519 master->flags = SPI_MASTER_GPIO_SS;
520
521 /* Basic HW init */
522 spi_hw_init(dev, dws);
523
524 if (dws->dma_ops && dws->dma_ops->dma_init) {
525 ret = dws->dma_ops->dma_init(dws);
526 if (ret) {
527 dev_warn(dev, "DMA init failed\n");
528 dws->dma_inited = 0;
529 } else {
530 master->can_dma = dws->dma_ops->can_dma;
531 master->flags |= SPI_CONTROLLER_MUST_TX;
532 }
533 }
534
535 ret = spi_register_master(master);
536 if (ret) {
537 dev_err(&master->dev, "problem registering spi master\n");
538 goto err_dma_exit;
539 }
540
541 dw_spi_debugfs_init(dws);
542 return 0;
543
544err_dma_exit:
545 if (dws->dma_ops && dws->dma_ops->dma_exit)
546 dws->dma_ops->dma_exit(dws);
547 spi_enable_chip(dws, 0);
548 free_irq(dws->irq, master);
549err_free_master:
550 spi_master_put(master);
551 return ret;
552}
553EXPORT_SYMBOL_GPL(dw_spi_add_host);
554
555void dw_spi_remove_host(struct dw_spi *dws)
556{
557 dw_spi_debugfs_remove(dws);
558
559 spi_unregister_master(dws->master);
560
561 if (dws->dma_ops && dws->dma_ops->dma_exit)
562 dws->dma_ops->dma_exit(dws);
563
564 spi_shutdown_chip(dws);
565
566 free_irq(dws->irq, dws->master);
567}
568EXPORT_SYMBOL_GPL(dw_spi_remove_host);
569
570int dw_spi_suspend_host(struct dw_spi *dws)
571{
572 int ret;
573
574 ret = spi_master_suspend(dws->master);
575 if (ret)
576 return ret;
577
578 spi_shutdown_chip(dws);
579 return 0;
580}
581EXPORT_SYMBOL_GPL(dw_spi_suspend_host);
582
583int dw_spi_resume_host(struct dw_spi *dws)
584{
585 int ret;
586
587 spi_hw_init(&dws->master->dev, dws);
588 ret = spi_master_resume(dws->master);
589 if (ret)
590 dev_err(&dws->master->dev, "fail to start queue (%d)\n", ret);
591 return ret;
592}
593EXPORT_SYMBOL_GPL(dw_spi_resume_host);
594
595MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
596MODULE_DESCRIPTION("Driver for DesignWare SPI controller core");
597MODULE_LICENSE("GPL v2");