/* * PS3 virtual uart * * Copyright (C) 2006 Sony Computer Entertainment Inc. * Copyright 2006 Sony Corp. * * 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; version 2 of the License. * * 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. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #include #include "vuart.h" MODULE_AUTHOR("Sony Corporation"); MODULE_LICENSE("GPL v2"); MODULE_DESCRIPTION("PS3 vuart"); /** * vuart - An inter-partition data link service. * port 0: PS3 AV Settings. * port 2: PS3 System Manager. * * The vuart provides a bi-directional byte stream data link between logical * partitions. Its primary role is as a communications link between the guest * OS and the system policy module. The current HV does not support any * connections other than those listed. */ enum {PORT_COUNT = 3,}; enum vuart_param { PARAM_TX_TRIGGER = 0, PARAM_RX_TRIGGER = 1, PARAM_INTERRUPT_MASK = 2, PARAM_RX_BUF_SIZE = 3, /* read only */ PARAM_RX_BYTES = 4, /* read only */ PARAM_TX_BUF_SIZE = 5, /* read only */ PARAM_TX_BYTES = 6, /* read only */ PARAM_INTERRUPT_STATUS = 7, /* read only */ }; enum vuart_interrupt_bit { INTERRUPT_BIT_TX = 0, INTERRUPT_BIT_RX = 1, INTERRUPT_BIT_DISCONNECT = 2, }; enum vuart_interrupt_mask { INTERRUPT_MASK_TX = 1, INTERRUPT_MASK_RX = 2, INTERRUPT_MASK_DISCONNECT = 4, }; /** * struct ps3_vuart_port_priv - private vuart device data. */ struct ps3_vuart_port_priv { u64 interrupt_mask; struct { spinlock_t lock; struct list_head head; } tx_list; struct { struct ps3_vuart_work work; unsigned long bytes_held; spinlock_t lock; struct list_head head; } rx_list; struct ps3_vuart_stats stats; }; static struct ps3_vuart_port_priv *to_port_priv( struct ps3_system_bus_device *dev) { BUG_ON(!dev); BUG_ON(!dev->driver_priv); return (struct ps3_vuart_port_priv *)dev->driver_priv; } /** * struct ports_bmp - bitmap indicating ports needing service. * * A 256 bit read only bitmap indicating ports needing service. Do not write * to these bits. Must not cross a page boundary. */ struct ports_bmp { u64 status; u64 unused[3]; } __attribute__((aligned(32))); #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__) static void __maybe_unused _dump_ports_bmp( const struct ports_bmp *bmp, const char *func, int line) { pr_debug("%s:%d: ports_bmp: %016llxh\n", func, line, bmp->status); } #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__) static void __maybe_unused _dump_port_params(unsigned int port_number, const char *func, int line) { #if defined(DEBUG) static const char *strings[] = { "tx_trigger ", "rx_trigger ", "interrupt_mask ", "rx_buf_size ", "rx_bytes ", "tx_buf_size ", "tx_bytes ", "interrupt_status", }; int result; unsigned int i; u64 value; for (i = 0; i < ARRAY_SIZE(strings); i++) { result = lv1_get_virtual_uart_param(port_number, i, &value); if (result) { pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line, port_number, strings[i], ps3_result(result)); continue; } pr_debug("%s:%d: port_%u: %s = %lxh\n", func, line, port_number, strings[i], value); } #endif } int ps3_vuart_get_triggers(struct ps3_system_bus_device *dev, struct vuart_triggers *trig) { int result; u64 size; u64 val; u64 tx; result = lv1_get_virtual_uart_param(dev->port_number, PARAM_TX_TRIGGER, &tx); trig->tx = tx; if (result) { dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n", __func__, __LINE__, ps3_result(result)); return result; } result = lv1_get_virtual_uart_param(dev->port_number, PARAM_RX_BUF_SIZE, &size); if (result) { dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n", __func__, __LINE__, ps3_result(result)); return result; } result = lv1_get_virtual_uart_param(dev->port_number, PARAM_RX_TRIGGER, &val); if (result) { dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n", __func__, __LINE__, ps3_result(result)); return result; } trig->rx = size - val; dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__, trig->tx, trig->rx); return result; } int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx, unsigned int rx) { int result; u64 size; result = lv1_set_virtual_uart_param(dev->port_number, PARAM_TX_TRIGGER, tx); if (result) { dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n", __func__, __LINE__, ps3_result(result)); return result; } result = lv1_get_virtual_uart_param(dev->port_number, PARAM_RX_BUF_SIZE, &size); if (result) { dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n", __func__, __LINE__, ps3_result(result)); return result; } result = lv1_set_virtual_uart_param(dev->port_number, PARAM_RX_TRIGGER, size - rx); if (result) { dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n", __func__, __LINE__, ps3_result(result)); return result; } dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__, tx, rx); return result; } static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev, u64 *bytes_waiting) { int result; result = lv1_get_virtual_uart_param(dev->port_number, PARAM_RX_BYTES, bytes_waiting); if (result) dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n", __func__, __LINE__, ps3_result
/*
 * arch/arm/mach-ixp4xx/coyote-pci.c
 *
 * PCI setup routines for ADI Engineering Coyote platform
 *
 * Copyright (C) 2002 Jungo Software Technologies.
 * Copyright (C) 2003 MontaVista Softwrae, Inc.
 *
 * Maintainer: Deepak Saxena <dsaxena@mvista.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <asm/mach-types.h>
#include <mach/hardware.h>
#include <asm/irq.h>
#include <asm/mach/pci.h>

#define SLOT0_DEVID	14
#define SLOT1_DEVID	15

/* PCI controller GPIO to IRQ pin mappings */
#define SLOT0_INTA	6
#define SLOT1_INTA	11

void __init coyote_pci_preinit(void)
{
	irq_set_irq_type(IXP4XX_GPIO_IRQ(SLOT0_INTA), IRQ_TYPE_LEVEL_LOW);
	irq_set_irq_type(IXP4XX_GPIO_IRQ(SLOT1_INTA), IRQ_TYPE_LEVEL_LOW);
	ixp4xx_pci_preinit();
}

static int __init coyote_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
{
	if (slot == SLOT0_DEVID)
		return IXP4XX_GPIO_IRQ(SLOT0_INTA);
	else if (slot == SLOT1_DEVID)
		return IXP4XX_GPIO_IRQ(SLOT1_INTA);
	else return -1;
}

struct hw_pci coyote_pci __initdata = {
	.nr_controllers = 1,
	.ops		= &ixp4xx_ops,
	.preinit =        coyote_pci_preinit,
	.setup =          ixp4xx_setup,
	.map_irq =        coyote_map_irq,
};

int __init coyote_pci_init(void)
{
	if (machine_is_adi_coyote())
		pci_common_init(&coyote_pci);
	return 0;
}

subsys_initcall(coyote_pci_init);
andle_interrupt_disconnect(dev); if (result) ps3_vuart_disable_interrupt_disconnect(dev); } if (status & INTERRUPT_MASK_TX) { priv->stats.tx_interrupts++; result = ps3_vuart_handle_interrupt_tx(dev); if (result) ps3_vuart_disable_interrupt_tx(dev); } if (status & INTERRUPT_MASK_RX) { priv->stats.rx_interrupts++; result = ps3_vuart_handle_interrupt_rx(dev); if (result) ps3_vuart_disable_interrupt_rx(dev); } return 0; } struct vuart_bus_priv { struct ports_bmp *bmp; unsigned int virq; struct mutex probe_mutex; int use_count; struct ps3_system_bus_device *devices[PORT_COUNT]; } static vuart_bus_priv; /** * ps3_vuart_irq_handler - first stage interrupt handler * * Loops finding any interrupting port and its associated instance data. * Passes control to the second stage port specific interrupt handler. Loops * until all outstanding interrupts are serviced. */ static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private) { struct vuart_bus_priv *bus_priv = _private; BUG_ON(!bus_priv); while (1) { unsigned int port; dump_ports_bmp(bus_priv->bmp); port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status); if (port == BITS_PER_LONG) break; BUG_ON(port >= PORT_COUNT); BUG_ON(!bus_priv->devices[port]); ps3_vuart_handle_port_interrupt(bus_priv->devices[port]); } return IRQ_HANDLED; } static int ps3_vuart_bus_interrupt_get(void) { int result; pr_debug(" -> %s:%d\n", __func__, __LINE__); vuart_bus_priv.use_count++; BUG_ON(vuart_bus_priv.use_count > 2); if (vuart_bus_priv.use_count != 1) return 0; BUG_ON(vuart_bus_priv.bmp); vuart_bus_priv.bmp = kzalloc(sizeof(struct ports_bmp), GFP_KERNEL); if (!vuart_bus_priv.bmp) { pr_debug("%s:%d: kzalloc failed.\n", __func__, __LINE__); result = -ENOMEM; goto fail_bmp_malloc; } result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY, vuart_bus_priv.bmp, &vuart_bus_priv.virq); if (result) { pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n", __func__, __LINE__, result); result = -EPERM; goto fail_alloc_irq; } result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler, 0, "vuart", &vuart_bus_priv); if (result) { pr_debug("%s:%d: request_irq failed (%d)\n", __func__, __LINE__, result); goto fail_request_irq; } pr_debug(" <- %s:%d: ok\n", __func__, __LINE__); return result; fail_request_irq: ps3_vuart_irq_destroy(vuart_bus_priv.virq); vuart_bus_priv.virq = NO_IRQ; fail_alloc_irq: kfree(vuart_bus_priv.bmp); vuart_bus_priv.bmp = NULL; fail_bmp_malloc: vuart_bus_priv.use_count--; pr_debug(" <- %s:%d: failed\n", __func__, __LINE__); return result; } static int ps3_vuart_bus_interrupt_put(void) { pr_debug(" -> %s:%d\n", __func__, __LINE__); vuart_bus_priv.use_count--; BUG_ON(vuart_bus_priv.use_count < 0); if (vuart_bus_priv.use_count != 0) return 0; free_irq(vuart_bus_priv.virq, &vuart_bus_priv); ps3_vuart_irq_destroy(vuart_bus_priv.virq); vuart_bus_priv.virq = NO_IRQ; kfree(vuart_bus_priv.bmp); vuart_bus_priv.bmp = NULL; pr_debug(" <- %s:%d\n", __func__, __LINE__); return 0; } static int ps3_vuart_probe(struct ps3_system_bus_device *dev) { int result; struct ps3_vuart_port_driver *drv; struct ps3_vuart_port_priv *priv = NULL; dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); drv = ps3_system_bus_dev_to_vuart_drv(dev); BUG_ON(!drv); dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name); if (dev->port_number >= PORT_COUNT) { BUG(); return -EINVAL; } mutex_lock(&vuart_bus_priv.probe_mutex); result = ps3_vuart_bus_interrupt_get(); if (result) goto fail_setup_interrupt; if (vuart_bus_priv.devices[dev->port_number]) { dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__, __LINE__, dev->port_number); result = -EBUSY; goto fail_busy; } vuart_bus_priv.devices[dev->port_number] = dev; /* Setup dev->driver_priv. */ dev->driver_priv = kzalloc(sizeof(struct ps3_vuart_port_priv), GFP_KERNEL); if (!dev->driver_priv) { result = -ENOMEM; goto fail_dev_malloc; } priv = to_port_priv(dev); INIT_LIST_HEAD(&priv->tx_list.head); spin_lock_init(&priv->tx_list.lock); INIT_LIST_HEAD(&priv->rx_list.head); spin_lock_init(&priv->rx_list.lock); INIT_WORK(&priv->rx_list.work.work, ps3_vuart_work); priv->rx_list.work.trigger = 0; priv->rx_list.work.dev = dev; /* clear stale pending interrupts */ ps3_vuart_clear_rx_bytes(dev, 0); ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX); ps3_vuart_set_triggers(dev, 1, 1); if (drv->probe) result = drv->probe(dev); else { result = 0; dev_info(&dev->core, "%s:%d: no probe method\n", __func__, __LINE__); } if (result) { dev_dbg(&dev->core, "%s:%d: drv->probe failed\n", __func__, __LINE__); goto fail_probe; } mutex_unlock(&vuart_bus_priv.probe_mutex); return result; fail_probe: ps3_vuart_set_interrupt_mask(dev, 0); kfree(dev->driver_priv); dev->driver_priv = NULL; fail_dev_malloc: vuart_bus_priv.devices[dev->port_number] = NULL; fail_busy: ps3_vuart_bus_interrupt_put(); fail_setup_interrupt: mutex_unlock(&vuart_bus_priv.probe_mutex); dev_dbg(&dev->core, "%s:%d: failed\n", __func__, __LINE__); return result; } /** * ps3_vuart_cleanup - common cleanup helper. * @dev: The struct ps3_system_bus_device instance. * * Cleans interrupts and HV resources. Must be called with * vuart_bus_priv.probe_mutex held. Used by ps3_vuart_remove and * ps3_vuart_shutdown. After this call, polled reading will still work. */ static int ps3_vuart_cleanup(struct ps3_system_bus_device *dev) { dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); ps3_vuart_cancel_async(dev); ps3_vuart_set_interrupt_mask(dev, 0); ps3_vuart_bus_interrupt_put(); return 0; } /** * ps3_vuart_remove - Completely clean the device instance. * @dev: The struct ps3_system_bus_device instance. * * Cleans all memory, interrupts and HV resources. After this call the * device can no longer be used. */ static int ps3_vuart_remove(struct ps3_system_bus_device *dev) { struct ps3_vuart_port_priv *priv = to_port_priv(dev); struct ps3_vuart_port_driver *drv; BUG_ON(!dev); mutex_lock(&vuart_bus_priv.probe_mutex); dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__, dev->match_id); if (!dev->core.driver) { dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__, __LINE__); mutex_unlock(&vuart_bus_priv.probe_mutex); return 0; } drv = ps3_system_bus_dev_to_vuart_drv(dev); BUG_ON(!drv); if (drv->remove) { drv->remove(dev); } else { dev_dbg(&dev->core, "%s:%d: no remove method\n", __func__, __LINE__); BUG(); } ps3_vuart_cleanup(dev); vuart_bus_priv.devices[dev->port_number] = NULL; kfree(priv); priv = NULL; dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__); mutex_unlock(&vuart_bus_priv.probe_mutex); return 0; } /** * ps3_vuart_shutdown - Cleans interrupts and HV resources. * @dev: The struct ps3_system_bus_device instance. * * Cleans interrupts and HV resources. After this call the * device can still be used in polling mode. This behavior required * by sys-manager to be able to complete the device power operation * sequence. */ static int ps3_vuart_shutdown(struct ps3_system_bus_device *dev) { struct ps3_vuart_port_driver *drv; BUG_ON(!dev); mutex_lock(&vuart_bus_priv.probe_mutex); dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__, dev->match_id); if (!dev->core.driver) { dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__, __LINE__); mutex_unlock(&vuart_bus_priv.probe_mutex); return 0; } drv = ps3_system_bus_dev_to_vuart_drv(dev); BUG_ON(!drv); if (drv->shutdown) drv->shutdown(dev); else if (drv->remove) { dev_dbg(&dev->core, "%s:%d: no shutdown, calling remove\n", __func__, __LINE__); drv->remove(dev); } else { dev_dbg(&dev->core, "%s:%d: no shutdown method\n", __func__, __LINE__); BUG(); } ps3_vuart_cleanup(dev); dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__); mutex_unlock(&vuart_bus_priv.probe_mutex); return 0; } static int __init ps3_vuart_bus_init(void) { pr_debug("%s:%d:\n", __func__, __LINE__); if (!firmware_has_feature(FW_FEATURE_PS3_LV1)) return -ENODEV; mutex_init(&vuart_bus_priv.probe_mutex); return 0; } static void __exit ps3_vuart_bus_exit(void) { pr_debug("%s:%d:\n", __func__, __LINE__); } core_initcall(ps3_vuart_bus_init); module_exit(ps3_vuart_bus_exit); /** * ps3_vuart_port_driver_register - Add a vuart port device driver. */ int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv) { int result; pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name); BUG_ON(!drv->core.match_id); BUG_ON(!drv->core.core.name); drv->core.probe = ps3_vuart_probe; drv->core.remove = ps3_vuart_remove; drv->core.shutdown = ps3_vuart_shutdown; result = ps3_system_bus_driver_register(&drv->core); return result; } EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register); /** * ps3_vuart_port_driver_unregister - Remove a vuart port device driver. */ void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv) { pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name); ps3_system_bus_driver_unregister(&drv->core); } EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);