Linux Audio

Check our new training course

Loading...
v3.5.6
 
  1/*
  2 *  cb710/mmc.c
  3 *
  4 *  Copyright by Michał Mirosław, 2008-2009
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License version 2 as
  8 * published by the Free Software Foundation.
  9 */
 10#include <linux/kernel.h>
 11#include <linux/module.h>
 12#include <linux/pci.h>
 13#include <linux/delay.h>
 14#include "cb710-mmc.h"
 15
 16static const u8 cb710_clock_divider_log2[8] = {
 17/*	1, 2, 4, 8, 16, 32, 128, 512 */
 18	0, 1, 2, 3,  4,  5,   7,   9
 19};
 20#define CB710_MAX_DIVIDER_IDX	\
 21	(ARRAY_SIZE(cb710_clock_divider_log2) - 1)
 22
 23static const u8 cb710_src_freq_mhz[16] = {
 24	33, 10, 20, 25, 30, 35, 40, 45,
 25	50, 55, 60, 65, 70, 75, 80, 85
 26};
 27
 28static void cb710_mmc_select_clock_divider(struct mmc_host *mmc, int hz)
 29{
 30	struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
 31	struct pci_dev *pdev = cb710_slot_to_chip(slot)->pdev;
 32	u32 src_freq_idx;
 33	u32 divider_idx;
 34	int src_hz;
 35
 36	/* on CB710 in HP nx9500:
 37	 *   src_freq_idx == 0
 38	 *   indexes 1-7 work as written in the table
 39	 *   indexes 0,8-15 give no clock output
 40	 */
 41	pci_read_config_dword(pdev, 0x48, &src_freq_idx);
 42	src_freq_idx = (src_freq_idx >> 16) & 0xF;
 43	src_hz = cb710_src_freq_mhz[src_freq_idx] * 1000000;
 44
 45	for (divider_idx = 0; divider_idx < CB710_MAX_DIVIDER_IDX; ++divider_idx) {
 46		if (hz >= src_hz >> cb710_clock_divider_log2[divider_idx])
 47			break;
 48	}
 49
 50	if (src_freq_idx)
 51		divider_idx |= 0x8;
 52	else if (divider_idx == 0)
 53		divider_idx = 1;
 54
 55	cb710_pci_update_config_reg(pdev, 0x40, ~0xF0000000, divider_idx << 28);
 56
 57	dev_dbg(cb710_slot_dev(slot),
 58		"clock set to %d Hz, wanted %d Hz; src_freq_idx = %d, divider_idx = %d|%d\n",
 59		src_hz >> cb710_clock_divider_log2[divider_idx & 7],
 60		hz, src_freq_idx, divider_idx & 7, divider_idx & 8);
 61}
 62
 63static void __cb710_mmc_enable_irq(struct cb710_slot *slot,
 64	unsigned short enable, unsigned short mask)
 65{
 66	/* clear global IE
 67	 * - it gets set later if any interrupt sources are enabled */
 68	mask |= CB710_MMC_IE_IRQ_ENABLE;
 69
 70	/* look like interrupt is fired whenever
 71	 * WORD[0x0C] & WORD[0x10] != 0;
 72	 * -> bit 15 port 0x0C seems to be global interrupt enable
 73	 */
 74
 75	enable = (cb710_read_port_16(slot, CB710_MMC_IRQ_ENABLE_PORT)
 76		& ~mask) | enable;
 77
 78	if (enable)
 79		enable |= CB710_MMC_IE_IRQ_ENABLE;
 80
 81	cb710_write_port_16(slot, CB710_MMC_IRQ_ENABLE_PORT, enable);
 82}
 83
 84static void cb710_mmc_enable_irq(struct cb710_slot *slot,
 85	unsigned short enable, unsigned short mask)
 86{
 87	struct cb710_mmc_reader *reader = mmc_priv(cb710_slot_to_mmc(slot));
 88	unsigned long flags;
 89
 90	spin_lock_irqsave(&reader->irq_lock, flags);
 91	/* this is the only thing irq_lock protects */
 92	__cb710_mmc_enable_irq(slot, enable, mask);
 93	spin_unlock_irqrestore(&reader->irq_lock, flags);
 94}
 95
 96static void cb710_mmc_reset_events(struct cb710_slot *slot)
 97{
 98	cb710_write_port_8(slot, CB710_MMC_STATUS0_PORT, 0xFF);
 99	cb710_write_port_8(slot, CB710_MMC_STATUS1_PORT, 0xFF);
100	cb710_write_port_8(slot, CB710_MMC_STATUS2_PORT, 0xFF);
101}
102
103static void cb710_mmc_enable_4bit_data(struct cb710_slot *slot, int enable)
104{
105	if (enable)
106		cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT,
107			CB710_MMC_C1_4BIT_DATA_BUS, 0);
108	else
109		cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT,
110			0, CB710_MMC_C1_4BIT_DATA_BUS);
111}
112
113static int cb710_check_event(struct cb710_slot *slot, u8 what)
114{
115	u16 status;
116
117	status = cb710_read_port_16(slot, CB710_MMC_STATUS_PORT);
118
119	if (status & CB710_MMC_S0_FIFO_UNDERFLOW) {
120		/* it is just a guess, so log it */
121		dev_dbg(cb710_slot_dev(slot),
122			"CHECK : ignoring bit 6 in status %04X\n", status);
123		cb710_write_port_8(slot, CB710_MMC_STATUS0_PORT,
124			CB710_MMC_S0_FIFO_UNDERFLOW);
125		status &= ~CB710_MMC_S0_FIFO_UNDERFLOW;
126	}
127
128	if (status & CB710_MMC_STATUS_ERROR_EVENTS) {
129		dev_dbg(cb710_slot_dev(slot),
130			"CHECK : returning EIO on status %04X\n", status);
131		cb710_write_port_8(slot, CB710_MMC_STATUS0_PORT, status & 0xFF);
132		cb710_write_port_8(slot, CB710_MMC_STATUS1_PORT,
133			CB710_MMC_S1_RESET);
134		return -EIO;
135	}
136
137	/* 'what' is a bit in MMC_STATUS1 */
138	if ((status >> 8) & what) {
139		cb710_write_port_8(slot, CB710_MMC_STATUS1_PORT, what);
140		return 1;
141	}
142
143	return 0;
144}
145
146static int cb710_wait_for_event(struct cb710_slot *slot, u8 what)
147{
148	int err = 0;
149	unsigned limit = 2000000;	/* FIXME: real timeout */
150
151#ifdef CONFIG_CB710_DEBUG
152	u32 e, x;
153	e = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT);
154#endif
155
156	while (!(err = cb710_check_event(slot, what))) {
157		if (!--limit) {
158			cb710_dump_regs(cb710_slot_to_chip(slot),
159				CB710_DUMP_REGS_MMC);
160			err = -ETIMEDOUT;
161			break;
162		}
163		udelay(1);
164	}
165
166#ifdef CONFIG_CB710_DEBUG
167	x = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT);
168
169	limit = 2000000 - limit;
170	if (limit > 100)
171		dev_dbg(cb710_slot_dev(slot),
172			"WAIT10: waited %d loops, what %d, entry val %08X, exit val %08X\n",
173			limit, what, e, x);
174#endif
175	return err < 0 ? err : 0;
176}
177
178
179static int cb710_wait_while_busy(struct cb710_slot *slot, uint8_t mask)
180{
181	unsigned limit = 500000;	/* FIXME: real timeout */
182	int err = 0;
183
184#ifdef CONFIG_CB710_DEBUG
185	u32 e, x;
186	e = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT);
187#endif
188
189	while (cb710_read_port_8(slot, CB710_MMC_STATUS2_PORT) & mask) {
190		if (!--limit) {
191			cb710_dump_regs(cb710_slot_to_chip(slot),
192				CB710_DUMP_REGS_MMC);
193			err = -ETIMEDOUT;
194			break;
195		}
196		udelay(1);
197	}
198
199#ifdef CONFIG_CB710_DEBUG
200	x = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT);
201
202	limit = 500000 - limit;
203	if (limit > 100)
204		dev_dbg(cb710_slot_dev(slot),
205			"WAIT12: waited %d loops, mask %02X, entry val %08X, exit val %08X\n",
206			limit, mask, e, x);
207#endif
208	return err;
209}
210
211static void cb710_mmc_set_transfer_size(struct cb710_slot *slot,
212	size_t count, size_t blocksize)
213{
214	cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
215	cb710_write_port_32(slot, CB710_MMC_TRANSFER_SIZE_PORT,
216		((count - 1) << 16)|(blocksize - 1));
217
218	dev_vdbg(cb710_slot_dev(slot), "set up for %zu block%s of %zu bytes\n",
219		count, count == 1 ? "" : "s", blocksize);
220}
221
222static void cb710_mmc_fifo_hack(struct cb710_slot *slot)
223{
224	/* without this, received data is prepended with 8-bytes of zeroes */
225	u32 r1, r2;
226	int ok = 0;
227
228	r1 = cb710_read_port_32(slot, CB710_MMC_DATA_PORT);
229	r2 = cb710_read_port_32(slot, CB710_MMC_DATA_PORT);
230	if (cb710_read_port_8(slot, CB710_MMC_STATUS0_PORT)
231	    & CB710_MMC_S0_FIFO_UNDERFLOW) {
232		cb710_write_port_8(slot, CB710_MMC_STATUS0_PORT,
233			CB710_MMC_S0_FIFO_UNDERFLOW);
234		ok = 1;
235	}
236
237	dev_dbg(cb710_slot_dev(slot),
238		"FIFO-read-hack: expected STATUS0 bit was %s\n",
239		ok ? "set." : "NOT SET!");
240	dev_dbg(cb710_slot_dev(slot),
241		"FIFO-read-hack: dwords ignored: %08X %08X - %s\n",
242		r1, r2, (r1|r2) ? "BAD (NOT ZERO)!" : "ok");
243}
244
245static int cb710_mmc_receive_pio(struct cb710_slot *slot,
246	struct sg_mapping_iter *miter, size_t dw_count)
247{
248	if (!(cb710_read_port_8(slot, CB710_MMC_STATUS2_PORT) & CB710_MMC_S2_FIFO_READY)) {
249		int err = cb710_wait_for_event(slot,
250			CB710_MMC_S1_PIO_TRANSFER_DONE);
251		if (err)
252			return err;
253	}
254
255	cb710_sg_dwiter_write_from_io(miter,
256		slot->iobase + CB710_MMC_DATA_PORT, dw_count);
257
258	return 0;
259}
260
261static bool cb710_is_transfer_size_supported(struct mmc_data *data)
262{
263	return !(data->blksz & 15 && (data->blocks != 1 || data->blksz != 8));
264}
265
266static int cb710_mmc_receive(struct cb710_slot *slot, struct mmc_data *data)
267{
268	struct sg_mapping_iter miter;
269	size_t len, blocks = data->blocks;
270	int err = 0;
271
272	/* TODO: I don't know how/if the hardware handles non-16B-boundary blocks
273	 * except single 8B block */
274	if (unlikely(data->blksz & 15 && (data->blocks != 1 || data->blksz != 8)))
275		return -EINVAL;
276
277	sg_miter_start(&miter, data->sg, data->sg_len, SG_MITER_TO_SG);
278
279	cb710_modify_port_8(slot, CB710_MMC_CONFIG2_PORT,
280		15, CB710_MMC_C2_READ_PIO_SIZE_MASK);
281
282	cb710_mmc_fifo_hack(slot);
283
284	while (blocks-- > 0) {
285		len = data->blksz;
286
287		while (len >= 16) {
288			err = cb710_mmc_receive_pio(slot, &miter, 4);
289			if (err)
290				goto out;
291			len -= 16;
292		}
293
294		if (!len)
295			continue;
296
297		cb710_modify_port_8(slot, CB710_MMC_CONFIG2_PORT,
298			len - 1, CB710_MMC_C2_READ_PIO_SIZE_MASK);
299
300		len = (len >= 8) ? 4 : 2;
301		err = cb710_mmc_receive_pio(slot, &miter, len);
302		if (err)
303			goto out;
304	}
305out:
306	sg_miter_stop(&miter);
307	return err;
308}
309
310static int cb710_mmc_send(struct cb710_slot *slot, struct mmc_data *data)
311{
312	struct sg_mapping_iter miter;
313	size_t len, blocks = data->blocks;
314	int err = 0;
315
316	/* TODO: I don't know how/if the hardware handles multiple
317	 * non-16B-boundary blocks */
318	if (unlikely(data->blocks > 1 && data->blksz & 15))
319		return -EINVAL;
320
321	sg_miter_start(&miter, data->sg, data->sg_len, SG_MITER_FROM_SG);
322
323	cb710_modify_port_8(slot, CB710_MMC_CONFIG2_PORT,
324		0, CB710_MMC_C2_READ_PIO_SIZE_MASK);
325
326	while (blocks-- > 0) {
327		len = (data->blksz + 15) >> 4;
328		do {
329			if (!(cb710_read_port_8(slot, CB710_MMC_STATUS2_PORT)
330			    & CB710_MMC_S2_FIFO_EMPTY)) {
331				err = cb710_wait_for_event(slot,
332					CB710_MMC_S1_PIO_TRANSFER_DONE);
333				if (err)
334					goto out;
335			}
336			cb710_sg_dwiter_read_to_io(&miter,
337				slot->iobase + CB710_MMC_DATA_PORT, 4);
338		} while (--len);
339	}
340out:
341	sg_miter_stop(&miter);
342	return err;
343}
344
345static u16 cb710_encode_cmd_flags(struct cb710_mmc_reader *reader,
346	struct mmc_command *cmd)
347{
348	unsigned int flags = cmd->flags;
349	u16 cb_flags = 0;
350
351	/* Windows driver returned 0 for commands for which no response
352	 * is expected. It happened that there were only two such commands
353	 * used: MMC_GO_IDLE_STATE and MMC_GO_INACTIVE_STATE so it might
354	 * as well be a bug in that driver.
355	 *
356	 * Original driver set bit 14 for MMC/SD application
357	 * commands. There's no difference 'on the wire' and
358	 * it apparently works without it anyway.
359	 */
360
361	switch (flags & MMC_CMD_MASK) {
362	case MMC_CMD_AC:	cb_flags = CB710_MMC_CMD_AC;	break;
363	case MMC_CMD_ADTC:	cb_flags = CB710_MMC_CMD_ADTC;	break;
364	case MMC_CMD_BC:	cb_flags = CB710_MMC_CMD_BC;	break;
365	case MMC_CMD_BCR:	cb_flags = CB710_MMC_CMD_BCR;	break;
366	}
367
368	if (flags & MMC_RSP_BUSY)
369		cb_flags |= CB710_MMC_RSP_BUSY;
370
371	cb_flags |= cmd->opcode << CB710_MMC_CMD_CODE_SHIFT;
372
373	if (cmd->data && (cmd->data->flags & MMC_DATA_READ))
374		cb_flags |= CB710_MMC_DATA_READ;
375
376	if (flags & MMC_RSP_PRESENT) {
377		/* Windows driver set 01 at bits 4,3 except for
378		 * MMC_SET_BLOCKLEN where it set 10. Maybe the
379		 * hardware can do something special about this
380		 * command? The original driver looks buggy/incomplete
381		 * anyway so we ignore this for now.
382		 *
383		 * I assume that 00 here means no response is expected.
384		 */
385		cb_flags |= CB710_MMC_RSP_PRESENT;
386
387		if (flags & MMC_RSP_136)
388			cb_flags |= CB710_MMC_RSP_136;
389		if (!(flags & MMC_RSP_CRC))
390			cb_flags |= CB710_MMC_RSP_NO_CRC;
391	}
392
393	return cb_flags;
394}
395
396static void cb710_receive_response(struct cb710_slot *slot,
397	struct mmc_command *cmd)
398{
399	unsigned rsp_opcode, wanted_opcode;
400
401	/* Looks like final byte with CRC is always stripped (same as SDHCI) */
402	if (cmd->flags & MMC_RSP_136) {
403		u32 resp[4];
404
405		resp[0] = cb710_read_port_32(slot, CB710_MMC_RESPONSE3_PORT);
406		resp[1] = cb710_read_port_32(slot, CB710_MMC_RESPONSE2_PORT);
407		resp[2] = cb710_read_port_32(slot, CB710_MMC_RESPONSE1_PORT);
408		resp[3] = cb710_read_port_32(slot, CB710_MMC_RESPONSE0_PORT);
409		rsp_opcode = resp[0] >> 24;
410
411		cmd->resp[0] = (resp[0] << 8)|(resp[1] >> 24);
412		cmd->resp[1] = (resp[1] << 8)|(resp[2] >> 24);
413		cmd->resp[2] = (resp[2] << 8)|(resp[3] >> 24);
414		cmd->resp[3] = (resp[3] << 8);
415	} else {
416		rsp_opcode = cb710_read_port_32(slot, CB710_MMC_RESPONSE1_PORT) & 0x3F;
417		cmd->resp[0] = cb710_read_port_32(slot, CB710_MMC_RESPONSE0_PORT);
418	}
419
420	wanted_opcode = (cmd->flags & MMC_RSP_OPCODE) ? cmd->opcode : 0x3F;
421	if (rsp_opcode != wanted_opcode)
422		cmd->error = -EILSEQ;
423}
424
425static int cb710_mmc_transfer_data(struct cb710_slot *slot,
426	struct mmc_data *data)
427{
428	int error, to;
429
430	if (data->flags & MMC_DATA_READ)
431		error = cb710_mmc_receive(slot, data);
432	else
433		error = cb710_mmc_send(slot, data);
434
435	to = cb710_wait_for_event(slot, CB710_MMC_S1_DATA_TRANSFER_DONE);
436	if (!error)
437		error = to;
438
439	if (!error)
440		data->bytes_xfered = data->blksz * data->blocks;
441	return error;
442}
443
444static int cb710_mmc_command(struct mmc_host *mmc, struct mmc_command *cmd)
445{
446	struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
447	struct cb710_mmc_reader *reader = mmc_priv(mmc);
448	struct mmc_data *data = cmd->data;
449
450	u16 cb_cmd = cb710_encode_cmd_flags(reader, cmd);
451	dev_dbg(cb710_slot_dev(slot), "cmd request: 0x%04X\n", cb_cmd);
452
453	if (data) {
454		if (!cb710_is_transfer_size_supported(data)) {
455			data->error = -EINVAL;
456			return -1;
457		}
458		cb710_mmc_set_transfer_size(slot, data->blocks, data->blksz);
459	}
460
461	cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20|CB710_MMC_S2_BUSY_10);
462	cb710_write_port_16(slot, CB710_MMC_CMD_TYPE_PORT, cb_cmd);
463	cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
464	cb710_write_port_32(slot, CB710_MMC_CMD_PARAM_PORT, cmd->arg);
465	cb710_mmc_reset_events(slot);
466	cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
467	cb710_modify_port_8(slot, CB710_MMC_CONFIG0_PORT, 0x01, 0);
468
469	cmd->error = cb710_wait_for_event(slot, CB710_MMC_S1_COMMAND_SENT);
470	if (cmd->error)
471		return -1;
472
473	if (cmd->flags & MMC_RSP_PRESENT) {
474		cb710_receive_response(slot, cmd);
475		if (cmd->error)
476			return -1;
477	}
478
479	if (data)
480		data->error = cb710_mmc_transfer_data(slot, data);
481	return 0;
482}
483
484static void cb710_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
485{
486	struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
487	struct cb710_mmc_reader *reader = mmc_priv(mmc);
488
489	WARN_ON(reader->mrq != NULL);
490
491	reader->mrq = mrq;
492	cb710_mmc_enable_irq(slot, CB710_MMC_IE_TEST_MASK, 0);
493
494	if (!cb710_mmc_command(mmc, mrq->cmd) && mrq->stop)
495		cb710_mmc_command(mmc, mrq->stop);
496
497	tasklet_schedule(&reader->finish_req_tasklet);
498}
499
500static int cb710_mmc_powerup(struct cb710_slot *slot)
501{
502#ifdef CONFIG_CB710_DEBUG
503	struct cb710_chip *chip = cb710_slot_to_chip(slot);
504#endif
505	int err;
506
507	/* a lot of magic for now */
508	dev_dbg(cb710_slot_dev(slot), "bus powerup\n");
509	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
510	err = cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
511	if (unlikely(err))
512		return err;
513	cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0x80, 0);
514	cb710_modify_port_8(slot, CB710_MMC_CONFIG3_PORT, 0x80, 0);
515	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
516	mdelay(1);
517	dev_dbg(cb710_slot_dev(slot), "after delay 1\n");
518	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
519	err = cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
520	if (unlikely(err))
521		return err;
522	cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0x09, 0);
523	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
524	mdelay(1);
525	dev_dbg(cb710_slot_dev(slot), "after delay 2\n");
526	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
527	err = cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
528	if (unlikely(err))
529		return err;
530	cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0, 0x08);
531	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
532	mdelay(2);
533	dev_dbg(cb710_slot_dev(slot), "after delay 3\n");
534	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
535	cb710_modify_port_8(slot, CB710_MMC_CONFIG0_PORT, 0x06, 0);
536	cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0x70, 0);
537	cb710_modify_port_8(slot, CB710_MMC_CONFIG2_PORT, 0x80, 0);
538	cb710_modify_port_8(slot, CB710_MMC_CONFIG3_PORT, 0x03, 0);
539	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
540	err = cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
541	if (unlikely(err))
542		return err;
543	/* This port behaves weird: quick byte reads of 0x08,0x09 return
544	 * 0xFF,0x00 after writing 0xFFFF to 0x08; it works correctly when
545	 * read/written from userspace...  What am I missing here?
546	 * (it doesn't depend on write-to-read delay) */
547	cb710_write_port_16(slot, CB710_MMC_CONFIGB_PORT, 0xFFFF);
548	cb710_modify_port_8(slot, CB710_MMC_CONFIG0_PORT, 0x06, 0);
549	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
550	dev_dbg(cb710_slot_dev(slot), "bus powerup finished\n");
551
552	return cb710_check_event(slot, 0);
553}
554
555static void cb710_mmc_powerdown(struct cb710_slot *slot)
556{
557	cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0, 0x81);
558	cb710_modify_port_8(slot, CB710_MMC_CONFIG3_PORT, 0, 0x80);
559}
560
561static void cb710_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
562{
563	struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
564	struct cb710_mmc_reader *reader = mmc_priv(mmc);
565	int err;
566
567	cb710_mmc_select_clock_divider(mmc, ios->clock);
568
569	if (ios->power_mode != reader->last_power_mode)
570	switch (ios->power_mode) {
571	case MMC_POWER_ON:
572		err = cb710_mmc_powerup(slot);
573		if (err) {
574			dev_warn(cb710_slot_dev(slot),
575				"powerup failed (%d)- retrying\n", err);
576			cb710_mmc_powerdown(slot);
577			udelay(1);
578			err = cb710_mmc_powerup(slot);
579			if (err)
580				dev_warn(cb710_slot_dev(slot),
581					"powerup retry failed (%d) - expect errors\n",
 
 
 
 
 
 
582					err);
 
 
 
 
 
 
 
 
 
 
 
583		}
584		reader->last_power_mode = MMC_POWER_ON;
585		break;
586	case MMC_POWER_OFF:
587		cb710_mmc_powerdown(slot);
588		reader->last_power_mode = MMC_POWER_OFF;
589		break;
590	case MMC_POWER_UP:
591	default:
592		/* ignore */;
593	}
594
595	cb710_mmc_enable_4bit_data(slot, ios->bus_width != MMC_BUS_WIDTH_1);
596
597	cb710_mmc_enable_irq(slot, CB710_MMC_IE_TEST_MASK, 0);
598}
599
600static int cb710_mmc_get_ro(struct mmc_host *mmc)
601{
602	struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
603
604	return cb710_read_port_8(slot, CB710_MMC_STATUS3_PORT)
605		& CB710_MMC_S3_WRITE_PROTECTED;
606}
607
608static int cb710_mmc_get_cd(struct mmc_host *mmc)
609{
610	struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
611
612	return cb710_read_port_8(slot, CB710_MMC_STATUS3_PORT)
613		& CB710_MMC_S3_CARD_DETECTED;
614}
615
616static int cb710_mmc_irq_handler(struct cb710_slot *slot)
617{
618	struct mmc_host *mmc = cb710_slot_to_mmc(slot);
619	struct cb710_mmc_reader *reader = mmc_priv(mmc);
620	u32 status, config1, config2, irqen;
621
622	status = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT);
623	irqen = cb710_read_port_32(slot, CB710_MMC_IRQ_ENABLE_PORT);
624	config2 = cb710_read_port_32(slot, CB710_MMC_CONFIGB_PORT);
625	config1 = cb710_read_port_32(slot, CB710_MMC_CONFIG_PORT);
626
627	dev_dbg(cb710_slot_dev(slot), "interrupt; status: %08X, "
628		"ie: %08X, c2: %08X, c1: %08X\n",
629		status, irqen, config2, config1);
630
631	if (status & (CB710_MMC_S1_CARD_CHANGED << 8)) {
632		/* ack the event */
633		cb710_write_port_8(slot, CB710_MMC_STATUS1_PORT,
634			CB710_MMC_S1_CARD_CHANGED);
635		if ((irqen & CB710_MMC_IE_CISTATUS_MASK)
636		    == CB710_MMC_IE_CISTATUS_MASK)
637			mmc_detect_change(mmc, HZ/5);
638	} else {
639		dev_dbg(cb710_slot_dev(slot), "unknown interrupt (test)\n");
640		spin_lock(&reader->irq_lock);
641		__cb710_mmc_enable_irq(slot, 0, CB710_MMC_IE_TEST_MASK);
642		spin_unlock(&reader->irq_lock);
643	}
644
645	return 1;
646}
647
648static void cb710_mmc_finish_request_tasklet(unsigned long data)
649{
650	struct mmc_host *mmc = (void *)data;
651	struct cb710_mmc_reader *reader = mmc_priv(mmc);
652	struct mmc_request *mrq = reader->mrq;
653
654	reader->mrq = NULL;
655	mmc_request_done(mmc, mrq);
656}
657
658static const struct mmc_host_ops cb710_mmc_host = {
659	.request = cb710_mmc_request,
660	.set_ios = cb710_mmc_set_ios,
661	.get_ro = cb710_mmc_get_ro,
662	.get_cd = cb710_mmc_get_cd,
663};
664
665#ifdef CONFIG_PM
666
667static int cb710_mmc_suspend(struct platform_device *pdev, pm_message_t state)
668{
669	struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
670	struct mmc_host *mmc = cb710_slot_to_mmc(slot);
671	int err;
672
673	err = mmc_suspend_host(mmc);
674	if (err)
675		return err;
676
677	cb710_mmc_enable_irq(slot, 0, ~0);
678	return 0;
679}
680
681static int cb710_mmc_resume(struct platform_device *pdev)
682{
683	struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
684	struct mmc_host *mmc = cb710_slot_to_mmc(slot);
685
686	cb710_mmc_enable_irq(slot, 0, ~0);
687
688	return mmc_resume_host(mmc);
689}
690
691#endif /* CONFIG_PM */
692
693static int __devinit cb710_mmc_init(struct platform_device *pdev)
694{
695	struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
696	struct cb710_chip *chip = cb710_slot_to_chip(slot);
697	struct mmc_host *mmc;
698	struct cb710_mmc_reader *reader;
699	int err;
700	u32 val;
701
702	mmc = mmc_alloc_host(sizeof(*reader), cb710_slot_dev(slot));
703	if (!mmc)
704		return -ENOMEM;
705
706	dev_set_drvdata(&pdev->dev, mmc);
707
708	/* harmless (maybe) magic */
709	pci_read_config_dword(chip->pdev, 0x48, &val);
710	val = cb710_src_freq_mhz[(val >> 16) & 0xF];
711	dev_dbg(cb710_slot_dev(slot), "source frequency: %dMHz\n", val);
712	val *= 1000000;
713
714	mmc->ops = &cb710_mmc_host;
715	mmc->f_max = val;
716	mmc->f_min = val >> cb710_clock_divider_log2[CB710_MAX_DIVIDER_IDX];
717	mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
718	mmc->caps = MMC_CAP_4_BIT_DATA;
719
720	reader = mmc_priv(mmc);
721
722	tasklet_init(&reader->finish_req_tasklet,
723		cb710_mmc_finish_request_tasklet, (unsigned long)mmc);
724	spin_lock_init(&reader->irq_lock);
725	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
726
727	cb710_mmc_enable_irq(slot, 0, ~0);
728	cb710_set_irq_handler(slot, cb710_mmc_irq_handler);
729
730	err = mmc_add_host(mmc);
731	if (unlikely(err))
732		goto err_free_mmc;
733
734	dev_dbg(cb710_slot_dev(slot), "mmc_hostname is %s\n",
735		mmc_hostname(mmc));
736
737	cb710_mmc_enable_irq(slot, CB710_MMC_IE_CARD_INSERTION_STATUS, 0);
738
739	return 0;
740
741err_free_mmc:
742	dev_dbg(cb710_slot_dev(slot), "mmc_add_host() failed: %d\n", err);
743
744	cb710_set_irq_handler(slot, NULL);
745	mmc_free_host(mmc);
746	return err;
747}
748
749static int __devexit cb710_mmc_exit(struct platform_device *pdev)
750{
751	struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
752	struct mmc_host *mmc = cb710_slot_to_mmc(slot);
753	struct cb710_mmc_reader *reader = mmc_priv(mmc);
754
755	cb710_mmc_enable_irq(slot, 0, CB710_MMC_IE_CARD_INSERTION_STATUS);
756
757	mmc_remove_host(mmc);
758
759	/* IRQs should be disabled now, but let's stay on the safe side */
760	cb710_mmc_enable_irq(slot, 0, ~0);
761	cb710_set_irq_handler(slot, NULL);
762
763	/* clear config ports - just in case */
764	cb710_write_port_32(slot, CB710_MMC_CONFIG_PORT, 0);
765	cb710_write_port_16(slot, CB710_MMC_CONFIGB_PORT, 0);
766
767	tasklet_kill(&reader->finish_req_tasklet);
768
769	mmc_free_host(mmc);
770	return 0;
771}
772
773static struct platform_driver cb710_mmc_driver = {
774	.driver.name = "cb710-mmc",
775	.probe = cb710_mmc_init,
776	.remove = __devexit_p(cb710_mmc_exit),
777#ifdef CONFIG_PM
778	.suspend = cb710_mmc_suspend,
779	.resume = cb710_mmc_resume,
780#endif
781};
782
783module_platform_driver(cb710_mmc_driver);
784
785MODULE_AUTHOR("Michał Mirosław <mirq-linux@rere.qmqm.pl>");
786MODULE_DESCRIPTION("ENE CB710 memory card reader driver - MMC/SD part");
787MODULE_LICENSE("GPL");
788MODULE_ALIAS("platform:cb710-mmc");
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 *  cb710/mmc.c
  4 *
  5 *  Copyright by Michał Mirosław, 2008-2009
 
 
 
 
  6 */
  7#include <linux/kernel.h>
  8#include <linux/module.h>
  9#include <linux/pci.h>
 10#include <linux/delay.h>
 11#include "cb710-mmc.h"
 12
 13static const u8 cb710_clock_divider_log2[8] = {
 14/*	1, 2, 4, 8, 16, 32, 128, 512 */
 15	0, 1, 2, 3,  4,  5,   7,   9
 16};
 17#define CB710_MAX_DIVIDER_IDX	\
 18	(ARRAY_SIZE(cb710_clock_divider_log2) - 1)
 19
 20static const u8 cb710_src_freq_mhz[16] = {
 21	33, 10, 20, 25, 30, 35, 40, 45,
 22	50, 55, 60, 65, 70, 75, 80, 85
 23};
 24
 25static void cb710_mmc_select_clock_divider(struct mmc_host *mmc, int hz)
 26{
 27	struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
 28	struct pci_dev *pdev = cb710_slot_to_chip(slot)->pdev;
 29	u32 src_freq_idx;
 30	u32 divider_idx;
 31	int src_hz;
 32
 33	/* on CB710 in HP nx9500:
 34	 *   src_freq_idx == 0
 35	 *   indexes 1-7 work as written in the table
 36	 *   indexes 0,8-15 give no clock output
 37	 */
 38	pci_read_config_dword(pdev, 0x48, &src_freq_idx);
 39	src_freq_idx = (src_freq_idx >> 16) & 0xF;
 40	src_hz = cb710_src_freq_mhz[src_freq_idx] * 1000000;
 41
 42	for (divider_idx = 0; divider_idx < CB710_MAX_DIVIDER_IDX; ++divider_idx) {
 43		if (hz >= src_hz >> cb710_clock_divider_log2[divider_idx])
 44			break;
 45	}
 46
 47	if (src_freq_idx)
 48		divider_idx |= 0x8;
 49	else if (divider_idx == 0)
 50		divider_idx = 1;
 51
 52	cb710_pci_update_config_reg(pdev, 0x40, ~0xF0000000, divider_idx << 28);
 53
 54	dev_dbg(cb710_slot_dev(slot),
 55		"clock set to %d Hz, wanted %d Hz; src_freq_idx = %d, divider_idx = %d|%d\n",
 56		src_hz >> cb710_clock_divider_log2[divider_idx & 7],
 57		hz, src_freq_idx, divider_idx & 7, divider_idx & 8);
 58}
 59
 60static void __cb710_mmc_enable_irq(struct cb710_slot *slot,
 61	unsigned short enable, unsigned short mask)
 62{
 63	/* clear global IE
 64	 * - it gets set later if any interrupt sources are enabled */
 65	mask |= CB710_MMC_IE_IRQ_ENABLE;
 66
 67	/* look like interrupt is fired whenever
 68	 * WORD[0x0C] & WORD[0x10] != 0;
 69	 * -> bit 15 port 0x0C seems to be global interrupt enable
 70	 */
 71
 72	enable = (cb710_read_port_16(slot, CB710_MMC_IRQ_ENABLE_PORT)
 73		& ~mask) | enable;
 74
 75	if (enable)
 76		enable |= CB710_MMC_IE_IRQ_ENABLE;
 77
 78	cb710_write_port_16(slot, CB710_MMC_IRQ_ENABLE_PORT, enable);
 79}
 80
 81static void cb710_mmc_enable_irq(struct cb710_slot *slot,
 82	unsigned short enable, unsigned short mask)
 83{
 84	struct cb710_mmc_reader *reader = mmc_priv(cb710_slot_to_mmc(slot));
 85	unsigned long flags;
 86
 87	spin_lock_irqsave(&reader->irq_lock, flags);
 88	/* this is the only thing irq_lock protects */
 89	__cb710_mmc_enable_irq(slot, enable, mask);
 90	spin_unlock_irqrestore(&reader->irq_lock, flags);
 91}
 92
 93static void cb710_mmc_reset_events(struct cb710_slot *slot)
 94{
 95	cb710_write_port_8(slot, CB710_MMC_STATUS0_PORT, 0xFF);
 96	cb710_write_port_8(slot, CB710_MMC_STATUS1_PORT, 0xFF);
 97	cb710_write_port_8(slot, CB710_MMC_STATUS2_PORT, 0xFF);
 98}
 99
100static void cb710_mmc_enable_4bit_data(struct cb710_slot *slot, int enable)
101{
102	if (enable)
103		cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT,
104			CB710_MMC_C1_4BIT_DATA_BUS, 0);
105	else
106		cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT,
107			0, CB710_MMC_C1_4BIT_DATA_BUS);
108}
109
110static int cb710_check_event(struct cb710_slot *slot, u8 what)
111{
112	u16 status;
113
114	status = cb710_read_port_16(slot, CB710_MMC_STATUS_PORT);
115
116	if (status & CB710_MMC_S0_FIFO_UNDERFLOW) {
117		/* it is just a guess, so log it */
118		dev_dbg(cb710_slot_dev(slot),
119			"CHECK : ignoring bit 6 in status %04X\n", status);
120		cb710_write_port_8(slot, CB710_MMC_STATUS0_PORT,
121			CB710_MMC_S0_FIFO_UNDERFLOW);
122		status &= ~CB710_MMC_S0_FIFO_UNDERFLOW;
123	}
124
125	if (status & CB710_MMC_STATUS_ERROR_EVENTS) {
126		dev_dbg(cb710_slot_dev(slot),
127			"CHECK : returning EIO on status %04X\n", status);
128		cb710_write_port_8(slot, CB710_MMC_STATUS0_PORT, status & 0xFF);
129		cb710_write_port_8(slot, CB710_MMC_STATUS1_PORT,
130			CB710_MMC_S1_RESET);
131		return -EIO;
132	}
133
134	/* 'what' is a bit in MMC_STATUS1 */
135	if ((status >> 8) & what) {
136		cb710_write_port_8(slot, CB710_MMC_STATUS1_PORT, what);
137		return 1;
138	}
139
140	return 0;
141}
142
143static int cb710_wait_for_event(struct cb710_slot *slot, u8 what)
144{
145	int err = 0;
146	unsigned limit = 2000000;	/* FIXME: real timeout */
147
148#ifdef CONFIG_CB710_DEBUG
149	u32 e, x;
150	e = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT);
151#endif
152
153	while (!(err = cb710_check_event(slot, what))) {
154		if (!--limit) {
155			cb710_dump_regs(cb710_slot_to_chip(slot),
156				CB710_DUMP_REGS_MMC);
157			err = -ETIMEDOUT;
158			break;
159		}
160		udelay(1);
161	}
162
163#ifdef CONFIG_CB710_DEBUG
164	x = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT);
165
166	limit = 2000000 - limit;
167	if (limit > 100)
168		dev_dbg(cb710_slot_dev(slot),
169			"WAIT10: waited %d loops, what %d, entry val %08X, exit val %08X\n",
170			limit, what, e, x);
171#endif
172	return err < 0 ? err : 0;
173}
174
175
176static int cb710_wait_while_busy(struct cb710_slot *slot, uint8_t mask)
177{
178	unsigned limit = 500000;	/* FIXME: real timeout */
179	int err = 0;
180
181#ifdef CONFIG_CB710_DEBUG
182	u32 e, x;
183	e = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT);
184#endif
185
186	while (cb710_read_port_8(slot, CB710_MMC_STATUS2_PORT) & mask) {
187		if (!--limit) {
188			cb710_dump_regs(cb710_slot_to_chip(slot),
189				CB710_DUMP_REGS_MMC);
190			err = -ETIMEDOUT;
191			break;
192		}
193		udelay(1);
194	}
195
196#ifdef CONFIG_CB710_DEBUG
197	x = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT);
198
199	limit = 500000 - limit;
200	if (limit > 100)
201		dev_dbg(cb710_slot_dev(slot),
202			"WAIT12: waited %d loops, mask %02X, entry val %08X, exit val %08X\n",
203			limit, mask, e, x);
204#endif
205	return err;
206}
207
208static void cb710_mmc_set_transfer_size(struct cb710_slot *slot,
209	size_t count, size_t blocksize)
210{
211	cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
212	cb710_write_port_32(slot, CB710_MMC_TRANSFER_SIZE_PORT,
213		((count - 1) << 16)|(blocksize - 1));
214
215	dev_vdbg(cb710_slot_dev(slot), "set up for %zu block%s of %zu bytes\n",
216		count, count == 1 ? "" : "s", blocksize);
217}
218
219static void cb710_mmc_fifo_hack(struct cb710_slot *slot)
220{
221	/* without this, received data is prepended with 8-bytes of zeroes */
222	u32 r1, r2;
223	int ok = 0;
224
225	r1 = cb710_read_port_32(slot, CB710_MMC_DATA_PORT);
226	r2 = cb710_read_port_32(slot, CB710_MMC_DATA_PORT);
227	if (cb710_read_port_8(slot, CB710_MMC_STATUS0_PORT)
228	    & CB710_MMC_S0_FIFO_UNDERFLOW) {
229		cb710_write_port_8(slot, CB710_MMC_STATUS0_PORT,
230			CB710_MMC_S0_FIFO_UNDERFLOW);
231		ok = 1;
232	}
233
234	dev_dbg(cb710_slot_dev(slot),
235		"FIFO-read-hack: expected STATUS0 bit was %s\n",
236		ok ? "set." : "NOT SET!");
237	dev_dbg(cb710_slot_dev(slot),
238		"FIFO-read-hack: dwords ignored: %08X %08X - %s\n",
239		r1, r2, (r1|r2) ? "BAD (NOT ZERO)!" : "ok");
240}
241
242static int cb710_mmc_receive_pio(struct cb710_slot *slot,
243	struct sg_mapping_iter *miter, size_t dw_count)
244{
245	if (!(cb710_read_port_8(slot, CB710_MMC_STATUS2_PORT) & CB710_MMC_S2_FIFO_READY)) {
246		int err = cb710_wait_for_event(slot,
247			CB710_MMC_S1_PIO_TRANSFER_DONE);
248		if (err)
249			return err;
250	}
251
252	cb710_sg_dwiter_write_from_io(miter,
253		slot->iobase + CB710_MMC_DATA_PORT, dw_count);
254
255	return 0;
256}
257
258static bool cb710_is_transfer_size_supported(struct mmc_data *data)
259{
260	return !(data->blksz & 15 && (data->blocks != 1 || data->blksz != 8));
261}
262
263static int cb710_mmc_receive(struct cb710_slot *slot, struct mmc_data *data)
264{
265	struct sg_mapping_iter miter;
266	size_t len, blocks = data->blocks;
267	int err = 0;
268
269	/* TODO: I don't know how/if the hardware handles non-16B-boundary blocks
270	 * except single 8B block */
271	if (unlikely(data->blksz & 15 && (data->blocks != 1 || data->blksz != 8)))
272		return -EINVAL;
273
274	sg_miter_start(&miter, data->sg, data->sg_len, SG_MITER_TO_SG);
275
276	cb710_modify_port_8(slot, CB710_MMC_CONFIG2_PORT,
277		15, CB710_MMC_C2_READ_PIO_SIZE_MASK);
278
279	cb710_mmc_fifo_hack(slot);
280
281	while (blocks-- > 0) {
282		len = data->blksz;
283
284		while (len >= 16) {
285			err = cb710_mmc_receive_pio(slot, &miter, 4);
286			if (err)
287				goto out;
288			len -= 16;
289		}
290
291		if (!len)
292			continue;
293
294		cb710_modify_port_8(slot, CB710_MMC_CONFIG2_PORT,
295			len - 1, CB710_MMC_C2_READ_PIO_SIZE_MASK);
296
297		len = (len >= 8) ? 4 : 2;
298		err = cb710_mmc_receive_pio(slot, &miter, len);
299		if (err)
300			goto out;
301	}
302out:
303	sg_miter_stop(&miter);
304	return err;
305}
306
307static int cb710_mmc_send(struct cb710_slot *slot, struct mmc_data *data)
308{
309	struct sg_mapping_iter miter;
310	size_t len, blocks = data->blocks;
311	int err = 0;
312
313	/* TODO: I don't know how/if the hardware handles multiple
314	 * non-16B-boundary blocks */
315	if (unlikely(data->blocks > 1 && data->blksz & 15))
316		return -EINVAL;
317
318	sg_miter_start(&miter, data->sg, data->sg_len, SG_MITER_FROM_SG);
319
320	cb710_modify_port_8(slot, CB710_MMC_CONFIG2_PORT,
321		0, CB710_MMC_C2_READ_PIO_SIZE_MASK);
322
323	while (blocks-- > 0) {
324		len = (data->blksz + 15) >> 4;
325		do {
326			if (!(cb710_read_port_8(slot, CB710_MMC_STATUS2_PORT)
327			    & CB710_MMC_S2_FIFO_EMPTY)) {
328				err = cb710_wait_for_event(slot,
329					CB710_MMC_S1_PIO_TRANSFER_DONE);
330				if (err)
331					goto out;
332			}
333			cb710_sg_dwiter_read_to_io(&miter,
334				slot->iobase + CB710_MMC_DATA_PORT, 4);
335		} while (--len);
336	}
337out:
338	sg_miter_stop(&miter);
339	return err;
340}
341
342static u16 cb710_encode_cmd_flags(struct cb710_mmc_reader *reader,
343	struct mmc_command *cmd)
344{
345	unsigned int flags = cmd->flags;
346	u16 cb_flags = 0;
347
348	/* Windows driver returned 0 for commands for which no response
349	 * is expected. It happened that there were only two such commands
350	 * used: MMC_GO_IDLE_STATE and MMC_GO_INACTIVE_STATE so it might
351	 * as well be a bug in that driver.
352	 *
353	 * Original driver set bit 14 for MMC/SD application
354	 * commands. There's no difference 'on the wire' and
355	 * it apparently works without it anyway.
356	 */
357
358	switch (flags & MMC_CMD_MASK) {
359	case MMC_CMD_AC:	cb_flags = CB710_MMC_CMD_AC;	break;
360	case MMC_CMD_ADTC:	cb_flags = CB710_MMC_CMD_ADTC;	break;
361	case MMC_CMD_BC:	cb_flags = CB710_MMC_CMD_BC;	break;
362	case MMC_CMD_BCR:	cb_flags = CB710_MMC_CMD_BCR;	break;
363	}
364
365	if (flags & MMC_RSP_BUSY)
366		cb_flags |= CB710_MMC_RSP_BUSY;
367
368	cb_flags |= cmd->opcode << CB710_MMC_CMD_CODE_SHIFT;
369
370	if (cmd->data && (cmd->data->flags & MMC_DATA_READ))
371		cb_flags |= CB710_MMC_DATA_READ;
372
373	if (flags & MMC_RSP_PRESENT) {
374		/* Windows driver set 01 at bits 4,3 except for
375		 * MMC_SET_BLOCKLEN where it set 10. Maybe the
376		 * hardware can do something special about this
377		 * command? The original driver looks buggy/incomplete
378		 * anyway so we ignore this for now.
379		 *
380		 * I assume that 00 here means no response is expected.
381		 */
382		cb_flags |= CB710_MMC_RSP_PRESENT;
383
384		if (flags & MMC_RSP_136)
385			cb_flags |= CB710_MMC_RSP_136;
386		if (!(flags & MMC_RSP_CRC))
387			cb_flags |= CB710_MMC_RSP_NO_CRC;
388	}
389
390	return cb_flags;
391}
392
393static void cb710_receive_response(struct cb710_slot *slot,
394	struct mmc_command *cmd)
395{
396	unsigned rsp_opcode, wanted_opcode;
397
398	/* Looks like final byte with CRC is always stripped (same as SDHCI) */
399	if (cmd->flags & MMC_RSP_136) {
400		u32 resp[4];
401
402		resp[0] = cb710_read_port_32(slot, CB710_MMC_RESPONSE3_PORT);
403		resp[1] = cb710_read_port_32(slot, CB710_MMC_RESPONSE2_PORT);
404		resp[2] = cb710_read_port_32(slot, CB710_MMC_RESPONSE1_PORT);
405		resp[3] = cb710_read_port_32(slot, CB710_MMC_RESPONSE0_PORT);
406		rsp_opcode = resp[0] >> 24;
407
408		cmd->resp[0] = (resp[0] << 8)|(resp[1] >> 24);
409		cmd->resp[1] = (resp[1] << 8)|(resp[2] >> 24);
410		cmd->resp[2] = (resp[2] << 8)|(resp[3] >> 24);
411		cmd->resp[3] = (resp[3] << 8);
412	} else {
413		rsp_opcode = cb710_read_port_32(slot, CB710_MMC_RESPONSE1_PORT) & 0x3F;
414		cmd->resp[0] = cb710_read_port_32(slot, CB710_MMC_RESPONSE0_PORT);
415	}
416
417	wanted_opcode = (cmd->flags & MMC_RSP_OPCODE) ? cmd->opcode : 0x3F;
418	if (rsp_opcode != wanted_opcode)
419		cmd->error = -EILSEQ;
420}
421
422static int cb710_mmc_transfer_data(struct cb710_slot *slot,
423	struct mmc_data *data)
424{
425	int error, to;
426
427	if (data->flags & MMC_DATA_READ)
428		error = cb710_mmc_receive(slot, data);
429	else
430		error = cb710_mmc_send(slot, data);
431
432	to = cb710_wait_for_event(slot, CB710_MMC_S1_DATA_TRANSFER_DONE);
433	if (!error)
434		error = to;
435
436	if (!error)
437		data->bytes_xfered = data->blksz * data->blocks;
438	return error;
439}
440
441static int cb710_mmc_command(struct mmc_host *mmc, struct mmc_command *cmd)
442{
443	struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
444	struct cb710_mmc_reader *reader = mmc_priv(mmc);
445	struct mmc_data *data = cmd->data;
446
447	u16 cb_cmd = cb710_encode_cmd_flags(reader, cmd);
448	dev_dbg(cb710_slot_dev(slot), "cmd request: 0x%04X\n", cb_cmd);
449
450	if (data) {
451		if (!cb710_is_transfer_size_supported(data)) {
452			data->error = -EINVAL;
453			return -1;
454		}
455		cb710_mmc_set_transfer_size(slot, data->blocks, data->blksz);
456	}
457
458	cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20|CB710_MMC_S2_BUSY_10);
459	cb710_write_port_16(slot, CB710_MMC_CMD_TYPE_PORT, cb_cmd);
460	cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
461	cb710_write_port_32(slot, CB710_MMC_CMD_PARAM_PORT, cmd->arg);
462	cb710_mmc_reset_events(slot);
463	cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
464	cb710_modify_port_8(slot, CB710_MMC_CONFIG0_PORT, 0x01, 0);
465
466	cmd->error = cb710_wait_for_event(slot, CB710_MMC_S1_COMMAND_SENT);
467	if (cmd->error)
468		return -1;
469
470	if (cmd->flags & MMC_RSP_PRESENT) {
471		cb710_receive_response(slot, cmd);
472		if (cmd->error)
473			return -1;
474	}
475
476	if (data)
477		data->error = cb710_mmc_transfer_data(slot, data);
478	return 0;
479}
480
481static void cb710_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
482{
483	struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
484	struct cb710_mmc_reader *reader = mmc_priv(mmc);
485
486	WARN_ON(reader->mrq != NULL);
487
488	reader->mrq = mrq;
489	cb710_mmc_enable_irq(slot, CB710_MMC_IE_TEST_MASK, 0);
490
491	if (!cb710_mmc_command(mmc, mrq->cmd) && mrq->stop)
492		cb710_mmc_command(mmc, mrq->stop);
493
494	tasklet_schedule(&reader->finish_req_tasklet);
495}
496
497static int cb710_mmc_powerup(struct cb710_slot *slot)
498{
499#ifdef CONFIG_CB710_DEBUG
500	struct cb710_chip *chip = cb710_slot_to_chip(slot);
501#endif
502	int err;
503
504	/* a lot of magic for now */
505	dev_dbg(cb710_slot_dev(slot), "bus powerup\n");
506	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
507	err = cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
508	if (unlikely(err))
509		return err;
510	cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0x80, 0);
511	cb710_modify_port_8(slot, CB710_MMC_CONFIG3_PORT, 0x80, 0);
512	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
513	mdelay(1);
514	dev_dbg(cb710_slot_dev(slot), "after delay 1\n");
515	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
516	err = cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
517	if (unlikely(err))
518		return err;
519	cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0x09, 0);
520	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
521	mdelay(1);
522	dev_dbg(cb710_slot_dev(slot), "after delay 2\n");
523	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
524	err = cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
525	if (unlikely(err))
526		return err;
527	cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0, 0x08);
528	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
529	mdelay(2);
530	dev_dbg(cb710_slot_dev(slot), "after delay 3\n");
531	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
532	cb710_modify_port_8(slot, CB710_MMC_CONFIG0_PORT, 0x06, 0);
533	cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0x70, 0);
534	cb710_modify_port_8(slot, CB710_MMC_CONFIG2_PORT, 0x80, 0);
535	cb710_modify_port_8(slot, CB710_MMC_CONFIG3_PORT, 0x03, 0);
536	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
537	err = cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20);
538	if (unlikely(err))
539		return err;
540	/* This port behaves weird: quick byte reads of 0x08,0x09 return
541	 * 0xFF,0x00 after writing 0xFFFF to 0x08; it works correctly when
542	 * read/written from userspace...  What am I missing here?
543	 * (it doesn't depend on write-to-read delay) */
544	cb710_write_port_16(slot, CB710_MMC_CONFIGB_PORT, 0xFFFF);
545	cb710_modify_port_8(slot, CB710_MMC_CONFIG0_PORT, 0x06, 0);
546	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
547	dev_dbg(cb710_slot_dev(slot), "bus powerup finished\n");
548
549	return cb710_check_event(slot, 0);
550}
551
552static void cb710_mmc_powerdown(struct cb710_slot *slot)
553{
554	cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0, 0x81);
555	cb710_modify_port_8(slot, CB710_MMC_CONFIG3_PORT, 0, 0x80);
556}
557
558static void cb710_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
559{
560	struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
561	struct cb710_mmc_reader *reader = mmc_priv(mmc);
562	int err;
563
564	cb710_mmc_select_clock_divider(mmc, ios->clock);
565
566	if (ios->power_mode != reader->last_power_mode) {
567		switch (ios->power_mode) {
568		case MMC_POWER_ON:
 
 
 
 
 
 
569			err = cb710_mmc_powerup(slot);
570			if (err) {
571				dev_warn(cb710_slot_dev(slot),
572					"powerup failed (%d)- retrying\n", err);
573				cb710_mmc_powerdown(slot);
574				udelay(1);
575				err = cb710_mmc_powerup(slot);
576				if (err)
577					dev_warn(cb710_slot_dev(slot),
578						"powerup retry failed (%d) - expect errors\n",
579					err);
580			}
581			reader->last_power_mode = MMC_POWER_ON;
582			break;
583		case MMC_POWER_OFF:
584			cb710_mmc_powerdown(slot);
585			reader->last_power_mode = MMC_POWER_OFF;
586			break;
587		case MMC_POWER_UP:
588		default:
589			/* ignore */
590			break;
591		}
 
 
 
 
 
 
 
 
 
592	}
593
594	cb710_mmc_enable_4bit_data(slot, ios->bus_width != MMC_BUS_WIDTH_1);
595
596	cb710_mmc_enable_irq(slot, CB710_MMC_IE_TEST_MASK, 0);
597}
598
599static int cb710_mmc_get_ro(struct mmc_host *mmc)
600{
601	struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
602
603	return cb710_read_port_8(slot, CB710_MMC_STATUS3_PORT)
604		& CB710_MMC_S3_WRITE_PROTECTED;
605}
606
607static int cb710_mmc_get_cd(struct mmc_host *mmc)
608{
609	struct cb710_slot *slot = cb710_mmc_to_slot(mmc);
610
611	return cb710_read_port_8(slot, CB710_MMC_STATUS3_PORT)
612		& CB710_MMC_S3_CARD_DETECTED;
613}
614
615static int cb710_mmc_irq_handler(struct cb710_slot *slot)
616{
617	struct mmc_host *mmc = cb710_slot_to_mmc(slot);
618	struct cb710_mmc_reader *reader = mmc_priv(mmc);
619	u32 status, config1, config2, irqen;
620
621	status = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT);
622	irqen = cb710_read_port_32(slot, CB710_MMC_IRQ_ENABLE_PORT);
623	config2 = cb710_read_port_32(slot, CB710_MMC_CONFIGB_PORT);
624	config1 = cb710_read_port_32(slot, CB710_MMC_CONFIG_PORT);
625
626	dev_dbg(cb710_slot_dev(slot), "interrupt; status: %08X, "
627		"ie: %08X, c2: %08X, c1: %08X\n",
628		status, irqen, config2, config1);
629
630	if (status & (CB710_MMC_S1_CARD_CHANGED << 8)) {
631		/* ack the event */
632		cb710_write_port_8(slot, CB710_MMC_STATUS1_PORT,
633			CB710_MMC_S1_CARD_CHANGED);
634		if ((irqen & CB710_MMC_IE_CISTATUS_MASK)
635		    == CB710_MMC_IE_CISTATUS_MASK)
636			mmc_detect_change(mmc, HZ/5);
637	} else {
638		dev_dbg(cb710_slot_dev(slot), "unknown interrupt (test)\n");
639		spin_lock(&reader->irq_lock);
640		__cb710_mmc_enable_irq(slot, 0, CB710_MMC_IE_TEST_MASK);
641		spin_unlock(&reader->irq_lock);
642	}
643
644	return 1;
645}
646
647static void cb710_mmc_finish_request_tasklet(unsigned long data)
648{
649	struct mmc_host *mmc = (void *)data;
650	struct cb710_mmc_reader *reader = mmc_priv(mmc);
651	struct mmc_request *mrq = reader->mrq;
652
653	reader->mrq = NULL;
654	mmc_request_done(mmc, mrq);
655}
656
657static const struct mmc_host_ops cb710_mmc_host = {
658	.request = cb710_mmc_request,
659	.set_ios = cb710_mmc_set_ios,
660	.get_ro = cb710_mmc_get_ro,
661	.get_cd = cb710_mmc_get_cd,
662};
663
664#ifdef CONFIG_PM
665
666static int cb710_mmc_suspend(struct platform_device *pdev, pm_message_t state)
667{
668	struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
 
 
 
 
 
 
669
670	cb710_mmc_enable_irq(slot, 0, ~0);
671	return 0;
672}
673
674static int cb710_mmc_resume(struct platform_device *pdev)
675{
676	struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
 
677
678	cb710_mmc_enable_irq(slot, 0, ~0);
679	return 0;
 
680}
681
682#endif /* CONFIG_PM */
683
684static int cb710_mmc_init(struct platform_device *pdev)
685{
686	struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
687	struct cb710_chip *chip = cb710_slot_to_chip(slot);
688	struct mmc_host *mmc;
689	struct cb710_mmc_reader *reader;
690	int err;
691	u32 val;
692
693	mmc = mmc_alloc_host(sizeof(*reader), cb710_slot_dev(slot));
694	if (!mmc)
695		return -ENOMEM;
696
697	platform_set_drvdata(pdev, mmc);
698
699	/* harmless (maybe) magic */
700	pci_read_config_dword(chip->pdev, 0x48, &val);
701	val = cb710_src_freq_mhz[(val >> 16) & 0xF];
702	dev_dbg(cb710_slot_dev(slot), "source frequency: %dMHz\n", val);
703	val *= 1000000;
704
705	mmc->ops = &cb710_mmc_host;
706	mmc->f_max = val;
707	mmc->f_min = val >> cb710_clock_divider_log2[CB710_MAX_DIVIDER_IDX];
708	mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
709	mmc->caps = MMC_CAP_4_BIT_DATA;
710
711	reader = mmc_priv(mmc);
712
713	tasklet_init(&reader->finish_req_tasklet,
714		cb710_mmc_finish_request_tasklet, (unsigned long)mmc);
715	spin_lock_init(&reader->irq_lock);
716	cb710_dump_regs(chip, CB710_DUMP_REGS_MMC);
717
718	cb710_mmc_enable_irq(slot, 0, ~0);
719	cb710_set_irq_handler(slot, cb710_mmc_irq_handler);
720
721	err = mmc_add_host(mmc);
722	if (unlikely(err))
723		goto err_free_mmc;
724
725	dev_dbg(cb710_slot_dev(slot), "mmc_hostname is %s\n",
726		mmc_hostname(mmc));
727
728	cb710_mmc_enable_irq(slot, CB710_MMC_IE_CARD_INSERTION_STATUS, 0);
729
730	return 0;
731
732err_free_mmc:
733	dev_dbg(cb710_slot_dev(slot), "mmc_add_host() failed: %d\n", err);
734
735	cb710_set_irq_handler(slot, NULL);
736	mmc_free_host(mmc);
737	return err;
738}
739
740static int cb710_mmc_exit(struct platform_device *pdev)
741{
742	struct cb710_slot *slot = cb710_pdev_to_slot(pdev);
743	struct mmc_host *mmc = cb710_slot_to_mmc(slot);
744	struct cb710_mmc_reader *reader = mmc_priv(mmc);
745
746	cb710_mmc_enable_irq(slot, 0, CB710_MMC_IE_CARD_INSERTION_STATUS);
747
748	mmc_remove_host(mmc);
749
750	/* IRQs should be disabled now, but let's stay on the safe side */
751	cb710_mmc_enable_irq(slot, 0, ~0);
752	cb710_set_irq_handler(slot, NULL);
753
754	/* clear config ports - just in case */
755	cb710_write_port_32(slot, CB710_MMC_CONFIG_PORT, 0);
756	cb710_write_port_16(slot, CB710_MMC_CONFIGB_PORT, 0);
757
758	tasklet_kill(&reader->finish_req_tasklet);
759
760	mmc_free_host(mmc);
761	return 0;
762}
763
764static struct platform_driver cb710_mmc_driver = {
765	.driver.name = "cb710-mmc",
766	.probe = cb710_mmc_init,
767	.remove = cb710_mmc_exit,
768#ifdef CONFIG_PM
769	.suspend = cb710_mmc_suspend,
770	.resume = cb710_mmc_resume,
771#endif
772};
773
774module_platform_driver(cb710_mmc_driver);
775
776MODULE_AUTHOR("Michał Mirosław <mirq-linux@rere.qmqm.pl>");
777MODULE_DESCRIPTION("ENE CB710 memory card reader driver - MMC/SD part");
778MODULE_LICENSE("GPL");
779MODULE_ALIAS("platform:cb710-mmc");