/* arch/arm/mach-msm/kaiser-smd.c - MSM7200 shared memory communications.
 *
 * Copyright (C) 2007 Google, Inc.
 * Copyright (C) 2008 Martin Johnson.
 *
 * Authors: Martin Johnson <M.J.Johnson@massey.ac.nz>,
 *	    Lukas Gorris <lukas.gorris@gmail.com>,
 *	    Martin Ling <martin-linux-msm@earth.li>,
 *	    Patrick Remy <patrick.remy2000@gmail.com>
 *
 * Based on smd.c by Brian Swetland <swetland@google.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/wait.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/debugfs.h>
#include <linux/delay.h>
#include <asm/arch/msm_smd.h>
#include <asm/arch/msm_iomap.h>
#include <asm/arch/system.h>
#include <asm/io.h>

#include "smd_private.h"
#include "proc_comm.h"

#define MODULE_NAME "msm_smd"

// Toggle to enable debug printks.
#ifdef KAISER_SMD_DEBUG
#define D(x...) printk(x)
#else
#define D(x...) do {} while (0)
#endif

/* This spinlock is used to synchronize between the
 * irq handler and code that mutates the channel
 * list or fiddles with channel state. */
static DEFINE_SPINLOCK(smd_lock);

/* This mutex is used during open() and close()
 * operations to avoid races while creating or
 * destroying smd_channel structures. */
static DEFINE_MUTEX(smd_creation_mutex);

// Flag set when smd_core_init() has been called successfully.
static int smd_initialized = 0;

// FIFO states.
#define SMD_SS_CLOSED		0x00000000
#define SMD_SS_OPENING		0x00000001
#define SMD_SS_OPENED		0x00000002
#define SMD_SS_FLUSHING		0x00000003
#define SMD_SS_CLOSING		0x00000004
#define SMD_SS_RESET		0x00000005
#define SMD_SS_RESET_OPENING	0x00000006

// Size of the circular buffer in each shared memory FIFO.
#define BUF_SIZE 0x2000

// Maximum number of bytes to read/write before notifying.
#define BURST_SIZE 0xf4

// FIFO structure located in shared memory.
struct smd_fifo
{
	volatile unsigned int state;
	volatile unsigned char fDSR;
	volatile unsigned char fCTS;
	volatile unsigned char fCD;
	volatile unsigned char fRI;
	volatile unsigned char fHEAD;
	volatile unsigned char fTAIL;
	volatile unsigned char fSTATE;
	volatile unsigned char fUNUSED;
	volatile unsigned int tail;
	volatile unsigned int head;
	volatile unsigned int padding;
	volatile char buffer[BUF_SIZE];
};

// Bidirectional channel implemented using two FIFOs.
struct smd_channel
{
	volatile struct smd_fifo *send;
	volatile struct smd_fifo *recv;
	struct list_head ch_list;

	unsigned n;
	void *priv;
	void (*notify)(void *priv, unsigned flags);
};

// List of currently open channels.
static LIST_HEAD(smd_ch_list);

// How many bytes are available for reading from a channel.
int smd_read_avail(struct smd_channel *ch)
{
	if (ch->recv == NULL)
		return 0;

	return (ch->recv->head - ch->recv->tail + BUF_SIZE) % BUF_SIZE;
}

// How many bytes we are free to write to a channel.
int smd_write_avail(struct smd_channel *ch)
{
	if (ch->send == NULL)
		return 0;

	return (BUF_SIZE - 1) -
		((ch->send->head- ch->send->tail + BUF_SIZE) % BUF_SIZE);
}

#define MSM_A2M_INT(n) (MSM_CSR_BASE + 0x400 + (n) * 4)

// Interrupt the ARM9 to tell it we have written to the shared memory.
static inline void notify_other_smd(int ch)
{
	/* TODO: Isolate which line is used for which channel and set
	 * only the appropriate one for the given channel. */
	writel(1,MSM_A2M_INT(0));
	writel(1,MSM_A2M_INT(1));
	writel(1,MSM_A2M_INT(2));
}

// Check for changes to the FIFOs of all currently open channels.
void check_for_smd_data(void)
{
	struct smd_channel *ch;
	unsigned long flags;
	
	spin_lock_irqsave(&smd_lock, flags);
	list_for_each_entry(ch, &smd_ch_list, ch_list) {
		if(ch->recv->head != ch->recv->tail)
			ch->notify(ch->priv, SMD_EVENT_DATA);
		if(ch->send->head != ch->send->tail)
			notify_other_smd(ch->n);
	}
	spin_unlock_irqrestore(&smd_lock, flags);
}

// Interrupt handler called when the ARM9 has written to us.
static irqreturn_t smd_irq_handler(int irq, void *data)
{
	D("smd: irq %d\n", irq);
	/* TODO: work out which interrupt actually relates to
	* each FIFO. For now, just check all of them. */
	check_for_smd_data();
	return IRQ_HANDLED;
}

// Write data to a channel.
int smd_write(smd_channel_t *ch, const void *_data, int len)
{
	const unsigned char *buf = _data;
	unsigned int myhead;
	int sent = 0;

	if (ch->send == NULL)
		return -ENODEV;
	
	if (len < 0)
		return -EINVAL;

	myhead = ch->send->head;

	while (sent < len) {
	 	ch->send->buffer[myhead] = *buf++;

		if ((sent++ % BURST_SIZE) == 0) {
			ch->send->head = myhead;
			ch->send->fHEAD = 1;
			notify_other_smd(ch->n);
		}

		myhead = (myhead + 1) % BUF_SIZE;
	}

	ch->send->head = myhead;
	ch->send->fHEAD = 1;

	notify_other_smd(ch->n);

	return sent;
}

// Read data from a channel.
int smd_read(smd_channel_t *ch, void *data, int len)
{
	unsigned char *buf = data;
	unsigned int mytail;
	int recvd = 0;

	if (ch->recv == NULL)
		return -ENODEV;
	
	if (len < 0)
		return -EINVAL;

	mytail = ch->recv->tail;

	while (recvd < len) {
		if (buf)
			*buf++ = ch->recv->buffer[mytail];

		if ((recvd++ % BURST_SIZE) == 0) {
			ch->recv->tail = mytail;
			ch->recv->fTAIL = 1;
			notify_other_smd(ch->n);
		}

		mytail = (mytail + 1) % BUF_SIZE;
	}

	ch->recv->tail = mytail;
	ch->recv->fTAIL = 1;

	notify_other_smd(ch->n);

	return recvd;
}

// Set the state of a FIFO.
static void smd_set_state(volatile struct smd_fifo *hc,
				unsigned state, unsigned ch)
{
	if (state == SMD_SS_OPENED) {
		hc->fDSR = 1;
		hc->fCTS = 1;
		hc->fCD = 1;
	} else {
		hc->fDSR = 0;
		hc->fCTS = 0;
		hc->fCD = 0;
	}
	hc->state = state;
	hc->fSTATE = 1;
	notify_other_smd(ch);
}

// Force both sides of a channel to check for data.
void smd_kick(smd_channel_t *ch)
{
	unsigned long flags;

	spin_lock_irqsave(&smd_lock, flags);
	ch->notify(ch->priv, SMD_EVENT_DATA);
	notify_other_smd(ch->n);
	spin_unlock_irqrestore(&smd_lock, flags);
}

// Empty default notify function.
static void do_nothing_notify(void *priv, unsigned flags) {}

// Open an shared memory channel.
int smd_open(int n, smd_channel_t **_ch, void *priv,
		void (*notify)(void *, unsigned))
{
	struct smd_channel *ch;
	unsigned long flags;
	int res = 0;

	if (!smd_initialized) {
		printk(KERN_INFO "smd_open() before smd_core_init()\n");
		return -ENODEV;
	}

	if (notify == 0)
		notify = do_nothing_notify;

	mutex_lock(&smd_creation_mutex);

	spin_lock_irqsave(&smd_lock, flags);
	list_for_each_entry(ch, &smd_ch_list, ch_list) {
		if (ch->n == n) {
			res = -ENODEV;
			break;
		}
	}
	spin_unlock_irqrestore(&smd_lock, flags);
	if (res) goto out;

	ch = kzalloc(sizeof(struct smd_channel), GFP_KERNEL);
	if (ch == 0) {
		res = -ENOMEM;
		goto out;
	}

	switch (n) {
	case 0:
		// AT command channel to GSM modem.
		ch->send = (struct smd_fifo *)(MSM_SHARED_RAM_BASE+0x041f0);
		ch->recv = (struct smd_fifo *)(MSM_SHARED_RAM_BASE+0x32418);
		break;
	case 1:
		// Data channel to GSM modem.
		ch->send = (struct smd_fifo *)(MSM_SHARED_RAM_BASE+0x12298);
		ch->recv = (struct smd_fifo *)(MSM_SHARED_RAM_BASE+0x404c0);
		break;
	case 2:
		// Mystery channel.
		ch->send = (struct smd_fifo *)(MSM_SHARED_RAM_BASE+0x8220);
		ch->recv = (struct smd_fifo *)(MSM_SHARED_RAM_BASE+0x36448);
		break;
	case 3:
		// Mystery channel.
		ch->send = (struct smd_fifo *)(MSM_SHARED_RAM_BASE+0xa238);
		ch->recv = (struct smd_fifo *)(MSM_SHARED_RAM_BASE+0x38460);
		break;
	case 7:
		// Read-only channel from GPS.
		ch->send = NULL;
		ch->recv = (struct smd_fifo *)(MSM_SHARED_RAM_BASE+0x545b0);
		break;
	default:
		res = -ENODEV;
		goto out;
	}

	ch->priv = priv;
	ch->notify = notify;
	ch->n = n;

	*_ch = ch;

	// TODO: Set recv state, do other state changes...
	smd_set_state(ch->send, SMD_SS_OPENED, ch->n);

	spin_lock_irqsave(&smd_lock, flags);
	list_add(&ch->ch_list, &smd_ch_list);
	spin_unlock_irqrestore(&smd_lock, flags);

	// Trigger handling of any data already waiting in the FIFOs.
	smd_kick(ch);

out:
	mutex_unlock(&smd_creation_mutex);
	return res;
}

// Close an SMD channel.
int smd_close(smd_channel_t *ch)
{
	unsigned long flags;

	if (ch == NULL)
		return -EINVAL;

	mutex_lock(&smd_creation_mutex);
	spin_lock_irqsave(&smd_lock, flags);

	list_del(&ch->ch_list);
	D("smd_close: closing ch %d\n", ch->n);

	spin_unlock_irqrestore(&smd_lock, flags);
	mutex_unlock(&smd_creation_mutex);

	return 0;
}

// Init for Kaiser SMD driver.
int smd_core_init(void)
{
	int r;
	int i;
	printk(KERN_INFO "smd_core_init()\n");

	/* TODO: These interrupts are used by the ARM9 to tell us that it has
	 * written to the shared memory. They are associated with particular
	 * channels and should really be requested only when those channels
	 * are opened. */
	for (i = 0; i < 7; i++) {
		r = request_irq(INT_A9_M2A_0+i, smd_irq_handler,
			IRQF_TRIGGER_RISING, "smd_dev", 0);
		if (r < 0)
			return r;
		r = enable_irq_wake(INT_A9_M2A_0+i);
		if (r < 0)
			printk(KERN_ERR "smd_core_init: "
				"enable_irq_wake failed for INT_A9_M2A_%d\n",i);
	}

	smd_initialized = 1;

	printk(KERN_INFO "smd_core_init() done\n");

	return 0;
}

/* Generic driver glue follows below... */

static int __init msm_smd_probe(struct platform_device *pdev)
{
	printk(KERN_INFO "smd_init()\n");

	if (smd_core_init()) {
		printk(KERN_ERR "smd_core_init() failed\n");
		return -1;
	}

	return 0;
}

static struct platform_driver msm_smd_driver = {
	.probe = msm_smd_probe,
	.driver = {
		.name = MODULE_NAME,
		.owner = THIS_MODULE,
	},
};

static int __init msm_smd_init(void)
{
	return platform_driver_register(&msm_smd_driver);
}

module_init(msm_smd_init);

MODULE_DESCRIPTION("MSM Shared Memory Core for Kaiser");
MODULE_AUTHOR("Martin Johnson <>");
MODULE_LICENSE("GPL");


/* Unimplemented stubs for Kaiser that live here for lack of a better home... */

void *smem_alloc(unsigned id, unsigned size)
{
	return 0;
}

int msm_proc_comm(unsigned cmd, unsigned *data1, unsigned *data2)
{
	return -1;
}
