Changes in 4.9.276 ALSA: usb-audio: fix rate on Ozone Z90 USB headset media: dvb-usb: fix wrong definition Input: usbtouchscreen - fix control-request directions net: can: ems_usb: fix use-after-free in ems_usb_disconnect() usb: gadget: eem: fix echo command packet response issue USB: cdc-acm: blacklist Heimann USB Appset device ntfs: fix validity check for file name attribute iov_iter_fault_in_readable() should do nothing in xarray case Input: joydev - prevent use of not validated data in JSIOCSBTNMAP ioctl ARM: dts: at91: sama5d4: fix pinctrl muxing btrfs: clear defrag status of a root if starting transaction fails ext4: fix kernel infoleak via ext4_extent_header ext4: correct the cache_nr in tracepoint ext4_es_shrink_exit ext4: remove check for zero nr_to_scan in ext4_es_scan() ext4: fix avefreec in find_group_orlov SUNRPC: Fix the batch tasks count wraparound. SUNRPC: Should wake up the privileged task firstly. s390/cio: dont call css_wait_for_slow_path() inside a lock iio: ltr501: mark register holding upper 8 bits of ALS_DATA{0,1} and PS_DATA as volatile, too iio: ltr501: ltr559: fix initialization of LTR501_ALS_CONTR iio: ltr501: ltr501_read_ps(): add missing endianness conversion serial: sh-sci: Stop dmaengine transfer in sci_stop_tx() serial_cs: Add Option International GSM-Ready 56K/ISDN modem serial_cs: remove wrong GLOBETROTTER.cis entry ath9k: Fix kernel NULL pointer dereference during ath_reset_internal() ssb: sdio: Don't overwrite const buffer if block_write fails seq_buf: Make trace_seq_putmem_hex() support data longer than 8 fuse: check connected before queueing on fpq->io spi: spi-loopback-test: Fix 'tx_buf' might be 'rx_buf' spi: spi-topcliff-pch: Fix potential double free in pch_spi_process_messages() spi: omap-100k: Fix the length judgment problem crypto: nx - add missing MODULE_DEVICE_TABLE media: cpia2: fix memory leak in cpia2_usb_probe media: cobalt: fix race condition in setting HPD media: pvrusb2: fix warning in pvr2_i2c_core_done crypto: qat - check return code of qat_hal_rd_rel_reg() crypto: qat - remove unused macro in FW loader media: v4l2-core: Avoid the dangling pointer in v4l2_fh_release media: bt8xx: Fix a missing check bug in bt878_probe media: st-hva: Fix potential NULL pointer dereferences mmc: via-sdmmc: add a check against NULL pointer dereference crypto: shash - avoid comparing pointers to exported functions under CFI media: dvb_net: avoid speculation from net slot media: siano: fix device register error path btrfs: abort transaction if we fail to update the delayed inode btrfs: disable build on platforms having page size 256K regulator: da9052: Ensure enough delay time for .set_voltage_time_sel ACPI: processor idle: Fix up C-state latency if not ordered block_dump: remove block_dump feature in mark_inode_dirty() fs: dlm: cancel work sync othercon random32: Fix implicit truncation warning in prandom_seed_state() fs: dlm: fix memory leak when fenced ACPI: bus: Call kobject_put() in acpi_init() error path platform/x86: toshiba_acpi: Fix missing error code in toshiba_acpi_setup_keyboard() ACPI: tables: Add custom DSDT file as makefile prerequisite ia64: mca_drv: fix incorrect array size calculation media: s5p_cec: decrement usage count if disabled crypto: ixp4xx - dma_unmap the correct address crypto: ux500 - Fix error return code in hash_hw_final() sata_highbank: fix deferred probing pata_rb532_cf: fix deferred probing media: I2C: change 'RST' to "RSET" to fix multiple build errors pata_octeon_cf: avoid WARN_ON() in ata_host_activate() pata_ep93xx: fix deferred probing media: tc358743: Fix error return code in tc358743_probe_of() media: siano: Fix out-of-bounds warnings in smscore_load_firmware_family2() mmc: usdhi6rol0: fix error return code in usdhi6_probe() media: s5p-g2d: Fix a memory leak on ctx->fh.m2m_ctx hwmon: (max31722) Remove non-standard ACPI device IDs hwmon: (max31790) Fix fan speed reporting for fan7..12 spi: spi-sun6i: Fix chipselect/clock bug crypto: nx - Fix RCU warning in nx842_OF_upd_status ACPI: sysfs: Fix a buffer overrun problem with description_show() ocfs2: fix snprintf() checking net: pch_gbe: Propagate error from devm_gpio_request_one() ehea: fix error return code in ehea_restart_qps() RDMA/rxe: Fix failure during driver load drm: qxl: ensure surf.data is ininitialized wireless: carl9170: fix LEDS build errors & warnings brcmsmac: mac80211_if: Fix a resource leak in an error handling path ath10k: Fix an error code in ath10k_add_interface() netlabel: Fix memory leak in netlbl_mgmt_add_common netfilter: nft_exthdr: check for IPv6 packet before further processing net: ethernet: aeroflex: fix UAF in greth_of_remove net: ethernet: ezchip: fix UAF in nps_enet_remove net: ethernet: ezchip: fix error handling vxlan: add missing rcu_read_lock() in neigh_reduce() i40e: Fix error handling in i40e_vsi_open Bluetooth: mgmt: Fix slab-out-of-bounds in tlv_data_is_valid writeback: fix obtain a reference to a freeing memcg css net: sched: fix warning in tcindex_alloc_perfect_hash tty: nozomi: Fix a resource leak in an error handling function iio: adis_buffer: do not return ints in irq handlers iio: accel: bma180: Fix buffer alignment in iio_push_to_buffers_with_timestamp() iio: accel: bma220: Fix buffer alignment in iio_push_to_buffers_with_timestamp() iio: accel: kxcjk-1013: Fix buffer alignment in iio_push_to_buffers_with_timestamp() iio: accel: stk8312: Fix buffer alignment in iio_push_to_buffers_with_timestamp() iio: accel: stk8ba50: Fix buffer alignment in iio_push_to_buffers_with_timestamp() iio: adc: ti-ads1015: Fix buffer alignment in iio_push_to_buffers_with_timestamp() iio: adc: vf610: Fix buffer alignment in iio_push_to_buffers_with_timestamp() iio: gyro: bmg160: Fix buffer alignment in iio_push_to_buffers_with_timestamp() iio: humidity: am2315: Fix buffer alignment in iio_push_to_buffers_with_timestamp() iio: prox: pulsed-light: Fix buffer alignment in iio_push_to_buffers_with_timestamp() iio: light: isl29125: Fix buffer alignment in iio_push_to_buffers_with_timestamp() iio: light: tcs3414: Fix buffer alignment in iio_push_to_buffers_with_timestamp() Input: hil_kbd - fix error return code in hil_dev_connect() char: pcmcia: error out if 'num_bytes_read' is greater than 4 in set_protocol() tty: nozomi: Fix the error handling path of 'nozomi_card_init()' scsi: FlashPoint: Rename si_flags field s390: appldata depends on PROC_SYSCTL staging: gdm724x: check for buffer overflow in gdm_lte_multi_sdu_pkt() staging: gdm724x: check for overflow in gdm_lte_netif_rx() of: Fix truncation of memory sizes on 32-bit platforms scsi: mpt3sas: Fix error return value in _scsih_expander_add() phy: ti: dm816x: Fix the error handling path in 'dm816x_usb_phy_probe() extcon: sm5502: Drop invalid register write in sm5502_reg_data extcon: max8997: Add missing modalias string configfs: fix memleak in configfs_release_bin_file leds: ktd2692: Fix an error handling path mm/huge_memory.c: don't discard hugepage if other processes are mapping it selftests/vm/pkeys: fix alloc_random_pkey() to make it really, really random mmc: vub3000: fix control-request direction scsi: core: Retry I/O for Notify (Enable Spinup) Required error net: pch_gbe: Use proper accessors to BE data in pch_ptp_match() hugetlb: clear huge pte during flush function on mips platform atm: iphase: fix possible use-after-free in ia_module_exit() mISDN: fix possible use-after-free in HFC_cleanup() atm: nicstar: Fix possible use-after-free in nicstar_cleanup() net: Treat __napi_schedule_irqoff() as __napi_schedule() on PREEMPT_RT reiserfs: add check for invalid 1st journal block drm/virtio: Fix double free on probe failure udf: Fix NULL pointer dereference in udf_symlink function e100: handle eeprom as little endian clk: tegra: Ensure that PLLU configuration is applied properly ipv6: use prandom_u32() for ID generation RDMA/cxgb4: Fix missing error code in create_qp() dm space maps: don't reset space map allocation cursor when committing net: micrel: check return value after calling platform_get_resource() fjes: check return value after calling platform_get_resource() selinux: use __GFP_NOWARN with GFP_NOWAIT in the AVC xfrm: Fix error reporting in xfrm_state_construct. wlcore/wl12xx: Fix wl12xx get_mac error if device is in ELP wl1251: Fix possible buffer overflow in wl1251_cmd_scan cw1200: add missing MODULE_DEVICE_TABLE MIPS: add PMD table accounting into MIPS'pmd_alloc_one atm: nicstar: use 'dma_free_coherent' instead of 'kfree' atm: nicstar: register the interrupt handler in the right place RDMA/rxe: Don't overwrite errno from ib_umem_get() sfc: avoid double pci_remove of VFs sfc: error code if SRIOV cannot be disabled wireless: wext-spy: Fix out-of-bounds warning RDMA/cma: Fix rdma_resolve_route() memory leak Bluetooth: Fix the HCI to MGMT status conversion table Bluetooth: Shutdown controller after workqueues are flushed or cancelled Bluetooth: btusb: fix bt fiwmare downloading failure issue for qca btsoc. sctp: add size validation when walking chunks fuse: reject internal errno can: gw: synchronize rcu operations before removing gw job entry can: bcm: delay release of struct bcm_op after synchronize_rcu() mac80211: fix memory corruption in EAPOL handling powerpc/barrier: Avoid collision with clang's __lwsync macro pinctrl/amd: Add device HID for new AMD GPIO controller mmc: sdhci: Fix warning message when accessing RPMB in HS400 mode mmc: core: clear flags before allowing to retune ata: ahci_sunxi: Disable DIPM ASoC: tegra: Set driver_name=tegra for all machine drivers qemu_fw_cfg: Make fw_cfg_rev_attr a proper kobj_attribute ipmi/watchdog: Stop watchdog timer when the current action is 'none' power: supply: ab8500: Fix an old bug seq_buf: Fix overflow in seq_buf_putmem_hex() ipack/carriers/tpci200: Fix a double free in tpci200_pci_probe dm btree remove: assign new_root only when removal succeeds media: dtv5100: fix control-request directions media: zr364xx: fix memory leak in zr364xx_start_readpipe media: gspca/sq905: fix control-request direction media: gspca/sunplus: fix zero-length control requests media: uvcvideo: Fix pixel format change for Elgato Cam Link 4K jfs: fix GPF in diFree smackfs: restrict bytes count in smk_set_cipso() KVM: x86: Use guest MAXPHYADDR from CPUID.0x8000_0008 iff TDP is enabled KVM: X86: Disable hardware breakpoints unconditionally before kvm_x86->run() scsi: core: Fix bad pointer dereference when ehandler kthread is invalid tracing: Do not reference char * as a string in histograms fscrypt: don't ignore minor_hash when hash is 0 tty: serial: fsl_lpuart: fix the potential risk of division or modulo by zero misc/libmasm/module: Fix two use after free in ibmasm_init_one Revert "ALSA: bebob/oxfw: fix Kconfig entry for Mackie d.2 Pro" scsi: lpfc: Fix "Unexpected timeout" error in direct attach topology tty: serial: 8250: serial_cs: Fix a memory leak in error handling path fs/jfs: Fix missing error code in lmLogInit() scsi: iscsi: Add iscsi_cls_conn refcount helpers mfd: da9052/stmpe: Add and modify MODULE_DEVICE_TABLE s390/sclp_vt220: fix console name to match device ALSA: sb: Fix potential double-free of CSP mixer elements powerpc/ps3: Add dma_mask to ps3_dma_region gpio: zynq: Check return value of pm_runtime_get_sync ALSA: ppc: fix error return code in snd_pmac_probe() selftests/powerpc: Fix "no_handler" EBB selftest ASoC: soc-core: Fix the error return code in snd_soc_of_parse_audio_routing() ALSA: bebob: add support for ToneWeal FW66 usb: gadget: f_hid: fix endianness issue with descriptors usb: gadget: hid: fix error return code in hid_bind() powerpc/boot: Fixup device-tree on little endian backlight: lm3630a: Fix return code of .update_status() callback ALSA: hda: Add IRQ check for platform_get_irq() i2c: core: Disable client irq on reboot/shutdown lib/decompress_unlz4.c: correctly handle zero-padding around initrds. pwm: spear: Don't modify HW state in .remove callback power: supply: ab8500: Avoid NULL pointers power: reset: gpio-poweroff: add missing MODULE_DEVICE_TABLE ARM: 9087/1: kprobes: test-thumb: fix for LLVM_IAS=1 watchdog: Fix possible use-after-free in wdt_startup() watchdog: sc520_wdt: Fix possible use-after-free in wdt_turnoff() watchdog: Fix possible use-after-free by calling del_timer_sync() x86/fpu: Return proper error codes from user access functions orangefs: fix orangefs df output. ceph: remove bogus checks and WARN_ONs from ceph_set_page_dirty power: supply: charger-manager: add missing MODULE_DEVICE_TABLE power: supply: ab8500: add missing MODULE_DEVICE_TABLE pwm: tegra: Don't modify HW state in .remove callback ACPI: AMBA: Fix resource name in /proc/iomem virtio-blk: Fix memory leak among suspend/resume procedure virtio_console: Assure used length from device is limited PCI/sysfs: Fix dsm_label_utf16s_to_utf8s() buffer overrun power: supply: rt5033_battery: Fix device tree enumeration um: fix error return code in slip_open() um: fix error return code in winch_tramp() watchdog: aspeed: fix hardware timeout calculation nfs: fix acl memory leak of posix_acl_create() ubifs: Set/Clear I_LINKABLE under i_lock for whiteout inode x86/fpu: Limit xstate copy size in xstateregs_set() ALSA: isa: Fix error return code in snd_cmi8330_probe() hexagon: use common DISCARDS macro ARM: dts: exynos: fix PWM LED max brightness on Odroid XU/XU3 ARM: dts: exynos: fix PWM LED max brightness on Odroid XU4 rtc: fix snprintf() checking in is_rtc_hctosys() ARM: dts: r8a7779, marzen: Fix DU clock names reset: bail if try_module_get() fails memory: fsl_ifc: fix leak of IO mapping on probe failure memory: fsl_ifc: fix leak of private memory on probe failure ARM: dts: am335x: align ti,pindir-d0-out-d1-in property with dt-shema scsi: be2iscsi: Fix an error handling path in beiscsi_dev_probe() mips: always link byteswap helpers into decompressor mips: disable branch profiling in boot/decompress.o MIPS: vdso: Invalid GIC access through VDSO seq_file: disallow extremely large seq buffer allocations Linux 4.9.276 Signed-off-by: Greg Kroah-Hartman <gregkh@google.com> Change-Id: I595c090068eb1b1934b15a0d54394abc38b4b0cc
500 lines
13 KiB
C
500 lines
13 KiB
C
/*
|
|
* Copyright (C) 2012 - 2014 Allwinner Tech
|
|
* Pan Nan <pannan@allwinnertech.com>
|
|
*
|
|
* Copyright (C) 2014 Maxime Ripard
|
|
* Maxime Ripard <maxime.ripard@free-electrons.com>
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License as
|
|
* published by the Free Software Foundation; either version 2 of
|
|
* the License, or (at your option) any later version.
|
|
*/
|
|
|
|
#include <linux/clk.h>
|
|
#include <linux/delay.h>
|
|
#include <linux/device.h>
|
|
#include <linux/interrupt.h>
|
|
#include <linux/io.h>
|
|
#include <linux/module.h>
|
|
#include <linux/platform_device.h>
|
|
#include <linux/pm_runtime.h>
|
|
#include <linux/reset.h>
|
|
|
|
#include <linux/spi/spi.h>
|
|
|
|
#define SUN6I_FIFO_DEPTH 128
|
|
|
|
#define SUN6I_GBL_CTL_REG 0x04
|
|
#define SUN6I_GBL_CTL_BUS_ENABLE BIT(0)
|
|
#define SUN6I_GBL_CTL_MASTER BIT(1)
|
|
#define SUN6I_GBL_CTL_TP BIT(7)
|
|
#define SUN6I_GBL_CTL_RST BIT(31)
|
|
|
|
#define SUN6I_TFR_CTL_REG 0x08
|
|
#define SUN6I_TFR_CTL_CPHA BIT(0)
|
|
#define SUN6I_TFR_CTL_CPOL BIT(1)
|
|
#define SUN6I_TFR_CTL_SPOL BIT(2)
|
|
#define SUN6I_TFR_CTL_CS_MASK 0x30
|
|
#define SUN6I_TFR_CTL_CS(cs) (((cs) << 4) & SUN6I_TFR_CTL_CS_MASK)
|
|
#define SUN6I_TFR_CTL_CS_MANUAL BIT(6)
|
|
#define SUN6I_TFR_CTL_CS_LEVEL BIT(7)
|
|
#define SUN6I_TFR_CTL_DHB BIT(8)
|
|
#define SUN6I_TFR_CTL_FBS BIT(12)
|
|
#define SUN6I_TFR_CTL_XCH BIT(31)
|
|
|
|
#define SUN6I_INT_CTL_REG 0x10
|
|
#define SUN6I_INT_CTL_RF_OVF BIT(8)
|
|
#define SUN6I_INT_CTL_TC BIT(12)
|
|
|
|
#define SUN6I_INT_STA_REG 0x14
|
|
|
|
#define SUN6I_FIFO_CTL_REG 0x18
|
|
#define SUN6I_FIFO_CTL_RF_RST BIT(15)
|
|
#define SUN6I_FIFO_CTL_TF_RST BIT(31)
|
|
|
|
#define SUN6I_FIFO_STA_REG 0x1c
|
|
#define SUN6I_FIFO_STA_RF_CNT_MASK 0x7f
|
|
#define SUN6I_FIFO_STA_RF_CNT_BITS 0
|
|
#define SUN6I_FIFO_STA_TF_CNT_MASK 0x7f
|
|
#define SUN6I_FIFO_STA_TF_CNT_BITS 16
|
|
|
|
#define SUN6I_CLK_CTL_REG 0x24
|
|
#define SUN6I_CLK_CTL_CDR2_MASK 0xff
|
|
#define SUN6I_CLK_CTL_CDR2(div) (((div) & SUN6I_CLK_CTL_CDR2_MASK) << 0)
|
|
#define SUN6I_CLK_CTL_CDR1_MASK 0xf
|
|
#define SUN6I_CLK_CTL_CDR1(div) (((div) & SUN6I_CLK_CTL_CDR1_MASK) << 8)
|
|
#define SUN6I_CLK_CTL_DRS BIT(12)
|
|
|
|
#define SUN6I_BURST_CNT_REG 0x30
|
|
#define SUN6I_BURST_CNT(cnt) ((cnt) & 0xffffff)
|
|
|
|
#define SUN6I_XMIT_CNT_REG 0x34
|
|
#define SUN6I_XMIT_CNT(cnt) ((cnt) & 0xffffff)
|
|
|
|
#define SUN6I_BURST_CTL_CNT_REG 0x38
|
|
#define SUN6I_BURST_CTL_CNT_STC(cnt) ((cnt) & 0xffffff)
|
|
|
|
#define SUN6I_TXDATA_REG 0x200
|
|
#define SUN6I_RXDATA_REG 0x300
|
|
|
|
struct sun6i_spi {
|
|
struct spi_master *master;
|
|
void __iomem *base_addr;
|
|
struct clk *hclk;
|
|
struct clk *mclk;
|
|
struct reset_control *rstc;
|
|
|
|
struct completion done;
|
|
|
|
const u8 *tx_buf;
|
|
u8 *rx_buf;
|
|
int len;
|
|
};
|
|
|
|
static inline u32 sun6i_spi_read(struct sun6i_spi *sspi, u32 reg)
|
|
{
|
|
return readl(sspi->base_addr + reg);
|
|
}
|
|
|
|
static inline void sun6i_spi_write(struct sun6i_spi *sspi, u32 reg, u32 value)
|
|
{
|
|
writel(value, sspi->base_addr + reg);
|
|
}
|
|
|
|
static inline void sun6i_spi_drain_fifo(struct sun6i_spi *sspi, int len)
|
|
{
|
|
u32 reg, cnt;
|
|
u8 byte;
|
|
|
|
/* See how much data is available */
|
|
reg = sun6i_spi_read(sspi, SUN6I_FIFO_STA_REG);
|
|
reg &= SUN6I_FIFO_STA_RF_CNT_MASK;
|
|
cnt = reg >> SUN6I_FIFO_STA_RF_CNT_BITS;
|
|
|
|
if (len > cnt)
|
|
len = cnt;
|
|
|
|
while (len--) {
|
|
byte = readb(sspi->base_addr + SUN6I_RXDATA_REG);
|
|
if (sspi->rx_buf)
|
|
*sspi->rx_buf++ = byte;
|
|
}
|
|
}
|
|
|
|
static inline void sun6i_spi_fill_fifo(struct sun6i_spi *sspi, int len)
|
|
{
|
|
u8 byte;
|
|
|
|
if (len > sspi->len)
|
|
len = sspi->len;
|
|
|
|
while (len--) {
|
|
byte = sspi->tx_buf ? *sspi->tx_buf++ : 0;
|
|
writeb(byte, sspi->base_addr + SUN6I_TXDATA_REG);
|
|
sspi->len--;
|
|
}
|
|
}
|
|
|
|
static void sun6i_spi_set_cs(struct spi_device *spi, bool enable)
|
|
{
|
|
struct sun6i_spi *sspi = spi_master_get_devdata(spi->master);
|
|
u32 reg;
|
|
|
|
reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
|
|
reg &= ~SUN6I_TFR_CTL_CS_MASK;
|
|
reg |= SUN6I_TFR_CTL_CS(spi->chip_select);
|
|
|
|
if (enable)
|
|
reg |= SUN6I_TFR_CTL_CS_LEVEL;
|
|
else
|
|
reg &= ~SUN6I_TFR_CTL_CS_LEVEL;
|
|
|
|
sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg);
|
|
}
|
|
|
|
static size_t sun6i_spi_max_transfer_size(struct spi_device *spi)
|
|
{
|
|
return SUN6I_FIFO_DEPTH - 1;
|
|
}
|
|
|
|
static int sun6i_spi_transfer_one(struct spi_master *master,
|
|
struct spi_device *spi,
|
|
struct spi_transfer *tfr)
|
|
{
|
|
struct sun6i_spi *sspi = spi_master_get_devdata(master);
|
|
unsigned int mclk_rate, div, div_cdr1, div_cdr2, timeout;
|
|
unsigned int start, end, tx_time;
|
|
unsigned int tx_len = 0;
|
|
int ret = 0;
|
|
u32 reg;
|
|
|
|
/* We don't support transfer larger than the FIFO */
|
|
if (tfr->len > SUN6I_FIFO_DEPTH)
|
|
return -EINVAL;
|
|
|
|
reinit_completion(&sspi->done);
|
|
sspi->tx_buf = tfr->tx_buf;
|
|
sspi->rx_buf = tfr->rx_buf;
|
|
sspi->len = tfr->len;
|
|
|
|
/* Clear pending interrupts */
|
|
sun6i_spi_write(sspi, SUN6I_INT_STA_REG, ~0);
|
|
|
|
/* Reset FIFO */
|
|
sun6i_spi_write(sspi, SUN6I_FIFO_CTL_REG,
|
|
SUN6I_FIFO_CTL_RF_RST | SUN6I_FIFO_CTL_TF_RST);
|
|
|
|
/*
|
|
* Setup the transfer control register: Chip Select,
|
|
* polarities, etc.
|
|
*/
|
|
reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
|
|
|
|
if (spi->mode & SPI_CPOL)
|
|
reg |= SUN6I_TFR_CTL_CPOL;
|
|
else
|
|
reg &= ~SUN6I_TFR_CTL_CPOL;
|
|
|
|
if (spi->mode & SPI_CPHA)
|
|
reg |= SUN6I_TFR_CTL_CPHA;
|
|
else
|
|
reg &= ~SUN6I_TFR_CTL_CPHA;
|
|
|
|
if (spi->mode & SPI_LSB_FIRST)
|
|
reg |= SUN6I_TFR_CTL_FBS;
|
|
else
|
|
reg &= ~SUN6I_TFR_CTL_FBS;
|
|
|
|
/*
|
|
* If it's a TX only transfer, we don't want to fill the RX
|
|
* FIFO with bogus data
|
|
*/
|
|
if (sspi->rx_buf)
|
|
reg &= ~SUN6I_TFR_CTL_DHB;
|
|
else
|
|
reg |= SUN6I_TFR_CTL_DHB;
|
|
|
|
/* We want to control the chip select manually */
|
|
reg |= SUN6I_TFR_CTL_CS_MANUAL;
|
|
|
|
sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg);
|
|
|
|
/* Ensure that we have a parent clock fast enough */
|
|
mclk_rate = clk_get_rate(sspi->mclk);
|
|
if (mclk_rate < (2 * tfr->speed_hz)) {
|
|
clk_set_rate(sspi->mclk, 2 * tfr->speed_hz);
|
|
mclk_rate = clk_get_rate(sspi->mclk);
|
|
}
|
|
|
|
/*
|
|
* Setup clock divider.
|
|
*
|
|
* We have two choices there. Either we can use the clock
|
|
* divide rate 1, which is calculated thanks to this formula:
|
|
* SPI_CLK = MOD_CLK / (2 ^ cdr)
|
|
* Or we can use CDR2, which is calculated with the formula:
|
|
* SPI_CLK = MOD_CLK / (2 * (cdr + 1))
|
|
* Wether we use the former or the latter is set through the
|
|
* DRS bit.
|
|
*
|
|
* First try CDR2, and if we can't reach the expected
|
|
* frequency, fall back to CDR1.
|
|
*/
|
|
div_cdr1 = DIV_ROUND_UP(mclk_rate, tfr->speed_hz);
|
|
div_cdr2 = DIV_ROUND_UP(div_cdr1, 2);
|
|
if (div_cdr2 <= (SUN6I_CLK_CTL_CDR2_MASK + 1)) {
|
|
reg = SUN6I_CLK_CTL_CDR2(div_cdr2 - 1) | SUN6I_CLK_CTL_DRS;
|
|
} else {
|
|
div = min(SUN6I_CLK_CTL_CDR1_MASK, order_base_2(div_cdr1));
|
|
reg = SUN6I_CLK_CTL_CDR1(div);
|
|
}
|
|
|
|
sun6i_spi_write(sspi, SUN6I_CLK_CTL_REG, reg);
|
|
/* Finally enable the bus - doing so before might raise SCK to HIGH */
|
|
reg = sun6i_spi_read(sspi, SUN6I_GBL_CTL_REG);
|
|
reg |= SUN6I_GBL_CTL_BUS_ENABLE;
|
|
sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG, reg);
|
|
|
|
/* Setup the transfer now... */
|
|
if (sspi->tx_buf)
|
|
tx_len = tfr->len;
|
|
|
|
/* Setup the counters */
|
|
sun6i_spi_write(sspi, SUN6I_BURST_CNT_REG, SUN6I_BURST_CNT(tfr->len));
|
|
sun6i_spi_write(sspi, SUN6I_XMIT_CNT_REG, SUN6I_XMIT_CNT(tx_len));
|
|
sun6i_spi_write(sspi, SUN6I_BURST_CTL_CNT_REG,
|
|
SUN6I_BURST_CTL_CNT_STC(tx_len));
|
|
|
|
/* Fill the TX FIFO */
|
|
sun6i_spi_fill_fifo(sspi, SUN6I_FIFO_DEPTH);
|
|
|
|
/* Enable the interrupts */
|
|
sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, SUN6I_INT_CTL_TC);
|
|
|
|
/* Start the transfer */
|
|
reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
|
|
sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg | SUN6I_TFR_CTL_XCH);
|
|
|
|
tx_time = max(tfr->len * 8 * 2 / (tfr->speed_hz / 1000), 100U);
|
|
start = jiffies;
|
|
timeout = wait_for_completion_timeout(&sspi->done,
|
|
msecs_to_jiffies(tx_time));
|
|
end = jiffies;
|
|
if (!timeout) {
|
|
dev_warn(&master->dev,
|
|
"%s: timeout transferring %u bytes@%iHz for %i(%i)ms",
|
|
dev_name(&spi->dev), tfr->len, tfr->speed_hz,
|
|
jiffies_to_msecs(end - start), tx_time);
|
|
ret = -ETIMEDOUT;
|
|
goto out;
|
|
}
|
|
|
|
sun6i_spi_drain_fifo(sspi, SUN6I_FIFO_DEPTH);
|
|
|
|
out:
|
|
sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, 0);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static irqreturn_t sun6i_spi_handler(int irq, void *dev_id)
|
|
{
|
|
struct sun6i_spi *sspi = dev_id;
|
|
u32 status = sun6i_spi_read(sspi, SUN6I_INT_STA_REG);
|
|
|
|
/* Transfer complete */
|
|
if (status & SUN6I_INT_CTL_TC) {
|
|
sun6i_spi_write(sspi, SUN6I_INT_STA_REG, SUN6I_INT_CTL_TC);
|
|
complete(&sspi->done);
|
|
return IRQ_HANDLED;
|
|
}
|
|
|
|
return IRQ_NONE;
|
|
}
|
|
|
|
static int sun6i_spi_runtime_resume(struct device *dev)
|
|
{
|
|
struct spi_master *master = dev_get_drvdata(dev);
|
|
struct sun6i_spi *sspi = spi_master_get_devdata(master);
|
|
int ret;
|
|
|
|
ret = clk_prepare_enable(sspi->hclk);
|
|
if (ret) {
|
|
dev_err(dev, "Couldn't enable AHB clock\n");
|
|
goto out;
|
|
}
|
|
|
|
ret = clk_prepare_enable(sspi->mclk);
|
|
if (ret) {
|
|
dev_err(dev, "Couldn't enable module clock\n");
|
|
goto err;
|
|
}
|
|
|
|
ret = reset_control_deassert(sspi->rstc);
|
|
if (ret) {
|
|
dev_err(dev, "Couldn't deassert the device from reset\n");
|
|
goto err2;
|
|
}
|
|
|
|
sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG,
|
|
SUN6I_GBL_CTL_MASTER | SUN6I_GBL_CTL_TP);
|
|
|
|
return 0;
|
|
|
|
err2:
|
|
clk_disable_unprepare(sspi->mclk);
|
|
err:
|
|
clk_disable_unprepare(sspi->hclk);
|
|
out:
|
|
return ret;
|
|
}
|
|
|
|
static int sun6i_spi_runtime_suspend(struct device *dev)
|
|
{
|
|
struct spi_master *master = dev_get_drvdata(dev);
|
|
struct sun6i_spi *sspi = spi_master_get_devdata(master);
|
|
|
|
reset_control_assert(sspi->rstc);
|
|
clk_disable_unprepare(sspi->mclk);
|
|
clk_disable_unprepare(sspi->hclk);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int sun6i_spi_probe(struct platform_device *pdev)
|
|
{
|
|
struct spi_master *master;
|
|
struct sun6i_spi *sspi;
|
|
struct resource *res;
|
|
int ret = 0, irq;
|
|
|
|
master = spi_alloc_master(&pdev->dev, sizeof(struct sun6i_spi));
|
|
if (!master) {
|
|
dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
|
|
return -ENOMEM;
|
|
}
|
|
|
|
platform_set_drvdata(pdev, master);
|
|
sspi = spi_master_get_devdata(master);
|
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
sspi->base_addr = devm_ioremap_resource(&pdev->dev, res);
|
|
if (IS_ERR(sspi->base_addr)) {
|
|
ret = PTR_ERR(sspi->base_addr);
|
|
goto err_free_master;
|
|
}
|
|
|
|
irq = platform_get_irq(pdev, 0);
|
|
if (irq < 0) {
|
|
dev_err(&pdev->dev, "No spi IRQ specified\n");
|
|
ret = -ENXIO;
|
|
goto err_free_master;
|
|
}
|
|
|
|
ret = devm_request_irq(&pdev->dev, irq, sun6i_spi_handler,
|
|
0, "sun6i-spi", sspi);
|
|
if (ret) {
|
|
dev_err(&pdev->dev, "Cannot request IRQ\n");
|
|
goto err_free_master;
|
|
}
|
|
|
|
sspi->master = master;
|
|
master->max_speed_hz = 100 * 1000 * 1000;
|
|
master->min_speed_hz = 3 * 1000;
|
|
master->set_cs = sun6i_spi_set_cs;
|
|
master->transfer_one = sun6i_spi_transfer_one;
|
|
master->num_chipselect = 4;
|
|
master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
|
|
master->bits_per_word_mask = SPI_BPW_MASK(8);
|
|
master->dev.of_node = pdev->dev.of_node;
|
|
master->auto_runtime_pm = true;
|
|
master->max_transfer_size = sun6i_spi_max_transfer_size;
|
|
|
|
sspi->hclk = devm_clk_get(&pdev->dev, "ahb");
|
|
if (IS_ERR(sspi->hclk)) {
|
|
dev_err(&pdev->dev, "Unable to acquire AHB clock\n");
|
|
ret = PTR_ERR(sspi->hclk);
|
|
goto err_free_master;
|
|
}
|
|
|
|
sspi->mclk = devm_clk_get(&pdev->dev, "mod");
|
|
if (IS_ERR(sspi->mclk)) {
|
|
dev_err(&pdev->dev, "Unable to acquire module clock\n");
|
|
ret = PTR_ERR(sspi->mclk);
|
|
goto err_free_master;
|
|
}
|
|
|
|
init_completion(&sspi->done);
|
|
|
|
sspi->rstc = devm_reset_control_get(&pdev->dev, NULL);
|
|
if (IS_ERR(sspi->rstc)) {
|
|
dev_err(&pdev->dev, "Couldn't get reset controller\n");
|
|
ret = PTR_ERR(sspi->rstc);
|
|
goto err_free_master;
|
|
}
|
|
|
|
/*
|
|
* This wake-up/shutdown pattern is to be able to have the
|
|
* device woken up, even if runtime_pm is disabled
|
|
*/
|
|
ret = sun6i_spi_runtime_resume(&pdev->dev);
|
|
if (ret) {
|
|
dev_err(&pdev->dev, "Couldn't resume the device\n");
|
|
goto err_free_master;
|
|
}
|
|
|
|
pm_runtime_set_active(&pdev->dev);
|
|
pm_runtime_enable(&pdev->dev);
|
|
pm_runtime_idle(&pdev->dev);
|
|
|
|
ret = devm_spi_register_master(&pdev->dev, master);
|
|
if (ret) {
|
|
dev_err(&pdev->dev, "cannot register SPI master\n");
|
|
goto err_pm_disable;
|
|
}
|
|
|
|
return 0;
|
|
|
|
err_pm_disable:
|
|
pm_runtime_disable(&pdev->dev);
|
|
sun6i_spi_runtime_suspend(&pdev->dev);
|
|
err_free_master:
|
|
spi_master_put(master);
|
|
return ret;
|
|
}
|
|
|
|
static int sun6i_spi_remove(struct platform_device *pdev)
|
|
{
|
|
pm_runtime_force_suspend(&pdev->dev);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct of_device_id sun6i_spi_match[] = {
|
|
{ .compatible = "allwinner,sun6i-a31-spi", },
|
|
{}
|
|
};
|
|
MODULE_DEVICE_TABLE(of, sun6i_spi_match);
|
|
|
|
static const struct dev_pm_ops sun6i_spi_pm_ops = {
|
|
.runtime_resume = sun6i_spi_runtime_resume,
|
|
.runtime_suspend = sun6i_spi_runtime_suspend,
|
|
};
|
|
|
|
static struct platform_driver sun6i_spi_driver = {
|
|
.probe = sun6i_spi_probe,
|
|
.remove = sun6i_spi_remove,
|
|
.driver = {
|
|
.name = "sun6i-spi",
|
|
.of_match_table = sun6i_spi_match,
|
|
.pm = &sun6i_spi_pm_ops,
|
|
},
|
|
};
|
|
module_platform_driver(sun6i_spi_driver);
|
|
|
|
MODULE_AUTHOR("Pan Nan <pannan@allwinnertech.com>");
|
|
MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
|
|
MODULE_DESCRIPTION("Allwinner A31 SPI controller driver");
|
|
MODULE_LICENSE("GPL");
|