tifm_7xx1.c 11.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 *  tifm_7xx1.c - TI FlashMedia driver
 *
 *  Copyright (C) 2006 Alex Dubov <oakad@yahoo.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/tifm.h>
13
#include <linux/dma-mapping.h>
14
#include <linux/module.h>
15 16

#define DRIVER_NAME "tifm_7xx1"
17 18 19 20 21 22 23
#define DRIVER_VERSION "0.8"

#define TIFM_IRQ_ENABLE           0x80000000
#define TIFM_IRQ_SOCKMASK(x)      (x)
#define TIFM_IRQ_CARDMASK(x)      ((x) << 8)
#define TIFM_IRQ_FIFOMASK(x)      ((x) << 16)
#define TIFM_IRQ_SETALL           0xffffffff
24

25 26 27 28 29
static void tifm_7xx1_dummy_eject(struct tifm_adapter *fm,
				  struct tifm_dev *sock)
{
}

30 31 32 33 34
static void tifm_7xx1_eject(struct tifm_adapter *fm, struct tifm_dev *sock)
{
	unsigned long flags;

	spin_lock_irqsave(&fm->lock, flags);
35
	fm->socket_change_set |= 1 << sock->socket_id;
36
	tifm_queue_work(&fm->media_switcher);
37 38 39
	spin_unlock_irqrestore(&fm->lock, flags);
}

40
static irqreturn_t tifm_7xx1_isr(int irq, void *dev_id)
41 42
{
	struct tifm_adapter *fm = dev_id;
43
	struct tifm_dev *sock;
44
	unsigned int irq_status, cnt;
45 46 47 48 49 50 51 52 53 54 55

	spin_lock(&fm->lock);
	irq_status = readl(fm->addr + FM_INTERRUPT_STATUS);
	if (irq_status == 0 || irq_status == (~0)) {
		spin_unlock(&fm->lock);
		return IRQ_NONE;
	}

	if (irq_status & TIFM_IRQ_ENABLE) {
		writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);

A
Alex Dubov 已提交
56
		for (cnt = 0; cnt < fm->num_sockets; cnt++) {
57
			sock = fm->sockets[cnt];
58 59 60 61 62 63
			if (sock) {
				if ((irq_status >> cnt) & TIFM_IRQ_FIFOMASK(1))
					sock->data_event(sock);
				if ((irq_status >> cnt) & TIFM_IRQ_CARDMASK(1))
					sock->card_event(sock);
			}
64
		}
65

A
Alex Dubov 已提交
66 67
		fm->socket_change_set |= irq_status
					 & ((1 << fm->num_sockets) - 1);
68 69 70
	}
	writel(irq_status, fm->addr + FM_INTERRUPT_STATUS);

71 72 73
	if (fm->finish_me)
		complete_all(fm->finish_me);
	else if (!fm->socket_change_set)
74 75
		writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
	else
76
		tifm_queue_work(&fm->media_switcher);
77 78 79 80 81

	spin_unlock(&fm->lock);
	return IRQ_HANDLED;
}

82
static unsigned char tifm_7xx1_toggle_sock_power(char __iomem *sock_addr)
83 84 85 86 87 88
{
	unsigned int s_state;
	int cnt;

	writel(0x0e00, sock_addr + SOCK_CONTROL);

89
	for (cnt = 16; cnt <= 256; cnt <<= 1) {
A
Alex Dubov 已提交
90 91
		if (!(TIFM_SOCK_STATE_POWERED
		      & readl(sock_addr + SOCK_PRESENT_STATE)))
92
			break;
93 94

		msleep(cnt);
95 96 97 98
	}

	s_state = readl(sock_addr + SOCK_PRESENT_STATE);
	if (!(TIFM_SOCK_STATE_OCCUPIED & s_state))
99
		return 0;
100

101 102
	writel(readl(sock_addr + SOCK_CONTROL) | TIFM_CTRL_LED,
	       sock_addr + SOCK_CONTROL);
103

104 105 106 107 108
	/* xd needs some extra time before power on */
	if (((readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7)
	    == TIFM_TYPE_XD)
		msleep(40);

109 110
	writel((s_state & TIFM_CTRL_POWER_MASK) | 0x0c00,
	       sock_addr + SOCK_CONTROL);
111 112 113
	/* wait for power to stabilize */
	msleep(20);
	for (cnt = 16; cnt <= 256; cnt <<= 1) {
A
Alex Dubov 已提交
114 115
		if ((TIFM_SOCK_STATE_POWERED
		     & readl(sock_addr + SOCK_PRESENT_STATE)))
116
			break;
117 118

		msleep(cnt);
119 120
	}

121 122
	writel(readl(sock_addr + SOCK_CONTROL) & (~TIFM_CTRL_LED),
	       sock_addr + SOCK_CONTROL);
123 124 125 126

	return (readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7;
}

127 128 129 130 131 132
inline static void tifm_7xx1_sock_power_off(char __iomem *sock_addr)
{
	writel((~TIFM_CTRL_POWER_MASK) & readl(sock_addr + SOCK_CONTROL),
	       sock_addr + SOCK_CONTROL);
}

133 134
inline static char __iomem *
tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num)
135 136 137 138
{
	return base_addr + ((sock_num + 1) << 10);
}

139
static void tifm_7xx1_switch_media(struct work_struct *work)
140
{
141 142
	struct tifm_adapter *fm = container_of(work, struct tifm_adapter,
					       media_switcher);
143
	struct tifm_dev *sock;
144
	char __iomem *sock_addr;
145
	unsigned long flags;
146
	unsigned char media_id;
147
	unsigned int socket_change_set, cnt;
148

149 150 151
	spin_lock_irqsave(&fm->lock, flags);
	socket_change_set = fm->socket_change_set;
	fm->socket_change_set = 0;
152

153
	dev_dbg(fm->dev.parent, "checking media set %x\n",
154
		socket_change_set);
155

156
	if (!socket_change_set) {
157
		spin_unlock_irqrestore(&fm->lock, flags);
158 159
		return;
	}
160

161 162 163 164 165 166 167
	for (cnt = 0; cnt < fm->num_sockets; cnt++) {
		if (!(socket_change_set & (1 << cnt)))
			continue;
		sock = fm->sockets[cnt];
		if (sock) {
			printk(KERN_INFO
			       "%s : demand removing card from socket %u:%u\n",
168
			       dev_name(&fm->dev), fm->id, cnt);
169
			fm->sockets[cnt] = NULL;
170
			sock_addr = sock->addr;
171
			spin_unlock_irqrestore(&fm->lock, flags);
172 173
			device_unregister(&sock->dev);
			spin_lock_irqsave(&fm->lock, flags);
174 175
			tifm_7xx1_sock_power_off(sock_addr);
			writel(0x0e00, sock_addr + SOCK_CONTROL);
176 177 178 179 180 181 182 183 184 185 186 187 188
		}

		spin_unlock_irqrestore(&fm->lock, flags);

		media_id = tifm_7xx1_toggle_sock_power(
				tifm_7xx1_sock_addr(fm->addr, cnt));

		// tifm_alloc_device will check if media_id is valid
		sock = tifm_alloc_device(fm, cnt, media_id);
		if (sock) {
			sock->addr = tifm_7xx1_sock_addr(fm->addr, cnt);

			if (!device_register(&sock->dev)) {
189
				spin_lock_irqsave(&fm->lock, flags);
190 191 192 193 194
				if (!fm->sockets[cnt]) {
					fm->sockets[cnt] = sock;
					sock = NULL;
				}
				spin_unlock_irqrestore(&fm->lock, flags);
195
			}
196 197
			if (sock)
				tifm_free_device(&sock->dev);
198
		}
199 200
		spin_lock_irqsave(&fm->lock, flags);
	}
201

202 203 204 205 206 207 208 209 210 211
	writel(TIFM_IRQ_FIFOMASK(socket_change_set)
	       | TIFM_IRQ_CARDMASK(socket_change_set),
	       fm->addr + FM_CLEAR_INTERRUPT_ENABLE);

	writel(TIFM_IRQ_FIFOMASK(socket_change_set)
	       | TIFM_IRQ_CARDMASK(socket_change_set),
	       fm->addr + FM_SET_INTERRUPT_ENABLE);

	writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
	spin_unlock_irqrestore(&fm->lock, flags);
212 213
}

214 215
#ifdef CONFIG_PM

216 217
static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
{
218 219 220
	struct tifm_adapter *fm = pci_get_drvdata(dev);
	int cnt;

221
	dev_dbg(&dev->dev, "suspending host\n");
222

223 224 225 226 227
	for (cnt = 0; cnt < fm->num_sockets; cnt++) {
		if (fm->sockets[cnt])
			tifm_7xx1_sock_power_off(fm->sockets[cnt]->addr);
	}

228 229 230 231
	pci_save_state(dev);
	pci_enable_wake(dev, pci_choose_state(dev, state), 0);
	pci_disable_device(dev);
	pci_set_power_state(dev, pci_choose_state(dev, state));
232 233 234 235 236 237
	return 0;
}

static int tifm_7xx1_resume(struct pci_dev *dev)
{
	struct tifm_adapter *fm = pci_get_drvdata(dev);
238
	int rc;
239
	unsigned long timeout;
240
	unsigned int good_sockets = 0, bad_sockets = 0;
241
	unsigned long flags;
242
	unsigned char new_ids[fm->num_sockets];
243
	DECLARE_COMPLETION_ONSTACK(finish_resume);
244

245
	pci_set_power_state(dev, PCI_D0);
246
	pci_restore_state(dev);
247 248 249 250 251 252
	rc = pci_enable_device(dev);
	if (rc)
		return rc;
	pci_set_master(dev);

	dev_dbg(&dev->dev, "resuming host\n");
253

254 255 256
	for (rc = 0; rc < fm->num_sockets; rc++)
		new_ids[rc] = tifm_7xx1_toggle_sock_power(
					tifm_7xx1_sock_addr(fm->addr, rc));
257
	spin_lock_irqsave(&fm->lock, flags);
258 259 260 261 262 263
	for (rc = 0; rc < fm->num_sockets; rc++) {
		if (fm->sockets[rc]) {
			if (fm->sockets[rc]->type == new_ids[rc])
				good_sockets |= 1 << rc;
			else
				bad_sockets |= 1 << rc;
264 265 266
		}
	}

267
	writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
268
	       fm->addr + FM_SET_INTERRUPT_ENABLE);
269 270 271 272 273
	dev_dbg(&dev->dev, "change sets on resume: good %x, bad %x\n",
		good_sockets, bad_sockets);

	fm->socket_change_set = 0;
	if (good_sockets) {
274
		fm->finish_me = &finish_resume;
275
		spin_unlock_irqrestore(&fm->lock, flags);
276 277
		timeout = wait_for_completion_timeout(&finish_resume, HZ);
		dev_dbg(&dev->dev, "wait returned %lu\n", timeout);
278 279 280 281 282 283 284 285 286
		writel(TIFM_IRQ_FIFOMASK(good_sockets)
		       | TIFM_IRQ_CARDMASK(good_sockets),
		       fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
		writel(TIFM_IRQ_FIFOMASK(good_sockets)
		       | TIFM_IRQ_CARDMASK(good_sockets),
		       fm->addr + FM_SET_INTERRUPT_ENABLE);
		spin_lock_irqsave(&fm->lock, flags);
		fm->finish_me = NULL;
		fm->socket_change_set ^= good_sockets & fm->socket_change_set;
287 288
	}

289 290 291
	fm->socket_change_set |= bad_sockets;
	if (fm->socket_change_set)
		tifm_queue_work(&fm->media_switcher);
292

293
	spin_unlock_irqrestore(&fm->lock, flags);
294 295 296
	writel(TIFM_IRQ_ENABLE,
	       fm->addr + FM_SET_INTERRUPT_ENABLE);

297 298 299
	return 0;
}

300 301 302 303 304 305 306
#else

#define tifm_7xx1_suspend NULL
#define tifm_7xx1_resume NULL

#endif /* CONFIG_PM */

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
static int tifm_7xx1_dummy_has_ms_pif(struct tifm_adapter *fm,
				      struct tifm_dev *sock)
{
	return 0;
}

static int tifm_7xx1_has_ms_pif(struct tifm_adapter *fm, struct tifm_dev *sock)
{
	if (((fm->num_sockets == 4) && (sock->socket_id == 2))
	    || ((fm->num_sockets == 2) && (sock->socket_id == 0)))
		return 1;

	return 0;
}

322
static int tifm_7xx1_probe(struct pci_dev *dev,
A
Alex Dubov 已提交
323
			   const struct pci_device_id *dev_id)
324 325 326 327 328
{
	struct tifm_adapter *fm;
	int pci_dev_busy = 0;
	int rc;

329
	rc = pci_set_dma_mask(dev, DMA_BIT_MASK(32));
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
	if (rc)
		return rc;

	rc = pci_enable_device(dev);
	if (rc)
		return rc;

	pci_set_master(dev);

	rc = pci_request_regions(dev, DRIVER_NAME);
	if (rc) {
		pci_dev_busy = 1;
		goto err_out;
	}

	pci_intx(dev, 1);

347 348
	fm = tifm_alloc_adapter(dev->device == PCI_DEVICE_ID_TI_XX21_XX11_FM
				? 4 : 2, &dev->dev);
349 350 351 352 353
	if (!fm) {
		rc = -ENOMEM;
		goto err_out_int;
	}

354
	INIT_WORK(&fm->media_switcher, tifm_7xx1_switch_media);
355
	fm->eject = tifm_7xx1_eject;
356
	fm->has_ms_pif = tifm_7xx1_has_ms_pif;
357 358
	pci_set_drvdata(dev, fm);

359
	fm->addr = pci_ioremap_bar(dev, 0);
360 361
	if (!fm->addr) {
		rc = -ENODEV;
362
		goto err_out_free;
363
	}
364

365
	rc = request_irq(dev->irq, tifm_7xx1_isr, IRQF_SHARED, DRIVER_NAME, fm);
366 367 368
	if (rc)
		goto err_out_unmap;

369
	rc = tifm_add_adapter(fm);
370 371 372
	if (rc)
		goto err_out_irq;

373 374
	writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
	       fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
375
	writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
A
Alex Dubov 已提交
376
	       fm->addr + FM_SET_INTERRUPT_ENABLE);
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
	return 0;

err_out_irq:
	free_irq(dev->irq, fm);
err_out_unmap:
	iounmap(fm->addr);
err_out_free:
	tifm_free_adapter(fm);
err_out_int:
	pci_intx(dev, 0);
	pci_release_regions(dev);
err_out:
	if (!pci_dev_busy)
		pci_disable_device(dev);
	return rc;
}

static void tifm_7xx1_remove(struct pci_dev *dev)
{
	struct tifm_adapter *fm = pci_get_drvdata(dev);
397
	int cnt;
398

399
	fm->eject = tifm_7xx1_dummy_eject;
400
	fm->has_ms_pif = tifm_7xx1_dummy_has_ms_pif;
401 402 403 404
	writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
	mmiowb();
	free_irq(dev->irq, fm);

405 406
	tifm_remove_adapter(fm);

407 408 409
	for (cnt = 0; cnt < fm->num_sockets; cnt++)
		tifm_7xx1_sock_power_off(tifm_7xx1_sock_addr(fm->addr, cnt));

410 411 412 413 414 415 416 417
	iounmap(fm->addr);
	pci_intx(dev, 0);
	pci_release_regions(dev);

	pci_disable_device(dev);
	tifm_free_adapter(fm);
}

418
static const struct pci_device_id tifm_7xx1_pci_tbl[] = {
419 420 421 422 423 424
	{ PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX21_XX11_FM, PCI_ANY_ID,
	  PCI_ANY_ID, 0, 0, 0 }, /* xx21 - the one I have */
        { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX12_FM, PCI_ANY_ID,
	  PCI_ANY_ID, 0, 0, 0 },
	{ PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX20_FM, PCI_ANY_ID,
	  PCI_ANY_ID, 0, 0, 0 },
425 426 427 428 429 430 431 432 433 434 435 436
	{ }
};

static struct pci_driver tifm_7xx1_driver = {
	.name = DRIVER_NAME,
	.id_table = tifm_7xx1_pci_tbl,
	.probe = tifm_7xx1_probe,
	.remove = tifm_7xx1_remove,
	.suspend = tifm_7xx1_suspend,
	.resume = tifm_7xx1_resume,
};

D
Devendra Naga 已提交
437
module_pci_driver(tifm_7xx1_driver);
438 439 440 441 442
MODULE_AUTHOR("Alex Dubov");
MODULE_DESCRIPTION("TI FlashMedia host driver");
MODULE_LICENSE("GPL");
MODULE_DEVICE_TABLE(pci, tifm_7xx1_pci_tbl);
MODULE_VERSION(DRIVER_VERSION);