1 From b3077fb9ac2668fde05478fa0ef6197fecda87b6 Mon Sep 17 00:00:00 2001
2 From: Luke Wren <wren6991@gmail.com>
3 Date: Sat, 5 Sep 2015 01:14:45 +0100
4 Subject: [PATCH] Add SMI driver
6 Signed-off-by: Luke Wren <wren6991@gmail.com>
8 MISC: bcm2835: smi: use clock manager and fix reload issues
10 Use clock manager instead of self-made clockmanager.
12 Also fix some error paths that showd up during development
13 (especially missing release of dma resources on rmmod)
15 Signed-off-by: Martin Sperl <kernel@martin.sperl.org>
17 bcm2835_smi: re-add dereference to fix DMA transfers
19 bcm2835_smi_dev: Fix handling of word-odd lengths
21 The read and write functions did not use the correct pointer offset
22 when dealing with an odd number of bytes after a DMA transfer. Also,
23 only handle the remaining odd bytes if the DMA transfer completed
26 Submitted-by: @madimario (GitHub)
27 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
29 misc: bcm2835_smi: Use proper enum types for dma_{,un}map_single()
33 drivers/misc/bcm2835_smi.c:692:4: warning: implicit conversion from enumeration type 'enum dma_transfer_direction' to different enumeration type 'enum dma_data_direction' [-Wenum-conversion]
36 ./include/linux/dma-mapping.h:406:66: note: expanded from macro 'dma_map_single'
37 #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
38 ~~~~~~~~~~~~~~~~~~~~ ^
39 drivers/misc/bcm2835_smi.c:705:35: warning: implicit conversion from enumeration type 'enum dma_transfer_direction' to different enumeration type 'enum dma_data_direction' [-Wenum-conversion]
40 (inst->dev, phy_addr, n_bytes, DMA_MEM_TO_DEV);
41 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
42 ./include/linux/dma-mapping.h:407:70: note: expanded from macro 'dma_unmap_single'
43 #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
44 ~~~~~~~~~~~~~~~~~~~~~~ ^
45 drivers/misc/bcm2835_smi.c:751:12: warning: implicit conversion from enumeration type 'enum dma_transfer_direction' to different enumeration type 'enum dma_data_direction' [-Wenum-conversion]
48 ./include/linux/dma-mapping.h:406:66: note: expanded from macro 'dma_map_single'
49 #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
50 ~~~~~~~~~~~~~~~~~~~~ ^
51 drivers/misc/bcm2835_smi.c:761:50: warning: implicit conversion from enumeration type 'enum dma_transfer_direction' to different enumeration type 'enum dma_data_direction' [-Wenum-conversion]
52 dma_unmap_single(inst->dev, phy_addr, n_bytes, DMA_DEV_TO_MEM);
53 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
54 ./include/linux/dma-mapping.h:407:70: note: expanded from macro 'dma_unmap_single'
55 #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
56 ~~~~~~~~~~~~~~~~~~~~~~ ^
59 Use the proper enumerated type to clear up the warning. There is not
60 actually a bug here because the enumerated types have the same integer
63 DMA_MEM_TO_DEV = DMA_TO_DEVICE = 1
64 DMA_DEV_TO_MEM = DMA_FROM_DEVICE = 2
66 Fixes: 93254d0f7bc8 ("Add SMI driver")
67 Signed-off-by: Nathan Chancellor <nathan@kernel.org>
69 .../bindings/misc/brcm,bcm2835-smi-dev.txt | 17 +
70 .../bindings/misc/brcm,bcm2835-smi.txt | 48 +
71 drivers/char/broadcom/Kconfig | 9 +
72 drivers/char/broadcom/Makefile | 1 +
73 drivers/char/broadcom/bcm2835_smi_dev.c | 409 ++++++++
74 drivers/misc/Kconfig | 8 +
75 drivers/misc/Makefile | 1 +
76 drivers/misc/bcm2835_smi.c | 955 ++++++++++++++++++
77 include/linux/broadcom/bcm2835_smi.h | 391 +++++++
78 9 files changed, 1839 insertions(+)
79 create mode 100644 Documentation/devicetree/bindings/misc/brcm,bcm2835-smi-dev.txt
80 create mode 100644 Documentation/devicetree/bindings/misc/brcm,bcm2835-smi.txt
81 create mode 100644 drivers/char/broadcom/bcm2835_smi_dev.c
82 create mode 100644 drivers/misc/bcm2835_smi.c
83 create mode 100644 include/linux/broadcom/bcm2835_smi.h
86 +++ b/Documentation/devicetree/bindings/misc/brcm,bcm2835-smi-dev.txt
88 +* Broadcom BCM2835 SMI character device driver.
90 +SMI or secondary memory interface is a peripheral specific to certain Broadcom
91 +SOCs, and is helpful for talking to things like parallel-interface displays
92 +and NAND flashes (in fact, most things with a parallel register interface).
94 +This driver adds a character device which provides a user-space interface to
95 +an instance of the SMI driver.
98 +- compatible: "brcm,bcm2835-smi-dev"
99 +- smi_handle: a phandle to the smi node.
101 +Optional properties:
106 +++ b/Documentation/devicetree/bindings/misc/brcm,bcm2835-smi.txt
108 +* Broadcom BCM2835 SMI driver.
110 +SMI or secondary memory interface is a peripheral specific to certain Broadcom
111 +SOCs, and is helpful for talking to things like parallel-interface displays
112 +and NAND flashes (in fact, most things with a parallel register interface).
114 +Required properties:
115 +- compatible: "brcm,bcm2835-smi"
116 +- reg: Should contain location and length of SMI registers and SMI clkman regs
117 +- interrupts: *the* SMI interrupt.
118 +- pinctrl-names: should be "default".
119 +- pinctrl-0: the phandle of the gpio pin node.
120 +- brcm,smi-clock-source: the clock source for clkman
121 +- brcm,smi-clock-divisor: the integer clock divisor for clkman
122 +- dmas: the dma controller phandle and the DREQ number (4 on a 2835)
123 +- dma-names: the name used by the driver to request its channel.
126 +Optional properties:
131 +8 data pin configuration:
134 + compatible = "brcm,bcm2835-smi";
135 + reg = <0x7e600000 0x44>, <0x7e1010b0 0x8>;
136 + interrupts = <2 16>;
137 + pinctrl-names = "default";
138 + pinctrl-0 = <&smi_pins>;
139 + brcm,smi-clock-source = <6>;
140 + brcm,smi-clock-divisor = <4>;
142 + dma-names = "rx-tx";
147 +smi_pins: smi_pins {
148 + brcm,pins = <2 3 4 5 6 7 8 9 10 11 12 13 14 15>;
150 + brcm,function = <5 5 5 5 5 5 5 5 5 5 5 5 5 5>;
151 + /* /CS, /WE and /OE are pulled high, as they are
152 + generally active low signals */
153 + brcm,pull = <2 2 2 2 2 2 0 0 0 0 0 0 0 0>;
156 --- a/drivers/char/broadcom/Kconfig
157 +++ b/drivers/char/broadcom/Kconfig
158 @@ -24,3 +24,12 @@ config BCM2835_DEVGPIOMEM
159 Provides users with root-free access to the GPIO registers
160 on the 2835. Calling mmap(/dev/gpiomem) will map the GPIO
161 register page to the user's pointer.
163 +config BCM2835_SMI_DEV
164 + tristate "Character device driver for BCM2835 Secondary Memory Interface"
165 + depends on BCM2835_SMI
168 + This driver provides a character device interface (ioctl + read/write) to
169 + Broadcom's Secondary Memory interface. The low-level functionality is provided
170 + by the SMI driver itself.
171 --- a/drivers/char/broadcom/Makefile
172 +++ b/drivers/char/broadcom/Makefile
174 obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
175 obj-$(CONFIG_BCM2835_DEVGPIOMEM)+= bcm2835-gpiomem.o
176 +obj-$(CONFIG_BCM2835_SMI_DEV) += bcm2835_smi_dev.o
178 +++ b/drivers/char/broadcom/bcm2835_smi_dev.c
181 + * Character device driver for Broadcom Secondary Memory Interface
183 + * Written by Luke Wren <luke@raspberrypi.org>
184 + * Copyright (c) 2015, Raspberry Pi (Trading) Ltd.
186 + * Redistribution and use in source and binary forms, with or without
187 + * modification, are permitted provided that the following conditions
189 + * 1. Redistributions of source code must retain the above copyright
190 + * notice, this list of conditions, and the following disclaimer,
191 + * without modification.
192 + * 2. Redistributions in binary form must reproduce the above copyright
193 + * notice, this list of conditions and the following disclaimer in the
194 + * documentation and/or other materials provided with the distribution.
195 + * 3. The names of the above-listed copyright holders may not be used
196 + * to endorse or promote products derived from this software without
197 + * specific prior written permission.
199 + * ALTERNATIVELY, this software may be distributed under the terms of the
200 + * GNU General Public License ("GPL") version 2, as published by the Free
201 + * Software Foundation.
203 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
204 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
205 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
206 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
207 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
208 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
209 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
210 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
211 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
212 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
213 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
216 +#include <linux/kernel.h>
217 +#include <linux/module.h>
218 +#include <linux/of.h>
219 +#include <linux/platform_device.h>
220 +#include <linux/slab.h>
221 +#include <linux/mm.h>
222 +#include <linux/pagemap.h>
223 +#include <linux/fs.h>
224 +#include <linux/cdev.h>
225 +#include <linux/fs.h>
227 +#include <linux/broadcom/bcm2835_smi.h>
229 +#define DEVICE_NAME "bcm2835-smi-dev"
230 +#define DRIVER_NAME "smi-dev-bcm2835"
231 +#define DEVICE_MINOR 0
233 +static struct cdev bcm2835_smi_cdev;
234 +static dev_t bcm2835_smi_devid;
235 +static struct class *bcm2835_smi_class;
236 +static struct device *bcm2835_smi_dev;
238 +struct bcm2835_smi_dev_instance {
239 + struct device *dev;
242 +static struct bcm2835_smi_instance *smi_inst;
243 +static struct bcm2835_smi_dev_instance *inst;
245 +static const char *const ioctl_names[] = {
251 +/****************************************************************************
253 +* SMI chardev file ops
255 +***************************************************************************/
257 +bcm2835_smi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
261 + dev_info(inst->dev, "serving ioctl...");
264 + case BCM2835_SMI_IOC_GET_SETTINGS:{
265 + struct smi_settings *settings;
267 + dev_info(inst->dev, "Reading SMI settings to user.");
268 + settings = bcm2835_smi_get_settings_from_regs(smi_inst);
269 + if (copy_to_user((void *)arg, settings,
270 + sizeof(struct smi_settings)))
271 + dev_err(inst->dev, "settings copy failed.");
274 + case BCM2835_SMI_IOC_WRITE_SETTINGS:{
275 + struct smi_settings *settings;
277 + dev_info(inst->dev, "Setting user's SMI settings.");
278 + settings = bcm2835_smi_get_settings_from_regs(smi_inst);
279 + if (copy_from_user(settings, (void *)arg,
280 + sizeof(struct smi_settings)))
281 + dev_err(inst->dev, "settings copy failed.");
283 + bcm2835_smi_set_regs_from_settings(smi_inst);
286 + case BCM2835_SMI_IOC_ADDRESS:
287 + dev_info(inst->dev, "SMI address set: 0x%02x", (int)arg);
288 + bcm2835_smi_set_address(smi_inst, arg);
291 + dev_err(inst->dev, "invalid ioctl cmd: %d", cmd);
299 +static int bcm2835_smi_open(struct inode *inode, struct file *file)
301 + int dev = iminor(inode);
303 + dev_dbg(inst->dev, "SMI device opened.");
305 + if (dev != DEVICE_MINOR) {
307 + "bcm2835_smi_release: Unknown minor device: %d",
315 +static int bcm2835_smi_release(struct inode *inode, struct file *file)
317 + int dev = iminor(inode);
319 + if (dev != DEVICE_MINOR) {
321 + "bcm2835_smi_release: Unknown minor device %d", dev);
328 +static ssize_t dma_bounce_user(
329 + enum dma_transfer_direction dma_dir,
330 + char __user *user_ptr,
332 + struct bcm2835_smi_bounce_info *bounce)
336 + int count_left = count;
338 + while (count_left) {
342 + /* Wait for current chunk to complete: */
343 + if (down_timeout(&bounce->callback_sem,
344 + msecs_to_jiffies(1000))) {
345 + dev_err(inst->dev, "DMA bounce timed out");
346 + count -= (count_left);
350 + if (bounce->callback_sem.count >= DMA_BOUNCE_BUFFER_COUNT - 1)
351 + dev_err(inst->dev, "WARNING: Ring buffer overflow");
352 + chunk_size = count_left > DMA_BOUNCE_BUFFER_SIZE ?
353 + DMA_BOUNCE_BUFFER_SIZE : count_left;
354 + buf = bounce->buffer[chunk_no % DMA_BOUNCE_BUFFER_COUNT];
355 + if (dma_dir == DMA_DEV_TO_MEM)
356 + rv = copy_to_user(user_ptr, buf, chunk_size);
358 + rv = copy_from_user(buf, user_ptr, chunk_size);
360 + dev_err(inst->dev, "copy_*_user() failed!: %d", rv);
361 + user_ptr += chunk_size;
362 + count_left -= chunk_size;
369 +bcm2835_read_file(struct file *f, char __user *user_ptr,
370 + size_t count, loff_t *offs)
373 + size_t count_check;
375 + dev_dbg(inst->dev, "User reading %zu bytes from SMI.", count);
376 + /* We don't want to DMA a number of bytes % 4 != 0 (32 bit FIFO) */
377 + if (count > DMA_THRESHOLD_BYTES)
378 + odd_bytes = count & 0x3;
381 + count -= odd_bytes;
382 + count_check = count;
384 + struct bcm2835_smi_bounce_info *bounce;
386 + count = bcm2835_smi_user_dma(smi_inst,
387 + DMA_DEV_TO_MEM, user_ptr, count,
390 + count = dma_bounce_user(DMA_DEV_TO_MEM, user_ptr,
393 + if (odd_bytes && (count == count_check)) {
394 + /* Read from FIFO directly if not using DMA */
395 + uint8_t buf[DMA_THRESHOLD_BYTES];
396 + unsigned long bytes_not_transferred;
398 + bcm2835_smi_read_buf(smi_inst, buf, odd_bytes);
399 + bytes_not_transferred = copy_to_user(user_ptr + count, buf, odd_bytes);
400 + if (bytes_not_transferred)
401 + dev_err(inst->dev, "copy_to_user() failed.");
402 + count += odd_bytes - bytes_not_transferred;
408 +bcm2835_write_file(struct file *f, const char __user *user_ptr,
409 + size_t count, loff_t *offs)
412 + size_t count_check;
414 + dev_dbg(inst->dev, "User writing %zu bytes to SMI.", count);
415 + if (count > DMA_THRESHOLD_BYTES)
416 + odd_bytes = count & 0x3;
419 + count -= odd_bytes;
420 + count_check = count;
422 + struct bcm2835_smi_bounce_info *bounce;
424 + count = bcm2835_smi_user_dma(smi_inst,
425 + DMA_MEM_TO_DEV, (char __user *)user_ptr, count,
428 + count = dma_bounce_user(DMA_MEM_TO_DEV,
429 + (char __user *)user_ptr,
432 + if (odd_bytes && (count == count_check)) {
433 + uint8_t buf[DMA_THRESHOLD_BYTES];
434 + unsigned long bytes_not_transferred;
436 + bytes_not_transferred = copy_from_user(buf, user_ptr + count, odd_bytes);
437 + if (bytes_not_transferred)
438 + dev_err(inst->dev, "copy_from_user() failed.");
440 + bcm2835_smi_write_buf(smi_inst, buf, odd_bytes);
441 + count += odd_bytes - bytes_not_transferred;
446 +static const struct file_operations
447 +bcm2835_smi_fops = {
448 + .owner = THIS_MODULE,
449 + .unlocked_ioctl = bcm2835_smi_ioctl,
450 + .open = bcm2835_smi_open,
451 + .release = bcm2835_smi_release,
452 + .read = bcm2835_read_file,
453 + .write = bcm2835_write_file,
457 +/****************************************************************************
459 +* bcm2835_smi_probe - called when the driver is loaded.
461 +***************************************************************************/
463 +static int bcm2835_smi_dev_probe(struct platform_device *pdev)
467 + struct device *dev = &pdev->dev;
468 + struct device_node *node = dev->of_node, *smi_node;
471 + dev_err(dev, "No device tree node supplied!");
475 + smi_node = of_parse_phandle(node, "smi_handle", 0);
478 + dev_err(dev, "No such property: smi_handle");
482 + smi_inst = bcm2835_smi_get(smi_node);
485 + return -EPROBE_DEFER;
487 + /* Allocate buffers and instance data */
489 + inst = devm_kzalloc(dev, sizeof(*inst), GFP_KERNEL);
496 + /* Create character device entries */
498 + err = alloc_chrdev_region(&bcm2835_smi_devid,
499 + DEVICE_MINOR, 1, DEVICE_NAME);
501 + dev_err(inst->dev, "unable to allocate device number");
504 + cdev_init(&bcm2835_smi_cdev, &bcm2835_smi_fops);
505 + bcm2835_smi_cdev.owner = THIS_MODULE;
506 + err = cdev_add(&bcm2835_smi_cdev, bcm2835_smi_devid, 1);
508 + dev_err(inst->dev, "unable to register device");
510 + goto failed_cdev_add;
513 + /* Create sysfs entries */
515 + bcm2835_smi_class = class_create(THIS_MODULE, DEVICE_NAME);
516 + ptr_err = bcm2835_smi_class;
517 + if (IS_ERR(ptr_err))
518 + goto failed_class_create;
520 + bcm2835_smi_dev = device_create(bcm2835_smi_class, NULL,
521 + bcm2835_smi_devid, NULL,
523 + ptr_err = bcm2835_smi_dev;
524 + if (IS_ERR(ptr_err))
525 + goto failed_device_create;
527 + dev_info(inst->dev, "initialised");
531 +failed_device_create:
532 + class_destroy(bcm2835_smi_class);
533 +failed_class_create:
534 + cdev_del(&bcm2835_smi_cdev);
535 + err = PTR_ERR(ptr_err);
537 + unregister_chrdev_region(bcm2835_smi_devid, 1);
538 + dev_err(dev, "could not load bcm2835_smi_dev");
542 +/****************************************************************************
544 +* bcm2835_smi_remove - called when the driver is unloaded.
546 +***************************************************************************/
548 +static int bcm2835_smi_dev_remove(struct platform_device *pdev)
550 + device_destroy(bcm2835_smi_class, bcm2835_smi_devid);
551 + class_destroy(bcm2835_smi_class);
552 + cdev_del(&bcm2835_smi_cdev);
553 + unregister_chrdev_region(bcm2835_smi_devid, 1);
555 + dev_info(inst->dev, "SMI character dev removed - OK");
559 +/****************************************************************************
561 +* Register the driver with device tree
563 +***************************************************************************/
565 +static const struct of_device_id bcm2835_smi_dev_of_match[] = {
566 + {.compatible = "brcm,bcm2835-smi-dev",},
567 + { /* sentinel */ },
570 +MODULE_DEVICE_TABLE(of, bcm2835_smi_dev_of_match);
572 +static struct platform_driver bcm2835_smi_dev_driver = {
573 + .probe = bcm2835_smi_dev_probe,
574 + .remove = bcm2835_smi_dev_remove,
576 + .name = DRIVER_NAME,
577 + .owner = THIS_MODULE,
578 + .of_match_table = bcm2835_smi_dev_of_match,
582 +module_platform_driver(bcm2835_smi_dev_driver);
584 +MODULE_ALIAS("platform:smi-dev-bcm2835");
585 +MODULE_LICENSE("GPL");
587 + "Character device driver for BCM2835's secondary memory interface");
588 +MODULE_AUTHOR("Luke Wren <luke@raspberrypi.org>");
589 --- a/drivers/misc/Kconfig
590 +++ b/drivers/misc/Kconfig
591 @@ -9,6 +9,14 @@ config SENSORS_LIS3LV02D
596 + tristate "Broadcom 283x Secondary Memory Interface driver"
597 + depends on ARCH_BCM2835
600 + Driver for enabling and using Broadcom's Secondary/Slow Memory Interface.
601 + Appears as /dev/bcm2835_smi. For ioctl interface see drivers/misc/bcm2835_smi.h
604 tristate "Analog Devices Digital Potentiometers"
605 depends on (I2C || SPI) && SYSFS
606 --- a/drivers/misc/Makefile
607 +++ b/drivers/misc/Makefile
608 @@ -9,6 +9,7 @@ obj-$(CONFIG_AD525X_DPOT) += ad525x_dpot
609 obj-$(CONFIG_AD525X_DPOT_I2C) += ad525x_dpot-i2c.o
610 obj-$(CONFIG_AD525X_DPOT_SPI) += ad525x_dpot-spi.o
611 obj-$(CONFIG_ATMEL_SSC) += atmel-ssc.o
612 +obj-$(CONFIG_BCM2835_SMI) += bcm2835_smi.o
613 obj-$(CONFIG_DUMMY_IRQ) += dummy-irq.o
614 obj-$(CONFIG_ICS932S401) += ics932s401.o
615 obj-$(CONFIG_LKDTM) += lkdtm/
617 +++ b/drivers/misc/bcm2835_smi.c
620 + * Broadcom Secondary Memory Interface driver
622 + * Written by Luke Wren <luke@raspberrypi.org>
623 + * Copyright (c) 2015, Raspberry Pi (Trading) Ltd.
625 + * Redistribution and use in source and binary forms, with or without
626 + * modification, are permitted provided that the following conditions
628 + * 1. Redistributions of source code must retain the above copyright
629 + * notice, this list of conditions, and the following disclaimer,
630 + * without modification.
631 + * 2. Redistributions in binary form must reproduce the above copyright
632 + * notice, this list of conditions and the following disclaimer in the
633 + * documentation and/or other materials provided with the distribution.
634 + * 3. The names of the above-listed copyright holders may not be used
635 + * to endorse or promote products derived from this software without
636 + * specific prior written permission.
638 + * ALTERNATIVELY, this software may be distributed under the terms of the
639 + * GNU General Public License ("GPL") version 2, as published by the Free
640 + * Software Foundation.
642 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
643 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
644 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
645 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
646 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
647 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
648 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
649 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
650 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
651 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
652 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
655 +#include <linux/clk.h>
656 +#include <linux/kernel.h>
657 +#include <linux/module.h>
658 +#include <linux/of.h>
659 +#include <linux/platform_device.h>
660 +#include <linux/of_address.h>
661 +#include <linux/of_platform.h>
662 +#include <linux/mm.h>
663 +#include <linux/slab.h>
664 +#include <linux/pagemap.h>
665 +#include <linux/dma-mapping.h>
666 +#include <linux/dmaengine.h>
667 +#include <linux/semaphore.h>
668 +#include <linux/spinlock.h>
669 +#include <linux/io.h>
671 +#define BCM2835_SMI_IMPLEMENTATION
672 +#include <linux/broadcom/bcm2835_smi.h>
674 +#define DRIVER_NAME "smi-bcm2835"
676 +#define N_PAGES_FROM_BYTES(n) ((n + PAGE_SIZE-1) / PAGE_SIZE)
678 +#define DMA_WRITE_TO_MEM true
679 +#define DMA_READ_FROM_MEM false
681 +struct bcm2835_smi_instance {
682 + struct device *dev;
683 + struct smi_settings settings;
684 + __iomem void *smi_regs_ptr;
685 + dma_addr_t smi_regs_busaddr;
687 + struct dma_chan *dma_chan;
688 + struct dma_slave_config dma_config;
690 + struct bcm2835_smi_bounce_info bounce;
692 + struct scatterlist buffer_sgl;
696 + /* Sometimes we are called into in an atomic context (e.g. by
697 + JFFS2 + MTD) so we can't use a mutex */
698 + spinlock_t transaction_lock;
701 +/****************************************************************************
703 +* SMI peripheral setup
705 +***************************************************************************/
707 +static inline void write_smi_reg(struct bcm2835_smi_instance *inst,
708 + u32 val, unsigned reg)
710 + writel(val, inst->smi_regs_ptr + reg);
713 +static inline u32 read_smi_reg(struct bcm2835_smi_instance *inst, unsigned reg)
715 + return readl(inst->smi_regs_ptr + reg);
718 +/* Token-paste macro for e.g SMIDSR_RSTROBE -> value of SMIDSR_RSTROBE_MASK */
719 +#define _CONCAT(x, y) x##y
720 +#define CONCAT(x, y) _CONCAT(x, y)
722 +#define SET_BIT_FIELD(dest, field, bits) ((dest) = \
723 + ((dest) & ~CONCAT(field, _MASK)) | (((bits) << CONCAT(field, _OFFS))& \
724 + CONCAT(field, _MASK)))
725 +#define GET_BIT_FIELD(src, field) (((src) & \
726 + CONCAT(field, _MASK)) >> CONCAT(field, _OFFS))
728 +static void smi_dump_context_labelled(struct bcm2835_smi_instance *inst,
731 + dev_err(inst->dev, "SMI context dump: %s", label);
732 + dev_err(inst->dev, "SMICS: 0x%08x", read_smi_reg(inst, SMICS));
733 + dev_err(inst->dev, "SMIL: 0x%08x", read_smi_reg(inst, SMIL));
734 + dev_err(inst->dev, "SMIDSR: 0x%08x", read_smi_reg(inst, SMIDSR0));
735 + dev_err(inst->dev, "SMIDSW: 0x%08x", read_smi_reg(inst, SMIDSW0));
736 + dev_err(inst->dev, "SMIDC: 0x%08x", read_smi_reg(inst, SMIDC));
737 + dev_err(inst->dev, "SMIFD: 0x%08x", read_smi_reg(inst, SMIFD));
738 + dev_err(inst->dev, " ");
741 +static inline void smi_dump_context(struct bcm2835_smi_instance *inst)
743 + smi_dump_context_labelled(inst, "");
746 +static void smi_get_default_settings(struct bcm2835_smi_instance *inst)
748 + struct smi_settings *settings = &inst->settings;
750 + settings->data_width = SMI_WIDTH_16BIT;
751 + settings->pack_data = true;
753 + settings->read_setup_time = 1;
754 + settings->read_hold_time = 1;
755 + settings->read_pace_time = 1;
756 + settings->read_strobe_time = 3;
758 + settings->write_setup_time = settings->read_setup_time;
759 + settings->write_hold_time = settings->read_hold_time;
760 + settings->write_pace_time = settings->read_pace_time;
761 + settings->write_strobe_time = settings->read_strobe_time;
763 + settings->dma_enable = true;
764 + settings->dma_passthrough_enable = false;
765 + settings->dma_read_thresh = 0x01;
766 + settings->dma_write_thresh = 0x3f;
767 + settings->dma_panic_read_thresh = 0x20;
768 + settings->dma_panic_write_thresh = 0x20;
771 +void bcm2835_smi_set_regs_from_settings(struct bcm2835_smi_instance *inst)
773 + struct smi_settings *settings = &inst->settings;
774 + int smidsr_temp = 0, smidsw_temp = 0, smics_temp,
775 + smidcs_temp, smidc_temp = 0;
777 + spin_lock(&inst->transaction_lock);
779 + /* temporarily disable the peripheral: */
780 + smics_temp = read_smi_reg(inst, SMICS);
781 + write_smi_reg(inst, 0, SMICS);
782 + smidcs_temp = read_smi_reg(inst, SMIDCS);
783 + write_smi_reg(inst, 0, SMIDCS);
785 + if (settings->pack_data)
786 + smics_temp |= SMICS_PXLDAT;
788 + smics_temp &= ~SMICS_PXLDAT;
790 + SET_BIT_FIELD(smidsr_temp, SMIDSR_RWIDTH, settings->data_width);
791 + SET_BIT_FIELD(smidsr_temp, SMIDSR_RSETUP, settings->read_setup_time);
792 + SET_BIT_FIELD(smidsr_temp, SMIDSR_RHOLD, settings->read_hold_time);
793 + SET_BIT_FIELD(smidsr_temp, SMIDSR_RPACE, settings->read_pace_time);
794 + SET_BIT_FIELD(smidsr_temp, SMIDSR_RSTROBE, settings->read_strobe_time);
795 + write_smi_reg(inst, smidsr_temp, SMIDSR0);
797 + SET_BIT_FIELD(smidsw_temp, SMIDSW_WWIDTH, settings->data_width);
798 + if (settings->data_width == SMI_WIDTH_8BIT)
799 + smidsw_temp |= SMIDSW_WSWAP;
801 + smidsw_temp &= ~SMIDSW_WSWAP;
802 + SET_BIT_FIELD(smidsw_temp, SMIDSW_WSETUP, settings->write_setup_time);
803 + SET_BIT_FIELD(smidsw_temp, SMIDSW_WHOLD, settings->write_hold_time);
804 + SET_BIT_FIELD(smidsw_temp, SMIDSW_WPACE, settings->write_pace_time);
805 + SET_BIT_FIELD(smidsw_temp, SMIDSW_WSTROBE,
806 + settings->write_strobe_time);
807 + write_smi_reg(inst, smidsw_temp, SMIDSW0);
809 + SET_BIT_FIELD(smidc_temp, SMIDC_REQR, settings->dma_read_thresh);
810 + SET_BIT_FIELD(smidc_temp, SMIDC_REQW, settings->dma_write_thresh);
811 + SET_BIT_FIELD(smidc_temp, SMIDC_PANICR,
812 + settings->dma_panic_read_thresh);
813 + SET_BIT_FIELD(smidc_temp, SMIDC_PANICW,
814 + settings->dma_panic_write_thresh);
815 + if (settings->dma_passthrough_enable) {
816 + smidc_temp |= SMIDC_DMAP;
817 + smidsr_temp |= SMIDSR_RDREQ;
818 + write_smi_reg(inst, smidsr_temp, SMIDSR0);
819 + smidsw_temp |= SMIDSW_WDREQ;
820 + write_smi_reg(inst, smidsw_temp, SMIDSW0);
822 + smidc_temp &= ~SMIDC_DMAP;
823 + if (settings->dma_enable)
824 + smidc_temp |= SMIDC_DMAEN;
826 + smidc_temp &= ~SMIDC_DMAEN;
828 + write_smi_reg(inst, smidc_temp, SMIDC);
830 + /* re-enable (if was previously enabled) */
831 + write_smi_reg(inst, smics_temp, SMICS);
832 + write_smi_reg(inst, smidcs_temp, SMIDCS);
834 + spin_unlock(&inst->transaction_lock);
836 +EXPORT_SYMBOL(bcm2835_smi_set_regs_from_settings);
838 +struct smi_settings *bcm2835_smi_get_settings_from_regs
839 + (struct bcm2835_smi_instance *inst)
841 + struct smi_settings *settings = &inst->settings;
842 + int smidsr, smidsw, smidc;
844 + spin_lock(&inst->transaction_lock);
846 + smidsr = read_smi_reg(inst, SMIDSR0);
847 + smidsw = read_smi_reg(inst, SMIDSW0);
848 + smidc = read_smi_reg(inst, SMIDC);
850 + settings->pack_data = (read_smi_reg(inst, SMICS) & SMICS_PXLDAT) ?
853 + settings->data_width = GET_BIT_FIELD(smidsr, SMIDSR_RWIDTH);
854 + settings->read_setup_time = GET_BIT_FIELD(smidsr, SMIDSR_RSETUP);
855 + settings->read_hold_time = GET_BIT_FIELD(smidsr, SMIDSR_RHOLD);
856 + settings->read_pace_time = GET_BIT_FIELD(smidsr, SMIDSR_RPACE);
857 + settings->read_strobe_time = GET_BIT_FIELD(smidsr, SMIDSR_RSTROBE);
859 + settings->write_setup_time = GET_BIT_FIELD(smidsw, SMIDSW_WSETUP);
860 + settings->write_hold_time = GET_BIT_FIELD(smidsw, SMIDSW_WHOLD);
861 + settings->write_pace_time = GET_BIT_FIELD(smidsw, SMIDSW_WPACE);
862 + settings->write_strobe_time = GET_BIT_FIELD(smidsw, SMIDSW_WSTROBE);
864 + settings->dma_read_thresh = GET_BIT_FIELD(smidc, SMIDC_REQR);
865 + settings->dma_write_thresh = GET_BIT_FIELD(smidc, SMIDC_REQW);
866 + settings->dma_panic_read_thresh = GET_BIT_FIELD(smidc, SMIDC_PANICR);
867 + settings->dma_panic_write_thresh = GET_BIT_FIELD(smidc, SMIDC_PANICW);
868 + settings->dma_passthrough_enable = (smidc & SMIDC_DMAP) ? true : false;
869 + settings->dma_enable = (smidc & SMIDC_DMAEN) ? true : false;
871 + spin_unlock(&inst->transaction_lock);
875 +EXPORT_SYMBOL(bcm2835_smi_get_settings_from_regs);
877 +static inline void smi_set_address(struct bcm2835_smi_instance *inst,
878 + unsigned int address)
880 + int smia_temp = 0, smida_temp = 0;
882 + SET_BIT_FIELD(smia_temp, SMIA_ADDR, address);
883 + SET_BIT_FIELD(smida_temp, SMIDA_ADDR, address);
885 + /* Write to both address registers - user doesn't care whether we're
886 + doing programmed or direct transfers. */
887 + write_smi_reg(inst, smia_temp, SMIA);
888 + write_smi_reg(inst, smida_temp, SMIDA);
891 +static void smi_setup_regs(struct bcm2835_smi_instance *inst)
894 + dev_dbg(inst->dev, "Initialising SMI registers...");
895 + /* Disable the peripheral if already enabled */
896 + write_smi_reg(inst, 0, SMICS);
897 + write_smi_reg(inst, 0, SMIDCS);
899 + smi_get_default_settings(inst);
900 + bcm2835_smi_set_regs_from_settings(inst);
901 + smi_set_address(inst, 0);
903 + write_smi_reg(inst, read_smi_reg(inst, SMICS) | SMICS_ENABLE, SMICS);
904 + write_smi_reg(inst, read_smi_reg(inst, SMIDCS) | SMIDCS_ENABLE,
908 +/****************************************************************************
910 +* Low-level SMI access functions
911 +* Other modules should use the exported higher-level functions e.g.
912 +* bcm2835_smi_write_buf() unless they have a good reason to use these
914 +***************************************************************************/
916 +static inline uint32_t smi_read_single_word(struct bcm2835_smi_instance *inst)
920 + write_smi_reg(inst, SMIDCS_ENABLE, SMIDCS);
921 + write_smi_reg(inst, SMIDCS_ENABLE | SMIDCS_START, SMIDCS);
922 + /* Make sure things happen in the right order...*/
924 + while (!(read_smi_reg(inst, SMIDCS) & SMIDCS_DONE) &&
927 + if (timeout < 10000)
928 + return read_smi_reg(inst, SMIDD);
931 + "SMI direct read timed out (is the clock set up correctly?)");
935 +static inline void smi_write_single_word(struct bcm2835_smi_instance *inst,
940 + write_smi_reg(inst, SMIDCS_ENABLE | SMIDCS_WRITE, SMIDCS);
941 + write_smi_reg(inst, data, SMIDD);
942 + write_smi_reg(inst, SMIDCS_ENABLE | SMIDCS_WRITE | SMIDCS_START,
945 + while (!(read_smi_reg(inst, SMIDCS) & SMIDCS_DONE) &&
948 + if (timeout >= 10000)
950 + "SMI direct write timed out (is the clock set up correctly?)");
953 +/* Initiates a programmed read into the read FIFO. It is up to the caller to
954 + * read data from the FIFO - either via paced DMA transfer,
955 + * or polling SMICS_RXD to check whether data is available.
956 + * SMICS_ACTIVE will go low upon completion. */
957 +static void smi_init_programmed_read(struct bcm2835_smi_instance *inst,
962 + /* Disable the peripheral: */
963 + smics_temp = read_smi_reg(inst, SMICS) & ~(SMICS_ENABLE | SMICS_WRITE);
964 + write_smi_reg(inst, smics_temp, SMICS);
965 + while (read_smi_reg(inst, SMICS) & SMICS_ENABLE)
968 + /* Program the transfer count: */
969 + write_smi_reg(inst, num_transfers, SMIL);
971 + /* re-enable and start: */
972 + smics_temp |= SMICS_ENABLE;
973 + write_smi_reg(inst, smics_temp, SMICS);
974 + smics_temp |= SMICS_CLEAR;
975 + /* Just to be certain: */
977 + while (read_smi_reg(inst, SMICS) & SMICS_ACTIVE)
979 + write_smi_reg(inst, smics_temp, SMICS);
980 + smics_temp |= SMICS_START;
981 + write_smi_reg(inst, smics_temp, SMICS);
984 +/* Initiates a programmed write sequence, using data from the write FIFO.
985 + * It is up to the caller to initiate a DMA transfer before calling,
986 + * or use another method to keep the write FIFO topped up.
987 + * SMICS_ACTIVE will go low upon completion.
989 +static void smi_init_programmed_write(struct bcm2835_smi_instance *inst,
994 + /* Disable the peripheral: */
995 + smics_temp = read_smi_reg(inst, SMICS) & ~SMICS_ENABLE;
996 + write_smi_reg(inst, smics_temp, SMICS);
997 + while (read_smi_reg(inst, SMICS) & SMICS_ENABLE)
1000 + /* Program the transfer count: */
1001 + write_smi_reg(inst, num_transfers, SMIL);
1003 + /* setup, re-enable and start: */
1004 + smics_temp |= SMICS_WRITE | SMICS_ENABLE;
1005 + write_smi_reg(inst, smics_temp, SMICS);
1006 + smics_temp |= SMICS_START;
1007 + write_smi_reg(inst, smics_temp, SMICS);
1010 +/* Initiate a read and then poll FIFO for data, reading out as it appears. */
1011 +static void smi_read_fifo(struct bcm2835_smi_instance *inst,
1012 + uint32_t *dest, int n_bytes)
1014 + if (read_smi_reg(inst, SMICS) & SMICS_RXD) {
1015 + smi_dump_context_labelled(inst,
1016 + "WARNING: read FIFO not empty at start of read call.");
1017 + while (read_smi_reg(inst, SMICS))
1021 + /* Dispatch the read: */
1022 + if (inst->settings.data_width == SMI_WIDTH_8BIT)
1023 + smi_init_programmed_read(inst, n_bytes);
1024 + else if (inst->settings.data_width == SMI_WIDTH_16BIT)
1025 + smi_init_programmed_read(inst, n_bytes / 2);
1027 + dev_err(inst->dev, "Unsupported data width for read.");
1031 + /* Poll FIFO to keep it empty */
1032 + while (!(read_smi_reg(inst, SMICS) & SMICS_DONE))
1033 + if (read_smi_reg(inst, SMICS) & SMICS_RXD)
1034 + *dest++ = read_smi_reg(inst, SMID);
1036 + /* Ensure that the FIFO is emptied */
1037 + if (read_smi_reg(inst, SMICS) & SMICS_RXD) {
1040 + fifo_count = GET_BIT_FIELD(read_smi_reg(inst, SMIFD),
1042 + while (fifo_count--)
1043 + *dest++ = read_smi_reg(inst, SMID);
1046 + if (!(read_smi_reg(inst, SMICS) & SMICS_DONE))
1047 + smi_dump_context_labelled(inst,
1048 + "WARNING: transaction finished but done bit not set.");
1050 + if (read_smi_reg(inst, SMICS) & SMICS_RXD)
1051 + smi_dump_context_labelled(inst,
1052 + "WARNING: read FIFO not empty at end of read call.");
1056 +/* Initiate a write, and then keep the FIFO topped up. */
1057 +static void smi_write_fifo(struct bcm2835_smi_instance *inst,
1058 + uint32_t *src, int n_bytes)
1060 + int i, timeout = 0;
1062 + /* Empty FIFOs if not already so */
1063 + if (!(read_smi_reg(inst, SMICS) & SMICS_TXE)) {
1064 + smi_dump_context_labelled(inst,
1065 + "WARNING: write fifo not empty at start of write call.");
1066 + write_smi_reg(inst, read_smi_reg(inst, SMICS) | SMICS_CLEAR,
1070 + /* Initiate the transfer */
1071 + if (inst->settings.data_width == SMI_WIDTH_8BIT)
1072 + smi_init_programmed_write(inst, n_bytes);
1073 + else if (inst->settings.data_width == SMI_WIDTH_16BIT)
1074 + smi_init_programmed_write(inst, n_bytes / 2);
1076 + dev_err(inst->dev, "Unsupported data width for write.");
1079 + /* Fill the FIFO: */
1080 + for (i = 0; i < (n_bytes - 1) / 4 + 1; ++i) {
1081 + while (!(read_smi_reg(inst, SMICS) & SMICS_TXD))
1083 + write_smi_reg(inst, *src++, SMID);
1085 + /* Busy wait... */
1086 + while (!(read_smi_reg(inst, SMICS) & SMICS_DONE) && ++timeout <
1089 + if (timeout >= 1000000)
1090 + smi_dump_context_labelled(inst,
1091 + "Timed out on write operation!");
1092 + if (!(read_smi_reg(inst, SMICS) & SMICS_TXE))
1093 + smi_dump_context_labelled(inst,
1094 + "WARNING: FIFO not empty at end of write operation.");
1097 +/****************************************************************************
1099 +* SMI DMA operations
1101 +***************************************************************************/
1103 +/* Disable SMI and put it into the correct direction before doing DMA setup.
1104 + Stops spurious DREQs during setup. Peripheral is re-enabled by init_*() */
1105 +static void smi_disable(struct bcm2835_smi_instance *inst,
1106 + enum dma_transfer_direction direction)
1108 + int smics_temp = read_smi_reg(inst, SMICS) & ~SMICS_ENABLE;
1110 + if (direction == DMA_DEV_TO_MEM)
1111 + smics_temp &= ~SMICS_WRITE;
1113 + smics_temp |= SMICS_WRITE;
1114 + write_smi_reg(inst, smics_temp, SMICS);
1115 + while (read_smi_reg(inst, SMICS) & SMICS_ACTIVE)
1119 +static struct scatterlist *smi_scatterlist_from_buffer(
1120 + struct bcm2835_smi_instance *inst,
1123 + struct scatterlist *sg)
1125 + sg_init_table(sg, 1);
1126 + sg_dma_address(sg) = buf;
1127 + sg_dma_len(sg) = len;
1131 +static void smi_dma_callback_user_copy(void *param)
1133 + /* Notify the bottom half that a chunk is ready for user copy */
1134 + struct bcm2835_smi_instance *inst =
1135 + (struct bcm2835_smi_instance *)param;
1137 + up(&inst->bounce.callback_sem);
1140 +/* Creates a descriptor, assigns the given callback, and submits the
1141 + descriptor to dmaengine. Does not block - can queue up multiple
1142 + descriptors and then wait for them all to complete.
1143 + sg_len is the number of control blocks, NOT the number of bytes.
1144 + dir can be DMA_MEM_TO_DEV or DMA_DEV_TO_MEM.
1145 + callback can be NULL - in this case it is not called. */
1146 +static inline struct dma_async_tx_descriptor *smi_dma_submit_sgl(
1147 + struct bcm2835_smi_instance *inst,
1148 + struct scatterlist *sgl,
1150 + enum dma_transfer_direction dir,
1151 + dma_async_tx_callback callback)
1153 + struct dma_async_tx_descriptor *desc;
1155 + desc = dmaengine_prep_slave_sg(inst->dma_chan,
1159 + DMA_PREP_INTERRUPT | DMA_CTRL_ACK |
1162 + dev_err(inst->dev, "read_sgl: dma slave preparation failed!");
1163 + write_smi_reg(inst, read_smi_reg(inst, SMICS) & ~SMICS_ACTIVE,
1165 + while (read_smi_reg(inst, SMICS) & SMICS_ACTIVE)
1167 + write_smi_reg(inst, read_smi_reg(inst, SMICS) | SMICS_ACTIVE,
1171 + desc->callback = callback;
1172 + desc->callback_param = inst;
1173 + if (dmaengine_submit(desc) < 0)
1178 +/* NB this function blocks until the transfer is complete */
1180 +smi_dma_read_sgl(struct bcm2835_smi_instance *inst,
1181 + struct scatterlist *sgl, size_t sg_len, size_t n_bytes)
1183 + struct dma_async_tx_descriptor *desc;
1185 + /* Disable SMI and set to read before dispatching DMA - if SMI is in
1186 + * write mode and TX fifo is empty, it will generate a DREQ which may
1187 + * cause the read DMA to complete before the SMI read command is even
1188 + * dispatched! We want to dispatch DMA before SMI read so that reading
1189 + * is gapless, for logic analyser.
1192 + smi_disable(inst, DMA_DEV_TO_MEM);
1194 + desc = smi_dma_submit_sgl(inst, sgl, sg_len, DMA_DEV_TO_MEM, NULL);
1195 + dma_async_issue_pending(inst->dma_chan);
1197 + if (inst->settings.data_width == SMI_WIDTH_8BIT)
1198 + smi_init_programmed_read(inst, n_bytes);
1200 + smi_init_programmed_read(inst, n_bytes / 2);
1202 + if (dma_wait_for_async_tx(desc) == DMA_ERROR)
1203 + smi_dump_context_labelled(inst, "DMA timeout!");
1207 +smi_dma_write_sgl(struct bcm2835_smi_instance *inst,
1208 + struct scatterlist *sgl, size_t sg_len, size_t n_bytes)
1210 + struct dma_async_tx_descriptor *desc;
1212 + if (inst->settings.data_width == SMI_WIDTH_8BIT)
1213 + smi_init_programmed_write(inst, n_bytes);
1215 + smi_init_programmed_write(inst, n_bytes / 2);
1217 + desc = smi_dma_submit_sgl(inst, sgl, sg_len, DMA_MEM_TO_DEV, NULL);
1218 + dma_async_issue_pending(inst->dma_chan);
1220 + if (dma_wait_for_async_tx(desc) == DMA_ERROR)
1221 + smi_dump_context_labelled(inst, "DMA timeout!");
1223 + /* Wait for SMI to finish our writes */
1224 + while (!(read_smi_reg(inst, SMICS) & SMICS_DONE))
1228 +ssize_t bcm2835_smi_user_dma(
1229 + struct bcm2835_smi_instance *inst,
1230 + enum dma_transfer_direction dma_dir,
1231 + char __user *user_ptr, size_t count,
1232 + struct bcm2835_smi_bounce_info **bounce)
1234 + int chunk_no = 0, chunk_size, count_left = count;
1235 + struct scatterlist *sgl;
1236 + void (*init_trans_func)(struct bcm2835_smi_instance *, int);
1238 + spin_lock(&inst->transaction_lock);
1240 + if (dma_dir == DMA_DEV_TO_MEM)
1241 + init_trans_func = smi_init_programmed_read;
1243 + init_trans_func = smi_init_programmed_write;
1245 + smi_disable(inst, dma_dir);
1247 + sema_init(&inst->bounce.callback_sem, 0);
1249 + *bounce = &inst->bounce;
1250 + while (count_left) {
1251 + chunk_size = count_left > DMA_BOUNCE_BUFFER_SIZE ?
1252 + DMA_BOUNCE_BUFFER_SIZE : count_left;
1253 + if (chunk_size == DMA_BOUNCE_BUFFER_SIZE) {
1255 + &inst->bounce.sgl[chunk_no % DMA_BOUNCE_BUFFER_COUNT];
1257 + sgl = smi_scatterlist_from_buffer(
1259 + inst->bounce.phys[
1260 + chunk_no % DMA_BOUNCE_BUFFER_COUNT],
1262 + &inst->buffer_sgl);
1265 + if (!smi_dma_submit_sgl(inst, sgl, 1, dma_dir,
1266 + smi_dma_callback_user_copy
1268 + dev_err(inst->dev, "sgl submit failed");
1272 + count_left -= chunk_size;
1275 + dma_async_issue_pending(inst->dma_chan);
1277 + if (inst->settings.data_width == SMI_WIDTH_8BIT)
1278 + init_trans_func(inst, count);
1279 + else if (inst->settings.data_width == SMI_WIDTH_16BIT)
1280 + init_trans_func(inst, count / 2);
1282 + spin_unlock(&inst->transaction_lock);
1285 +EXPORT_SYMBOL(bcm2835_smi_user_dma);
1288 +/****************************************************************************
1290 +* High level buffer transfer functions - for use by other drivers
1292 +***************************************************************************/
1294 +/* Buffer must be physically contiguous - i.e. kmalloc, not vmalloc! */
1295 +void bcm2835_smi_write_buf(
1296 + struct bcm2835_smi_instance *inst,
1297 + const void *buf, size_t n_bytes)
1299 + int odd_bytes = n_bytes & 0x3;
1301 + n_bytes -= odd_bytes;
1303 + spin_lock(&inst->transaction_lock);
1305 + if (n_bytes > DMA_THRESHOLD_BYTES) {
1306 + dma_addr_t phy_addr = dma_map_single(
1311 + struct scatterlist *sgl =
1312 + smi_scatterlist_from_buffer(inst, phy_addr, n_bytes,
1313 + &inst->buffer_sgl);
1316 + smi_dump_context_labelled(inst,
1317 + "Error: could not create scatterlist for write!");
1320 + smi_dma_write_sgl(inst, sgl, 1, n_bytes);
1323 + (inst->dev, phy_addr, n_bytes, DMA_TO_DEVICE);
1324 + } else if (n_bytes) {
1325 + smi_write_fifo(inst, (uint32_t *) buf, n_bytes);
1329 + if (inst->settings.data_width == SMI_WIDTH_8BIT) {
1330 + while (odd_bytes--)
1331 + smi_write_single_word(inst, *(uint8_t *) (buf++));
1333 + while (odd_bytes >= 2) {
1334 + smi_write_single_word(inst, *(uint16_t *)buf);
1339 + /* Reading an odd number of bytes on a 16 bit bus is
1340 + a user bug. It's kinder to fail early and tell them
1341 + than to e.g. transparently give them the bottom byte
1342 + of a 16 bit transfer. */
1343 + dev_err(inst->dev,
1344 + "WARNING: odd number of bytes specified for wide transfer.");
1345 + dev_err(inst->dev,
1346 + "At least one byte dropped as a result.");
1351 + spin_unlock(&inst->transaction_lock);
1353 +EXPORT_SYMBOL(bcm2835_smi_write_buf);
1355 +void bcm2835_smi_read_buf(struct bcm2835_smi_instance *inst,
1356 + void *buf, size_t n_bytes)
1359 + /* SMI is inherently 32-bit, which causes surprising amounts of mess
1360 + for bytes % 4 != 0. Easiest to avoid this mess altogether
1361 + by handling remainder separately. */
1362 + int odd_bytes = n_bytes & 0x3;
1364 + spin_lock(&inst->transaction_lock);
1365 + n_bytes -= odd_bytes;
1366 + if (n_bytes > DMA_THRESHOLD_BYTES) {
1367 + dma_addr_t phy_addr = dma_map_single(inst->dev,
1370 + struct scatterlist *sgl = smi_scatterlist_from_buffer(
1371 + inst, phy_addr, n_bytes,
1372 + &inst->buffer_sgl);
1374 + smi_dump_context_labelled(inst,
1375 + "Error: could not create scatterlist for read!");
1378 + smi_dma_read_sgl(inst, sgl, 1, n_bytes);
1379 + dma_unmap_single(inst->dev, phy_addr, n_bytes, DMA_FROM_DEVICE);
1380 + } else if (n_bytes) {
1381 + smi_read_fifo(inst, (uint32_t *)buf, n_bytes);
1385 + if (inst->settings.data_width == SMI_WIDTH_8BIT) {
1386 + while (odd_bytes--)
1387 + *((uint8_t *) (buf++)) = smi_read_single_word(inst);
1389 + while (odd_bytes >= 2) {
1390 + *(uint16_t *) buf = smi_read_single_word(inst);
1395 + dev_err(inst->dev,
1396 + "WARNING: odd number of bytes specified for wide transfer.");
1397 + dev_err(inst->dev,
1398 + "At least one byte dropped as a result.");
1403 + spin_unlock(&inst->transaction_lock);
1405 +EXPORT_SYMBOL(bcm2835_smi_read_buf);
1407 +void bcm2835_smi_set_address(struct bcm2835_smi_instance *inst,
1408 + unsigned int address)
1410 + spin_lock(&inst->transaction_lock);
1411 + smi_set_address(inst, address);
1412 + spin_unlock(&inst->transaction_lock);
1414 +EXPORT_SYMBOL(bcm2835_smi_set_address);
1416 +struct bcm2835_smi_instance *bcm2835_smi_get(struct device_node *node)
1418 + struct platform_device *pdev;
1423 + pdev = of_find_device_by_node(node);
1427 + return platform_get_drvdata(pdev);
1429 +EXPORT_SYMBOL(bcm2835_smi_get);
1431 +/****************************************************************************
1433 +* bcm2835_smi_probe - called when the driver is loaded.
1435 +***************************************************************************/
1437 +static int bcm2835_smi_dma_setup(struct bcm2835_smi_instance *inst)
1441 + inst->dma_chan = dma_request_slave_channel(inst->dev, "rx-tx");
1443 + inst->dma_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1444 + inst->dma_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1445 + inst->dma_config.src_addr = inst->smi_regs_busaddr + SMID;
1446 + inst->dma_config.dst_addr = inst->dma_config.src_addr;
1447 + /* Direction unimportant - always overridden by prep_slave_sg */
1448 + inst->dma_config.direction = DMA_DEV_TO_MEM;
1449 + dmaengine_slave_config(inst->dma_chan, &inst->dma_config);
1450 + /* Alloc and map bounce buffers */
1451 + for (i = 0; i < DMA_BOUNCE_BUFFER_COUNT; ++i) {
1452 + inst->bounce.buffer[i] =
1453 + dmam_alloc_coherent(inst->dev, DMA_BOUNCE_BUFFER_SIZE,
1454 + &inst->bounce.phys[i],
1456 + if (!inst->bounce.buffer[i]) {
1457 + dev_err(inst->dev, "Could not allocate buffer!");
1461 + smi_scatterlist_from_buffer(
1463 + inst->bounce.phys[i],
1464 + DMA_BOUNCE_BUFFER_SIZE,
1465 + &inst->bounce.sgl[i]
1472 +static int bcm2835_smi_probe(struct platform_device *pdev)
1475 + struct device *dev = &pdev->dev;
1476 + struct device_node *node = dev->of_node;
1477 + struct resource *ioresource;
1478 + struct bcm2835_smi_instance *inst;
1479 + const __be32 *addr;
1481 + /* We require device tree support */
1484 + /* Allocate buffers and instance data */
1485 + inst = devm_kzalloc(dev, sizeof(struct bcm2835_smi_instance),
1491 + spin_lock_init(&inst->transaction_lock);
1493 + ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1494 + inst->smi_regs_ptr = devm_ioremap_resource(dev, ioresource);
1495 + if (IS_ERR(inst->smi_regs_ptr)) {
1496 + err = PTR_ERR(inst->smi_regs_ptr);
1499 + addr = of_get_address(node, 0, NULL, NULL);
1500 + inst->smi_regs_busaddr = be32_to_cpu(*addr);
1502 + err = bcm2835_smi_dma_setup(inst);
1506 + /* request clock */
1507 + inst->clk = devm_clk_get(dev, NULL);
1510 + clk_prepare_enable(inst->clk);
1512 + /* Finally, do peripheral setup */
1513 + smi_setup_regs(inst);
1515 + platform_set_drvdata(pdev, inst);
1517 + dev_info(inst->dev, "initialised");
1525 +/****************************************************************************
1527 +* bcm2835_smi_remove - called when the driver is unloaded.
1529 +***************************************************************************/
1531 +static int bcm2835_smi_remove(struct platform_device *pdev)
1533 + struct bcm2835_smi_instance *inst = platform_get_drvdata(pdev);
1534 + struct device *dev = inst->dev;
1536 + dmaengine_terminate_all(inst->dma_chan);
1537 + dma_release_channel(inst->dma_chan);
1539 + clk_disable_unprepare(inst->clk);
1541 + dev_info(dev, "SMI device removed - OK");
1545 +/****************************************************************************
1547 +* Register the driver with device tree
1549 +***************************************************************************/
1551 +static const struct of_device_id bcm2835_smi_of_match[] = {
1552 + {.compatible = "brcm,bcm2835-smi",},
1553 + { /* sentinel */ },
1556 +MODULE_DEVICE_TABLE(of, bcm2835_smi_of_match);
1558 +static struct platform_driver bcm2835_smi_driver = {
1559 + .probe = bcm2835_smi_probe,
1560 + .remove = bcm2835_smi_remove,
1562 + .name = DRIVER_NAME,
1563 + .owner = THIS_MODULE,
1564 + .of_match_table = bcm2835_smi_of_match,
1568 +module_platform_driver(bcm2835_smi_driver);
1570 +MODULE_ALIAS("platform:smi-bcm2835");
1571 +MODULE_LICENSE("GPL");
1572 +MODULE_DESCRIPTION("Device driver for BCM2835's secondary memory interface");
1573 +MODULE_AUTHOR("Luke Wren <luke@raspberrypi.org>");
1575 +++ b/include/linux/broadcom/bcm2835_smi.h
1578 + * Declarations and definitions for Broadcom's Secondary Memory Interface
1580 + * Written by Luke Wren <luke@raspberrypi.org>
1581 + * Copyright (c) 2015, Raspberry Pi (Trading) Ltd.
1582 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1584 + * Redistribution and use in source and binary forms, with or without
1585 + * modification, are permitted provided that the following conditions
1587 + * 1. Redistributions of source code must retain the above copyright
1588 + * notice, this list of conditions, and the following disclaimer,
1589 + * without modification.
1590 + * 2. Redistributions in binary form must reproduce the above copyright
1591 + * notice, this list of conditions and the following disclaimer in the
1592 + * documentation and/or other materials provided with the distribution.
1593 + * 3. The names of the above-listed copyright holders may not be used
1594 + * to endorse or promote products derived from this software without
1595 + * specific prior written permission.
1597 + * ALTERNATIVELY, this software may be distributed under the terms of the
1598 + * GNU General Public License ("GPL") version 2, as published by the Free
1599 + * Software Foundation.
1601 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1602 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1603 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1604 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1605 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1606 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1607 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1608 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1609 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1610 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1611 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1614 +#ifndef BCM2835_SMI_H
1615 +#define BCM2835_SMI_H
1617 +#include <linux/ioctl.h>
1620 +#include <stdint.h>
1621 +#include <stdbool.h>
1624 +#define BCM2835_SMI_IOC_MAGIC 0x1
1625 +#define BCM2835_SMI_INVALID_HANDLE (~0)
1627 +/* IOCTLs 0x100...0x1ff are not device-specific - we can use them */
1628 +#define BCM2835_SMI_IOC_GET_SETTINGS _IO(BCM2835_SMI_IOC_MAGIC, 0)
1629 +#define BCM2835_SMI_IOC_WRITE_SETTINGS _IO(BCM2835_SMI_IOC_MAGIC, 1)
1630 +#define BCM2835_SMI_IOC_ADDRESS _IO(BCM2835_SMI_IOC_MAGIC, 2)
1631 +#define BCM2835_SMI_IOC_MAX 2
1633 +#define SMI_WIDTH_8BIT 0
1634 +#define SMI_WIDTH_16BIT 1
1635 +#define SMI_WIDTH_9BIT 2
1636 +#define SMI_WIDTH_18BIT 3
1638 +/* max number of bytes where DMA will not be used */
1639 +#define DMA_THRESHOLD_BYTES 128
1640 +#define DMA_BOUNCE_BUFFER_SIZE (1024 * 1024 / 2)
1641 +#define DMA_BOUNCE_BUFFER_COUNT 3
1644 +struct smi_settings {
1646 + /* Whether or not to pack multiple SMI transfers into a
1647 + single 32 bit FIFO word */
1650 + /* Timing for reads (writes the same but for WE)
1652 + * OE ----------+ +--------------------
1655 + * SD -<==============================>-----------
1656 + * SA -<=========================================>-
1657 + * <-setup-> <-strobe -> <-hold -> <- pace ->
1660 + int read_setup_time;
1661 + int read_hold_time;
1662 + int read_pace_time;
1663 + int read_strobe_time;
1665 + int write_setup_time;
1666 + int write_hold_time;
1667 + int write_pace_time;
1668 + int write_strobe_time;
1670 + bool dma_enable; /* DREQs */
1671 + bool dma_passthrough_enable; /* External DREQs */
1672 + int dma_read_thresh;
1673 + int dma_write_thresh;
1674 + int dma_panic_read_thresh;
1675 + int dma_panic_write_thresh;
1678 +/****************************************************************************
1680 +* Declare exported SMI functions
1682 +***************************************************************************/
1686 +#include <linux/dmaengine.h> /* for enum dma_transfer_direction */
1687 +#include <linux/of.h>
1688 +#include <linux/semaphore.h>
1690 +struct bcm2835_smi_instance;
1692 +struct bcm2835_smi_bounce_info {
1693 + struct semaphore callback_sem;
1694 + void *buffer[DMA_BOUNCE_BUFFER_COUNT];
1695 + dma_addr_t phys[DMA_BOUNCE_BUFFER_COUNT];
1696 + struct scatterlist sgl[DMA_BOUNCE_BUFFER_COUNT];
1700 +void bcm2835_smi_set_regs_from_settings(struct bcm2835_smi_instance *);
1702 +struct smi_settings *bcm2835_smi_get_settings_from_regs(
1703 + struct bcm2835_smi_instance *inst);
1705 +void bcm2835_smi_write_buf(
1706 + struct bcm2835_smi_instance *inst,
1710 +void bcm2835_smi_read_buf(
1711 + struct bcm2835_smi_instance *inst,
1715 +void bcm2835_smi_set_address(struct bcm2835_smi_instance *inst,
1716 + unsigned int address);
1718 +ssize_t bcm2835_smi_user_dma(
1719 + struct bcm2835_smi_instance *inst,
1720 + enum dma_transfer_direction dma_dir,
1721 + char __user *user_ptr,
1723 + struct bcm2835_smi_bounce_info **bounce);
1725 +struct bcm2835_smi_instance *bcm2835_smi_get(struct device_node *node);
1727 +#endif /* __KERNEL__ */
1729 +/****************************************************************
1731 +* Implementation-only declarations
1733 +****************************************************************/
1735 +#ifdef BCM2835_SMI_IMPLEMENTATION
1737 +/* Clock manager registers for SMI clock: */
1738 +#define CM_SMI_BASE_ADDRESS ((BCM2708_PERI_BASE) + 0x1010b0)
1739 +/* Clock manager "password" to protect registers from spurious writes */
1740 +#define CM_PWD (0x5a << 24)
1742 +#define CM_SMI_CTL 0x00
1743 +#define CM_SMI_DIV 0x04
1745 +#define CM_SMI_CTL_FLIP (1 << 8)
1746 +#define CM_SMI_CTL_BUSY (1 << 7)
1747 +#define CM_SMI_CTL_KILL (1 << 5)
1748 +#define CM_SMI_CTL_ENAB (1 << 4)
1749 +#define CM_SMI_CTL_SRC_MASK (0xf)
1750 +#define CM_SMI_CTL_SRC_OFFS (0)
1752 +#define CM_SMI_DIV_DIVI_MASK (0xf << 12)
1753 +#define CM_SMI_DIV_DIVI_OFFS (12)
1754 +#define CM_SMI_DIV_DIVF_MASK (0xff << 4)
1755 +#define CM_SMI_DIV_DIVF_OFFS (4)
1757 +/* SMI register mapping:*/
1758 +#define SMI_BASE_ADDRESS ((BCM2708_PERI_BASE) + 0x600000)
1760 +#define SMICS 0x00 /* control + status register */
1761 +#define SMIL 0x04 /* length/count (n external txfers) */
1762 +#define SMIA 0x08 /* address register */
1763 +#define SMID 0x0c /* data register */
1764 +#define SMIDSR0 0x10 /* device 0 read settings */
1765 +#define SMIDSW0 0x14 /* device 0 write settings */
1766 +#define SMIDSR1 0x18 /* device 1 read settings */
1767 +#define SMIDSW1 0x1c /* device 1 write settings */
1768 +#define SMIDSR2 0x20 /* device 2 read settings */
1769 +#define SMIDSW2 0x24 /* device 2 write settings */
1770 +#define SMIDSR3 0x28 /* device 3 read settings */
1771 +#define SMIDSW3 0x2c /* device 3 write settings */
1772 +#define SMIDC 0x30 /* DMA control registers */
1773 +#define SMIDCS 0x34 /* direct control/status register */
1774 +#define SMIDA 0x38 /* direct address register */
1775 +#define SMIDD 0x3c /* direct data registers */
1776 +#define SMIFD 0x40 /* FIFO debug register */
1780 +/* Control and Status register bits:
1781 + * SMICS_RXF : RX fifo full: 1 when RX fifo is full
1782 + * SMICS_TXE : TX fifo empty: 1 when empty.
1783 + * SMICS_RXD : RX fifo contains data: 1 when there is data.
1784 + * SMICS_TXD : TX fifo can accept data: 1 when true.
1785 + * SMICS_RXR : RX fifo needs reading: 1 when fifo more than 3/4 full, or
1786 + * when "DONE" and fifo not emptied.
1787 + * SMICS_TXW : TX fifo needs writing: 1 when less than 1/4 full.
1788 + * SMICS_AFERR : AXI FIFO error: 1 when fifo read when empty or written
1789 + * when full. Write 1 to clear.
1790 + * SMICS_EDREQ : 1 when external DREQ received.
1791 + * SMICS_PXLDAT : Pixel data: write 1 to enable pixel transfer modes.
1792 + * SMICS_SETERR : 1 if there was an error writing to setup regs (e.g.
1793 + * tx was in progress). Write 1 to clear.
1794 + * SMICS_PVMODE : Set to 1 to enable pixel valve mode.
1795 + * SMICS_INTR : Set to 1 to enable interrupt on RX.
1796 + * SMICS_INTT : Set to 1 to enable interrupt on TX.
1797 + * SMICS_INTD : Set to 1 to enable interrupt on DONE condition.
1798 + * SMICS_TEEN : Tear effect mode enabled: Programmed transfers will wait
1799 + * for a TE trigger before writing.
1800 + * SMICS_PAD1 : Padding settings for external transfers. For writes: the
1801 + * number of bytes initially written to the TX fifo that
1802 + * SMICS_PAD0 : should be ignored. For reads: the number of bytes that will
1803 + * be read before the data, and should be dropped.
1804 + * SMICS_WRITE : Transfer direction: 1 = write to external device, 0 = read
1805 + * SMICS_CLEAR : Write 1 to clear the FIFOs.
1806 + * SMICS_START : Write 1 to start the programmed transfer.
1807 + * SMICS_ACTIVE : Reads as 1 when a programmed transfer is underway.
1808 + * SMICS_DONE : Reads as 1 when transfer finished. For RX, not set until
1810 + * SMICS_ENABLE : Set to 1 to enable the SMI peripheral, 0 to disable.
1813 +#define SMICS_RXF (1 << 31)
1814 +#define SMICS_TXE (1 << 30)
1815 +#define SMICS_RXD (1 << 29)
1816 +#define SMICS_TXD (1 << 28)
1817 +#define SMICS_RXR (1 << 27)
1818 +#define SMICS_TXW (1 << 26)
1819 +#define SMICS_AFERR (1 << 25)
1820 +#define SMICS_EDREQ (1 << 15)
1821 +#define SMICS_PXLDAT (1 << 14)
1822 +#define SMICS_SETERR (1 << 13)
1823 +#define SMICS_PVMODE (1 << 12)
1824 +#define SMICS_INTR (1 << 11)
1825 +#define SMICS_INTT (1 << 10)
1826 +#define SMICS_INTD (1 << 9)
1827 +#define SMICS_TEEN (1 << 8)
1828 +#define SMICS_PAD1 (1 << 7)
1829 +#define SMICS_PAD0 (1 << 6)
1830 +#define SMICS_WRITE (1 << 5)
1831 +#define SMICS_CLEAR (1 << 4)
1832 +#define SMICS_START (1 << 3)
1833 +#define SMICS_ACTIVE (1 << 2)
1834 +#define SMICS_DONE (1 << 1)
1835 +#define SMICS_ENABLE (1 << 0)
1837 +/* Address register bits: */
1839 +#define SMIA_DEVICE_MASK ((1 << 9) | (1 << 8))
1840 +#define SMIA_DEVICE_OFFS (8)
1841 +#define SMIA_ADDR_MASK (0x3f) /* bits 5 -> 0 */
1842 +#define SMIA_ADDR_OFFS (0)
1844 +/* DMA control register bits:
1845 + * SMIDC_DMAEN : DMA enable: set 1: DMA requests will be issued.
1846 + * SMIDC_DMAP : DMA passthrough: when set to 0, top two data pins are used by
1847 + * SMI as usual. When set to 1, the top two pins are used for
1848 + * external DREQs: pin 16 read request, 17 write.
1849 + * SMIDC_PANIC* : Threshold at which DMA will panic during read/write.
1850 + * SMIDC_REQ* : Threshold at which DMA will generate a DREQ.
1853 +#define SMIDC_DMAEN (1 << 28)
1854 +#define SMIDC_DMAP (1 << 24)
1855 +#define SMIDC_PANICR_MASK (0x3f << 18)
1856 +#define SMIDC_PANICR_OFFS (18)
1857 +#define SMIDC_PANICW_MASK (0x3f << 12)
1858 +#define SMIDC_PANICW_OFFS (12)
1859 +#define SMIDC_REQR_MASK (0x3f << 6)
1860 +#define SMIDC_REQR_OFFS (6)
1861 +#define SMIDC_REQW_MASK (0x3f)
1862 +#define SMIDC_REQW_OFFS (0)
1864 +/* Device settings register bits: same for all 4 (or 3?) device register sets.
1865 + * Device read settings:
1866 + * SMIDSR_RWIDTH : Read transfer width. 00 = 8bit, 01 = 16bit,
1867 + * 10 = 18bit, 11 = 9bit.
1868 + * SMIDSR_RSETUP : Read setup time: number of core cycles between chip
1869 + * select/address and read strobe. Min 1, max 64.
1870 + * SMIDSR_MODE68 : 1 for System 68 mode (i.e. enable + direction pins,
1871 + * rather than OE + WE pin)
1872 + * SMIDSR_FSETUP : If set to 1, setup time only applies to first
1873 + * transfer after address change.
1874 + * SMIDSR_RHOLD : Number of core cycles between read strobe going
1875 + * inactive and CS/address going inactive. Min 1, max 64
1876 + * SMIDSR_RPACEALL : When set to 1, this device's RPACE value will always
1877 + * be used for the next transaction, even if it is not
1879 + * SMIDSR_RPACE : Number of core cycles spent waiting between CS
1880 + * deassert and start of next transfer. Min 1, max 128
1881 + * SMIDSR_RDREQ : 1 = use external DMA request on SD16 to pace reads
1882 + * from device. Must also set DMAP in SMICS.
1883 + * SMIDSR_RSTROBE : Number of cycles to assert the read strobe.
1886 +#define SMIDSR_RWIDTH_MASK ((1<<31)|(1<<30))
1887 +#define SMIDSR_RWIDTH_OFFS (30)
1888 +#define SMIDSR_RSETUP_MASK (0x3f << 24)
1889 +#define SMIDSR_RSETUP_OFFS (24)
1890 +#define SMIDSR_MODE68 (1 << 23)
1891 +#define SMIDSR_FSETUP (1 << 22)
1892 +#define SMIDSR_RHOLD_MASK (0x3f << 16)
1893 +#define SMIDSR_RHOLD_OFFS (16)
1894 +#define SMIDSR_RPACEALL (1 << 15)
1895 +#define SMIDSR_RPACE_MASK (0x7f << 8)
1896 +#define SMIDSR_RPACE_OFFS (8)
1897 +#define SMIDSR_RDREQ (1 << 7)
1898 +#define SMIDSR_RSTROBE_MASK (0x7f)
1899 +#define SMIDSR_RSTROBE_OFFS (0)
1901 +/* Device write settings:
1902 + * SMIDSW_WWIDTH : Write transfer width. 00 = 8bit, 01 = 16bit,
1903 + * 10= 18bit, 11 = 9bit.
1904 + * SMIDSW_WSETUP : Number of cycles between CS assert and write strobe.
1906 + * SMIDSW_WFORMAT : Pixel format of input. 0 = 16bit RGB 565,
1907 + * 1 = 32bit RGBA 8888
1908 + * SMIDSW_WSWAP : 1 = swap pixel data bits. (Use with SMICS_PXLDAT)
1909 + * SMIDSW_WHOLD : Time between WE deassert and CS deassert. 1 to 64
1910 + * SMIDSW_WPACEALL : 1: this device's WPACE will be used for the next
1911 + * transfer, regardless of that transfer's device.
1912 + * SMIDSW_WPACE : Cycles between CS deassert and next CS assert.
1914 + * SMIDSW_WDREQ : Use external DREQ on pin 17 to pace writes. DMAP must
1915 + * be set in SMICS.
1916 + * SMIDSW_WSTROBE : Number of cycles to assert the write strobe.
1919 +#define SMIDSW_WWIDTH_MASK ((1<<31)|(1<<30))
1920 +#define SMIDSW_WWIDTH_OFFS (30)
1921 +#define SMIDSW_WSETUP_MASK (0x3f << 24)
1922 +#define SMIDSW_WSETUP_OFFS (24)
1923 +#define SMIDSW_WFORMAT (1 << 23)
1924 +#define SMIDSW_WSWAP (1 << 22)
1925 +#define SMIDSW_WHOLD_MASK (0x3f << 16)
1926 +#define SMIDSW_WHOLD_OFFS (16)
1927 +#define SMIDSW_WPACEALL (1 << 15)
1928 +#define SMIDSW_WPACE_MASK (0x7f << 8)
1929 +#define SMIDSW_WPACE_OFFS (8)
1930 +#define SMIDSW_WDREQ (1 << 7)
1931 +#define SMIDSW_WSTROBE_MASK (0x7f)
1932 +#define SMIDSW_WSTROBE_OFFS (0)
1934 +/* Direct transfer control + status register
1935 + * SMIDCS_WRITE : Direction of transfer: 1 -> write, 0 -> read
1936 + * SMIDCS_DONE : 1 when a transfer has finished. Write 1 to clear.
1937 + * SMIDCS_START : Write 1 to start a transfer, if one is not already underway.
1938 + * SMIDCE_ENABLE: Write 1 to enable SMI in direct mode.
1941 +#define SMIDCS_WRITE (1 << 3)
1942 +#define SMIDCS_DONE (1 << 2)
1943 +#define SMIDCS_START (1 << 1)
1944 +#define SMIDCS_ENABLE (1 << 0)
1946 +/* Direct transfer address register
1947 + * SMIDA_DEVICE : Indicates which of the device settings banks should be used.
1948 + * SMIDA_ADDR : The value to be asserted on the address pins.
1951 +#define SMIDA_DEVICE_MASK ((1<<9)|(1<<8))
1952 +#define SMIDA_DEVICE_OFFS (8)
1953 +#define SMIDA_ADDR_MASK (0x3f)
1954 +#define SMIDA_ADDR_OFFS (0)
1956 +/* FIFO debug register
1957 + * SMIFD_FLVL : The high-tide mark of FIFO count during the most recent txfer
1958 + * SMIFD_FCNT : The current FIFO count.
1960 +#define SMIFD_FLVL_MASK (0x3f << 8)
1961 +#define SMIFD_FLVL_OFFS (8)
1962 +#define SMIFD_FCNT_MASK (0x3f)
1963 +#define SMIFD_FCNT_OFFS (0)
1965 +#endif /* BCM2835_SMI_IMPLEMENTATION */
1967 +#endif /* BCM2835_SMI_H */