added kernel modules

bug_fixes_integration_tx
meexmachina 2021-08-10 10:38:33 +03:00
rodzic d9e774d0de
commit b3f34b9f4e
5 zmienionych plików z 1118 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,20 @@
// Description: Overlay to enable character device interface for SMI.
// Author: Luke Wren <luke@raspberrypi.org>
/dts-v1/;
/plugin/;
/{
compatible = "brcm,bcm2835";
fragment@0 {
target = <&soc>;
__overlay__ {
smi_dev {
compatible = "brcm,bcm2835-smi-dev";
smi_handle = <&smi>;
status = "okay";
};
};
};
};

Wyświetl plik

@ -0,0 +1,37 @@
// Description: Overlay to enable the secondary memory interface peripheral
// Author: Luke Wren
/dts-v1/;
/plugin/;
/{
compatible = "brcm,bcm2835";
fragment@0 {
target = <&smi>;
__overlay__ {
pinctrl-names = "default";
pinctrl-0 = <&smi_pins>;
status = "okay";
};
};
fragment@1 {
target = <&gpio>;
__overlay__ {
smi_pins: smi_pins {
/* Don't configure the top two address bits, as
these are already used as ID_SD and ID_SC */
brcm,pins = <2 3 4 5 6 7 8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25>;
/* Alt 1: SMI */
brcm,function = <5 5 5 5 5 5 5 5 5 5 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 0 0 0
0 0 0 0 0 0 0>;
};
};
};
};

Wyświetl plik

@ -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 */

Wyświetl plik

@ -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 %zu 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 %zu 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>");

Wyświetl plik

@ -0,0 +1,268 @@
/**
* NAND flash 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/mtd/nand.h>
#include <linux/mtd/partitions.h>
#include <linux/broadcom/bcm2835_smi.h>
#define DEVICE_NAME "bcm2835-smi-nand"
#define DRIVER_NAME "smi-nand-bcm2835"
struct bcm2835_smi_nand_host {
struct bcm2835_smi_instance *smi_inst;
struct nand_chip nand_chip;
struct mtd_info mtd;
struct device *dev;
};
/****************************************************************************
*
* NAND functionality implementation
*
****************************************************************************/
#define SMI_NAND_CLE_PIN 0x01
#define SMI_NAND_ALE_PIN 0x02
static inline void bcm2835_smi_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
unsigned int ctrl)
{
uint32_t cmd32 = cmd;
uint32_t addr = ~(SMI_NAND_CLE_PIN | SMI_NAND_ALE_PIN);
struct bcm2835_smi_nand_host *host = dev_get_drvdata(mtd->dev.parent);
struct bcm2835_smi_instance *inst = host->smi_inst;
if (ctrl & NAND_CLE)
addr |= SMI_NAND_CLE_PIN;
if (ctrl & NAND_ALE)
addr |= SMI_NAND_ALE_PIN;
/* Lower ALL the CS pins! */
if (ctrl & NAND_NCE)
addr &= (SMI_NAND_CLE_PIN | SMI_NAND_ALE_PIN);
bcm2835_smi_set_address(inst, addr);
if (cmd != NAND_CMD_NONE)
bcm2835_smi_write_buf(inst, &cmd32, 1);
}
static inline uint8_t bcm2835_smi_nand_read_byte(struct mtd_info *mtd)
{
uint8_t byte;
struct bcm2835_smi_nand_host *host = dev_get_drvdata(mtd->dev.parent);
struct bcm2835_smi_instance *inst = host->smi_inst;
bcm2835_smi_read_buf(inst, &byte, 1);
return byte;
}
static inline void bcm2835_smi_nand_write_byte(struct mtd_info *mtd,
uint8_t byte)
{
struct bcm2835_smi_nand_host *host = dev_get_drvdata(mtd->dev.parent);
struct bcm2835_smi_instance *inst = host->smi_inst;
bcm2835_smi_write_buf(inst, &byte, 1);
}
static inline void bcm2835_smi_nand_write_buf(struct mtd_info *mtd,
const uint8_t *buf, int len)
{
struct bcm2835_smi_nand_host *host = dev_get_drvdata(mtd->dev.parent);
struct bcm2835_smi_instance *inst = host->smi_inst;
bcm2835_smi_write_buf(inst, buf, len);
}
static inline void bcm2835_smi_nand_read_buf(struct mtd_info *mtd,
uint8_t *buf, int len)
{
struct bcm2835_smi_nand_host *host = dev_get_drvdata(mtd->dev.parent);
struct bcm2835_smi_instance *inst = host->smi_inst;
bcm2835_smi_read_buf(inst, buf, len);
}
/****************************************************************************
*
* Probe and remove functions
*
***************************************************************************/
static int bcm2835_smi_nand_probe(struct platform_device *pdev)
{
struct bcm2835_smi_nand_host *host;
struct nand_chip *this;
struct mtd_info *mtd;
struct device *dev = &pdev->dev;
struct device_node *node = dev->of_node, *smi_node;
struct mtd_part_parser_data ppdata;
struct smi_settings *smi_settings;
struct bcm2835_smi_instance *smi_inst;
int ret = -ENXIO;
if (!node) {
dev_err(dev, "No device tree node supplied!");
return -EINVAL;
}
smi_node = of_parse_phandle(node, "smi_handle", 0);
/* Request use of SMI peripheral: */
smi_inst = bcm2835_smi_get(smi_node);
if (!smi_inst) {
dev_err(dev, "Could not register with SMI.");
return -EPROBE_DEFER;
}
/* Set SMI timing and bus width */
smi_settings = bcm2835_smi_get_settings_from_regs(smi_inst);
smi_settings->data_width = SMI_WIDTH_8BIT;
smi_settings->read_setup_time = 2;
smi_settings->read_hold_time = 1;
smi_settings->read_pace_time = 1;
smi_settings->read_strobe_time = 3;
smi_settings->write_setup_time = 2;
smi_settings->write_hold_time = 1;
smi_settings->write_pace_time = 1;
smi_settings->write_strobe_time = 3;
bcm2835_smi_set_regs_from_settings(smi_inst);
host = devm_kzalloc(dev, sizeof(struct bcm2835_smi_nand_host),
GFP_KERNEL);
if (!host)
return -ENOMEM;
host->dev = dev;
host->smi_inst = smi_inst;
platform_set_drvdata(pdev, host);
/* Link the structures together */
this = &host->nand_chip;
mtd = &host->mtd;
mtd->priv = this;
mtd->owner = THIS_MODULE;
mtd->dev.parent = dev;
mtd->name = DRIVER_NAME;
ppdata.of_node = node;
/* 20 us command delay time... */
this->chip_delay = 20;
this->priv = host;
this->cmd_ctrl = bcm2835_smi_nand_cmd_ctrl;
this->read_byte = bcm2835_smi_nand_read_byte;
this->write_byte = bcm2835_smi_nand_write_byte;
this->write_buf = bcm2835_smi_nand_write_buf;
this->read_buf = bcm2835_smi_nand_read_buf;
this->ecc.mode = NAND_ECC_SOFT;
/* Should never be accessed directly: */
this->IO_ADDR_R = (void *)0xdeadbeef;
this->IO_ADDR_W = (void *)0xdeadbeef;
/* First scan to find the device and get the page size */
if (nand_scan_ident(mtd, 1, NULL))
return -ENXIO;
/* Second phase scan */
if (nand_scan_tail(mtd))
return -ENXIO;
ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
if (!ret)
return 0;
nand_release(mtd);
return -EINVAL;
}
static int bcm2835_smi_nand_remove(struct platform_device *pdev)
{
struct bcm2835_smi_nand_host *host = platform_get_drvdata(pdev);
nand_release(&host->mtd);
return 0;
}
/****************************************************************************
*
* Register the driver with device tree
*
***************************************************************************/
static const struct of_device_id bcm2835_smi_nand_of_match[] = {
{.compatible = "brcm,bcm2835-smi-nand",},
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, bcm2835_smi_nand_of_match);
static struct platform_driver bcm2835_smi_nand_driver = {
.probe = bcm2835_smi_nand_probe,
.remove = bcm2835_smi_nand_remove,
.driver = {
.name = DRIVER_NAME,
.owner = THIS_MODULE,
.of_match_table = bcm2835_smi_nand_of_match,
},
};
module_platform_driver(bcm2835_smi_nand_driver);
MODULE_ALIAS("platform:smi-nand-bcm2835");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION
("Driver for NAND chips using Broadcom Secondary Memory Interface");
MODULE_AUTHOR("Luke Wren <luke@raspberrypi.org>");