18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * MicroWire interface driver for OMAP 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright 2003 MontaVista Software Inc. <source@mvista.com> 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Ported to 2.6 OMAP uwire interface. 78c2ecf20Sopenharmony_ci * Copyright (C) 2004 Texas Instruments. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Generalization patches by Juha Yrjola <juha.yrjola@nokia.com> 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * Copyright (C) 2005 David Brownell (ported to 2.6 SPI interface) 128c2ecf20Sopenharmony_ci * Copyright (C) 2006 Nokia 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * Many updates by Imre Deak <imre.deak@nokia.com> 158c2ecf20Sopenharmony_ci * 168c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it 178c2ecf20Sopenharmony_ci * under the terms of the GNU General Public License as published by the 188c2ecf20Sopenharmony_ci * Free Software Foundation; either version 2 of the License, or (at your 198c2ecf20Sopenharmony_ci * option) any later version. 208c2ecf20Sopenharmony_ci * 218c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 228c2ecf20Sopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 238c2ecf20Sopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 248c2ecf20Sopenharmony_ci * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 258c2ecf20Sopenharmony_ci * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 268c2ecf20Sopenharmony_ci * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 278c2ecf20Sopenharmony_ci * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 288c2ecf20Sopenharmony_ci * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 298c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 308c2ecf20Sopenharmony_ci * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 318c2ecf20Sopenharmony_ci */ 328c2ecf20Sopenharmony_ci#include <linux/kernel.h> 338c2ecf20Sopenharmony_ci#include <linux/init.h> 348c2ecf20Sopenharmony_ci#include <linux/delay.h> 358c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 368c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 378c2ecf20Sopenharmony_ci#include <linux/err.h> 388c2ecf20Sopenharmony_ci#include <linux/clk.h> 398c2ecf20Sopenharmony_ci#include <linux/slab.h> 408c2ecf20Sopenharmony_ci#include <linux/device.h> 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci#include <linux/spi/spi.h> 438c2ecf20Sopenharmony_ci#include <linux/spi/spi_bitbang.h> 448c2ecf20Sopenharmony_ci#include <linux/module.h> 458c2ecf20Sopenharmony_ci#include <linux/io.h> 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci#include <mach/hardware.h> 488c2ecf20Sopenharmony_ci#include <asm/mach-types.h> 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci#include <mach/mux.h> 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci#include <mach/omap7xx.h> /* OMAP7XX_IO_CONF registers */ 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci/* FIXME address is now a platform device resource, 568c2ecf20Sopenharmony_ci * and irqs should show there too... 578c2ecf20Sopenharmony_ci */ 588c2ecf20Sopenharmony_ci#define UWIRE_BASE_PHYS 0xFFFB3000 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci/* uWire Registers: */ 618c2ecf20Sopenharmony_ci#define UWIRE_IO_SIZE 0x20 628c2ecf20Sopenharmony_ci#define UWIRE_TDR 0x00 638c2ecf20Sopenharmony_ci#define UWIRE_RDR 0x00 648c2ecf20Sopenharmony_ci#define UWIRE_CSR 0x01 658c2ecf20Sopenharmony_ci#define UWIRE_SR1 0x02 668c2ecf20Sopenharmony_ci#define UWIRE_SR2 0x03 678c2ecf20Sopenharmony_ci#define UWIRE_SR3 0x04 688c2ecf20Sopenharmony_ci#define UWIRE_SR4 0x05 698c2ecf20Sopenharmony_ci#define UWIRE_SR5 0x06 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci/* CSR bits */ 728c2ecf20Sopenharmony_ci#define RDRB (1 << 15) 738c2ecf20Sopenharmony_ci#define CSRB (1 << 14) 748c2ecf20Sopenharmony_ci#define START (1 << 13) 758c2ecf20Sopenharmony_ci#define CS_CMD (1 << 12) 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci/* SR1 or SR2 bits */ 788c2ecf20Sopenharmony_ci#define UWIRE_READ_FALLING_EDGE 0x0001 798c2ecf20Sopenharmony_ci#define UWIRE_READ_RISING_EDGE 0x0000 808c2ecf20Sopenharmony_ci#define UWIRE_WRITE_FALLING_EDGE 0x0000 818c2ecf20Sopenharmony_ci#define UWIRE_WRITE_RISING_EDGE 0x0002 828c2ecf20Sopenharmony_ci#define UWIRE_CS_ACTIVE_LOW 0x0000 838c2ecf20Sopenharmony_ci#define UWIRE_CS_ACTIVE_HIGH 0x0004 848c2ecf20Sopenharmony_ci#define UWIRE_FREQ_DIV_2 0x0000 858c2ecf20Sopenharmony_ci#define UWIRE_FREQ_DIV_4 0x0008 868c2ecf20Sopenharmony_ci#define UWIRE_FREQ_DIV_8 0x0010 878c2ecf20Sopenharmony_ci#define UWIRE_CHK_READY 0x0020 888c2ecf20Sopenharmony_ci#define UWIRE_CLK_INVERTED 0x0040 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_cistruct uwire_spi { 928c2ecf20Sopenharmony_ci struct spi_bitbang bitbang; 938c2ecf20Sopenharmony_ci struct clk *ck; 948c2ecf20Sopenharmony_ci}; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_cistruct uwire_state { 978c2ecf20Sopenharmony_ci unsigned div1_idx; 988c2ecf20Sopenharmony_ci}; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci/* REVISIT compile time constant for idx_shift? */ 1018c2ecf20Sopenharmony_ci/* 1028c2ecf20Sopenharmony_ci * Or, put it in a structure which is used throughout the driver; 1038c2ecf20Sopenharmony_ci * that avoids having to issue two loads for each bit of static data. 1048c2ecf20Sopenharmony_ci */ 1058c2ecf20Sopenharmony_cistatic unsigned int uwire_idx_shift; 1068c2ecf20Sopenharmony_cistatic void __iomem *uwire_base; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_cistatic inline void uwire_write_reg(int idx, u16 val) 1098c2ecf20Sopenharmony_ci{ 1108c2ecf20Sopenharmony_ci __raw_writew(val, uwire_base + (idx << uwire_idx_shift)); 1118c2ecf20Sopenharmony_ci} 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_cistatic inline u16 uwire_read_reg(int idx) 1148c2ecf20Sopenharmony_ci{ 1158c2ecf20Sopenharmony_ci return __raw_readw(uwire_base + (idx << uwire_idx_shift)); 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistatic inline void omap_uwire_configure_mode(u8 cs, unsigned long flags) 1198c2ecf20Sopenharmony_ci{ 1208c2ecf20Sopenharmony_ci u16 w, val = 0; 1218c2ecf20Sopenharmony_ci int shift, reg; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci if (flags & UWIRE_CLK_INVERTED) 1248c2ecf20Sopenharmony_ci val ^= 0x03; 1258c2ecf20Sopenharmony_ci val = flags & 0x3f; 1268c2ecf20Sopenharmony_ci if (cs & 1) 1278c2ecf20Sopenharmony_ci shift = 6; 1288c2ecf20Sopenharmony_ci else 1298c2ecf20Sopenharmony_ci shift = 0; 1308c2ecf20Sopenharmony_ci if (cs <= 1) 1318c2ecf20Sopenharmony_ci reg = UWIRE_SR1; 1328c2ecf20Sopenharmony_ci else 1338c2ecf20Sopenharmony_ci reg = UWIRE_SR2; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci w = uwire_read_reg(reg); 1368c2ecf20Sopenharmony_ci w &= ~(0x3f << shift); 1378c2ecf20Sopenharmony_ci w |= val << shift; 1388c2ecf20Sopenharmony_ci uwire_write_reg(reg, w); 1398c2ecf20Sopenharmony_ci} 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_cistatic int wait_uwire_csr_flag(u16 mask, u16 val, int might_not_catch) 1428c2ecf20Sopenharmony_ci{ 1438c2ecf20Sopenharmony_ci u16 w; 1448c2ecf20Sopenharmony_ci int c = 0; 1458c2ecf20Sopenharmony_ci unsigned long max_jiffies = jiffies + HZ; 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci for (;;) { 1488c2ecf20Sopenharmony_ci w = uwire_read_reg(UWIRE_CSR); 1498c2ecf20Sopenharmony_ci if ((w & mask) == val) 1508c2ecf20Sopenharmony_ci break; 1518c2ecf20Sopenharmony_ci if (time_after(jiffies, max_jiffies)) { 1528c2ecf20Sopenharmony_ci printk(KERN_ERR "%s: timeout. reg=%#06x " 1538c2ecf20Sopenharmony_ci "mask=%#06x val=%#06x\n", 1548c2ecf20Sopenharmony_ci __func__, w, mask, val); 1558c2ecf20Sopenharmony_ci return -1; 1568c2ecf20Sopenharmony_ci } 1578c2ecf20Sopenharmony_ci c++; 1588c2ecf20Sopenharmony_ci if (might_not_catch && c > 64) 1598c2ecf20Sopenharmony_ci break; 1608c2ecf20Sopenharmony_ci } 1618c2ecf20Sopenharmony_ci return 0; 1628c2ecf20Sopenharmony_ci} 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_cistatic void uwire_set_clk1_div(int div1_idx) 1658c2ecf20Sopenharmony_ci{ 1668c2ecf20Sopenharmony_ci u16 w; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci w = uwire_read_reg(UWIRE_SR3); 1698c2ecf20Sopenharmony_ci w &= ~(0x03 << 1); 1708c2ecf20Sopenharmony_ci w |= div1_idx << 1; 1718c2ecf20Sopenharmony_ci uwire_write_reg(UWIRE_SR3, w); 1728c2ecf20Sopenharmony_ci} 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_cistatic void uwire_chipselect(struct spi_device *spi, int value) 1758c2ecf20Sopenharmony_ci{ 1768c2ecf20Sopenharmony_ci struct uwire_state *ust = spi->controller_state; 1778c2ecf20Sopenharmony_ci u16 w; 1788c2ecf20Sopenharmony_ci int old_cs; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci BUG_ON(wait_uwire_csr_flag(CSRB, 0, 0)); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci w = uwire_read_reg(UWIRE_CSR); 1848c2ecf20Sopenharmony_ci old_cs = (w >> 10) & 0x03; 1858c2ecf20Sopenharmony_ci if (value == BITBANG_CS_INACTIVE || old_cs != spi->chip_select) { 1868c2ecf20Sopenharmony_ci /* Deselect this CS, or the previous CS */ 1878c2ecf20Sopenharmony_ci w &= ~CS_CMD; 1888c2ecf20Sopenharmony_ci uwire_write_reg(UWIRE_CSR, w); 1898c2ecf20Sopenharmony_ci } 1908c2ecf20Sopenharmony_ci /* activate specfied chipselect */ 1918c2ecf20Sopenharmony_ci if (value == BITBANG_CS_ACTIVE) { 1928c2ecf20Sopenharmony_ci uwire_set_clk1_div(ust->div1_idx); 1938c2ecf20Sopenharmony_ci /* invert clock? */ 1948c2ecf20Sopenharmony_ci if (spi->mode & SPI_CPOL) 1958c2ecf20Sopenharmony_ci uwire_write_reg(UWIRE_SR4, 1); 1968c2ecf20Sopenharmony_ci else 1978c2ecf20Sopenharmony_ci uwire_write_reg(UWIRE_SR4, 0); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci w = spi->chip_select << 10; 2008c2ecf20Sopenharmony_ci w |= CS_CMD; 2018c2ecf20Sopenharmony_ci uwire_write_reg(UWIRE_CSR, w); 2028c2ecf20Sopenharmony_ci } 2038c2ecf20Sopenharmony_ci} 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_cistatic int uwire_txrx(struct spi_device *spi, struct spi_transfer *t) 2068c2ecf20Sopenharmony_ci{ 2078c2ecf20Sopenharmony_ci unsigned len = t->len; 2088c2ecf20Sopenharmony_ci unsigned bits = t->bits_per_word; 2098c2ecf20Sopenharmony_ci unsigned bytes; 2108c2ecf20Sopenharmony_ci u16 val, w; 2118c2ecf20Sopenharmony_ci int status = 0; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci if (!t->tx_buf && !t->rx_buf) 2148c2ecf20Sopenharmony_ci return 0; 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci w = spi->chip_select << 10; 2178c2ecf20Sopenharmony_ci w |= CS_CMD; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci if (t->tx_buf) { 2208c2ecf20Sopenharmony_ci const u8 *buf = t->tx_buf; 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci /* NOTE: DMA could be used for TX transfers */ 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci /* write one or two bytes at a time */ 2258c2ecf20Sopenharmony_ci while (len >= 1) { 2268c2ecf20Sopenharmony_ci /* tx bit 15 is first sent; we byteswap multibyte words 2278c2ecf20Sopenharmony_ci * (msb-first) on the way out from memory. 2288c2ecf20Sopenharmony_ci */ 2298c2ecf20Sopenharmony_ci val = *buf++; 2308c2ecf20Sopenharmony_ci if (bits > 8) { 2318c2ecf20Sopenharmony_ci bytes = 2; 2328c2ecf20Sopenharmony_ci val |= *buf++ << 8; 2338c2ecf20Sopenharmony_ci } else 2348c2ecf20Sopenharmony_ci bytes = 1; 2358c2ecf20Sopenharmony_ci val <<= 16 - bits; 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci#ifdef VERBOSE 2388c2ecf20Sopenharmony_ci pr_debug("%s: write-%d =%04x\n", 2398c2ecf20Sopenharmony_ci dev_name(&spi->dev), bits, val); 2408c2ecf20Sopenharmony_ci#endif 2418c2ecf20Sopenharmony_ci if (wait_uwire_csr_flag(CSRB, 0, 0)) 2428c2ecf20Sopenharmony_ci goto eio; 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci uwire_write_reg(UWIRE_TDR, val); 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci /* start write */ 2478c2ecf20Sopenharmony_ci val = START | w | (bits << 5); 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci uwire_write_reg(UWIRE_CSR, val); 2508c2ecf20Sopenharmony_ci len -= bytes; 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci /* Wait till write actually starts. 2538c2ecf20Sopenharmony_ci * This is needed with MPU clock 60+ MHz. 2548c2ecf20Sopenharmony_ci * REVISIT: we may not have time to catch it... 2558c2ecf20Sopenharmony_ci */ 2568c2ecf20Sopenharmony_ci if (wait_uwire_csr_flag(CSRB, CSRB, 1)) 2578c2ecf20Sopenharmony_ci goto eio; 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci status += bytes; 2608c2ecf20Sopenharmony_ci } 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci /* REVISIT: save this for later to get more i/o overlap */ 2638c2ecf20Sopenharmony_ci if (wait_uwire_csr_flag(CSRB, 0, 0)) 2648c2ecf20Sopenharmony_ci goto eio; 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci } else if (t->rx_buf) { 2678c2ecf20Sopenharmony_ci u8 *buf = t->rx_buf; 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci /* read one or two bytes at a time */ 2708c2ecf20Sopenharmony_ci while (len) { 2718c2ecf20Sopenharmony_ci if (bits > 8) { 2728c2ecf20Sopenharmony_ci bytes = 2; 2738c2ecf20Sopenharmony_ci } else 2748c2ecf20Sopenharmony_ci bytes = 1; 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci /* start read */ 2778c2ecf20Sopenharmony_ci val = START | w | (bits << 0); 2788c2ecf20Sopenharmony_ci uwire_write_reg(UWIRE_CSR, val); 2798c2ecf20Sopenharmony_ci len -= bytes; 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci /* Wait till read actually starts */ 2828c2ecf20Sopenharmony_ci (void) wait_uwire_csr_flag(CSRB, CSRB, 1); 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci if (wait_uwire_csr_flag(RDRB | CSRB, 2858c2ecf20Sopenharmony_ci RDRB, 0)) 2868c2ecf20Sopenharmony_ci goto eio; 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci /* rx bit 0 is last received; multibyte words will 2898c2ecf20Sopenharmony_ci * be properly byteswapped on the way to memory. 2908c2ecf20Sopenharmony_ci */ 2918c2ecf20Sopenharmony_ci val = uwire_read_reg(UWIRE_RDR); 2928c2ecf20Sopenharmony_ci val &= (1 << bits) - 1; 2938c2ecf20Sopenharmony_ci *buf++ = (u8) val; 2948c2ecf20Sopenharmony_ci if (bytes == 2) 2958c2ecf20Sopenharmony_ci *buf++ = val >> 8; 2968c2ecf20Sopenharmony_ci status += bytes; 2978c2ecf20Sopenharmony_ci#ifdef VERBOSE 2988c2ecf20Sopenharmony_ci pr_debug("%s: read-%d =%04x\n", 2998c2ecf20Sopenharmony_ci dev_name(&spi->dev), bits, val); 3008c2ecf20Sopenharmony_ci#endif 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci } 3038c2ecf20Sopenharmony_ci } 3048c2ecf20Sopenharmony_ci return status; 3058c2ecf20Sopenharmony_cieio: 3068c2ecf20Sopenharmony_ci return -EIO; 3078c2ecf20Sopenharmony_ci} 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_cistatic int uwire_setup_transfer(struct spi_device *spi, struct spi_transfer *t) 3108c2ecf20Sopenharmony_ci{ 3118c2ecf20Sopenharmony_ci struct uwire_state *ust = spi->controller_state; 3128c2ecf20Sopenharmony_ci struct uwire_spi *uwire; 3138c2ecf20Sopenharmony_ci unsigned flags = 0; 3148c2ecf20Sopenharmony_ci unsigned hz; 3158c2ecf20Sopenharmony_ci unsigned long rate; 3168c2ecf20Sopenharmony_ci int div1_idx; 3178c2ecf20Sopenharmony_ci int div1; 3188c2ecf20Sopenharmony_ci int div2; 3198c2ecf20Sopenharmony_ci int status; 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci uwire = spi_master_get_devdata(spi->master); 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci /* mode 0..3, clock inverted separately; 3248c2ecf20Sopenharmony_ci * standard nCS signaling; 3258c2ecf20Sopenharmony_ci * don't treat DI=high as "not ready" 3268c2ecf20Sopenharmony_ci */ 3278c2ecf20Sopenharmony_ci if (spi->mode & SPI_CS_HIGH) 3288c2ecf20Sopenharmony_ci flags |= UWIRE_CS_ACTIVE_HIGH; 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci if (spi->mode & SPI_CPOL) 3318c2ecf20Sopenharmony_ci flags |= UWIRE_CLK_INVERTED; 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ci switch (spi->mode & (SPI_CPOL | SPI_CPHA)) { 3348c2ecf20Sopenharmony_ci case SPI_MODE_0: 3358c2ecf20Sopenharmony_ci case SPI_MODE_3: 3368c2ecf20Sopenharmony_ci flags |= UWIRE_WRITE_FALLING_EDGE | UWIRE_READ_RISING_EDGE; 3378c2ecf20Sopenharmony_ci break; 3388c2ecf20Sopenharmony_ci case SPI_MODE_1: 3398c2ecf20Sopenharmony_ci case SPI_MODE_2: 3408c2ecf20Sopenharmony_ci flags |= UWIRE_WRITE_RISING_EDGE | UWIRE_READ_FALLING_EDGE; 3418c2ecf20Sopenharmony_ci break; 3428c2ecf20Sopenharmony_ci } 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_ci /* assume it's already enabled */ 3458c2ecf20Sopenharmony_ci rate = clk_get_rate(uwire->ck); 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci if (t != NULL) 3488c2ecf20Sopenharmony_ci hz = t->speed_hz; 3498c2ecf20Sopenharmony_ci else 3508c2ecf20Sopenharmony_ci hz = spi->max_speed_hz; 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_ci if (!hz) { 3538c2ecf20Sopenharmony_ci pr_debug("%s: zero speed?\n", dev_name(&spi->dev)); 3548c2ecf20Sopenharmony_ci status = -EINVAL; 3558c2ecf20Sopenharmony_ci goto done; 3568c2ecf20Sopenharmony_ci } 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci /* F_INT = mpu_xor_clk / DIV1 */ 3598c2ecf20Sopenharmony_ci for (div1_idx = 0; div1_idx < 4; div1_idx++) { 3608c2ecf20Sopenharmony_ci switch (div1_idx) { 3618c2ecf20Sopenharmony_ci case 0: 3628c2ecf20Sopenharmony_ci div1 = 2; 3638c2ecf20Sopenharmony_ci break; 3648c2ecf20Sopenharmony_ci case 1: 3658c2ecf20Sopenharmony_ci div1 = 4; 3668c2ecf20Sopenharmony_ci break; 3678c2ecf20Sopenharmony_ci case 2: 3688c2ecf20Sopenharmony_ci div1 = 7; 3698c2ecf20Sopenharmony_ci break; 3708c2ecf20Sopenharmony_ci default: 3718c2ecf20Sopenharmony_ci case 3: 3728c2ecf20Sopenharmony_ci div1 = 10; 3738c2ecf20Sopenharmony_ci break; 3748c2ecf20Sopenharmony_ci } 3758c2ecf20Sopenharmony_ci div2 = (rate / div1 + hz - 1) / hz; 3768c2ecf20Sopenharmony_ci if (div2 <= 8) 3778c2ecf20Sopenharmony_ci break; 3788c2ecf20Sopenharmony_ci } 3798c2ecf20Sopenharmony_ci if (div1_idx == 4) { 3808c2ecf20Sopenharmony_ci pr_debug("%s: lowest clock %ld, need %d\n", 3818c2ecf20Sopenharmony_ci dev_name(&spi->dev), rate / 10 / 8, hz); 3828c2ecf20Sopenharmony_ci status = -EDOM; 3838c2ecf20Sopenharmony_ci goto done; 3848c2ecf20Sopenharmony_ci } 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci /* we have to cache this and reset in uwire_chipselect as this is a 3878c2ecf20Sopenharmony_ci * global parameter and another uwire device can change it under 3888c2ecf20Sopenharmony_ci * us */ 3898c2ecf20Sopenharmony_ci ust->div1_idx = div1_idx; 3908c2ecf20Sopenharmony_ci uwire_set_clk1_div(div1_idx); 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_ci rate /= div1; 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci switch (div2) { 3958c2ecf20Sopenharmony_ci case 0: 3968c2ecf20Sopenharmony_ci case 1: 3978c2ecf20Sopenharmony_ci case 2: 3988c2ecf20Sopenharmony_ci flags |= UWIRE_FREQ_DIV_2; 3998c2ecf20Sopenharmony_ci rate /= 2; 4008c2ecf20Sopenharmony_ci break; 4018c2ecf20Sopenharmony_ci case 3: 4028c2ecf20Sopenharmony_ci case 4: 4038c2ecf20Sopenharmony_ci flags |= UWIRE_FREQ_DIV_4; 4048c2ecf20Sopenharmony_ci rate /= 4; 4058c2ecf20Sopenharmony_ci break; 4068c2ecf20Sopenharmony_ci case 5: 4078c2ecf20Sopenharmony_ci case 6: 4088c2ecf20Sopenharmony_ci case 7: 4098c2ecf20Sopenharmony_ci case 8: 4108c2ecf20Sopenharmony_ci flags |= UWIRE_FREQ_DIV_8; 4118c2ecf20Sopenharmony_ci rate /= 8; 4128c2ecf20Sopenharmony_ci break; 4138c2ecf20Sopenharmony_ci } 4148c2ecf20Sopenharmony_ci omap_uwire_configure_mode(spi->chip_select, flags); 4158c2ecf20Sopenharmony_ci pr_debug("%s: uwire flags %02x, armxor %lu KHz, SCK %lu KHz\n", 4168c2ecf20Sopenharmony_ci __func__, flags, 4178c2ecf20Sopenharmony_ci clk_get_rate(uwire->ck) / 1000, 4188c2ecf20Sopenharmony_ci rate / 1000); 4198c2ecf20Sopenharmony_ci status = 0; 4208c2ecf20Sopenharmony_cidone: 4218c2ecf20Sopenharmony_ci return status; 4228c2ecf20Sopenharmony_ci} 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_cistatic int uwire_setup(struct spi_device *spi) 4258c2ecf20Sopenharmony_ci{ 4268c2ecf20Sopenharmony_ci struct uwire_state *ust = spi->controller_state; 4278c2ecf20Sopenharmony_ci bool initial_setup = false; 4288c2ecf20Sopenharmony_ci int status; 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ci if (ust == NULL) { 4318c2ecf20Sopenharmony_ci ust = kzalloc(sizeof(*ust), GFP_KERNEL); 4328c2ecf20Sopenharmony_ci if (ust == NULL) 4338c2ecf20Sopenharmony_ci return -ENOMEM; 4348c2ecf20Sopenharmony_ci spi->controller_state = ust; 4358c2ecf20Sopenharmony_ci initial_setup = true; 4368c2ecf20Sopenharmony_ci } 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci status = uwire_setup_transfer(spi, NULL); 4398c2ecf20Sopenharmony_ci if (status && initial_setup) 4408c2ecf20Sopenharmony_ci kfree(ust); 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci return status; 4438c2ecf20Sopenharmony_ci} 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_cistatic void uwire_cleanup(struct spi_device *spi) 4468c2ecf20Sopenharmony_ci{ 4478c2ecf20Sopenharmony_ci kfree(spi->controller_state); 4488c2ecf20Sopenharmony_ci} 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_cistatic void uwire_off(struct uwire_spi *uwire) 4518c2ecf20Sopenharmony_ci{ 4528c2ecf20Sopenharmony_ci uwire_write_reg(UWIRE_SR3, 0); 4538c2ecf20Sopenharmony_ci clk_disable_unprepare(uwire->ck); 4548c2ecf20Sopenharmony_ci spi_master_put(uwire->bitbang.master); 4558c2ecf20Sopenharmony_ci} 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_cistatic int uwire_probe(struct platform_device *pdev) 4588c2ecf20Sopenharmony_ci{ 4598c2ecf20Sopenharmony_ci struct spi_master *master; 4608c2ecf20Sopenharmony_ci struct uwire_spi *uwire; 4618c2ecf20Sopenharmony_ci int status; 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_ci master = spi_alloc_master(&pdev->dev, sizeof *uwire); 4648c2ecf20Sopenharmony_ci if (!master) 4658c2ecf20Sopenharmony_ci return -ENODEV; 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_ci uwire = spi_master_get_devdata(master); 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci uwire_base = devm_ioremap(&pdev->dev, UWIRE_BASE_PHYS, UWIRE_IO_SIZE); 4708c2ecf20Sopenharmony_ci if (!uwire_base) { 4718c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, "can't ioremap UWIRE\n"); 4728c2ecf20Sopenharmony_ci spi_master_put(master); 4738c2ecf20Sopenharmony_ci return -ENOMEM; 4748c2ecf20Sopenharmony_ci } 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, uwire); 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ci uwire->ck = devm_clk_get(&pdev->dev, "fck"); 4798c2ecf20Sopenharmony_ci if (IS_ERR(uwire->ck)) { 4808c2ecf20Sopenharmony_ci status = PTR_ERR(uwire->ck); 4818c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, "no functional clock?\n"); 4828c2ecf20Sopenharmony_ci spi_master_put(master); 4838c2ecf20Sopenharmony_ci return status; 4848c2ecf20Sopenharmony_ci } 4858c2ecf20Sopenharmony_ci clk_prepare_enable(uwire->ck); 4868c2ecf20Sopenharmony_ci 4878c2ecf20Sopenharmony_ci if (cpu_is_omap7xx()) 4888c2ecf20Sopenharmony_ci uwire_idx_shift = 1; 4898c2ecf20Sopenharmony_ci else 4908c2ecf20Sopenharmony_ci uwire_idx_shift = 2; 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_ci uwire_write_reg(UWIRE_SR3, 1); 4938c2ecf20Sopenharmony_ci 4948c2ecf20Sopenharmony_ci /* the spi->mode bits understood by this driver: */ 4958c2ecf20Sopenharmony_ci master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; 4968c2ecf20Sopenharmony_ci master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 16); 4978c2ecf20Sopenharmony_ci master->flags = SPI_MASTER_HALF_DUPLEX; 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_ci master->bus_num = 2; /* "official" */ 5008c2ecf20Sopenharmony_ci master->num_chipselect = 4; 5018c2ecf20Sopenharmony_ci master->setup = uwire_setup; 5028c2ecf20Sopenharmony_ci master->cleanup = uwire_cleanup; 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci uwire->bitbang.master = master; 5058c2ecf20Sopenharmony_ci uwire->bitbang.chipselect = uwire_chipselect; 5068c2ecf20Sopenharmony_ci uwire->bitbang.setup_transfer = uwire_setup_transfer; 5078c2ecf20Sopenharmony_ci uwire->bitbang.txrx_bufs = uwire_txrx; 5088c2ecf20Sopenharmony_ci 5098c2ecf20Sopenharmony_ci status = spi_bitbang_start(&uwire->bitbang); 5108c2ecf20Sopenharmony_ci if (status < 0) { 5118c2ecf20Sopenharmony_ci uwire_off(uwire); 5128c2ecf20Sopenharmony_ci } 5138c2ecf20Sopenharmony_ci return status; 5148c2ecf20Sopenharmony_ci} 5158c2ecf20Sopenharmony_ci 5168c2ecf20Sopenharmony_cistatic int uwire_remove(struct platform_device *pdev) 5178c2ecf20Sopenharmony_ci{ 5188c2ecf20Sopenharmony_ci struct uwire_spi *uwire = platform_get_drvdata(pdev); 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci // FIXME remove all child devices, somewhere ... 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci spi_bitbang_stop(&uwire->bitbang); 5238c2ecf20Sopenharmony_ci uwire_off(uwire); 5248c2ecf20Sopenharmony_ci return 0; 5258c2ecf20Sopenharmony_ci} 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci/* work with hotplug and coldplug */ 5288c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:omap_uwire"); 5298c2ecf20Sopenharmony_ci 5308c2ecf20Sopenharmony_cistatic struct platform_driver uwire_driver = { 5318c2ecf20Sopenharmony_ci .driver = { 5328c2ecf20Sopenharmony_ci .name = "omap_uwire", 5338c2ecf20Sopenharmony_ci }, 5348c2ecf20Sopenharmony_ci .probe = uwire_probe, 5358c2ecf20Sopenharmony_ci .remove = uwire_remove, 5368c2ecf20Sopenharmony_ci // suspend ... unuse ck 5378c2ecf20Sopenharmony_ci // resume ... use ck 5388c2ecf20Sopenharmony_ci}; 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_cistatic int __init omap_uwire_init(void) 5418c2ecf20Sopenharmony_ci{ 5428c2ecf20Sopenharmony_ci /* FIXME move these into the relevant board init code. also, include 5438c2ecf20Sopenharmony_ci * H3 support; it uses tsc2101 like H2 (on a different chipselect). 5448c2ecf20Sopenharmony_ci */ 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_ci if (machine_is_omap_h2()) { 5478c2ecf20Sopenharmony_ci /* defaults: W21 SDO, U18 SDI, V19 SCL */ 5488c2ecf20Sopenharmony_ci omap_cfg_reg(N14_1610_UWIRE_CS0); 5498c2ecf20Sopenharmony_ci omap_cfg_reg(N15_1610_UWIRE_CS1); 5508c2ecf20Sopenharmony_ci } 5518c2ecf20Sopenharmony_ci if (machine_is_omap_perseus2()) { 5528c2ecf20Sopenharmony_ci /* configure pins: MPU_UW_nSCS1, MPU_UW_SDO, MPU_UW_SCLK */ 5538c2ecf20Sopenharmony_ci int val = omap_readl(OMAP7XX_IO_CONF_9) & ~0x00EEE000; 5548c2ecf20Sopenharmony_ci omap_writel(val | 0x00AAA000, OMAP7XX_IO_CONF_9); 5558c2ecf20Sopenharmony_ci } 5568c2ecf20Sopenharmony_ci 5578c2ecf20Sopenharmony_ci return platform_driver_register(&uwire_driver); 5588c2ecf20Sopenharmony_ci} 5598c2ecf20Sopenharmony_ci 5608c2ecf20Sopenharmony_cistatic void __exit omap_uwire_exit(void) 5618c2ecf20Sopenharmony_ci{ 5628c2ecf20Sopenharmony_ci platform_driver_unregister(&uwire_driver); 5638c2ecf20Sopenharmony_ci} 5648c2ecf20Sopenharmony_ci 5658c2ecf20Sopenharmony_cisubsys_initcall(omap_uwire_init); 5668c2ecf20Sopenharmony_cimodule_exit(omap_uwire_exit); 5678c2ecf20Sopenharmony_ci 5688c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 5698c2ecf20Sopenharmony_ci 570