1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930 |
- From 75b517005b8a733b84735cacfa9cdb3f301db6a0 Mon Sep 17 00:00:00 2001
- From: Luke Wren <wren6991@gmail.com>
- Date: Sat, 5 Sep 2015 01:14:45 +0100
- Subject: [PATCH] Add SMI driver
- Signed-off-by: Luke Wren <wren6991@gmail.com>
- ---
- .../bindings/misc/brcm,bcm2835-smi-dev.txt | 17 +
- .../devicetree/bindings/misc/brcm,bcm2835-smi.txt | 48 +
- drivers/char/broadcom/Kconfig | 8 +
- drivers/char/broadcom/Makefile | 2 +-
- drivers/char/broadcom/bcm2835_smi_dev.c | 402 +++++++++
- drivers/misc/Kconfig | 8 +
- drivers/misc/Makefile | 1 +
- drivers/misc/bcm2835_smi.c | 985 +++++++++++++++++++++
- include/linux/broadcom/bcm2835_smi.h | 391 ++++++++
- 9 files changed, 1861 insertions(+), 1 deletion(-)
- create mode 100644 Documentation/devicetree/bindings/misc/brcm,bcm2835-smi-dev.txt
- create mode 100644 Documentation/devicetree/bindings/misc/brcm,bcm2835-smi.txt
- create mode 100644 drivers/char/broadcom/bcm2835_smi_dev.c
- create mode 100644 drivers/misc/bcm2835_smi.c
- create mode 100644 include/linux/broadcom/bcm2835_smi.h
- --- /dev/null
- +++ b/Documentation/devicetree/bindings/misc/brcm,bcm2835-smi-dev.txt
- @@ -0,0 +1,17 @@
- +* Broadcom BCM2835 SMI character device driver.
- +
- +SMI or secondary memory interface is a peripheral specific to certain Broadcom
- +SOCs, and is helpful for talking to things like parallel-interface displays
- +and NAND flashes (in fact, most things with a parallel register interface).
- +
- +This driver adds a character device which provides a user-space interface to
- +an instance of the SMI driver.
- +
- +Required properties:
- +- compatible: "brcm,bcm2835-smi-dev"
- +- smi_handle: a phandle to the smi node.
- +
- +Optional properties:
- +- None.
- +
- +
- --- /dev/null
- +++ b/Documentation/devicetree/bindings/misc/brcm,bcm2835-smi.txt
- @@ -0,0 +1,48 @@
- +* Broadcom BCM2835 SMI driver.
- +
- +SMI or secondary memory interface is a peripheral specific to certain Broadcom
- +SOCs, and is helpful for talking to things like parallel-interface displays
- +and NAND flashes (in fact, most things with a parallel register interface).
- +
- +Required properties:
- +- compatible: "brcm,bcm2835-smi"
- +- reg: Should contain location and length of SMI registers and SMI clkman regs
- +- interrupts: *the* SMI interrupt.
- +- pinctrl-names: should be "default".
- +- pinctrl-0: the phandle of the gpio pin node.
- +- brcm,smi-clock-source: the clock source for clkman
- +- brcm,smi-clock-divisor: the integer clock divisor for clkman
- +- dmas: the dma controller phandle and the DREQ number (4 on a 2835)
- +- dma-names: the name used by the driver to request its channel.
- + Should be "rx-tx".
- +
- +Optional properties:
- +- None.
- +
- +Examples:
- +
- +8 data pin configuration:
- +
- +smi: smi@7e600000 {
- + compatible = "brcm,bcm2835-smi";
- + reg = <0x7e600000 0x44>, <0x7e1010b0 0x8>;
- + interrupts = <2 16>;
- + pinctrl-names = "default";
- + pinctrl-0 = <&smi_pins>;
- + brcm,smi-clock-source = <6>;
- + brcm,smi-clock-divisor = <4>;
- + dmas = <&dma 4>;
- + dma-names = "rx-tx";
- +
- + status = "okay";
- +};
- +
- +smi_pins: smi_pins {
- + brcm,pins = <2 3 4 5 6 7 8 9 10 11 12 13 14 15>;
- + /* Alt 1: SMI */
- + brcm,function = <5 5 5 5 5 5 5 5 5 5 5 5 5 5>;
- + /* /CS, /WE and /OE are pulled high, as they are
- + generally active low signals */
- + brcm,pull = <2 2 2 2 2 2 0 0 0 0 0 0 0 0>;
- +};
- +
- --- a/drivers/char/broadcom/Kconfig
- +++ b/drivers/char/broadcom/Kconfig
- @@ -41,3 +41,11 @@ config BCM2835_DEVGPIOMEM
- on the 2835. Calling mmap(/dev/gpiomem) will map the GPIO
- register page to the user's pointer.
-
- +config BCM2835_SMI_DEV
- + tristate "Character device driver for BCM2835 Secondary Memory Interface"
- + depends on (MACH_BCM2708 || MACH_BCM2709 || ARCH_BCM2835) && BCM2835_SMI
- + default m
- + help
- + This driver provides a character device interface (ioctl + read/write) to
- + Broadcom's Secondary Memory interface. The low-level functionality is provided
- + by the SMI driver itself.
- --- a/drivers/char/broadcom/Makefile
- +++ b/drivers/char/broadcom/Makefile
- @@ -3,4 +3,4 @@ obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
- obj-$(CONFIG_BCM_VC_SM) += vc_sm/
-
- obj-$(CONFIG_BCM2835_DEVGPIOMEM)+= bcm2835-gpiomem.o
- -
- +obj-$(CONFIG_BCM2835_SMI_DEV) += bcm2835_smi_dev.o
- --- /dev/null
- +++ b/drivers/char/broadcom/bcm2835_smi_dev.c
- @@ -0,0 +1,402 @@
- +/**
- + * Character device driver for Broadcom Secondary Memory Interface
- + *
- + * Written by Luke Wren <luke@raspberrypi.org>
- + * Copyright (c) 2015, Raspberry Pi (Trading) Ltd.
- + *
- + * Redistribution and use in source and binary forms, with or without
- + * modification, are permitted provided that the following conditions
- + * are met:
- + * 1. Redistributions of source code must retain the above copyright
- + * notice, this list of conditions, and the following disclaimer,
- + * without modification.
- + * 2. Redistributions in binary form must reproduce the above copyright
- + * notice, this list of conditions and the following disclaimer in the
- + * documentation and/or other materials provided with the distribution.
- + * 3. The names of the above-listed copyright holders may not be used
- + * to endorse or promote products derived from this software without
- + * specific prior written permission.
- + *
- + * ALTERNATIVELY, this software may be distributed under the terms of the
- + * GNU General Public License ("GPL") version 2, as published by the Free
- + * Software Foundation.
- + *
- + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- + */
- +
- +#include <linux/kernel.h>
- +#include <linux/module.h>
- +#include <linux/of.h>
- +#include <linux/platform_device.h>
- +#include <linux/slab.h>
- +#include <linux/mm.h>
- +#include <linux/pagemap.h>
- +#include <linux/fs.h>
- +#include <linux/cdev.h>
- +#include <linux/fs.h>
- +
- +#include <linux/broadcom/bcm2835_smi.h>
- +
- +#define DEVICE_NAME "bcm2835-smi-dev"
- +#define DRIVER_NAME "smi-dev-bcm2835"
- +#define DEVICE_MINOR 0
- +
- +static struct cdev bcm2835_smi_cdev;
- +static dev_t bcm2835_smi_devid;
- +static struct class *bcm2835_smi_class;
- +static struct device *bcm2835_smi_dev;
- +
- +struct bcm2835_smi_dev_instance {
- + struct device *dev;
- +};
- +
- +static struct bcm2835_smi_instance *smi_inst;
- +static struct bcm2835_smi_dev_instance *inst;
- +
- +static const char *const ioctl_names[] = {
- + "READ_SETTINGS",
- + "WRITE_SETTINGS",
- + "ADDRESS"
- +};
- +
- +/****************************************************************************
- +*
- +* SMI chardev file ops
- +*
- +***************************************************************************/
- +static long
- +bcm2835_smi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
- +{
- + long ret = 0;
- +
- + dev_info(inst->dev, "serving ioctl...");
- +
- + switch (cmd) {
- + case BCM2835_SMI_IOC_GET_SETTINGS:{
- + struct smi_settings *settings;
- +
- + dev_info(inst->dev, "Reading SMI settings to user.");
- + settings = bcm2835_smi_get_settings_from_regs(smi_inst);
- + if (copy_to_user((void *)arg, settings,
- + sizeof(struct smi_settings)))
- + dev_err(inst->dev, "settings copy failed.");
- + break;
- + }
- + case BCM2835_SMI_IOC_WRITE_SETTINGS:{
- + struct smi_settings *settings;
- +
- + dev_info(inst->dev, "Setting user's SMI settings.");
- + settings = bcm2835_smi_get_settings_from_regs(smi_inst);
- + if (copy_from_user(settings, (void *)arg,
- + sizeof(struct smi_settings)))
- + dev_err(inst->dev, "settings copy failed.");
- + else
- + bcm2835_smi_set_regs_from_settings(smi_inst);
- + break;
- + }
- + case BCM2835_SMI_IOC_ADDRESS:
- + dev_info(inst->dev, "SMI address set: 0x%02x", (int)arg);
- + bcm2835_smi_set_address(smi_inst, arg);
- + break;
- + default:
- + dev_err(inst->dev, "invalid ioctl cmd: %d", cmd);
- + ret = -ENOTTY;
- + break;
- + }
- +
- + return ret;
- +}
- +
- +static int bcm2835_smi_open(struct inode *inode, struct file *file)
- +{
- + int dev = iminor(inode);
- +
- + dev_dbg(inst->dev, "SMI device opened.");
- +
- + if (dev != DEVICE_MINOR) {
- + dev_err(inst->dev,
- + "bcm2835_smi_release: Unknown minor device: %d",
- + dev);
- + return -ENXIO;
- + }
- +
- + return 0;
- +}
- +
- +static int bcm2835_smi_release(struct inode *inode, struct file *file)
- +{
- + int dev = iminor(inode);
- +
- + if (dev != DEVICE_MINOR) {
- + dev_err(inst->dev,
- + "bcm2835_smi_release: Unknown minor device %d", dev);
- + return -ENXIO;
- + }
- +
- + return 0;
- +}
- +
- +static ssize_t dma_bounce_user(
- + enum dma_transfer_direction dma_dir,
- + char __user *user_ptr,
- + size_t count,
- + struct bcm2835_smi_bounce_info *bounce)
- +{
- + int chunk_size;
- + int chunk_no = 0;
- + int count_left = count;
- +
- + while (count_left) {
- + int rv;
- + void *buf;
- +
- + /* Wait for current chunk to complete: */
- + if (down_timeout(&bounce->callback_sem,
- + msecs_to_jiffies(1000))) {
- + dev_err(inst->dev, "DMA bounce timed out");
- + count -= (count_left);
- + break;
- + }
- +
- + if (bounce->callback_sem.count >= DMA_BOUNCE_BUFFER_COUNT - 1)
- + dev_err(inst->dev, "WARNING: Ring buffer overflow");
- + chunk_size = count_left > DMA_BOUNCE_BUFFER_SIZE ?
- + DMA_BOUNCE_BUFFER_SIZE : count_left;
- + buf = bounce->buffer[chunk_no % DMA_BOUNCE_BUFFER_COUNT];
- + if (dma_dir == DMA_DEV_TO_MEM)
- + rv = copy_to_user(user_ptr, buf, chunk_size);
- + else
- + rv = copy_from_user(buf, user_ptr, chunk_size);
- + if (rv)
- + dev_err(inst->dev, "copy_*_user() failed!: %d", rv);
- + user_ptr += chunk_size;
- + count_left -= chunk_size;
- + chunk_no++;
- + }
- + return count;
- +}
- +
- +static ssize_t
- +bcm2835_read_file(struct file *f, char __user *user_ptr,
- + size_t count, loff_t *offs)
- +{
- + int odd_bytes;
- +
- + dev_dbg(inst->dev, "User reading %d bytes from SMI.", count);
- + /* We don't want to DMA a number of bytes % 4 != 0 (32 bit FIFO) */
- + if (count > DMA_THRESHOLD_BYTES)
- + odd_bytes = count & 0x3;
- + else
- + odd_bytes = count;
- + count -= odd_bytes;
- + if (count) {
- + struct bcm2835_smi_bounce_info *bounce;
- +
- + count = bcm2835_smi_user_dma(smi_inst,
- + DMA_DEV_TO_MEM, user_ptr, count,
- + &bounce);
- + if (count)
- + count = dma_bounce_user(DMA_DEV_TO_MEM, user_ptr,
- + count, bounce);
- + }
- + if (odd_bytes) {
- + /* Read from FIFO directly if not using DMA */
- + uint8_t buf[DMA_THRESHOLD_BYTES];
- +
- + bcm2835_smi_read_buf(smi_inst, buf, odd_bytes);
- + if (copy_to_user(user_ptr, buf, odd_bytes))
- + dev_err(inst->dev, "copy_to_user() failed.");
- + count += odd_bytes;
- +
- + }
- + return count;
- +}
- +
- +static ssize_t
- +bcm2835_write_file(struct file *f, const char __user *user_ptr,
- + size_t count, loff_t *offs)
- +{
- + int odd_bytes;
- +
- + dev_dbg(inst->dev, "User writing %d bytes to SMI.", count);
- + if (count > DMA_THRESHOLD_BYTES)
- + odd_bytes = count & 0x3;
- + else
- + odd_bytes = count;
- + count -= odd_bytes;
- + if (count) {
- + struct bcm2835_smi_bounce_info *bounce;
- +
- + count = bcm2835_smi_user_dma(smi_inst,
- + DMA_MEM_TO_DEV, (char __user *)user_ptr, count,
- + &bounce);
- + if (count)
- + count = dma_bounce_user(DMA_MEM_TO_DEV,
- + (char __user *)user_ptr,
- + count, bounce);
- + }
- + if (odd_bytes) {
- + uint8_t buf[DMA_THRESHOLD_BYTES];
- +
- + if (copy_from_user(buf, user_ptr, odd_bytes))
- + dev_err(inst->dev, "copy_from_user() failed.");
- + else
- + bcm2835_smi_write_buf(smi_inst, buf, odd_bytes);
- + count += odd_bytes;
- + }
- + return count;
- +}
- +
- +static const struct file_operations
- +bcm2835_smi_fops = {
- + .owner = THIS_MODULE,
- + .unlocked_ioctl = bcm2835_smi_ioctl,
- + .open = bcm2835_smi_open,
- + .release = bcm2835_smi_release,
- + .read = bcm2835_read_file,
- + .write = bcm2835_write_file,
- +};
- +
- +
- +/****************************************************************************
- +*
- +* bcm2835_smi_probe - called when the driver is loaded.
- +*
- +***************************************************************************/
- +
- +static int bcm2835_smi_dev_probe(struct platform_device *pdev)
- +{
- + int err;
- + void *ptr_err;
- + struct device *dev = &pdev->dev;
- + struct device_node *node = dev->of_node, *smi_node;
- +
- + if (!node) {
- + dev_err(dev, "No device tree node supplied!");
- + return -EINVAL;
- + }
- +
- + smi_node = of_parse_phandle(node, "smi_handle", 0);
- +
- + if (!smi_node) {
- + dev_err(dev, "No such property: smi_handle");
- + return -ENXIO;
- + }
- +
- + smi_inst = bcm2835_smi_get(smi_node);
- +
- + if (!smi_inst)
- + return -EPROBE_DEFER;
- +
- + /* Allocate buffers and instance data */
- +
- + inst = devm_kzalloc(dev, sizeof(*inst), GFP_KERNEL);
- +
- + if (!inst)
- + return -ENOMEM;
- +
- + inst->dev = dev;
- +
- + /* Create character device entries */
- +
- + err = alloc_chrdev_region(&bcm2835_smi_devid,
- + DEVICE_MINOR, 1, DEVICE_NAME);
- + if (err != 0) {
- + dev_err(inst->dev, "unable to allocate device number");
- + return -ENOMEM;
- + }
- + cdev_init(&bcm2835_smi_cdev, &bcm2835_smi_fops);
- + bcm2835_smi_cdev.owner = THIS_MODULE;
- + err = cdev_add(&bcm2835_smi_cdev, bcm2835_smi_devid, 1);
- + if (err != 0) {
- + dev_err(inst->dev, "unable to register device");
- + err = -ENOMEM;
- + goto failed_cdev_add;
- + }
- +
- + /* Create sysfs entries */
- +
- + bcm2835_smi_class = class_create(THIS_MODULE, DEVICE_NAME);
- + ptr_err = bcm2835_smi_class;
- + if (IS_ERR(ptr_err))
- + goto failed_class_create;
- +
- + bcm2835_smi_dev = device_create(bcm2835_smi_class, NULL,
- + bcm2835_smi_devid, NULL,
- + "smi");
- + ptr_err = bcm2835_smi_dev;
- + if (IS_ERR(ptr_err))
- + goto failed_device_create;
- +
- + dev_info(inst->dev, "initialised");
- +
- + return 0;
- +
- +failed_device_create:
- + class_destroy(bcm2835_smi_class);
- +failed_class_create:
- + cdev_del(&bcm2835_smi_cdev);
- + err = PTR_ERR(ptr_err);
- +failed_cdev_add:
- + unregister_chrdev_region(bcm2835_smi_devid, 1);
- + dev_err(dev, "could not load bcm2835_smi_dev");
- + return err;
- +}
- +
- +/****************************************************************************
- +*
- +* bcm2835_smi_remove - called when the driver is unloaded.
- +*
- +***************************************************************************/
- +
- +static int bcm2835_smi_dev_remove(struct platform_device *pdev)
- +{
- + device_destroy(bcm2835_smi_class, bcm2835_smi_devid);
- + class_destroy(bcm2835_smi_class);
- + cdev_del(&bcm2835_smi_cdev);
- + unregister_chrdev_region(bcm2835_smi_devid, 1);
- +
- + dev_info(inst->dev, "SMI character dev removed - OK");
- + return 0;
- +}
- +
- +/****************************************************************************
- +*
- +* Register the driver with device tree
- +*
- +***************************************************************************/
- +
- +static const struct of_device_id bcm2835_smi_dev_of_match[] = {
- + {.compatible = "brcm,bcm2835-smi-dev",},
- + { /* sentinel */ },
- +};
- +
- +MODULE_DEVICE_TABLE(of, bcm2835_smi_dev_of_match);
- +
- +static struct platform_driver bcm2835_smi_dev_driver = {
- + .probe = bcm2835_smi_dev_probe,
- + .remove = bcm2835_smi_dev_remove,
- + .driver = {
- + .name = DRIVER_NAME,
- + .owner = THIS_MODULE,
- + .of_match_table = bcm2835_smi_dev_of_match,
- + },
- +};
- +
- +module_platform_driver(bcm2835_smi_dev_driver);
- +
- +MODULE_ALIAS("platform:smi-dev-bcm2835");
- +MODULE_LICENSE("GPL");
- +MODULE_DESCRIPTION(
- + "Character device driver for BCM2835's secondary memory interface");
- +MODULE_AUTHOR("Luke Wren <luke@raspberrypi.org>");
- --- a/drivers/misc/Kconfig
- +++ b/drivers/misc/Kconfig
- @@ -10,6 +10,14 @@ config SENSORS_LIS3LV02D
- select INPUT_POLLDEV
- default n
-
- +config BCM2835_SMI
- + tristate "Broadcom 283x Secondary Memory Interface driver"
- + depends on MACH_BCM2708 || MACH_BCM2709 || ARCH_BCM2835
- + default m
- + help
- + Driver for enabling and using Broadcom's Secondary/Slow Memory Interface.
- + Appears as /dev/bcm2835_smi. For ioctl interface see drivers/misc/bcm2835_smi.h
- +
- config AD525X_DPOT
- tristate "Analog Devices Digital Potentiometers"
- depends on (I2C || SPI) && SYSFS
- --- a/drivers/misc/Makefile
- +++ b/drivers/misc/Makefile
- @@ -9,6 +9,7 @@ obj-$(CONFIG_AD525X_DPOT_SPI) += ad525x_
- obj-$(CONFIG_INTEL_MID_PTI) += pti.o
- obj-$(CONFIG_ATMEL_SSC) += atmel-ssc.o
- obj-$(CONFIG_ATMEL_TCLIB) += atmel_tclib.o
- +obj-$(CONFIG_BCM2835_SMI) += bcm2835_smi.o
- obj-$(CONFIG_BMP085) += bmp085.o
- obj-$(CONFIG_BMP085_I2C) += bmp085-i2c.o
- obj-$(CONFIG_BMP085_SPI) += bmp085-spi.o
- --- /dev/null
- +++ b/drivers/misc/bcm2835_smi.c
- @@ -0,0 +1,985 @@
- +/**
- + * Broadcom Secondary Memory Interface driver
- + *
- + * Written by Luke Wren <luke@raspberrypi.org>
- + * Copyright (c) 2015, Raspberry Pi (Trading) Ltd.
- + *
- + * Redistribution and use in source and binary forms, with or without
- + * modification, are permitted provided that the following conditions
- + * are met:
- + * 1. Redistributions of source code must retain the above copyright
- + * notice, this list of conditions, and the following disclaimer,
- + * without modification.
- + * 2. Redistributions in binary form must reproduce the above copyright
- + * notice, this list of conditions and the following disclaimer in the
- + * documentation and/or other materials provided with the distribution.
- + * 3. The names of the above-listed copyright holders may not be used
- + * to endorse or promote products derived from this software without
- + * specific prior written permission.
- + *
- + * ALTERNATIVELY, this software may be distributed under the terms of the
- + * GNU General Public License ("GPL") version 2, as published by the Free
- + * Software Foundation.
- + *
- + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- + */
- +
- +#include <linux/kernel.h>
- +#include <linux/module.h>
- +#include <linux/of.h>
- +#include <linux/platform_device.h>
- +#include <linux/of_address.h>
- +#include <linux/of_platform.h>
- +#include <linux/mm.h>
- +#include <linux/slab.h>
- +#include <linux/pagemap.h>
- +#include <linux/dma-mapping.h>
- +#include <linux/dmaengine.h>
- +#include <linux/semaphore.h>
- +#include <linux/spinlock.h>
- +#include <linux/io.h>
- +
- +#define BCM2835_SMI_IMPLEMENTATION
- +#include <linux/broadcom/bcm2835_smi.h>
- +
- +#define DRIVER_NAME "smi-bcm2835"
- +
- +#define N_PAGES_FROM_BYTES(n) ((n + PAGE_SIZE-1) / PAGE_SIZE)
- +
- +#define DMA_WRITE_TO_MEM true
- +#define DMA_READ_FROM_MEM false
- +
- +struct bcm2835_smi_instance {
- + struct device *dev;
- + struct smi_settings settings;
- + __iomem void *smi_regs_ptr, *cm_smi_regs_ptr;
- + dma_addr_t smi_regs_busaddr;
- +
- + struct dma_chan *dma_chan;
- + struct dma_slave_config dma_config;
- +
- + struct bcm2835_smi_bounce_info bounce;
- +
- + struct scatterlist buffer_sgl;
- +
- + int clock_source;
- + int clock_divisor;
- +
- + /* Sometimes we are called into in an atomic context (e.g. by
- + JFFS2 + MTD) so we can't use a mutex */
- + spinlock_t transaction_lock;
- +};
- +
- +/****************************************************************************
- +*
- +* SMI clock manager setup
- +*
- +***************************************************************************/
- +
- +static inline void write_smi_cm_reg(struct bcm2835_smi_instance *inst,
- + u32 val, unsigned reg)
- +{
- + writel(CM_PWD | val, inst->cm_smi_regs_ptr + reg);
- +}
- +
- +static inline u32 read_smi_cm_reg(struct bcm2835_smi_instance *inst,
- + unsigned reg)
- +{
- + return readl(inst->cm_smi_regs_ptr + reg);
- +}
- +
- +static void smi_setup_clock(struct bcm2835_smi_instance *inst)
- +{
- + dev_dbg(inst->dev, "Setting up clock...");
- + /* Disable SMI clock and wait for it to stop. */
- + write_smi_cm_reg(inst, 0, CM_SMI_CTL);
- + while (read_smi_cm_reg(inst, CM_SMI_CTL) & CM_SMI_CTL_BUSY)
- + ;
- +
- + write_smi_cm_reg(inst, (inst->clock_divisor << CM_SMI_DIV_DIVI_OFFS),
- + CM_SMI_DIV);
- + write_smi_cm_reg(inst, (inst->clock_source << CM_SMI_CTL_SRC_OFFS),
- + CM_SMI_CTL);
- +
- + /* Enable the clock */
- + write_smi_cm_reg(inst, (inst->clock_source << CM_SMI_CTL_SRC_OFFS) |
- + CM_SMI_CTL_ENAB, CM_SMI_CTL);
- +}
- +
- +/****************************************************************************
- +*
- +* SMI peripheral setup
- +*
- +***************************************************************************/
- +
- +static inline void write_smi_reg(struct bcm2835_smi_instance *inst,
- + u32 val, unsigned reg)
- +{
- + writel(val, inst->smi_regs_ptr + reg);
- +}
- +
- +static inline u32 read_smi_reg(struct bcm2835_smi_instance *inst, unsigned reg)
- +{
- + return readl(inst->smi_regs_ptr + reg);
- +}
- +
- +/* Token-paste macro for e.g SMIDSR_RSTROBE -> value of SMIDSR_RSTROBE_MASK */
- +#define _CONCAT(x, y) x##y
- +#define CONCAT(x, y) _CONCAT(x, y)
- +
- +#define SET_BIT_FIELD(dest, field, bits) ((dest) = \
- + ((dest) & ~CONCAT(field, _MASK)) | (((bits) << CONCAT(field, _OFFS))& \
- + CONCAT(field, _MASK)))
- +#define GET_BIT_FIELD(src, field) (((src) & \
- + CONCAT(field, _MASK)) >> CONCAT(field, _OFFS))
- +
- +static void smi_dump_context_labelled(struct bcm2835_smi_instance *inst,
- + const char *label)
- +{
- + dev_err(inst->dev, "SMI context dump: %s", label);
- + dev_err(inst->dev, "SMICS: 0x%08x", read_smi_reg(inst, SMICS));
- + dev_err(inst->dev, "SMIL: 0x%08x", read_smi_reg(inst, SMIL));
- + dev_err(inst->dev, "SMIDSR: 0x%08x", read_smi_reg(inst, SMIDSR0));
- + dev_err(inst->dev, "SMIDSW: 0x%08x", read_smi_reg(inst, SMIDSW0));
- + dev_err(inst->dev, "SMIDC: 0x%08x", read_smi_reg(inst, SMIDC));
- + dev_err(inst->dev, "SMIFD: 0x%08x", read_smi_reg(inst, SMIFD));
- + dev_err(inst->dev, " ");
- +}
- +
- +static inline void smi_dump_context(struct bcm2835_smi_instance *inst)
- +{
- + smi_dump_context_labelled(inst, "");
- +}
- +
- +static void smi_get_default_settings(struct bcm2835_smi_instance *inst)
- +{
- + struct smi_settings *settings = &inst->settings;
- +
- + settings->data_width = SMI_WIDTH_16BIT;
- + settings->pack_data = true;
- +
- + settings->read_setup_time = 1;
- + settings->read_hold_time = 1;
- + settings->read_pace_time = 1;
- + settings->read_strobe_time = 3;
- +
- + settings->write_setup_time = settings->read_setup_time;
- + settings->write_hold_time = settings->read_hold_time;
- + settings->write_pace_time = settings->read_pace_time;
- + settings->write_strobe_time = settings->read_strobe_time;
- +
- + settings->dma_enable = true;
- + settings->dma_passthrough_enable = false;
- + settings->dma_read_thresh = 0x01;
- + settings->dma_write_thresh = 0x3f;
- + settings->dma_panic_read_thresh = 0x20;
- + settings->dma_panic_write_thresh = 0x20;
- +}
- +
- +void bcm2835_smi_set_regs_from_settings(struct bcm2835_smi_instance *inst)
- +{
- + struct smi_settings *settings = &inst->settings;
- + int smidsr_temp = 0, smidsw_temp = 0, smics_temp,
- + smidcs_temp, smidc_temp = 0;
- +
- + spin_lock(&inst->transaction_lock);
- +
- + /* temporarily disable the peripheral: */
- + smics_temp = read_smi_reg(inst, SMICS);
- + write_smi_reg(inst, 0, SMICS);
- + smidcs_temp = read_smi_reg(inst, SMIDCS);
- + write_smi_reg(inst, 0, SMIDCS);
- +
- + if (settings->pack_data)
- + smics_temp |= SMICS_PXLDAT;
- + else
- + smics_temp &= ~SMICS_PXLDAT;
- +
- + SET_BIT_FIELD(smidsr_temp, SMIDSR_RWIDTH, settings->data_width);
- + SET_BIT_FIELD(smidsr_temp, SMIDSR_RSETUP, settings->read_setup_time);
- + SET_BIT_FIELD(smidsr_temp, SMIDSR_RHOLD, settings->read_hold_time);
- + SET_BIT_FIELD(smidsr_temp, SMIDSR_RPACE, settings->read_pace_time);
- + SET_BIT_FIELD(smidsr_temp, SMIDSR_RSTROBE, settings->read_strobe_time);
- + write_smi_reg(inst, smidsr_temp, SMIDSR0);
- +
- + SET_BIT_FIELD(smidsw_temp, SMIDSW_WWIDTH, settings->data_width);
- + if (settings->data_width == SMI_WIDTH_8BIT)
- + smidsw_temp |= SMIDSW_WSWAP;
- + else
- + smidsw_temp &= ~SMIDSW_WSWAP;
- + SET_BIT_FIELD(smidsw_temp, SMIDSW_WSETUP, settings->write_setup_time);
- + SET_BIT_FIELD(smidsw_temp, SMIDSW_WHOLD, settings->write_hold_time);
- + SET_BIT_FIELD(smidsw_temp, SMIDSW_WPACE, settings->write_pace_time);
- + SET_BIT_FIELD(smidsw_temp, SMIDSW_WSTROBE,
- + settings->write_strobe_time);
- + write_smi_reg(inst, smidsw_temp, SMIDSW0);
- +
- + SET_BIT_FIELD(smidc_temp, SMIDC_REQR, settings->dma_read_thresh);
- + SET_BIT_FIELD(smidc_temp, SMIDC_REQW, settings->dma_write_thresh);
- + SET_BIT_FIELD(smidc_temp, SMIDC_PANICR,
- + settings->dma_panic_read_thresh);
- + SET_BIT_FIELD(smidc_temp, SMIDC_PANICW,
- + settings->dma_panic_write_thresh);
- + if (settings->dma_passthrough_enable) {
- + smidc_temp |= SMIDC_DMAP;
- + smidsr_temp |= SMIDSR_RDREQ;
- + write_smi_reg(inst, smidsr_temp, SMIDSR0);
- + smidsw_temp |= SMIDSW_WDREQ;
- + write_smi_reg(inst, smidsw_temp, SMIDSW0);
- + } else
- + smidc_temp &= ~SMIDC_DMAP;
- + if (settings->dma_enable)
- + smidc_temp |= SMIDC_DMAEN;
- + else
- + smidc_temp &= ~SMIDC_DMAEN;
- +
- + write_smi_reg(inst, smidc_temp, SMIDC);
- +
- + /* re-enable (if was previously enabled) */
- + write_smi_reg(inst, smics_temp, SMICS);
- + write_smi_reg(inst, smidcs_temp, SMIDCS);
- +
- + spin_unlock(&inst->transaction_lock);
- +}
- +EXPORT_SYMBOL(bcm2835_smi_set_regs_from_settings);
- +
- +struct smi_settings *bcm2835_smi_get_settings_from_regs
- + (struct bcm2835_smi_instance *inst)
- +{
- + struct smi_settings *settings = &inst->settings;
- + int smidsr, smidsw, smidc;
- +
- + spin_lock(&inst->transaction_lock);
- +
- + smidsr = read_smi_reg(inst, SMIDSR0);
- + smidsw = read_smi_reg(inst, SMIDSW0);
- + smidc = read_smi_reg(inst, SMIDC);
- +
- + settings->pack_data = (read_smi_reg(inst, SMICS) & SMICS_PXLDAT) ?
- + true : false;
- +
- + settings->data_width = GET_BIT_FIELD(smidsr, SMIDSR_RWIDTH);
- + settings->read_setup_time = GET_BIT_FIELD(smidsr, SMIDSR_RSETUP);
- + settings->read_hold_time = GET_BIT_FIELD(smidsr, SMIDSR_RHOLD);
- + settings->read_pace_time = GET_BIT_FIELD(smidsr, SMIDSR_RPACE);
- + settings->read_strobe_time = GET_BIT_FIELD(smidsr, SMIDSR_RSTROBE);
- +
- + settings->write_setup_time = GET_BIT_FIELD(smidsw, SMIDSW_WSETUP);
- + settings->write_hold_time = GET_BIT_FIELD(smidsw, SMIDSW_WHOLD);
- + settings->write_pace_time = GET_BIT_FIELD(smidsw, SMIDSW_WPACE);
- + settings->write_strobe_time = GET_BIT_FIELD(smidsw, SMIDSW_WSTROBE);
- +
- + settings->dma_read_thresh = GET_BIT_FIELD(smidc, SMIDC_REQR);
- + settings->dma_write_thresh = GET_BIT_FIELD(smidc, SMIDC_REQW);
- + settings->dma_panic_read_thresh = GET_BIT_FIELD(smidc, SMIDC_PANICR);
- + settings->dma_panic_write_thresh = GET_BIT_FIELD(smidc, SMIDC_PANICW);
- + settings->dma_passthrough_enable = (smidc & SMIDC_DMAP) ? true : false;
- + settings->dma_enable = (smidc & SMIDC_DMAEN) ? true : false;
- +
- + spin_unlock(&inst->transaction_lock);
- +
- + return settings;
- +}
- +EXPORT_SYMBOL(bcm2835_smi_get_settings_from_regs);
- +
- +static inline void smi_set_address(struct bcm2835_smi_instance *inst,
- + unsigned int address)
- +{
- + int smia_temp = 0, smida_temp = 0;
- +
- + SET_BIT_FIELD(smia_temp, SMIA_ADDR, address);
- + SET_BIT_FIELD(smida_temp, SMIDA_ADDR, address);
- +
- + /* Write to both address registers - user doesn't care whether we're
- + doing programmed or direct transfers. */
- + write_smi_reg(inst, smia_temp, SMIA);
- + write_smi_reg(inst, smida_temp, SMIDA);
- +}
- +
- +static void smi_setup_regs(struct bcm2835_smi_instance *inst)
- +{
- +
- + dev_dbg(inst->dev, "Initialising SMI registers...");
- + /* Disable the peripheral if already enabled */
- + write_smi_reg(inst, 0, SMICS);
- + write_smi_reg(inst, 0, SMIDCS);
- +
- + smi_get_default_settings(inst);
- + bcm2835_smi_set_regs_from_settings(inst);
- + smi_set_address(inst, 0);
- +
- + write_smi_reg(inst, read_smi_reg(inst, SMICS) | SMICS_ENABLE, SMICS);
- + write_smi_reg(inst, read_smi_reg(inst, SMIDCS) | SMIDCS_ENABLE,
- + SMIDCS);
- +}
- +
- +/****************************************************************************
- +*
- +* Low-level SMI access functions
- +* Other modules should use the exported higher-level functions e.g.
- +* bcm2835_smi_write_buf() unless they have a good reason to use these
- +*
- +***************************************************************************/
- +
- +static inline uint32_t smi_read_single_word(struct bcm2835_smi_instance *inst)
- +{
- + int timeout = 0;
- +
- + write_smi_reg(inst, SMIDCS_ENABLE, SMIDCS);
- + write_smi_reg(inst, SMIDCS_ENABLE | SMIDCS_START, SMIDCS);
- + /* Make sure things happen in the right order...*/
- + mb();
- + while (!(read_smi_reg(inst, SMIDCS) & SMIDCS_DONE) &&
- + ++timeout < 10000)
- + ;
- + if (timeout < 10000)
- + return read_smi_reg(inst, SMIDD);
- +
- + dev_err(inst->dev,
- + "SMI direct read timed out (is the clock set up correctly?)");
- + return 0;
- +}
- +
- +static inline void smi_write_single_word(struct bcm2835_smi_instance *inst,
- + uint32_t data)
- +{
- + int timeout = 0;
- +
- + write_smi_reg(inst, SMIDCS_ENABLE | SMIDCS_WRITE, SMIDCS);
- + write_smi_reg(inst, data, SMIDD);
- + write_smi_reg(inst, SMIDCS_ENABLE | SMIDCS_WRITE | SMIDCS_START,
- + SMIDCS);
- +
- + while (!(read_smi_reg(inst, SMIDCS) & SMIDCS_DONE) &&
- + ++timeout < 10000)
- + ;
- + if (timeout >= 10000)
- + dev_err(inst->dev,
- + "SMI direct write timed out (is the clock set up correctly?)");
- +}
- +
- +/* Initiates a programmed read into the read FIFO. It is up to the caller to
- + * read data from the FIFO - either via paced DMA transfer,
- + * or polling SMICS_RXD to check whether data is available.
- + * SMICS_ACTIVE will go low upon completion. */
- +static void smi_init_programmed_read(struct bcm2835_smi_instance *inst,
- + int num_transfers)
- +{
- + int smics_temp;
- +
- + /* Disable the peripheral: */
- + smics_temp = read_smi_reg(inst, SMICS) & ~(SMICS_ENABLE | SMICS_WRITE);
- + write_smi_reg(inst, smics_temp, SMICS);
- + while (read_smi_reg(inst, SMICS) & SMICS_ENABLE)
- + ;
- +
- + /* Program the transfer count: */
- + write_smi_reg(inst, num_transfers, SMIL);
- +
- + /* re-enable and start: */
- + smics_temp |= SMICS_ENABLE;
- + write_smi_reg(inst, smics_temp, SMICS);
- + smics_temp |= SMICS_CLEAR;
- + /* Just to be certain: */
- + mb();
- + while (read_smi_reg(inst, SMICS) & SMICS_ACTIVE)
- + ;
- + write_smi_reg(inst, smics_temp, SMICS);
- + smics_temp |= SMICS_START;
- + write_smi_reg(inst, smics_temp, SMICS);
- +}
- +
- +/* Initiates a programmed write sequence, using data from the write FIFO.
- + * It is up to the caller to initiate a DMA transfer before calling,
- + * or use another method to keep the write FIFO topped up.
- + * SMICS_ACTIVE will go low upon completion.
- + */
- +static void smi_init_programmed_write(struct bcm2835_smi_instance *inst,
- + int num_transfers)
- +{
- + int smics_temp;
- +
- + /* Disable the peripheral: */
- + smics_temp = read_smi_reg(inst, SMICS) & ~SMICS_ENABLE;
- + write_smi_reg(inst, smics_temp, SMICS);
- + while (read_smi_reg(inst, SMICS) & SMICS_ENABLE)
- + ;
- +
- + /* Program the transfer count: */
- + write_smi_reg(inst, num_transfers, SMIL);
- +
- + /* setup, re-enable and start: */
- + smics_temp |= SMICS_WRITE | SMICS_ENABLE;
- + write_smi_reg(inst, smics_temp, SMICS);
- + smics_temp |= SMICS_START;
- + write_smi_reg(inst, smics_temp, SMICS);
- +}
- +
- +/* Initiate a read and then poll FIFO for data, reading out as it appears. */
- +static void smi_read_fifo(struct bcm2835_smi_instance *inst,
- + uint32_t *dest, int n_bytes)
- +{
- + if (read_smi_reg(inst, SMICS) & SMICS_RXD) {
- + smi_dump_context_labelled(inst,
- + "WARNING: read FIFO not empty at start of read call.");
- + while (read_smi_reg(inst, SMICS))
- + ;
- + }
- +
- + /* Dispatch the read: */
- + if (inst->settings.data_width == SMI_WIDTH_8BIT)
- + smi_init_programmed_read(inst, n_bytes);
- + else if (inst->settings.data_width == SMI_WIDTH_16BIT)
- + smi_init_programmed_read(inst, n_bytes / 2);
- + else {
- + dev_err(inst->dev, "Unsupported data width for read.");
- + return;
- + }
- +
- + /* Poll FIFO to keep it empty */
- + while (!(read_smi_reg(inst, SMICS) & SMICS_DONE))
- + if (read_smi_reg(inst, SMICS) & SMICS_RXD)
- + *dest++ = read_smi_reg(inst, SMID);
- +
- + /* Ensure that the FIFO is emptied */
- + if (read_smi_reg(inst, SMICS) & SMICS_RXD) {
- + int fifo_count;
- +
- + fifo_count = GET_BIT_FIELD(read_smi_reg(inst, SMIFD),
- + SMIFD_FCNT);
- + while (fifo_count--)
- + *dest++ = read_smi_reg(inst, SMID);
- + }
- +
- + if (!(read_smi_reg(inst, SMICS) & SMICS_DONE))
- + smi_dump_context_labelled(inst,
- + "WARNING: transaction finished but done bit not set.");
- +
- + if (read_smi_reg(inst, SMICS) & SMICS_RXD)
- + smi_dump_context_labelled(inst,
- + "WARNING: read FIFO not empty at end of read call.");
- +
- +}
- +
- +/* Initiate a write, and then keep the FIFO topped up. */
- +static void smi_write_fifo(struct bcm2835_smi_instance *inst,
- + uint32_t *src, int n_bytes)
- +{
- + int i, timeout = 0;
- +
- + /* Empty FIFOs if not already so */
- + if (!(read_smi_reg(inst, SMICS) & SMICS_TXE)) {
- + smi_dump_context_labelled(inst,
- + "WARNING: write fifo not empty at start of write call.");
- + write_smi_reg(inst, read_smi_reg(inst, SMICS) | SMICS_CLEAR,
- + SMICS);
- + }
- +
- + /* Initiate the transfer */
- + if (inst->settings.data_width == SMI_WIDTH_8BIT)
- + smi_init_programmed_write(inst, n_bytes);
- + else if (inst->settings.data_width == SMI_WIDTH_16BIT)
- + smi_init_programmed_write(inst, n_bytes / 2);
- + else {
- + dev_err(inst->dev, "Unsupported data width for write.");
- + return;
- + }
- + /* Fill the FIFO: */
- + for (i = 0; i < (n_bytes - 1) / 4 + 1; ++i) {
- + while (!(read_smi_reg(inst, SMICS) & SMICS_TXD))
- + ;
- + write_smi_reg(inst, *src++, SMID);
- + }
- + /* Busy wait... */
- + while (!(read_smi_reg(inst, SMICS) & SMICS_DONE) && ++timeout <
- + 1000000)
- + ;
- + if (timeout >= 1000000)
- + smi_dump_context_labelled(inst,
- + "Timed out on write operation!");
- + if (!(read_smi_reg(inst, SMICS) & SMICS_TXE))
- + smi_dump_context_labelled(inst,
- + "WARNING: FIFO not empty at end of write operation.");
- +}
- +
- +/****************************************************************************
- +*
- +* SMI DMA operations
- +*
- +***************************************************************************/
- +
- +/* Disable SMI and put it into the correct direction before doing DMA setup.
- + Stops spurious DREQs during setup. Peripheral is re-enabled by init_*() */
- +static void smi_disable(struct bcm2835_smi_instance *inst,
- + enum dma_transfer_direction direction)
- +{
- + int smics_temp = read_smi_reg(inst, SMICS) & ~SMICS_ENABLE;
- +
- + if (direction == DMA_DEV_TO_MEM)
- + smics_temp &= ~SMICS_WRITE;
- + else
- + smics_temp |= SMICS_WRITE;
- + write_smi_reg(inst, smics_temp, SMICS);
- + while (read_smi_reg(inst, SMICS) & SMICS_ACTIVE)
- + ;
- +}
- +
- +static struct scatterlist *smi_scatterlist_from_buffer(
- + struct bcm2835_smi_instance *inst,
- + dma_addr_t buf,
- + size_t len,
- + struct scatterlist *sg)
- +{
- + sg_init_table(sg, 1);
- + sg_dma_address(sg) = buf;
- + sg_dma_len(sg) = len;
- + return sg;
- +}
- +
- +static void smi_dma_callback_user_copy(void *param)
- +{
- + /* Notify the bottom half that a chunk is ready for user copy */
- + struct bcm2835_smi_instance *inst =
- + (struct bcm2835_smi_instance *)param;
- +
- + up(&inst->bounce.callback_sem);
- +}
- +
- +/* Creates a descriptor, assigns the given callback, and submits the
- + descriptor to dmaengine. Does not block - can queue up multiple
- + descriptors and then wait for them all to complete.
- + sg_len is the number of control blocks, NOT the number of bytes.
- + dir can be DMA_MEM_TO_DEV or DMA_DEV_TO_MEM.
- + callback can be NULL - in this case it is not called. */
- +static inline struct dma_async_tx_descriptor *smi_dma_submit_sgl(
- + struct bcm2835_smi_instance *inst,
- + struct scatterlist *sgl,
- + size_t sg_len,
- + enum dma_transfer_direction dir,
- + dma_async_tx_callback callback)
- +{
- + struct dma_async_tx_descriptor *desc;
- +
- + desc = dmaengine_prep_slave_sg(inst->dma_chan,
- + sgl,
- + sg_len,
- + dir,
- + DMA_PREP_INTERRUPT | DMA_CTRL_ACK |
- + DMA_PREP_FENCE);
- + if (!desc) {
- + dev_err(inst->dev, "read_sgl: dma slave preparation failed!");
- + write_smi_reg(inst, read_smi_reg(inst, SMICS) & ~SMICS_ACTIVE,
- + SMICS);
- + while (read_smi_reg(inst, SMICS) & SMICS_ACTIVE)
- + cpu_relax();
- + write_smi_reg(inst, read_smi_reg(inst, SMICS) | SMICS_ACTIVE,
- + SMICS);
- + return NULL;
- + }
- + desc->callback = callback;
- + desc->callback_param = inst;
- + if (dmaengine_submit(desc) < 0)
- + return NULL;
- + return desc;
- +}
- +
- +/* NB this function blocks until the transfer is complete */
- +static void
- +smi_dma_read_sgl(struct bcm2835_smi_instance *inst,
- + struct scatterlist *sgl, size_t sg_len, size_t n_bytes)
- +{
- + struct dma_async_tx_descriptor *desc;
- +
- + /* Disable SMI and set to read before dispatching DMA - if SMI is in
- + * write mode and TX fifo is empty, it will generate a DREQ which may
- + * cause the read DMA to complete before the SMI read command is even
- + * dispatched! We want to dispatch DMA before SMI read so that reading
- + * is gapless, for logic analyser.
- + */
- +
- + smi_disable(inst, DMA_DEV_TO_MEM);
- +
- + desc = smi_dma_submit_sgl(inst, sgl, sg_len, DMA_DEV_TO_MEM, NULL);
- + dma_async_issue_pending(inst->dma_chan);
- +
- + if (inst->settings.data_width == SMI_WIDTH_8BIT)
- + smi_init_programmed_read(inst, n_bytes);
- + else
- + smi_init_programmed_read(inst, n_bytes / 2);
- +
- + if (dma_wait_for_async_tx(desc) == DMA_ERROR)
- + smi_dump_context_labelled(inst, "DMA timeout!");
- +}
- +
- +static void
- +smi_dma_write_sgl(struct bcm2835_smi_instance *inst,
- + struct scatterlist *sgl, size_t sg_len, size_t n_bytes)
- +{
- + struct dma_async_tx_descriptor *desc;
- +
- + if (inst->settings.data_width == SMI_WIDTH_8BIT)
- + smi_init_programmed_write(inst, n_bytes);
- + else
- + smi_init_programmed_write(inst, n_bytes / 2);
- +
- + desc = smi_dma_submit_sgl(inst, sgl, sg_len, DMA_MEM_TO_DEV, NULL);
- + dma_async_issue_pending(inst->dma_chan);
- +
- + if (dma_wait_for_async_tx(desc) == DMA_ERROR)
- + smi_dump_context_labelled(inst, "DMA timeout!");
- + else
- + /* Wait for SMI to finish our writes */
- + while (!(read_smi_reg(inst, SMICS) & SMICS_DONE))
- + cpu_relax();
- +}
- +
- +ssize_t bcm2835_smi_user_dma(
- + struct bcm2835_smi_instance *inst,
- + enum dma_transfer_direction dma_dir,
- + char __user *user_ptr, size_t count,
- + struct bcm2835_smi_bounce_info **bounce)
- +{
- + int chunk_no = 0, chunk_size, count_left = count;
- + struct scatterlist *sgl;
- + void (*init_trans_func)(struct bcm2835_smi_instance *, int);
- +
- + spin_lock(&inst->transaction_lock);
- +
- + if (dma_dir == DMA_DEV_TO_MEM)
- + init_trans_func = smi_init_programmed_read;
- + else
- + init_trans_func = smi_init_programmed_write;
- +
- + smi_disable(inst, dma_dir);
- +
- + sema_init(&inst->bounce.callback_sem, 0);
- + if (bounce)
- + *bounce = &inst->bounce;
- + while (count_left) {
- + chunk_size = count_left > DMA_BOUNCE_BUFFER_SIZE ?
- + DMA_BOUNCE_BUFFER_SIZE : count_left;
- + if (chunk_size == DMA_BOUNCE_BUFFER_SIZE) {
- + sgl =
- + &inst->bounce.sgl[chunk_no % DMA_BOUNCE_BUFFER_COUNT];
- + } else {
- + sgl = smi_scatterlist_from_buffer(
- + inst,
- + inst->bounce.phys[
- + chunk_no % DMA_BOUNCE_BUFFER_COUNT],
- + chunk_size,
- + &inst->buffer_sgl);
- + }
- +
- + if (!smi_dma_submit_sgl(inst, sgl, 1, dma_dir,
- + smi_dma_callback_user_copy
- + )) {
- + dev_err(inst->dev, "sgl submit failed");
- + count = 0;
- + goto out;
- + }
- + count_left -= chunk_size;
- + chunk_no++;
- + }
- + dma_async_issue_pending(inst->dma_chan);
- +
- + if (inst->settings.data_width == SMI_WIDTH_8BIT)
- + init_trans_func(inst, count);
- + else if (inst->settings.data_width == SMI_WIDTH_16BIT)
- + init_trans_func(inst, count / 2);
- +out:
- + spin_unlock(&inst->transaction_lock);
- + return count;
- +}
- +EXPORT_SYMBOL(bcm2835_smi_user_dma);
- +
- +
- +/****************************************************************************
- +*
- +* High level buffer transfer functions - for use by other drivers
- +*
- +***************************************************************************/
- +
- +/* Buffer must be physically contiguous - i.e. kmalloc, not vmalloc! */
- +void bcm2835_smi_write_buf(
- + struct bcm2835_smi_instance *inst,
- + const void *buf, size_t n_bytes)
- +{
- + int odd_bytes = n_bytes & 0x3;
- +
- + n_bytes -= odd_bytes;
- +
- + spin_lock(&inst->transaction_lock);
- +
- + if (n_bytes > DMA_THRESHOLD_BYTES) {
- + dma_addr_t phy_addr = dma_map_single(
- + inst->dev,
- + (void *)buf,
- + n_bytes,
- + DMA_MEM_TO_DEV);
- + struct scatterlist *sgl =
- + smi_scatterlist_from_buffer(inst, phy_addr, n_bytes,
- + &inst->buffer_sgl);
- +
- + if (!sgl) {
- + smi_dump_context_labelled(inst,
- + "Error: could not create scatterlist for write!");
- + goto out;
- + }
- + smi_dma_write_sgl(inst, sgl, 1, n_bytes);
- +
- + dma_unmap_single
- + (inst->dev, phy_addr, n_bytes, DMA_MEM_TO_DEV);
- + } else if (n_bytes) {
- + smi_write_fifo(inst, (uint32_t *) buf, n_bytes);
- + }
- + buf += n_bytes;
- +
- + if (inst->settings.data_width == SMI_WIDTH_8BIT) {
- + while (odd_bytes--)
- + smi_write_single_word(inst, *(uint8_t *) (buf++));
- + } else {
- + while (odd_bytes >= 2) {
- + smi_write_single_word(inst, *(uint16_t *)buf);
- + buf += 2;
- + odd_bytes -= 2;
- + }
- + if (odd_bytes) {
- + /* Reading an odd number of bytes on a 16 bit bus is
- + a user bug. It's kinder to fail early and tell them
- + than to e.g. transparently give them the bottom byte
- + of a 16 bit transfer. */
- + dev_err(inst->dev,
- + "WARNING: odd number of bytes specified for wide transfer.");
- + dev_err(inst->dev,
- + "At least one byte dropped as a result.");
- + dump_stack();
- + }
- + }
- +out:
- + spin_unlock(&inst->transaction_lock);
- +}
- +EXPORT_SYMBOL(bcm2835_smi_write_buf);
- +
- +void bcm2835_smi_read_buf(struct bcm2835_smi_instance *inst,
- + void *buf, size_t n_bytes)
- +{
- +
- + /* SMI is inherently 32-bit, which causes surprising amounts of mess
- + for bytes % 4 != 0. Easiest to avoid this mess altogether
- + by handling remainder separately. */
- + int odd_bytes = n_bytes & 0x3;
- +
- + spin_lock(&inst->transaction_lock);
- + n_bytes -= odd_bytes;
- + if (n_bytes > DMA_THRESHOLD_BYTES) {
- + dma_addr_t phy_addr = dma_map_single(inst->dev,
- + buf, n_bytes,
- + DMA_DEV_TO_MEM);
- + struct scatterlist *sgl = smi_scatterlist_from_buffer(
- + inst, phy_addr, n_bytes,
- + &inst->buffer_sgl);
- + if (!sgl) {
- + smi_dump_context_labelled(inst,
- + "Error: could not create scatterlist for read!");
- + goto out;
- + }
- + smi_dma_read_sgl(inst, sgl, 1, n_bytes);
- + dma_unmap_single(inst->dev, phy_addr, n_bytes, DMA_DEV_TO_MEM);
- + } else if (n_bytes) {
- + smi_read_fifo(inst, (uint32_t *)buf, n_bytes);
- + }
- + buf += n_bytes;
- +
- + if (inst->settings.data_width == SMI_WIDTH_8BIT) {
- + while (odd_bytes--)
- + *((uint8_t *) (buf++)) = smi_read_single_word(inst);
- + } else {
- + while (odd_bytes >= 2) {
- + *(uint16_t *) buf = smi_read_single_word(inst);
- + buf += 2;
- + odd_bytes -= 2;
- + }
- + if (odd_bytes) {
- + dev_err(inst->dev,
- + "WARNING: odd number of bytes specified for wide transfer.");
- + dev_err(inst->dev,
- + "At least one byte dropped as a result.");
- + dump_stack();
- + }
- + }
- +out:
- + spin_unlock(&inst->transaction_lock);
- +}
- +EXPORT_SYMBOL(bcm2835_smi_read_buf);
- +
- +void bcm2835_smi_set_address(struct bcm2835_smi_instance *inst,
- + unsigned int address)
- +{
- + spin_lock(&inst->transaction_lock);
- + smi_set_address(inst, address);
- + spin_unlock(&inst->transaction_lock);
- +}
- +EXPORT_SYMBOL(bcm2835_smi_set_address);
- +
- +struct bcm2835_smi_instance *bcm2835_smi_get(struct device_node *node)
- +{
- + struct platform_device *pdev;
- +
- + if (!node)
- + return NULL;
- +
- + pdev = of_find_device_by_node(node);
- + if (!pdev)
- + return NULL;
- +
- + return platform_get_drvdata(pdev);
- +}
- +EXPORT_SYMBOL(bcm2835_smi_get);
- +
- +/****************************************************************************
- +*
- +* bcm2835_smi_probe - called when the driver is loaded.
- +*
- +***************************************************************************/
- +
- +static int bcm2835_smi_dma_setup(struct bcm2835_smi_instance *inst)
- +{
- + int i, rv = 0;
- +
- + inst->dma_chan = dma_request_slave_channel(inst->dev, "rx-tx");
- +
- + inst->dma_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
- + inst->dma_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
- + inst->dma_config.src_addr = inst->smi_regs_busaddr + SMID;
- + inst->dma_config.dst_addr = inst->dma_config.src_addr;
- + /* Direction unimportant - always overridden by prep_slave_sg */
- + inst->dma_config.direction = DMA_DEV_TO_MEM;
- + dmaengine_slave_config(inst->dma_chan, &inst->dma_config);
- + /* Alloc and map bounce buffers */
- + for (i = 0; i < DMA_BOUNCE_BUFFER_COUNT; ++i) {
- + inst->bounce.buffer[i] =
- + dmam_alloc_coherent(inst->dev, DMA_BOUNCE_BUFFER_SIZE,
- + &inst->bounce.phys[i],
- + GFP_KERNEL);
- + if (!inst->bounce.buffer[i]) {
- + dev_err(inst->dev, "Could not allocate buffer!");
- + rv = -ENOMEM;
- + break;
- + }
- + smi_scatterlist_from_buffer(
- + inst,
- + inst->bounce.phys[i],
- + DMA_BOUNCE_BUFFER_SIZE,
- + &inst->bounce.sgl[i]
- + );
- + }
- +
- + return rv;
- +}
- +
- +static int bcm2835_smi_probe(struct platform_device *pdev)
- +{
- + int err;
- + struct device *dev = &pdev->dev;
- + struct device_node *node = dev->of_node;
- + struct resource *ioresource;
- + struct bcm2835_smi_instance *inst;
- +
- + /* Allocate buffers and instance data */
- +
- + inst = devm_kzalloc(dev, sizeof(struct bcm2835_smi_instance),
- + GFP_KERNEL);
- +
- + if (!inst)
- + return -ENOMEM;
- +
- + inst->dev = dev;
- + spin_lock_init(&inst->transaction_lock);
- +
- + /* We require device tree support */
- + if (!node)
- + return -EINVAL;
- +
- + ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- + inst->smi_regs_ptr = devm_ioremap_resource(dev, ioresource);
- + ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 1);
- + inst->cm_smi_regs_ptr = devm_ioremap_resource(dev, ioresource);
- + inst->smi_regs_busaddr = be32_to_cpu(
- + *of_get_address(node, 0, NULL, NULL));
- + of_property_read_u32(node,
- + "brcm,smi-clock-source",
- + &inst->clock_source);
- + of_property_read_u32(node,
- + "brcm,smi-clock-divisor",
- + &inst->clock_divisor);
- +
- + err = bcm2835_smi_dma_setup(inst);
- + if (err)
- + return err;
- +
- + /* Finally, do peripheral setup */
- +
- + smi_setup_clock(inst);
- + smi_setup_regs(inst);
- +
- + platform_set_drvdata(pdev, inst);
- +
- + dev_info(inst->dev, "initialised");
- +
- + return 0;
- +}
- +
- +/****************************************************************************
- +*
- +* bcm2835_smi_remove - called when the driver is unloaded.
- +*
- +***************************************************************************/
- +
- +static int bcm2835_smi_remove(struct platform_device *pdev)
- +{
- + struct bcm2835_smi_instance *inst = platform_get_drvdata(pdev);
- + struct device *dev = inst->dev;
- +
- + dev_info(dev, "SMI device removed - OK");
- + return 0;
- +}
- +
- +/****************************************************************************
- +*
- +* Register the driver with device tree
- +*
- +***************************************************************************/
- +
- +static const struct of_device_id bcm2835_smi_of_match[] = {
- + {.compatible = "brcm,bcm2835-smi",},
- + { /* sentinel */ },
- +};
- +
- +MODULE_DEVICE_TABLE(of, bcm2835_smi_of_match);
- +
- +static struct platform_driver bcm2835_smi_driver = {
- + .probe = bcm2835_smi_probe,
- + .remove = bcm2835_smi_remove,
- + .driver = {
- + .name = DRIVER_NAME,
- + .owner = THIS_MODULE,
- + .of_match_table = bcm2835_smi_of_match,
- + },
- +};
- +
- +module_platform_driver(bcm2835_smi_driver);
- +
- +MODULE_ALIAS("platform:smi-bcm2835");
- +MODULE_LICENSE("GPL");
- +MODULE_DESCRIPTION("Device driver for BCM2835's secondary memory interface");
- +MODULE_AUTHOR("Luke Wren <luke@raspberrypi.org>");
- --- /dev/null
- +++ b/include/linux/broadcom/bcm2835_smi.h
- @@ -0,0 +1,391 @@
- +/**
- + * Declarations and definitions for Broadcom's Secondary Memory Interface
- + *
- + * Written by Luke Wren <luke@raspberrypi.org>
- + * Copyright (c) 2015, Raspberry Pi (Trading) Ltd.
- + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
- + *
- + * Redistribution and use in source and binary forms, with or without
- + * modification, are permitted provided that the following conditions
- + * are met:
- + * 1. Redistributions of source code must retain the above copyright
- + * notice, this list of conditions, and the following disclaimer,
- + * without modification.
- + * 2. Redistributions in binary form must reproduce the above copyright
- + * notice, this list of conditions and the following disclaimer in the
- + * documentation and/or other materials provided with the distribution.
- + * 3. The names of the above-listed copyright holders may not be used
- + * to endorse or promote products derived from this software without
- + * specific prior written permission.
- + *
- + * ALTERNATIVELY, this software may be distributed under the terms of the
- + * GNU General Public License ("GPL") version 2, as published by the Free
- + * Software Foundation.
- + *
- + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- + */
- +
- +#ifndef BCM2835_SMI_H
- +#define BCM2835_SMI_H
- +
- +#include <linux/ioctl.h>
- +
- +#ifndef __KERNEL__
- +#include <stdint.h>
- +#include <stdbool.h>
- +#endif
- +
- +#define BCM2835_SMI_IOC_MAGIC 0x1
- +#define BCM2835_SMI_INVALID_HANDLE (~0)
- +
- +/* IOCTLs 0x100...0x1ff are not device-specific - we can use them */
- +#define BCM2835_SMI_IOC_GET_SETTINGS _IO(BCM2835_SMI_IOC_MAGIC, 0)
- +#define BCM2835_SMI_IOC_WRITE_SETTINGS _IO(BCM2835_SMI_IOC_MAGIC, 1)
- +#define BCM2835_SMI_IOC_ADDRESS _IO(BCM2835_SMI_IOC_MAGIC, 2)
- +#define BCM2835_SMI_IOC_MAX 2
- +
- +#define SMI_WIDTH_8BIT 0
- +#define SMI_WIDTH_16BIT 1
- +#define SMI_WIDTH_9BIT 2
- +#define SMI_WIDTH_18BIT 3
- +
- +/* max number of bytes where DMA will not be used */
- +#define DMA_THRESHOLD_BYTES 128
- +#define DMA_BOUNCE_BUFFER_SIZE (1024 * 1024 / 2)
- +#define DMA_BOUNCE_BUFFER_COUNT 3
- +
- +
- +struct smi_settings {
- + int data_width;
- + /* Whether or not to pack multiple SMI transfers into a
- + single 32 bit FIFO word */
- + bool pack_data;
- +
- + /* Timing for reads (writes the same but for WE)
- + *
- + * OE ----------+ +--------------------
- + * | |
- + * +----------+
- + * SD -<==============================>-----------
- + * SA -<=========================================>-
- + * <-setup-> <-strobe -> <-hold -> <- pace ->
- + */
- +
- + int read_setup_time;
- + int read_hold_time;
- + int read_pace_time;
- + int read_strobe_time;
- +
- + int write_setup_time;
- + int write_hold_time;
- + int write_pace_time;
- + int write_strobe_time;
- +
- + bool dma_enable; /* DREQs */
- + bool dma_passthrough_enable; /* External DREQs */
- + int dma_read_thresh;
- + int dma_write_thresh;
- + int dma_panic_read_thresh;
- + int dma_panic_write_thresh;
- +};
- +
- +/****************************************************************************
- +*
- +* Declare exported SMI functions
- +*
- +***************************************************************************/
- +
- +#ifdef __KERNEL__
- +
- +#include <linux/dmaengine.h> /* for enum dma_transfer_direction */
- +#include <linux/of.h>
- +#include <linux/semaphore.h>
- +
- +struct bcm2835_smi_instance;
- +
- +struct bcm2835_smi_bounce_info {
- + struct semaphore callback_sem;
- + void *buffer[DMA_BOUNCE_BUFFER_COUNT];
- + dma_addr_t phys[DMA_BOUNCE_BUFFER_COUNT];
- + struct scatterlist sgl[DMA_BOUNCE_BUFFER_COUNT];
- +};
- +
- +
- +void bcm2835_smi_set_regs_from_settings(struct bcm2835_smi_instance *);
- +
- +struct smi_settings *bcm2835_smi_get_settings_from_regs(
- + struct bcm2835_smi_instance *inst);
- +
- +void bcm2835_smi_write_buf(
- + struct bcm2835_smi_instance *inst,
- + const void *buf,
- + size_t n_bytes);
- +
- +void bcm2835_smi_read_buf(
- + struct bcm2835_smi_instance *inst,
- + void *buf,
- + size_t n_bytes);
- +
- +void bcm2835_smi_set_address(struct bcm2835_smi_instance *inst,
- + unsigned int address);
- +
- +ssize_t bcm2835_smi_user_dma(
- + struct bcm2835_smi_instance *inst,
- + enum dma_transfer_direction dma_dir,
- + char __user *user_ptr,
- + size_t count,
- + struct bcm2835_smi_bounce_info **bounce);
- +
- +struct bcm2835_smi_instance *bcm2835_smi_get(struct device_node *node);
- +
- +#endif /* __KERNEL__ */
- +
- +/****************************************************************
- +*
- +* Implementation-only declarations
- +*
- +****************************************************************/
- +
- +#ifdef BCM2835_SMI_IMPLEMENTATION
- +
- +/* Clock manager registers for SMI clock: */
- +#define CM_SMI_BASE_ADDRESS ((BCM2708_PERI_BASE) + 0x1010b0)
- +/* Clock manager "password" to protect registers from spurious writes */
- +#define CM_PWD (0x5a << 24)
- +
- +#define CM_SMI_CTL 0x00
- +#define CM_SMI_DIV 0x04
- +
- +#define CM_SMI_CTL_FLIP (1 << 8)
- +#define CM_SMI_CTL_BUSY (1 << 7)
- +#define CM_SMI_CTL_KILL (1 << 5)
- +#define CM_SMI_CTL_ENAB (1 << 4)
- +#define CM_SMI_CTL_SRC_MASK (0xf)
- +#define CM_SMI_CTL_SRC_OFFS (0)
- +
- +#define CM_SMI_DIV_DIVI_MASK (0xf << 12)
- +#define CM_SMI_DIV_DIVI_OFFS (12)
- +#define CM_SMI_DIV_DIVF_MASK (0xff << 4)
- +#define CM_SMI_DIV_DIVF_OFFS (4)
- +
- +/* SMI register mapping:*/
- +#define SMI_BASE_ADDRESS ((BCM2708_PERI_BASE) + 0x600000)
- +
- +#define SMICS 0x00 /* control + status register */
- +#define SMIL 0x04 /* length/count (n external txfers) */
- +#define SMIA 0x08 /* address register */
- +#define SMID 0x0c /* data register */
- +#define SMIDSR0 0x10 /* device 0 read settings */
- +#define SMIDSW0 0x14 /* device 0 write settings */
- +#define SMIDSR1 0x18 /* device 1 read settings */
- +#define SMIDSW1 0x1c /* device 1 write settings */
- +#define SMIDSR2 0x20 /* device 2 read settings */
- +#define SMIDSW2 0x24 /* device 2 write settings */
- +#define SMIDSR3 0x28 /* device 3 read settings */
- +#define SMIDSW3 0x2c /* device 3 write settings */
- +#define SMIDC 0x30 /* DMA control registers */
- +#define SMIDCS 0x34 /* direct control/status register */
- +#define SMIDA 0x38 /* direct address register */
- +#define SMIDD 0x3c /* direct data registers */
- +#define SMIFD 0x40 /* FIFO debug register */
- +
- +
- +
- +/* Control and Status register bits:
- + * SMICS_RXF : RX fifo full: 1 when RX fifo is full
- + * SMICS_TXE : TX fifo empty: 1 when empty.
- + * SMICS_RXD : RX fifo contains data: 1 when there is data.
- + * SMICS_TXD : TX fifo can accept data: 1 when true.
- + * SMICS_RXR : RX fifo needs reading: 1 when fifo more than 3/4 full, or
- + * when "DONE" and fifo not emptied.
- + * SMICS_TXW : TX fifo needs writing: 1 when less than 1/4 full.
- + * SMICS_AFERR : AXI FIFO error: 1 when fifo read when empty or written
- + * when full. Write 1 to clear.
- + * SMICS_EDREQ : 1 when external DREQ received.
- + * SMICS_PXLDAT : Pixel data: write 1 to enable pixel transfer modes.
- + * SMICS_SETERR : 1 if there was an error writing to setup regs (e.g.
- + * tx was in progress). Write 1 to clear.
- + * SMICS_PVMODE : Set to 1 to enable pixel valve mode.
- + * SMICS_INTR : Set to 1 to enable interrupt on RX.
- + * SMICS_INTT : Set to 1 to enable interrupt on TX.
- + * SMICS_INTD : Set to 1 to enable interrupt on DONE condition.
- + * SMICS_TEEN : Tear effect mode enabled: Programmed transfers will wait
- + * for a TE trigger before writing.
- + * SMICS_PAD1 : Padding settings for external transfers. For writes: the
- + * number of bytes initially written to the TX fifo that
- + * SMICS_PAD0 : should be ignored. For reads: the number of bytes that will
- + * be read before the data, and should be dropped.
- + * SMICS_WRITE : Transfer direction: 1 = write to external device, 0 = read
- + * SMICS_CLEAR : Write 1 to clear the FIFOs.
- + * SMICS_START : Write 1 to start the programmed transfer.
- + * SMICS_ACTIVE : Reads as 1 when a programmed transfer is underway.
- + * SMICS_DONE : Reads as 1 when transfer finished. For RX, not set until
- + * FIFO emptied.
- + * SMICS_ENABLE : Set to 1 to enable the SMI peripheral, 0 to disable.
- + */
- +
- +#define SMICS_RXF (1 << 31)
- +#define SMICS_TXE (1 << 30)
- +#define SMICS_RXD (1 << 29)
- +#define SMICS_TXD (1 << 28)
- +#define SMICS_RXR (1 << 27)
- +#define SMICS_TXW (1 << 26)
- +#define SMICS_AFERR (1 << 25)
- +#define SMICS_EDREQ (1 << 15)
- +#define SMICS_PXLDAT (1 << 14)
- +#define SMICS_SETERR (1 << 13)
- +#define SMICS_PVMODE (1 << 12)
- +#define SMICS_INTR (1 << 11)
- +#define SMICS_INTT (1 << 10)
- +#define SMICS_INTD (1 << 9)
- +#define SMICS_TEEN (1 << 8)
- +#define SMICS_PAD1 (1 << 7)
- +#define SMICS_PAD0 (1 << 6)
- +#define SMICS_WRITE (1 << 5)
- +#define SMICS_CLEAR (1 << 4)
- +#define SMICS_START (1 << 3)
- +#define SMICS_ACTIVE (1 << 2)
- +#define SMICS_DONE (1 << 1)
- +#define SMICS_ENABLE (1 << 0)
- +
- +/* Address register bits: */
- +
- +#define SMIA_DEVICE_MASK ((1 << 9) | (1 << 8))
- +#define SMIA_DEVICE_OFFS (8)
- +#define SMIA_ADDR_MASK (0x3f) /* bits 5 -> 0 */
- +#define SMIA_ADDR_OFFS (0)
- +
- +/* DMA control register bits:
- + * SMIDC_DMAEN : DMA enable: set 1: DMA requests will be issued.
- + * SMIDC_DMAP : DMA passthrough: when set to 0, top two data pins are used by
- + * SMI as usual. When set to 1, the top two pins are used for
- + * external DREQs: pin 16 read request, 17 write.
- + * SMIDC_PANIC* : Threshold at which DMA will panic during read/write.
- + * SMIDC_REQ* : Threshold at which DMA will generate a DREQ.
- + */
- +
- +#define SMIDC_DMAEN (1 << 28)
- +#define SMIDC_DMAP (1 << 24)
- +#define SMIDC_PANICR_MASK (0x3f << 18)
- +#define SMIDC_PANICR_OFFS (18)
- +#define SMIDC_PANICW_MASK (0x3f << 12)
- +#define SMIDC_PANICW_OFFS (12)
- +#define SMIDC_REQR_MASK (0x3f << 6)
- +#define SMIDC_REQR_OFFS (6)
- +#define SMIDC_REQW_MASK (0x3f)
- +#define SMIDC_REQW_OFFS (0)
- +
- +/* Device settings register bits: same for all 4 (or 3?) device register sets.
- + * Device read settings:
- + * SMIDSR_RWIDTH : Read transfer width. 00 = 8bit, 01 = 16bit,
- + * 10 = 18bit, 11 = 9bit.
- + * SMIDSR_RSETUP : Read setup time: number of core cycles between chip
- + * select/address and read strobe. Min 1, max 64.
- + * SMIDSR_MODE68 : 1 for System 68 mode (i.e. enable + direction pins,
- + * rather than OE + WE pin)
- + * SMIDSR_FSETUP : If set to 1, setup time only applies to first
- + * transfer after address change.
- + * SMIDSR_RHOLD : Number of core cycles between read strobe going
- + * inactive and CS/address going inactive. Min 1, max 64
- + * SMIDSR_RPACEALL : When set to 1, this device's RPACE value will always
- + * be used for the next transaction, even if it is not
- + * to this device.
- + * SMIDSR_RPACE : Number of core cycles spent waiting between CS
- + * deassert and start of next transfer. Min 1, max 128
- + * SMIDSR_RDREQ : 1 = use external DMA request on SD16 to pace reads
- + * from device. Must also set DMAP in SMICS.
- + * SMIDSR_RSTROBE : Number of cycles to assert the read strobe.
- + * min 1, max 128.
- + */
- +#define SMIDSR_RWIDTH_MASK ((1<<31)|(1<<30))
- +#define SMIDSR_RWIDTH_OFFS (30)
- +#define SMIDSR_RSETUP_MASK (0x3f << 24)
- +#define SMIDSR_RSETUP_OFFS (24)
- +#define SMIDSR_MODE68 (1 << 23)
- +#define SMIDSR_FSETUP (1 << 22)
- +#define SMIDSR_RHOLD_MASK (0x3f << 16)
- +#define SMIDSR_RHOLD_OFFS (16)
- +#define SMIDSR_RPACEALL (1 << 15)
- +#define SMIDSR_RPACE_MASK (0x7f << 8)
- +#define SMIDSR_RPACE_OFFS (8)
- +#define SMIDSR_RDREQ (1 << 7)
- +#define SMIDSR_RSTROBE_MASK (0x7f)
- +#define SMIDSR_RSTROBE_OFFS (0)
- +
- +/* Device write settings:
- + * SMIDSW_WWIDTH : Write transfer width. 00 = 8bit, 01 = 16bit,
- + * 10= 18bit, 11 = 9bit.
- + * SMIDSW_WSETUP : Number of cycles between CS assert and write strobe.
- + * Min 1, max 64.
- + * SMIDSW_WFORMAT : Pixel format of input. 0 = 16bit RGB 565,
- + * 1 = 32bit RGBA 8888
- + * SMIDSW_WSWAP : 1 = swap pixel data bits. (Use with SMICS_PXLDAT)
- + * SMIDSW_WHOLD : Time between WE deassert and CS deassert. 1 to 64
- + * SMIDSW_WPACEALL : 1: this device's WPACE will be used for the next
- + * transfer, regardless of that transfer's device.
- + * SMIDSW_WPACE : Cycles between CS deassert and next CS assert.
- + * Min 1, max 128
- + * SMIDSW_WDREQ : Use external DREQ on pin 17 to pace writes. DMAP must
- + * be set in SMICS.
- + * SMIDSW_WSTROBE : Number of cycles to assert the write strobe.
- + * Min 1, max 128
- + */
- +#define SMIDSW_WWIDTH_MASK ((1<<31)|(1<<30))
- +#define SMIDSW_WWIDTH_OFFS (30)
- +#define SMIDSW_WSETUP_MASK (0x3f << 24)
- +#define SMIDSW_WSETUP_OFFS (24)
- +#define SMIDSW_WFORMAT (1 << 23)
- +#define SMIDSW_WSWAP (1 << 22)
- +#define SMIDSW_WHOLD_MASK (0x3f << 16)
- +#define SMIDSW_WHOLD_OFFS (16)
- +#define SMIDSW_WPACEALL (1 << 15)
- +#define SMIDSW_WPACE_MASK (0x7f << 8)
- +#define SMIDSW_WPACE_OFFS (8)
- +#define SMIDSW_WDREQ (1 << 7)
- +#define SMIDSW_WSTROBE_MASK (0x7f)
- +#define SMIDSW_WSTROBE_OFFS (0)
- +
- +/* Direct transfer control + status register
- + * SMIDCS_WRITE : Direction of transfer: 1 -> write, 0 -> read
- + * SMIDCS_DONE : 1 when a transfer has finished. Write 1 to clear.
- + * SMIDCS_START : Write 1 to start a transfer, if one is not already underway.
- + * SMIDCE_ENABLE: Write 1 to enable SMI in direct mode.
- + */
- +
- +#define SMIDCS_WRITE (1 << 3)
- +#define SMIDCS_DONE (1 << 2)
- +#define SMIDCS_START (1 << 1)
- +#define SMIDCS_ENABLE (1 << 0)
- +
- +/* Direct transfer address register
- + * SMIDA_DEVICE : Indicates which of the device settings banks should be used.
- + * SMIDA_ADDR : The value to be asserted on the address pins.
- + */
- +
- +#define SMIDA_DEVICE_MASK ((1<<9)|(1<<8))
- +#define SMIDA_DEVICE_OFFS (8)
- +#define SMIDA_ADDR_MASK (0x3f)
- +#define SMIDA_ADDR_OFFS (0)
- +
- +/* FIFO debug register
- + * SMIFD_FLVL : The high-tide mark of FIFO count during the most recent txfer
- + * SMIFD_FCNT : The current FIFO count.
- + */
- +#define SMIFD_FLVL_MASK (0x3f << 8)
- +#define SMIFD_FLVL_OFFS (8)
- +#define SMIFD_FCNT_MASK (0x3f)
- +#define SMIFD_FCNT_OFFS (0)
- +
- +#endif /* BCM2835_SMI_IMPLEMENTATION */
- +
- +#endif /* BCM2835_SMI_H */
|