maple.c 21.4 KB
Newer Older
1 2 3
/*
 * Core maple bus functionality
 *
4
 *  Copyright (C) 2007 - 2009 Adrian McMenamin
5
 *  Copyright (C) 2001 - 2008 Paul Mundt
6
 *  Copyright (C) 2000 - 2001 YAEGASHI Takeshi
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *  Copyright (C) 2001 M. R. Brown
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/maple.h>
#include <linux/dma-mapping.h>
22
#include <linux/delay.h>
23
#include <linux/module.h>
24 25 26
#include <asm/cacheflush.h>
#include <asm/dma.h>
#include <asm/io.h>
27 28
#include <mach/dma.h>
#include <mach/sysasic.h>
29

30
MODULE_AUTHOR("Adrian McMenamin <adrian@mcmen.demon.co.uk>");
31 32 33 34 35 36 37 38 39 40 41 42 43
MODULE_DESCRIPTION("Maple bus driver for Dreamcast");
MODULE_LICENSE("GPL v2");
MODULE_SUPPORTED_DEVICE("{{SEGA, Dreamcast/Maple}}");

static void maple_dma_handler(struct work_struct *work);
static void maple_vblank_handler(struct work_struct *work);

static DECLARE_WORK(maple_dma_process, maple_dma_handler);
static DECLARE_WORK(maple_vblank_process, maple_vblank_handler);

static LIST_HEAD(maple_waitq);
static LIST_HEAD(maple_sentq);

44 45
/* mutex to protect queue of waiting packets */
static DEFINE_MUTEX(maple_wlist_lock);
46

47
static struct maple_driver maple_unsupported_device;
48 49 50 51
static struct device maple_bus;
static int subdevice_map[MAPLE_PORTS];
static unsigned long *maple_sendbuf, *maple_sendptr, *maple_lastptr;
static unsigned long maple_pnp_time;
52
static int started, scanning, fullscan;
53 54 55
static struct kmem_cache *maple_queue_cache;

struct maple_device_specify {
56 57
	int port;
	int unit;
58 59
};

60 61 62
static bool checked[MAPLE_PORTS];
static bool empty[MAPLE_PORTS];
static struct maple_device *baseunits[MAPLE_PORTS];
A
Adrian McMenamin 已提交
63

64
/**
65 66 67 68 69
 * maple_driver_register - register a maple driver
 * @drv: maple driver to be registered.
 *
 * Registers the passed in @drv, while updating the bus type.
 * Devices with matching function IDs will be automatically probed.
70
 */
71
int maple_driver_register(struct maple_driver *drv)
72
{
73 74
	if (!drv)
		return -EINVAL;
75 76 77 78

	drv->drv.bus = &maple_bus_type;

	return driver_register(&drv->drv);
79 80 81
}
EXPORT_SYMBOL_GPL(maple_driver_register);

82 83 84 85 86 87 88 89 90 91 92
/**
 * maple_driver_unregister - unregister a maple driver.
 * @drv: maple driver to unregister.
 *
 * Cleans up after maple_driver_register(). To be invoked in the exit
 * path of any module drivers.
 */
void maple_driver_unregister(struct maple_driver *drv)
{
	driver_unregister(&drv->drv);
}
93
EXPORT_SYMBOL_GPL(maple_driver_unregister);
94

95
/* set hardware registers to enable next round of dma */
96
static void maple_dma_reset(void)
97
{
98
	__raw_writel(MAPLE_MAGIC, MAPLE_RESET);
99
	/* set trig type to 0 for software trigger, 1 for hardware (VBLANK) */
100
	__raw_writel(1, MAPLE_TRIGTYPE);
101 102 103 104 105 106 107 108
	/*
	* Maple system register
	* bits 31 - 16	timeout in units of 20nsec
	* bit 12	hard trigger - set 0 to keep responding to VBLANK
	* bits 9 - 8	set 00 for 2 Mbps, 01 for 1 Mbps
	* bits 3 - 0	delay (in 1.3ms) between VBLANK and start of DMA
	* max delay is 11
	*/
109 110 111
	__raw_writel(MAPLE_2MBPS | MAPLE_TIMEOUT(0xFFFF), MAPLE_SPEED);
	__raw_writel(virt_to_phys(maple_sendbuf), MAPLE_DMAADDR);
	__raw_writel(1, MAPLE_ENABLE);
112 113 114 115 116 117 118 119 120 121
}

/**
 * maple_getcond_callback - setup handling MAPLE_COMMAND_GETCOND
 * @dev: device responding
 * @callback: handler callback
 * @interval: interval in jiffies between callbacks
 * @function: the function code for the device
 */
void maple_getcond_callback(struct maple_device *dev,
122 123
			void (*callback) (struct mapleq *mq),
			unsigned long interval, unsigned long function)
124
{
125 126 127 128
	dev->callback = callback;
	dev->interval = interval;
	dev->function = cpu_to_be32(function);
	dev->when = jiffies;
129 130 131 132 133
}
EXPORT_SYMBOL_GPL(maple_getcond_callback);

static int maple_dma_done(void)
{
134
	return (__raw_readl(MAPLE_STATE) & 1) == 0;
135 136 137 138
}

static void maple_release_device(struct device *dev)
{
139 140
	struct maple_device *mdev;
	struct mapleq *mq;
141

142 143
	mdev = to_maple_dev(dev);
	mq = mdev->mq;
144 145
	kmem_cache_free(maple_queue_cache, mq->recvbuf);
	kfree(mq);
146
	kfree(mdev);
147 148
}

149
/**
150
 * maple_add_packet - add a single instruction to the maple bus queue
151 152 153 154 155
 * @mdev: maple device
 * @function: function on device being queried
 * @command: maple command to add
 * @length: length of command string (in 32 bit words)
 * @data: remainder of command string
156
 */
157 158
int maple_add_packet(struct maple_device *mdev, u32 function, u32 command,
	size_t length, void *data)
159
{
160
	int ret = 0;
161 162 163
	void *sendbuf = NULL;

	if (length) {
164
		sendbuf = kzalloc(length * 4, GFP_KERNEL);
165 166 167 168 169 170 171 172 173 174 175 176 177 178
		if (!sendbuf) {
			ret = -ENOMEM;
			goto out;
		}
		((__be32 *)sendbuf)[0] = cpu_to_be32(function);
	}

	mdev->mq->command = command;
	mdev->mq->length = length;
	if (length > 1)
		memcpy(sendbuf + 4, data, (length - 1) * 4);
	mdev->mq->sendbuf = sendbuf;

	mutex_lock(&maple_wlist_lock);
179
	list_add_tail(&mdev->mq->list, &maple_waitq);
180 181 182 183
	mutex_unlock(&maple_wlist_lock);
out:
	return ret;
}
184
EXPORT_SYMBOL_GPL(maple_add_packet);
185

186
static struct mapleq *maple_allocq(struct maple_device *mdev)
187
{
188
	struct mapleq *mq;
189

190
	mq = kzalloc(sizeof(*mq), GFP_KERNEL);
191
	if (!mq)
192
		goto failed_nomem;
193

194
	INIT_LIST_HEAD(&mq->list);
195
	mq->dev = mdev;
196
	mq->recvbuf = kmem_cache_zalloc(maple_queue_cache, GFP_KERNEL);
197 198
	if (!mq->recvbuf)
		goto failed_p2;
199
	mq->recvbuf->buf = &((mq->recvbuf->bufx)[0]);
200

201
	return mq;
202 203 204 205

failed_p2:
	kfree(mq);
failed_nomem:
206 207
	dev_err(&mdev->dev, "could not allocate memory for device (%d, %d)\n",
		mdev->port, mdev->unit);
208
	return NULL;
209 210 211 212
}

static struct maple_device *maple_alloc_dev(int port, int unit)
{
213
	struct maple_device *mdev;
214

215 216 217
	/* zero this out to avoid kobj subsystem
	* thinking it has already been registered */

218 219
	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
	if (!mdev)
220
		return NULL;
221

222 223
	mdev->port = port;
	mdev->unit = unit;
224

225
	mdev->mq = maple_allocq(mdev);
226

227 228
	if (!mdev->mq) {
		kfree(mdev);
229 230
		return NULL;
	}
231 232
	mdev->dev.bus = &maple_bus_type;
	mdev->dev.parent = &maple_bus;
233
	init_waitqueue_head(&mdev->maple_wait);
234
	return mdev;
235 236 237 238
}

static void maple_free_dev(struct maple_device *mdev)
{
239 240
	kmem_cache_free(maple_queue_cache, mdev->mq->recvbuf);
	kfree(mdev->mq);
241
	kfree(mdev);
242 243 244 245 246 247 248
}

/* process the command queue into a maple command block
 * terminating command has bit 32 of first long set to 0
 */
static void maple_build_block(struct mapleq *mq)
{
249 250
	int port, unit, from, to, len;
	unsigned long *lsendbuf = mq->sendbuf;
251

252 253 254 255 256
	port = mq->dev->port & 3;
	unit = mq->dev->unit;
	len = mq->length;
	from = port << 6;
	to = (port << 6) | (unit > 0 ? (1 << (unit - 1)) & 0x1f : 0x20);
257

258 259
	*maple_lastptr &= 0x7fffffff;
	maple_lastptr = maple_sendptr;
260

261
	*maple_sendptr++ = (port << 16) | len | 0x80000000;
262
	*maple_sendptr++ = virt_to_phys(mq->recvbuf->buf);
263 264 265 266
	*maple_sendptr++ =
	    mq->command | (to << 8) | (from << 16) | (len << 24);
	while (len-- > 0)
		*maple_sendptr++ = *lsendbuf++;
267 268 269 270 271
}

/* build up command queue */
static void maple_send(void)
{
272
	int i, maple_packets = 0;
273 274
	struct mapleq *mq, *nmq;

275
	if (!maple_dma_done())
276
		return;
277 278

	/* disable DMA */
279
	__raw_writel(0, MAPLE_ENABLE);
280 281 282 283

	if (!list_empty(&maple_sentq))
		goto finish;

284
	mutex_lock(&maple_wlist_lock);
285
	if (list_empty(&maple_waitq)) {
286
		mutex_unlock(&maple_wlist_lock);
287
		goto finish;
288
	}
289

290 291
	maple_lastptr = maple_sendbuf;
	maple_sendptr = maple_sendbuf;
292

293 294
	list_for_each_entry_safe(mq, nmq, &maple_waitq, list) {
		maple_build_block(mq);
295 296
		list_del_init(&mq->list);
		list_add_tail(&mq->list, &maple_sentq);
297 298 299
		if (maple_packets++ > MAPLE_MAXPACKETS)
			break;
	}
300
	mutex_unlock(&maple_wlist_lock);
301 302
	if (maple_packets > 0) {
		for (i = 0; i < (1 << MAPLE_DMA_PAGES); i++)
303
			sh_sync_dma_for_device(maple_sendbuf + i * PAGE_SIZE,
304 305
				       PAGE_SIZE, DMA_BIDIRECTIONAL);
	}
306 307 308

finish:
	maple_dma_reset();
309 310
}

311
/* check if there is a driver registered likely to match this device */
312
static int maple_check_matching_driver(struct device_driver *driver,
313
					void *devptr)
314
{
315 316 317 318 319
	struct maple_driver *maple_drv;
	struct maple_device *mdev;

	mdev = devptr;
	maple_drv = to_maple_driver(driver);
320 321
	if (mdev->devinfo.function & cpu_to_be32(maple_drv->function))
		return 1;
322
	return 0;
323 324 325 326
}

static void maple_detach_driver(struct maple_device *mdev)
{
327
	device_unregister(&mdev->dev);
328 329 330
}

/* process initial MAPLE_COMMAND_DEVINFO for each device or port */
331
static void maple_attach_driver(struct maple_device *mdev)
332
{
333
	char *p, *recvbuf;
334
	unsigned long function;
335
	int matched, error;
336

337
	recvbuf = mdev->mq->recvbuf->buf;
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
	/* copy the data as individual elements in
	* case of memory optimisation */
	memcpy(&mdev->devinfo.function, recvbuf + 4, 4);
	memcpy(&mdev->devinfo.function_data[0], recvbuf + 8, 12);
	memcpy(&mdev->devinfo.area_code, recvbuf + 20, 1);
	memcpy(&mdev->devinfo.connector_direction, recvbuf + 21, 1);
	memcpy(&mdev->devinfo.product_name[0], recvbuf + 22, 30);
	memcpy(&mdev->devinfo.standby_power, recvbuf + 112, 2);
	memcpy(&mdev->devinfo.max_power, recvbuf + 114, 2);
	memcpy(mdev->product_name, mdev->devinfo.product_name, 30);
	mdev->product_name[30] = '\0';
	memcpy(mdev->product_licence, mdev->devinfo.product_licence, 60);
	mdev->product_licence[60] = '\0';

	for (p = mdev->product_name + 29; mdev->product_name <= p; p--)
353 354 355 356
		if (*p == ' ')
			*p = '\0';
		else
			break;
357
	for (p = mdev->product_licence + 59; mdev->product_licence <= p; p--)
358 359 360 361 362
		if (*p == ' ')
			*p = '\0';
		else
			break;

363
	function = be32_to_cpu(mdev->devinfo.function);
364

365 366 367
	dev_info(&mdev->dev, "detected %s: function 0x%lX: at (%d, %d)\n",
		mdev->product_name, function, mdev->port, mdev->unit);

368 369 370
	if (function > 0x200) {
		/* Do this silently - as not a real device */
		function = 0;
371
		mdev->driver = &maple_unsupported_device;
372
		dev_set_name(&mdev->dev, "%d:0.port", mdev->port);
373 374
	} else {
		matched =
375
			bus_for_each_drv(&maple_bus_type, NULL, mdev,
376
				maple_check_matching_driver);
377 378 379

		if (matched == 0) {
			/* Driver does not exist yet */
380 381
			dev_info(&mdev->dev, "no driver found\n");
			mdev->driver = &maple_unsupported_device;
382
		}
383 384
		dev_set_name(&mdev->dev, "%d:0%d.%lX", mdev->port,
			     mdev->unit, function);
385
	}
386

387 388
	mdev->function = function;
	mdev->dev.release = &maple_release_device;
389 390 391 392 393 394 395

	atomic_set(&mdev->busy, 0);
	error = device_register(&mdev->dev);
	if (error) {
		dev_warn(&mdev->dev, "could not register device at"
			" (%d, %d), with error 0x%X\n", mdev->unit,
			mdev->port, error);
396 397 398
		maple_free_dev(mdev);
		mdev = NULL;
		return;
399
	}
400 401 402 403 404 405 406
}

/*
 * if device has been registered for the given
 * port and unit then return 1 - allows identification
 * of which devices need to be attached or detached
 */
407
static int check_maple_device(struct device *device, void *portptr)
408
{
409 410 411 412 413 414 415 416
	struct maple_device_specify *ds;
	struct maple_device *mdev;

	ds = portptr;
	mdev = to_maple_dev(device);
	if (mdev->port == ds->port && mdev->unit == ds->unit)
		return 1;
	return 0;
417 418 419 420
}

static int setup_maple_commands(struct device *device, void *ignored)
{
421
	int add;
422 423 424 425 426 427
	struct maple_device *mdev = to_maple_dev(device);
	if (mdev->interval > 0 && atomic_read(&mdev->busy) == 0 &&
		time_after(jiffies, mdev->when)) {
		/* bounce if we cannot add */
		add = maple_add_packet(mdev,
			be32_to_cpu(mdev->devinfo.function),
428 429
			MAPLE_COMMAND_GETCOND, 1, NULL);
		if (!add)
430
			mdev->when = jiffies + mdev->interval;
431
	} else {
432
		if (time_after(jiffies, maple_pnp_time))
433 434 435 436 437 438 439 440
			/* Ensure we don't have block reads and devinfo
			* calls interfering with one another - so flag the
			* device as busy */
			if (atomic_read(&mdev->busy) == 0) {
				atomic_set(&mdev->busy, 1);
				maple_add_packet(mdev, 0,
					MAPLE_COMMAND_DEVINFO, 0, NULL);
			}
441 442
	}
	return 0;
443 444 445 446 447
}

/* VBLANK bottom half - implemented via workqueue */
static void maple_vblank_handler(struct work_struct *work)
{
448 449 450 451
	int x, locking;
	struct maple_device *mdev;

	if (!maple_dma_done())
452
		return;
453

454
	__raw_writel(0, MAPLE_ENABLE);
455

456 457 458 459 460 461 462
	if (!list_empty(&maple_sentq))
		goto finish;

	/*
	* Set up essential commands - to fetch data and
	* check devices are still present
	*/
463
	bus_for_each_dev(&maple_bus_type, NULL, NULL,
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
		setup_maple_commands);

	if (time_after(jiffies, maple_pnp_time)) {
		/*
		* Scan the empty ports - bus is flakey and may have
		* mis-reported emptyness
		*/
		for (x = 0; x < MAPLE_PORTS; x++) {
			if (checked[x] && empty[x]) {
				mdev = baseunits[x];
				if (!mdev)
					break;
				atomic_set(&mdev->busy, 1);
				locking = maple_add_packet(mdev, 0,
					MAPLE_COMMAND_DEVINFO, 0, NULL);
				if (!locking)
					break;
				}
			}
483

484 485
		maple_pnp_time = jiffies + MAPLE_PNP_INTERVAL;
	}
486

487 488
finish:
	maple_send();
489 490
}

491
/* handle devices added via hotplugs - placing them on queue for DEVINFO */
492 493
static void maple_map_subunits(struct maple_device *mdev, int submask)
{
494 495 496 497
	int retval, k, devcheck;
	struct maple_device *mdev_add;
	struct maple_device_specify ds;

498
	ds.port = mdev->port;
499 500 501 502
	for (k = 0; k < 5; k++) {
		ds.unit = k + 1;
		retval =
		    bus_for_each_dev(&maple_bus_type, NULL, &ds,
503
				     check_maple_device);
504 505 506 507 508 509 510 511 512
		if (retval) {
			submask = submask >> 1;
			continue;
		}
		devcheck = submask & 0x01;
		if (devcheck) {
			mdev_add = maple_alloc_dev(mdev->port, k + 1);
			if (!mdev_add)
				return;
513
			atomic_set(&mdev_add->busy, 1);
514 515 516
			maple_add_packet(mdev_add, 0, MAPLE_COMMAND_DEVINFO,
				0, NULL);
			/* mark that we are checking sub devices */
517 518 519 520
			scanning = 1;
		}
		submask = submask >> 1;
	}
521 522 523 524 525
}

/* mark a device as removed */
static void maple_clean_submap(struct maple_device *mdev)
{
526
	int killbit;
527

528 529 530 531
	killbit = (mdev->unit > 0 ? (1 << (mdev->unit - 1)) & 0x1f : 0x20);
	killbit = ~killbit;
	killbit &= 0xFF;
	subdevice_map[mdev->port] = subdevice_map[mdev->port] & killbit;
532 533 534
}

/* handle empty port or hotplug removal */
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
static void maple_response_none(struct maple_device *mdev)
{
	maple_clean_submap(mdev);

	if (likely(mdev->unit != 0)) {
		/*
		* Block devices play up
		* and give the impression they have
		* been removed even when still in place or
		* trip the mtd layer when they have
		* really gone - this code traps that eventuality
		* and ensures we aren't overloaded with useless
		* error messages
		*/
		if (mdev->can_unload) {
			if (!mdev->can_unload(mdev)) {
				atomic_set(&mdev->busy, 2);
				wake_up(&mdev->maple_wait);
				return;
			}
		}

		dev_info(&mdev->dev, "detaching device at (%d, %d)\n",
			mdev->port, mdev->unit);
559 560
		maple_detach_driver(mdev);
		return;
561 562 563 564 565 566 567 568 569
	} else {
		if (!started || !fullscan) {
			if (checked[mdev->port] == false) {
				checked[mdev->port] = true;
				empty[mdev->port] = true;
				dev_info(&mdev->dev, "no devices"
					" to port %d\n", mdev->port);
			}
			return;
A
Adrian McMenamin 已提交
570
		}
571
	}
572 573
	/* Some hardware devices generate false detach messages on unit 0 */
	atomic_set(&mdev->busy, 0);
574 575 576 577
}

/* preprocess hotplugs or scans */
static void maple_response_devinfo(struct maple_device *mdev,
578
				   char *recvbuf)
579
{
580
	char submask;
A
Adrian McMenamin 已提交
581 582 583 584 585 586 587
	if (!started || (scanning == 2) || !fullscan) {
		if ((mdev->unit == 0) && (checked[mdev->port] == false)) {
			checked[mdev->port] = true;
			maple_attach_driver(mdev);
		} else {
			if (mdev->unit != 0)
				maple_attach_driver(mdev);
588 589 590 591
			if (mdev->unit == 0) {
				empty[mdev->port] = false;
				maple_attach_driver(mdev);
			}
A
Adrian McMenamin 已提交
592
		}
593 594 595 596 597 598 599 600
	}
	if (mdev->unit == 0) {
		submask = recvbuf[2] & 0x1F;
		if (submask ^ subdevice_map[mdev->port]) {
			maple_map_subunits(mdev, submask);
			subdevice_map[mdev->port] = submask;
		}
	}
601 602
}

603 604 605 606 607 608 609 610 611 612 613
static void maple_response_fileerr(struct maple_device *mdev, void *recvbuf)
{
	if (mdev->fileerr_handler) {
		mdev->fileerr_handler(mdev, recvbuf);
		return;
	} else
		dev_warn(&mdev->dev, "device at (%d, %d) reports"
			"file error 0x%X\n", mdev->port, mdev->unit,
			((int *)recvbuf)[1]);
}

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
static void maple_port_rescan(void)
{
	int i;
	struct maple_device *mdev;

	fullscan = 1;
	for (i = 0; i < MAPLE_PORTS; i++) {
		if (checked[i] == false) {
			fullscan = 0;
			mdev = baseunits[i];
			maple_add_packet(mdev, 0, MAPLE_COMMAND_DEVINFO,
				0, NULL);
		}
	}
}

630 631 632
/* maple dma end bottom half - implemented via workqueue */
static void maple_dma_handler(struct work_struct *work)
{
633
	struct mapleq *mq, *nmq;
634
	struct maple_device *mdev;
635 636 637 638 639
	char *recvbuf;
	enum maple_code code;

	if (!maple_dma_done())
		return;
640
	__raw_writel(0, MAPLE_ENABLE);
641 642
	if (!list_empty(&maple_sentq)) {
		list_for_each_entry_safe(mq, nmq, &maple_sentq, list) {
643 644
			mdev = mq->dev;
			recvbuf = mq->recvbuf->buf;
645
			sh_sync_dma_for_device(recvbuf, 0x400, DMA_FROM_DEVICE);
646
			code = recvbuf[0];
647 648
			kfree(mq->sendbuf);
			list_del_init(&mq->list);
649 650
			switch (code) {
			case MAPLE_RESPONSE_NONE:
651
				maple_response_none(mdev);
652 653 654
				break;

			case MAPLE_RESPONSE_DEVINFO:
655 656
				maple_response_devinfo(mdev, recvbuf);
				atomic_set(&mdev->busy, 0);
657 658 659
				break;

			case MAPLE_RESPONSE_DATATRF:
660 661 662 663
				if (mdev->callback)
					mdev->callback(mq);
				atomic_set(&mdev->busy, 0);
				wake_up(&mdev->maple_wait);
664 665 666
				break;

			case MAPLE_RESPONSE_FILEERR:
667 668 669 670 671
				maple_response_fileerr(mdev, recvbuf);
				atomic_set(&mdev->busy, 0);
				wake_up(&mdev->maple_wait);
				break;

672 673 674
			case MAPLE_RESPONSE_AGAIN:
			case MAPLE_RESPONSE_BADCMD:
			case MAPLE_RESPONSE_BADFUNC:
675 676 677 678
				dev_warn(&mdev->dev, "non-fatal error"
					" 0x%X at (%d, %d)\n", code,
					mdev->port, mdev->unit);
				atomic_set(&mdev->busy, 0);
679 680 681
				break;

			case MAPLE_RESPONSE_ALLINFO:
682 683 684 685 686
				dev_notice(&mdev->dev, "extended"
				" device information request for (%d, %d)"
				" but call is not supported\n", mdev->port,
				mdev->unit);
				atomic_set(&mdev->busy, 0);
687 688 689
				break;

			case MAPLE_RESPONSE_OK:
690 691
				atomic_set(&mdev->busy, 0);
				wake_up(&mdev->maple_wait);
692 693 694 695 696 697
				break;

			default:
				break;
			}
		}
698
		/* if scanning is 1 then we have subdevices to check */
699 700 701 702 703
		if (scanning == 1) {
			maple_send();
			scanning = 2;
		} else
			scanning = 0;
704 705 706 707
		/*check if we have actually tested all ports yet */
		if (!fullscan)
			maple_port_rescan();
		/* mark that we have been through the first scan */
708
		started = 1;
709
	}
710
	maple_send();
711 712
}

713
static irqreturn_t maple_dma_interrupt(int irq, void *dev_id)
714
{
715 716 717
	/* Load everything into the bottom half */
	schedule_work(&maple_dma_process);
	return IRQ_HANDLED;
718 719
}

720
static irqreturn_t maple_vblank_interrupt(int irq, void *dev_id)
721
{
722 723
	schedule_work(&maple_vblank_process);
	return IRQ_HANDLED;
724 725 726 727
}

static int maple_set_dma_interrupt_handler(void)
{
728 729
	return request_irq(HW_EVENT_MAPLE_DMA, maple_dma_interrupt,
		IRQF_SHARED, "maple bus DMA", &maple_unsupported_device);
730 731 732 733
}

static int maple_set_vblank_interrupt_handler(void)
{
734 735
	return request_irq(HW_EVENT_VSYNC, maple_vblank_interrupt,
		IRQF_SHARED, "maple bus VBLANK", &maple_unsupported_device);
736 737 738 739
}

static int maple_get_dma_buffer(void)
{
740 741 742 743 744 745
	maple_sendbuf =
	    (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
				      MAPLE_DMA_PAGES);
	if (!maple_sendbuf)
		return -ENOMEM;
	return 0;
746 747
}

748
static int maple_match_bus_driver(struct device *devptr,
749
				  struct device_driver *drvptr)
750
{
751 752
	struct maple_driver *maple_drv = to_maple_driver(drvptr);
	struct maple_device *maple_dev = to_maple_dev(devptr);
753 754 755 756 757

	/* Trap empty port case */
	if (maple_dev->devinfo.function == 0xFFFFFFFF)
		return 0;
	else if (maple_dev->devinfo.function &
758
		 cpu_to_be32(maple_drv->function))
759 760
		return 1;
	return 0;
761 762
}

763 764
static int maple_bus_uevent(struct device *dev,
			    struct kobj_uevent_env *env)
765
{
766
	return 0;
767 768 769 770 771 772
}

static void maple_bus_release(struct device *dev)
{
}

773
static struct maple_driver maple_unsupported_device = {
774
	.drv = {
775
		.name = "maple_unsupported_device",
776
		.bus = &maple_bus_type,
777
	},
778
};
779
/*
780 781
 * maple_bus_type - core maple bus structure
 */
782
struct bus_type maple_bus_type = {
783
	.name = "maple",
784
	.match = maple_match_bus_driver,
785
	.uevent = maple_bus_uevent,
786 787 788 789
};
EXPORT_SYMBOL_GPL(maple_bus_type);

static struct device maple_bus = {
790
	.init_name = "maple",
791
	.release = maple_bus_release,
792 793 794 795
};

static int __init maple_bus_init(void)
{
796 797
	int retval, i;
	struct maple_device *mdev[MAPLE_PORTS];
798

799
	__raw_writel(0, MAPLE_ENABLE);
800 801 802 803 804 805 806 807 808

	retval = device_register(&maple_bus);
	if (retval)
		goto cleanup;

	retval = bus_register(&maple_bus_type);
	if (retval)
		goto cleanup_device;

809
	retval = driver_register(&maple_unsupported_device.drv);
810 811 812 813 814 815
	if (retval)
		goto cleanup_bus;

	/* allocate memory for maple bus dma */
	retval = maple_get_dma_buffer();
	if (retval) {
816
		dev_err(&maple_bus, "failed to allocate DMA buffers\n");
817 818 819 820 821 822
		goto cleanup_basic;
	}

	/* set up DMA interrupt handler */
	retval = maple_set_dma_interrupt_handler();
	if (retval) {
823 824
		dev_err(&maple_bus, "bus failed to grab maple "
			"DMA IRQ\n");
825 826 827 828 829 830
		goto cleanup_dma;
	}

	/* set up VBLANK interrupt handler */
	retval = maple_set_vblank_interrupt_handler();
	if (retval) {
831
		dev_err(&maple_bus, "bus failed to grab VBLANK IRQ\n");
832 833 834
		goto cleanup_irq;
	}

835
	maple_queue_cache = KMEM_CACHE(maple_buffer, SLAB_HWCACHE_ALIGN);
836 837 838 839

	if (!maple_queue_cache)
		goto cleanup_bothirqs;

840 841 842
	INIT_LIST_HEAD(&maple_waitq);
	INIT_LIST_HEAD(&maple_sentq);

843 844
	/* setup maple ports */
	for (i = 0; i < MAPLE_PORTS; i++) {
A
Adrian McMenamin 已提交
845
		checked[i] = false;
846
		empty[i] = false;
847 848 849 850 851 852
		mdev[i] = maple_alloc_dev(i, 0);
		if (!mdev[i]) {
			while (i-- > 0)
				maple_free_dev(mdev[i]);
			goto cleanup_cache;
		}
853 854
		baseunits[i] = mdev[i];
		atomic_set(&mdev[i]->busy, 1);
855
		maple_add_packet(mdev[i], 0, MAPLE_COMMAND_DEVINFO, 0, NULL);
856 857 858
		subdevice_map[i] = 0;
	}

859 860
	maple_pnp_time = jiffies + HZ;
	/* prepare initial queue */
861
	maple_send();
862
	dev_info(&maple_bus, "bus core now registered\n");
863 864 865

	return 0;

866
cleanup_cache:
867 868
	kmem_cache_destroy(maple_queue_cache);

869
cleanup_bothirqs:
870 871
	free_irq(HW_EVENT_VSYNC, 0);

872
cleanup_irq:
873 874
	free_irq(HW_EVENT_MAPLE_DMA, 0);

875
cleanup_dma:
876 877
	free_pages((unsigned long) maple_sendbuf, MAPLE_DMA_PAGES);

878
cleanup_basic:
879
	driver_unregister(&maple_unsupported_device.drv);
880

881
cleanup_bus:
882 883
	bus_unregister(&maple_bus_type);

884
cleanup_device:
885 886
	device_unregister(&maple_bus);

887
cleanup:
888
	printk(KERN_ERR "Maple bus registration failed\n");
889
	return retval;
890
}
891 892
/* Push init to later to ensure hardware gets detected */
fs_initcall(maple_bus_init);