^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Core maple bus functionality
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2007 - 2009 Adrian McMenamin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2001 - 2008 Paul Mundt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2000 - 2001 YAEGASHI Takeshi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2001 M. R. Brown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * This file is subject to the terms and conditions of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * License. See the file "COPYING" in the main directory of this archive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/maple.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <asm/dma.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <mach/dma.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <mach/sysasic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) MODULE_AUTHOR("Adrian McMenamin <adrian@mcmen.demon.co.uk>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) MODULE_DESCRIPTION("Maple bus driver for Dreamcast");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) MODULE_SUPPORTED_DEVICE("{{SEGA, Dreamcast/Maple}}");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static void maple_dma_handler(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static void maple_vblank_handler(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static DECLARE_WORK(maple_dma_process, maple_dma_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static DECLARE_WORK(maple_vblank_process, maple_vblank_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static LIST_HEAD(maple_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static LIST_HEAD(maple_sentq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* mutex to protect queue of waiting packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static DEFINE_MUTEX(maple_wlist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static struct maple_driver maple_unsupported_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static struct device maple_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int subdevice_map[MAPLE_PORTS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static unsigned long *maple_sendbuf, *maple_sendptr, *maple_lastptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static unsigned long maple_pnp_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static int started, scanning, fullscan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static struct kmem_cache *maple_queue_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct maple_device_specify {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) int unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static bool checked[MAPLE_PORTS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static bool empty[MAPLE_PORTS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static struct maple_device *baseunits[MAPLE_PORTS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * maple_driver_register - register a maple driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * @drv: maple driver to be registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * Registers the passed in @drv, while updating the bus type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * Devices with matching function IDs will be automatically probed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int maple_driver_register(struct maple_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (!drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) drv->drv.bus = &maple_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return driver_register(&drv->drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) EXPORT_SYMBOL_GPL(maple_driver_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * maple_driver_unregister - unregister a maple driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * @drv: maple driver to unregister.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * Cleans up after maple_driver_register(). To be invoked in the exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * path of any module drivers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) void maple_driver_unregister(struct maple_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) driver_unregister(&drv->drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) EXPORT_SYMBOL_GPL(maple_driver_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* set hardware registers to enable next round of dma */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static void maple_dma_reset(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) __raw_writel(MAPLE_MAGIC, MAPLE_RESET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /* set trig type to 0 for software trigger, 1 for hardware (VBLANK) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) __raw_writel(1, MAPLE_TRIGTYPE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * Maple system register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * bits 31 - 16 timeout in units of 20nsec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * bit 12 hard trigger - set 0 to keep responding to VBLANK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * bits 9 - 8 set 00 for 2 Mbps, 01 for 1 Mbps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * bits 3 - 0 delay (in 1.3ms) between VBLANK and start of DMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * max delay is 11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) __raw_writel(MAPLE_2MBPS | MAPLE_TIMEOUT(0xFFFF), MAPLE_SPEED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) __raw_writel(virt_to_phys(maple_sendbuf), MAPLE_DMAADDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) __raw_writel(1, MAPLE_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * maple_getcond_callback - setup handling MAPLE_COMMAND_GETCOND
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * @dev: device responding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * @callback: handler callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * @interval: interval in jiffies between callbacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * @function: the function code for the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) void maple_getcond_callback(struct maple_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) void (*callback) (struct mapleq *mq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) unsigned long interval, unsigned long function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) dev->callback = callback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) dev->interval = interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) dev->function = cpu_to_be32(function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) dev->when = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) EXPORT_SYMBOL_GPL(maple_getcond_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static int maple_dma_done(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return (__raw_readl(MAPLE_STATE) & 1) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static void maple_release_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct maple_device *mdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct mapleq *mq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) mdev = to_maple_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) mq = mdev->mq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) kmem_cache_free(maple_queue_cache, mq->recvbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) kfree(mq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) kfree(mdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * maple_add_packet - add a single instruction to the maple bus queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * @mdev: maple device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * @function: function on device being queried
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * @command: maple command to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * @length: length of command string (in 32 bit words)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * @data: remainder of command string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) int maple_add_packet(struct maple_device *mdev, u32 function, u32 command,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) size_t length, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) void *sendbuf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) sendbuf = kcalloc(length, 4, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (!sendbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ((__be32 *)sendbuf)[0] = cpu_to_be32(function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) mdev->mq->command = command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) mdev->mq->length = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (length > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) memcpy(sendbuf + 4, data, (length - 1) * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) mdev->mq->sendbuf = sendbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) mutex_lock(&maple_wlist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) list_add_tail(&mdev->mq->list, &maple_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) mutex_unlock(&maple_wlist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) EXPORT_SYMBOL_GPL(maple_add_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static struct mapleq *maple_allocq(struct maple_device *mdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct mapleq *mq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) mq = kzalloc(sizeof(*mq), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (!mq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) goto failed_nomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) INIT_LIST_HEAD(&mq->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) mq->dev = mdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) mq->recvbuf = kmem_cache_zalloc(maple_queue_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (!mq->recvbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) goto failed_p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) mq->recvbuf->buf = &((mq->recvbuf->bufx)[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return mq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) failed_p2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) kfree(mq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) failed_nomem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) dev_err(&mdev->dev, "could not allocate memory for device (%d, %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) mdev->port, mdev->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static struct maple_device *maple_alloc_dev(int port, int unit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct maple_device *mdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /* zero this out to avoid kobj subsystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * thinking it has already been registered */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (!mdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) mdev->port = port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) mdev->unit = unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) mdev->mq = maple_allocq(mdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (!mdev->mq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) kfree(mdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) mdev->dev.bus = &maple_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) mdev->dev.parent = &maple_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) init_waitqueue_head(&mdev->maple_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return mdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static void maple_free_dev(struct maple_device *mdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) kmem_cache_free(maple_queue_cache, mdev->mq->recvbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) kfree(mdev->mq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) kfree(mdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /* process the command queue into a maple command block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * terminating command has bit 32 of first long set to 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static void maple_build_block(struct mapleq *mq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) int port, unit, from, to, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) unsigned long *lsendbuf = mq->sendbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) port = mq->dev->port & 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) unit = mq->dev->unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) len = mq->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) from = port << 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) to = (port << 6) | (unit > 0 ? (1 << (unit - 1)) & 0x1f : 0x20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) *maple_lastptr &= 0x7fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) maple_lastptr = maple_sendptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) *maple_sendptr++ = (port << 16) | len | 0x80000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) *maple_sendptr++ = virt_to_phys(mq->recvbuf->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) *maple_sendptr++ =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) mq->command | (to << 8) | (from << 16) | (len << 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) while (len-- > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) *maple_sendptr++ = *lsendbuf++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /* build up command queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static void maple_send(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) int i, maple_packets = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct mapleq *mq, *nmq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (!maple_dma_done())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) /* disable DMA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) __raw_writel(0, MAPLE_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (!list_empty(&maple_sentq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) goto finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) mutex_lock(&maple_wlist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (list_empty(&maple_waitq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) mutex_unlock(&maple_wlist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) goto finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) maple_lastptr = maple_sendbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) maple_sendptr = maple_sendbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) list_for_each_entry_safe(mq, nmq, &maple_waitq, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) maple_build_block(mq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) list_del_init(&mq->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) list_add_tail(&mq->list, &maple_sentq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (maple_packets++ > MAPLE_MAXPACKETS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) mutex_unlock(&maple_wlist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (maple_packets > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) for (i = 0; i < (1 << MAPLE_DMA_PAGES); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) __flush_purge_region(maple_sendbuf + i * PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) finish:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) maple_dma_reset();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) /* check if there is a driver registered likely to match this device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static int maple_check_matching_driver(struct device_driver *driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) void *devptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct maple_driver *maple_drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) struct maple_device *mdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) mdev = devptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) maple_drv = to_maple_driver(driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (mdev->devinfo.function & cpu_to_be32(maple_drv->function))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static void maple_detach_driver(struct maple_device *mdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) device_unregister(&mdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) /* process initial MAPLE_COMMAND_DEVINFO for each device or port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) static void maple_attach_driver(struct maple_device *mdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) char *p, *recvbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) unsigned long function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) int matched, error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) recvbuf = mdev->mq->recvbuf->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) /* copy the data as individual elements in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * case of memory optimisation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) memcpy(&mdev->devinfo.function, recvbuf + 4, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) memcpy(&mdev->devinfo.function_data[0], recvbuf + 8, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) memcpy(&mdev->devinfo.area_code, recvbuf + 20, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) memcpy(&mdev->devinfo.connector_direction, recvbuf + 21, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) memcpy(&mdev->devinfo.product_name[0], recvbuf + 22, 30);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) memcpy(&mdev->devinfo.standby_power, recvbuf + 112, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) memcpy(&mdev->devinfo.max_power, recvbuf + 114, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) memcpy(mdev->product_name, mdev->devinfo.product_name, 30);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) mdev->product_name[30] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) memcpy(mdev->product_licence, mdev->devinfo.product_licence, 60);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) mdev->product_licence[60] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) for (p = mdev->product_name + 29; mdev->product_name <= p; p--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (*p == ' ')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) for (p = mdev->product_licence + 59; mdev->product_licence <= p; p--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (*p == ' ')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) function = be32_to_cpu(mdev->devinfo.function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) dev_info(&mdev->dev, "detected %s: function 0x%lX: at (%d, %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) mdev->product_name, function, mdev->port, mdev->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (function > 0x200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) /* Do this silently - as not a real device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) function = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) mdev->driver = &maple_unsupported_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) dev_set_name(&mdev->dev, "%d:0.port", mdev->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) matched =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) bus_for_each_drv(&maple_bus_type, NULL, mdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) maple_check_matching_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (matched == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) /* Driver does not exist yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) dev_info(&mdev->dev, "no driver found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) mdev->driver = &maple_unsupported_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) dev_set_name(&mdev->dev, "%d:0%d.%lX", mdev->port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) mdev->unit, function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) mdev->function = function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) mdev->dev.release = &maple_release_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) atomic_set(&mdev->busy, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) error = device_register(&mdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) dev_warn(&mdev->dev, "could not register device at"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) " (%d, %d), with error 0x%X\n", mdev->unit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) mdev->port, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) maple_free_dev(mdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) mdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * if device has been registered for the given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * port and unit then return 1 - allows identification
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * of which devices need to be attached or detached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static int check_maple_device(struct device *device, void *portptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) struct maple_device_specify *ds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct maple_device *mdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) ds = portptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) mdev = to_maple_dev(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (mdev->port == ds->port && mdev->unit == ds->unit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static int setup_maple_commands(struct device *device, void *ignored)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) int add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) struct maple_device *mdev = to_maple_dev(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if (mdev->interval > 0 && atomic_read(&mdev->busy) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) time_after(jiffies, mdev->when)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /* bounce if we cannot add */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) add = maple_add_packet(mdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) be32_to_cpu(mdev->devinfo.function),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) MAPLE_COMMAND_GETCOND, 1, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (!add)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) mdev->when = jiffies + mdev->interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (time_after(jiffies, maple_pnp_time))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) /* Ensure we don't have block reads and devinfo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * calls interfering with one another - so flag the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * device as busy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (atomic_read(&mdev->busy) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) atomic_set(&mdev->busy, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) maple_add_packet(mdev, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) MAPLE_COMMAND_DEVINFO, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) /* VBLANK bottom half - implemented via workqueue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static void maple_vblank_handler(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) int x, locking;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct maple_device *mdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) if (!maple_dma_done())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) __raw_writel(0, MAPLE_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) if (!list_empty(&maple_sentq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) goto finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * Set up essential commands - to fetch data and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * check devices are still present
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) bus_for_each_dev(&maple_bus_type, NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) setup_maple_commands);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (time_after(jiffies, maple_pnp_time)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * Scan the empty ports - bus is flakey and may have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * mis-reported emptyness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) for (x = 0; x < MAPLE_PORTS; x++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (checked[x] && empty[x]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) mdev = baseunits[x];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (!mdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) atomic_set(&mdev->busy, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) locking = maple_add_packet(mdev, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) MAPLE_COMMAND_DEVINFO, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (!locking)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) maple_pnp_time = jiffies + MAPLE_PNP_INTERVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) finish:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) maple_send();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) /* handle devices added via hotplugs - placing them on queue for DEVINFO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) static void maple_map_subunits(struct maple_device *mdev, int submask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) int retval, k, devcheck;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) struct maple_device *mdev_add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) struct maple_device_specify ds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) ds.port = mdev->port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) for (k = 0; k < 5; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) ds.unit = k + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) retval =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) bus_for_each_dev(&maple_bus_type, NULL, &ds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) check_maple_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) submask = submask >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) devcheck = submask & 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) if (devcheck) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) mdev_add = maple_alloc_dev(mdev->port, k + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) if (!mdev_add)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) atomic_set(&mdev_add->busy, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) maple_add_packet(mdev_add, 0, MAPLE_COMMAND_DEVINFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) /* mark that we are checking sub devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) scanning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) submask = submask >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) /* mark a device as removed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) static void maple_clean_submap(struct maple_device *mdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) int killbit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) killbit = (mdev->unit > 0 ? (1 << (mdev->unit - 1)) & 0x1f : 0x20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) killbit = ~killbit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) killbit &= 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) subdevice_map[mdev->port] = subdevice_map[mdev->port] & killbit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) /* handle empty port or hotplug removal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) static void maple_response_none(struct maple_device *mdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) maple_clean_submap(mdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (likely(mdev->unit != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) * Block devices play up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) * and give the impression they have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) * been removed even when still in place or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * trip the mtd layer when they have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * really gone - this code traps that eventuality
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) * and ensures we aren't overloaded with useless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) * error messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) if (mdev->can_unload) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (!mdev->can_unload(mdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) atomic_set(&mdev->busy, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) wake_up(&mdev->maple_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) dev_info(&mdev->dev, "detaching device at (%d, %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) mdev->port, mdev->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) maple_detach_driver(mdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) if (!started || !fullscan) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (checked[mdev->port] == false) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) checked[mdev->port] = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) empty[mdev->port] = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) dev_info(&mdev->dev, "no devices"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) " to port %d\n", mdev->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) /* Some hardware devices generate false detach messages on unit 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) atomic_set(&mdev->busy, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) /* preprocess hotplugs or scans */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) static void maple_response_devinfo(struct maple_device *mdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) char *recvbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) char submask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) if (!started || (scanning == 2) || !fullscan) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) if ((mdev->unit == 0) && (checked[mdev->port] == false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) checked[mdev->port] = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) maple_attach_driver(mdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (mdev->unit != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) maple_attach_driver(mdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) if (mdev->unit == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) empty[mdev->port] = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) maple_attach_driver(mdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) if (mdev->unit == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) submask = recvbuf[2] & 0x1F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (submask ^ subdevice_map[mdev->port]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) maple_map_subunits(mdev, submask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) subdevice_map[mdev->port] = submask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) static void maple_response_fileerr(struct maple_device *mdev, void *recvbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) if (mdev->fileerr_handler) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) mdev->fileerr_handler(mdev, recvbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) dev_warn(&mdev->dev, "device at (%d, %d) reports"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) "file error 0x%X\n", mdev->port, mdev->unit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) ((int *)recvbuf)[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) static void maple_port_rescan(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) struct maple_device *mdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) fullscan = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) for (i = 0; i < MAPLE_PORTS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (checked[i] == false) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) fullscan = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) mdev = baseunits[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) maple_add_packet(mdev, 0, MAPLE_COMMAND_DEVINFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) /* maple dma end bottom half - implemented via workqueue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) static void maple_dma_handler(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) struct mapleq *mq, *nmq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) struct maple_device *mdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) char *recvbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) enum maple_code code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) if (!maple_dma_done())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) __raw_writel(0, MAPLE_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if (!list_empty(&maple_sentq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) list_for_each_entry_safe(mq, nmq, &maple_sentq, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) mdev = mq->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) recvbuf = mq->recvbuf->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) __flush_invalidate_region(sh_cacheop_vaddr(recvbuf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 0x400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) code = recvbuf[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) kfree(mq->sendbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) list_del_init(&mq->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) switch (code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) case MAPLE_RESPONSE_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) maple_response_none(mdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) case MAPLE_RESPONSE_DEVINFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) maple_response_devinfo(mdev, recvbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) atomic_set(&mdev->busy, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) case MAPLE_RESPONSE_DATATRF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) if (mdev->callback)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) mdev->callback(mq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) atomic_set(&mdev->busy, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) wake_up(&mdev->maple_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) case MAPLE_RESPONSE_FILEERR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) maple_response_fileerr(mdev, recvbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) atomic_set(&mdev->busy, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) wake_up(&mdev->maple_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) case MAPLE_RESPONSE_AGAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) case MAPLE_RESPONSE_BADCMD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) case MAPLE_RESPONSE_BADFUNC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) dev_warn(&mdev->dev, "non-fatal error"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) " 0x%X at (%d, %d)\n", code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) mdev->port, mdev->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) atomic_set(&mdev->busy, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) case MAPLE_RESPONSE_ALLINFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) dev_notice(&mdev->dev, "extended"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) " device information request for (%d, %d)"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) " but call is not supported\n", mdev->port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) mdev->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) atomic_set(&mdev->busy, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) case MAPLE_RESPONSE_OK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) atomic_set(&mdev->busy, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) wake_up(&mdev->maple_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) /* if scanning is 1 then we have subdevices to check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) if (scanning == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) maple_send();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) scanning = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) scanning = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) /*check if we have actually tested all ports yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) if (!fullscan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) maple_port_rescan();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) /* mark that we have been through the first scan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) started = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) maple_send();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) static irqreturn_t maple_dma_interrupt(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) /* Load everything into the bottom half */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) schedule_work(&maple_dma_process);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) static irqreturn_t maple_vblank_interrupt(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) schedule_work(&maple_vblank_process);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) static int maple_set_dma_interrupt_handler(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) return request_irq(HW_EVENT_MAPLE_DMA, maple_dma_interrupt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) IRQF_SHARED, "maple bus DMA", &maple_unsupported_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) static int maple_set_vblank_interrupt_handler(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) return request_irq(HW_EVENT_VSYNC, maple_vblank_interrupt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) IRQF_SHARED, "maple bus VBLANK", &maple_unsupported_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) static int maple_get_dma_buffer(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) maple_sendbuf =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) MAPLE_DMA_PAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) if (!maple_sendbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) static int maple_match_bus_driver(struct device *devptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) struct device_driver *drvptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) struct maple_driver *maple_drv = to_maple_driver(drvptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) struct maple_device *maple_dev = to_maple_dev(devptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) /* Trap empty port case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) if (maple_dev->devinfo.function == 0xFFFFFFFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) else if (maple_dev->devinfo.function &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) cpu_to_be32(maple_drv->function))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) static int maple_bus_uevent(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) struct kobj_uevent_env *env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) static void maple_bus_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) static struct maple_driver maple_unsupported_device = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) .drv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) .name = "maple_unsupported_device",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) .bus = &maple_bus_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) * maple_bus_type - core maple bus structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) struct bus_type maple_bus_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) .name = "maple",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) .match = maple_match_bus_driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) .uevent = maple_bus_uevent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) EXPORT_SYMBOL_GPL(maple_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) static struct device maple_bus = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) .init_name = "maple",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) .release = maple_bus_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) static int __init maple_bus_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) int retval, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) struct maple_device *mdev[MAPLE_PORTS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) __raw_writel(0, MAPLE_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) retval = device_register(&maple_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) retval = bus_register(&maple_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) goto cleanup_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) retval = driver_register(&maple_unsupported_device.drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) goto cleanup_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) /* allocate memory for maple bus dma */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) retval = maple_get_dma_buffer();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) dev_err(&maple_bus, "failed to allocate DMA buffers\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) goto cleanup_basic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) /* set up DMA interrupt handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) retval = maple_set_dma_interrupt_handler();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) dev_err(&maple_bus, "bus failed to grab maple "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) "DMA IRQ\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) goto cleanup_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) /* set up VBLANK interrupt handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) retval = maple_set_vblank_interrupt_handler();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) dev_err(&maple_bus, "bus failed to grab VBLANK IRQ\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) goto cleanup_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) maple_queue_cache = KMEM_CACHE(maple_buffer, SLAB_HWCACHE_ALIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) if (!maple_queue_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) goto cleanup_bothirqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) INIT_LIST_HEAD(&maple_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) INIT_LIST_HEAD(&maple_sentq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) /* setup maple ports */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) for (i = 0; i < MAPLE_PORTS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) checked[i] = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) empty[i] = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) mdev[i] = maple_alloc_dev(i, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) if (!mdev[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) while (i-- > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) maple_free_dev(mdev[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) goto cleanup_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) baseunits[i] = mdev[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) atomic_set(&mdev[i]->busy, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) maple_add_packet(mdev[i], 0, MAPLE_COMMAND_DEVINFO, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) subdevice_map[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) maple_pnp_time = jiffies + HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) /* prepare initial queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) maple_send();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) dev_info(&maple_bus, "bus core now registered\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) cleanup_cache:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) kmem_cache_destroy(maple_queue_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) cleanup_bothirqs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) free_irq(HW_EVENT_VSYNC, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) cleanup_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) free_irq(HW_EVENT_MAPLE_DMA, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) cleanup_dma:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) free_pages((unsigned long) maple_sendbuf, MAPLE_DMA_PAGES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) cleanup_basic:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) driver_unregister(&maple_unsupported_device.drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) cleanup_bus:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) bus_unregister(&maple_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) cleanup_device:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) device_unregister(&maple_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) printk(KERN_ERR "Maple bus registration failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) /* Push init to later to ensure hardware gets detected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) fs_initcall(maple_bus_init);