1 /* Generic Philips CL RC632 Routines
3 * (C) Harald Welte <laforge@gnumonks.org>
5 * Licensed under GNU General Public License, Version 2
11 #include <sys/types.h>
13 #include <rfid/rfid.h>
14 #include <rfid/rfid_asic.h>
15 #include <rfid/rfid_asic_rc632.h>
16 #include <rfid/rfid_reader_cm5121.h>
17 #include <rfid/rfid_layer2_iso14443a.h>
19 #include "rfid_iso14443_common.h"
21 //#include "rc632_14443a.h"
24 #define ENTER() DEBUGP("entering\n")
25 struct rfid_asic rc632;
27 /* Register and FIFO Access functions */
29 rc632_reg_write(struct rfid_asic_handle *handle,
33 return handle->rath->rat->priv.rc632.fn.reg_write(handle->rath, reg, val);
37 rc632_reg_read(struct rfid_asic_handle *handle,
41 return handle->rath->rat->priv.rc632.fn.reg_read(handle->rath, reg, val);
45 rc632_fifo_write(struct rfid_asic_handle *handle,
47 const unsigned char *buf,
50 return handle->rath->rat->priv.rc632.fn.fifo_write(handle->rath,
55 rc632_fifo_read(struct rfid_asic_handle *handle,
59 return handle->rath->rat->priv.rc632.fn.fifo_read(handle->rath, len, buf);
64 rc632_set_bits(struct rfid_asic_handle *handle,
71 ret = rc632_reg_read(handle, reg, &tmp);
75 /* if bits are already set, no need to set them again */
76 if ((tmp & val) == val)
79 return rc632_reg_write(handle, reg, (tmp|val)&0xff);
83 rc632_clear_bits(struct rfid_asic_handle *handle,
90 ret = rc632_reg_read(handle, reg, &tmp);
92 DEBUGP("error during reg_read(%p, %d):%d\n",
96 /* if bits are already cleared, no need to clear them again */
100 return rc632_reg_write(handle, reg, (tmp & ~val)&0xff);
106 rc632_turn_on_rf(struct rfid_asic_handle *handle)
109 return rc632_set_bits(handle, RC632_REG_TX_CONTROL, 0x03);
113 rc632_turn_off_rf(struct rfid_asic_handle *handle)
116 return rc632_clear_bits(handle, RC632_REG_TX_CONTROL, 0x03);
120 rc632_power_up(struct rfid_asic_handle *handle)
123 return rc632_clear_bits(handle, RC632_REG_CONTROL,
124 RC632_CONTROL_POWERDOWN);
128 rc632_power_down(struct rfid_asic_handle *handle)
130 return rc632_set_bits(handle, RC632_REG_CONTROL,
131 RC632_CONTROL_POWERDOWN);
134 /* Stupid RC623 implementations don't evaluate interrupts but poll the
135 * command register for "status idle" */
137 rc632_wait_idle(struct rfid_asic_handle *handle, unsigned int time)
139 unsigned char cmd = 0xff;
143 ret = rc632_reg_read(handle, RC632_REG_COMMAND, &cmd);
148 /* FIXME: read second time ?? */
154 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &foo);
156 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &foo);
161 /* Fixme: Abort after some timeout */
168 rc632_transmit(struct rfid_asic_handle *handle,
169 const unsigned char *buf,
171 unsigned int timeout)
175 ret = rc632_fifo_write(handle, len, buf, 0x03);
179 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_TRANSMIT);
183 return rc632_wait_idle(handle, timeout);
187 tcl_toggle_pcb(struct rfid_asic_handle *handle)
189 // FIXME: toggle something between 0x0a and 0x0b
194 rc632_transcieve(struct rfid_asic_handle *handle,
195 const unsigned char *tx_buf,
196 unsigned char tx_len,
197 unsigned char *rx_buf,
198 unsigned char *rx_len,
204 ret = rc632_fifo_write(handle, tx_len, tx_buf, 0x03);
208 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_TRANSCIEVE);
213 tcl_toggle_pcb(handle);
215 ret = rc632_wait_idle(handle, timer);
219 ret = rc632_reg_read(handle, RC632_REG_FIFO_LENGTH, rx_len);
226 DEBUGP("rx_len == 0\n");
228 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &tmp);
229 rc632_reg_read(handle, RC632_REG_CHANNEL_REDUNDANCY, &tmp);
234 return rc632_fifo_read(handle, *rx_len, rx_buf);
238 rc632_read_eeprom(struct rfid_asic_handle *handle)
240 unsigned char recvbuf[60];
241 unsigned char sndbuf[3];
248 ret = rc632_fifo_write(handle, 3, sndbuf, 0x03);
252 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_READ_E2);
258 ret = rc632_fifo_read(handle, sizeof(recvbuf), recvbuf);
262 // FIXME: do something with eeprom contents
267 rc632_calc_crc16_from(struct rfid_asic_handle *handle)
269 unsigned char sndbuf[2] = { 0x01, 0x02 };
270 unsigned char crc_lsb = 0x00 , crc_msb = 0x00;
273 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0x12);
277 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0xe0);
281 ret = rc632_fifo_write(handle, sizeof(sndbuf), sndbuf, 3);
285 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_CALC_CRC);
289 usleep(10000); // FIXME: no checking for cmd completion?
291 ret = rc632_reg_read(handle, RC632_REG_CRC_RESULT_LSB, &crc_lsb);
295 ret = rc632_reg_read(handle, RC632_REG_CRC_RESULT_MSB, &crc_msb);
299 // FIXME: what to do with crc result?
305 rc632_register_dump(struct rfid_asic_handle *handle, unsigned char *buf)
310 for (i = 0; i <= 0x3f; i++) {
311 ret = rc632_reg_read(handle, i, &buf[i]);
312 // do we want error checks?
319 /* generic FIFO access functions (if no more efficient ones provided by
320 * transport driver) */
325 // FIXME: implementation (not needed for CM 5121)
332 // FIXME: implementation (not neded for CM 5121)
337 rc632_init(struct rfid_asic_handle *ah)
342 ret = rc632_power_up(ah);
346 /* disable register paging */
347 ret = rc632_reg_write(ah, 0x00, 0x00);
351 /* set some sane default values */
352 ret = rc632_reg_write(ah, 0x11, 0x5b);
357 ret = rc632_turn_on_rf(ah);
365 rc632_fini(struct rfid_asic_handle *ah)
370 ret = rc632_turn_off_rf(ah);
374 ret = rc632_power_down(ah);
381 struct rfid_asic_handle *
382 rc632_open(struct rfid_asic_transport_handle *th)
384 struct rfid_asic_handle *h;
386 h = malloc(sizeof(*h));
389 memset(h, 0, sizeof(*h));
394 h->mtu = h->mru = 40; /* FIXME */
396 if (rc632_init(h) < 0) {
405 rc632_close(struct rfid_asic_handle *h)
413 * Philips CL RC632 primitives for ISO 14443-A compliant PICC's
415 * (C) 2005 by Harald Welte <laforge@gnumonks.org>
420 rc632_iso14443a_init(struct rfid_asic_handle *handle)
424 // FIXME: some fifo work (drain fifo?)
426 /* flush fifo (our way) */
427 ret = rc632_reg_write(handle, RC632_REG_CONTROL, 0x01);
429 ret = rc632_reg_write(handle, RC632_REG_TX_CONTROL,
430 (RC632_TXCTRL_TX1_RF_EN |
431 RC632_TXCTRL_TX2_RF_EN |
432 RC632_TXCTRL_TX2_INV |
433 RC632_TXCTRL_FORCE_100_ASK |
434 RC632_TXCTRL_MOD_SRC_INT));
438 ret = rc632_reg_write(handle, RC632_REG_CW_CONDUCTANCE,
439 CM5121_CW_CONDUCTANCE);
443 ret = rc632_reg_write(handle, RC632_REG_MOD_CONDUCTANCE,
444 CM5121_MOD_CONDUCTANCE);
448 ret = rc632_reg_write(handle, RC632_REG_CODER_CONTROL,
449 (RC632_CDRCTRL_TXCD_14443A |
450 RC632_CDRCTRL_RATE_106K));
454 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH, 0x13);
458 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH_SOF, 0x3f);
462 ret = rc632_reg_write(handle, RC632_REG_TYPE_B_FRAMING, 0x00);
466 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL1,
467 (RC632_RXCTRL1_GAIN_35DB |
468 RC632_RXCTRL1_ISO14443 |
469 RC632_RXCTRL1_SUBCP_8));
473 ret = rc632_reg_write(handle, RC632_REG_DECODER_CONTROL,
474 (RC632_DECCTRL_MANCHESTER |
475 RC632_DECCTRL_RXFR_14443A));
479 ret = rc632_reg_write(handle, RC632_REG_BIT_PHASE,
480 CM5121_14443A_BITPHASE);
484 ret = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
485 CM5121_14443A_THRESHOLD);
489 ret = rc632_reg_write(handle, RC632_REG_BPSK_DEM_CONTROL, 0x00);
493 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL2,
494 (RC632_RXCTRL2_DECSRC_INT |
495 RC632_RXCTRL2_CLK_Q));
499 ret = rc632_reg_write(handle, RC632_REG_RX_WAIT, 0x03);
503 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
504 (RC632_CR_PARITY_ENABLE |
505 RC632_CR_PARITY_ODD));
509 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0x63);
513 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0x63);
521 rc632_iso14443a_fini(struct iso14443a_handle *handle_14443)
525 ret = rc632_turn_off_rf(handle);
535 /* issue a 14443-3 A PCD -> PICC command in a short frame, such as REQA, WUPA */
537 rc632_iso14443a_transcieve_sf(struct rfid_asic_handle *handle,
539 struct iso14443a_atqa *atqa)
542 unsigned char tx_buf[1];
543 unsigned char rx_len = 2;
545 memset(atqa, 0, sizeof(atqa));
549 /* transfer only 7 bits of last byte in frame */
550 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x07);
555 ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
556 RC632_CONTROL_CRYPTO1_ON);
561 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
562 (RC632_CR_PARITY_ENABLE |
563 RC632_CR_PARITY_ODD));
565 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
566 RC632_CR_TX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
572 ret = rc632_transcieve(handle, tx_buf, sizeof(tx_buf),
573 (unsigned char *)atqa, &rx_len, 0x32, 0);
575 DEBUGP("error during rc632_transcieve()\n");
579 /* switch back to normal 8bit last byte */
580 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x00);
585 DEBUGP("rx_len(%d) != 2\n", rx_len);
592 /* trasncieve regular frame */
594 rc632_iso14443a_transcieve(struct rfid_asic_handle *handle,
595 const unsigned char *tx_buf, unsigned int tx_len,
596 unsigned char *rx_buf, unsigned int *rx_len,
597 unsigned int timeout, unsigned int flags)
600 unsigned char rxl = *rx_len & 0xff;
602 memset(rx_buf, 0, *rx_len);
605 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
606 (RC632_CR_PARITY_ENABLE |
607 RC632_CR_PARITY_ODD |
608 RC632_CR_TX_CRC_ENABLE |
609 RC632_CR_RX_CRC_ENABLE));
611 ret = rc632_set_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
612 RC632_CR_TX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
616 ret = rc632_transcieve(handle, tx_buf, tx_len, rx_buf, &rxl, 0x32, 0);
625 /* transcieve anti collission bitframe */
627 rc632_iso14443a_transcieve_acf(struct rfid_asic_handle *handle,
628 struct iso14443a_anticol_cmd *acf,
629 unsigned int *bit_of_col)
632 unsigned char rx_buf[64];
633 unsigned char rx_len = sizeof(rx_buf);
634 unsigned char rx_align = 0, tx_last_bits, tx_bytes;
636 unsigned char error_flag;
637 *bit_of_col = ISO14443A_BITOFCOL_NONE;
638 memset(rx_buf, 0, sizeof(rx_buf));
640 /* disable mifare cryto */
641 ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
642 RC632_CONTROL_CRYPTO1_ON);
646 /* disable CRC summing */
648 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
649 (RC632_CR_PARITY_ENABLE |
650 RC632_CR_PARITY_ODD));
652 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
653 RC632_CR_TX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
658 tx_last_bits = acf->nvb & 0x0f; /* lower nibble indicates bits */
659 tx_bytes = acf->nvb >> 4;
662 rx_align = (tx_last_bits+1) % 8;/* rx frame complements tx */
665 //rx_align = 8 - tx_last_bits;/* rx frame complements tx */
667 /* set RxAlign and TxLastBits*/
668 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING,
669 (rx_align << 4) | (tx_last_bits));
673 ret = rc632_transcieve(handle, (unsigned char *)acf, tx_bytes,
674 rx_buf, &rx_len, 0x32, 0);
678 /* bitwise-OR the two halves of the split byte */
679 acf->uid_bits[tx_bytes-2] = (
680 (acf->uid_bits[tx_bytes-2] & (0xff >> (8-tx_last_bits)))
683 memcpy(&acf->uid_bits[tx_bytes+1-2], &rx_buf[1], rx_len-1);
685 /* determine whether there was a collission */
686 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
690 if (error_flag & RC632_ERR_FLAG_COL_ERR) {
691 /* retrieve bit of collission */
692 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
696 /* bit of collission relative to start of part 1 of
697 * anticollision frame (!) */
698 *bit_of_col = 2*8 + boc;
704 static int rc632_iso14443b_init(struct rfid_asic_handle *handle)
708 // FIXME: some FIFO work
710 /* flush fifo (our way) */
711 ret = rc632_reg_write(handle, RC632_REG_CONTROL, 0x01);
715 ret = rc632_reg_write(handle, RC632_REG_TX_CONTROL,
716 (RC632_TXCTRL_TX1_RF_EN |
717 RC632_TXCTRL_TX2_RF_EN |
718 RC632_TXCTRL_TX2_INV |
719 RC632_TXCTRL_MOD_SRC_INT));
723 ret = rc632_reg_write(handle, RC632_REG_CW_CONDUCTANCE, 0x3f);
727 ret = rc632_reg_write(handle, RC632_REG_MOD_CONDUCTANCE, 0x04);
731 ret = rc632_reg_write(handle, RC632_REG_CODER_CONTROL,
732 (RC632_CDRCTRL_TXCD_NRZ |
733 RC632_CDRCTRL_RATE_14443B));
737 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH, 0x13);
741 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH_SOF, 0x3f);
745 ret = rc632_reg_write(handle, RC632_REG_TYPE_B_FRAMING,
746 (RC632_TBFRAMING_SOF_11L_3H |
747 (6 << RC632_TBFRAMING_SPACE_SHIFT) |
748 RC632_TBFRAMING_EOF_11));
752 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL1,
753 (RC632_RXCTRL1_GAIN_35DB |
754 RC632_RXCTRL1_ISO14443 |
755 RC632_RXCTRL1_SUBCP_8));
759 ret = rc632_reg_write(handle, RC632_REG_DECODER_CONTROL,
760 (RC632_DECCTRL_BPSK |
761 RC632_DECCTRL_RXFR_14443B));
765 ret = rc632_reg_write(handle, RC632_REG_BIT_PHASE,
766 CM5121_14443B_BITPHASE);
770 ret = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
771 CM5121_14443B_THRESHOLD);
775 ret = rc632_reg_write(handle, RC632_REG_BPSK_DEM_CONTROL,
776 ((0x2 & RC632_BPSKD_TAUB_MASK)<<RC632_BPSKD_TAUB_SHIFT |
777 (0x3 & RC632_BPSKD_TAUD_MASK)<<RC632_BPSKD_TAUD_SHIFT |
778 RC632_BPSKD_FILTER_AMP_DETECT |
779 RC632_BPSKD_NO_RX_EOF |
780 RC632_BPSKD_NO_RX_EGT));
784 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL2,
785 (RC632_RXCTRL2_AUTO_PD |
786 RC632_RXCTRL2_DECSRC_INT));
790 ret = rc632_reg_write(handle, RC632_REG_RX_WAIT, 0x03);
794 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
795 (RC632_CR_TX_CRC_ENABLE |
796 RC632_CR_RX_CRC_ENABLE |
801 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0xff);
805 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0xff);
813 struct rfid_asic rc632 = {
814 .name = "Philips CL RC632",
815 .fc = ISO14443_FREQ_CARRIER,
817 .fn.power_up = &rc632_power_up,
818 .fn.power_down = &rc632_power_down,
819 .fn.turn_on_rf = &rc632_turn_on_rf,
820 .fn.turn_off_rf = &rc632_turn_off_rf,
821 .fn.transcieve = &rc632_iso14443a_transcieve,
823 .init = &rc632_iso14443a_init,
824 .transcieve_sf = &rc632_iso14443a_transcieve_sf,
825 .transcieve_acf = &rc632_iso14443a_transcieve_acf,
828 .init = &rc632_iso14443b_init,