1 /* Generic Philips CL RC632 Routines
3 * (C) Harald Welte <laforge@gnumonks.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
27 #include <rfid/rfid.h>
28 #include <rfid/rfid_asic.h>
29 #include <rfid/rfid_asic_rc632.h>
30 #include <rfid/rfid_reader_cm5121.h>
31 #include <rfid/rfid_layer2_iso14443a.h>
33 #include "rfid_iso14443_common.h"
35 //#include "rc632_14443a.h"
38 #define ENTER() DEBUGP("entering\n")
39 struct rfid_asic rc632;
41 /* Register and FIFO Access functions */
43 rc632_reg_write(struct rfid_asic_handle *handle,
47 return handle->rath->rat->priv.rc632.fn.reg_write(handle->rath, reg, val);
51 rc632_reg_read(struct rfid_asic_handle *handle,
55 return handle->rath->rat->priv.rc632.fn.reg_read(handle->rath, reg, val);
59 rc632_fifo_write(struct rfid_asic_handle *handle,
61 const unsigned char *buf,
64 return handle->rath->rat->priv.rc632.fn.fifo_write(handle->rath,
69 rc632_fifo_read(struct rfid_asic_handle *handle,
73 return handle->rath->rat->priv.rc632.fn.fifo_read(handle->rath, len, buf);
78 rc632_set_bits(struct rfid_asic_handle *handle,
85 ret = rc632_reg_read(handle, reg, &tmp);
89 /* if bits are already set, no need to set them again */
90 if ((tmp & val) == val)
93 return rc632_reg_write(handle, reg, (tmp|val)&0xff);
97 rc632_clear_bits(struct rfid_asic_handle *handle,
104 ret = rc632_reg_read(handle, reg, &tmp);
106 DEBUGP("error during reg_read(%p, %d):%d\n",
110 /* if bits are already cleared, no need to clear them again */
111 if ((tmp & val) == 0)
114 return rc632_reg_write(handle, reg, (tmp & ~val)&0xff);
120 rc632_turn_on_rf(struct rfid_asic_handle *handle)
123 return rc632_set_bits(handle, RC632_REG_TX_CONTROL, 0x03);
127 rc632_turn_off_rf(struct rfid_asic_handle *handle)
130 return rc632_clear_bits(handle, RC632_REG_TX_CONTROL, 0x03);
134 rc632_power_up(struct rfid_asic_handle *handle)
137 return rc632_clear_bits(handle, RC632_REG_CONTROL,
138 RC632_CONTROL_POWERDOWN);
142 rc632_power_down(struct rfid_asic_handle *handle)
144 return rc632_set_bits(handle, RC632_REG_CONTROL,
145 RC632_CONTROL_POWERDOWN);
148 /* Stupid RC623 implementations don't evaluate interrupts but poll the
149 * command register for "status idle" */
151 rc632_wait_idle(struct rfid_asic_handle *handle, unsigned int time)
153 unsigned char cmd = 0xff;
157 ret = rc632_reg_read(handle, RC632_REG_COMMAND, &cmd);
162 /* FIXME: read second time ?? */
168 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &foo);
170 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &foo);
175 /* Fixme: Abort after some timeout */
182 rc632_transmit(struct rfid_asic_handle *handle,
183 const unsigned char *buf,
185 unsigned int timeout)
189 ret = rc632_fifo_write(handle, len, buf, 0x03);
193 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_TRANSMIT);
197 return rc632_wait_idle(handle, timeout);
201 tcl_toggle_pcb(struct rfid_asic_handle *handle)
203 // FIXME: toggle something between 0x0a and 0x0b
208 rc632_transcieve(struct rfid_asic_handle *handle,
209 const unsigned char *tx_buf,
210 unsigned char tx_len,
211 unsigned char *rx_buf,
212 unsigned char *rx_len,
218 ret = rc632_fifo_write(handle, tx_len, tx_buf, 0x03);
222 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_TRANSCIEVE);
227 tcl_toggle_pcb(handle);
229 ret = rc632_wait_idle(handle, timer);
233 ret = rc632_reg_read(handle, RC632_REG_FIFO_LENGTH, rx_len);
240 DEBUGP("rx_len == 0\n");
242 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &tmp);
243 rc632_reg_read(handle, RC632_REG_CHANNEL_REDUNDANCY, &tmp);
248 return rc632_fifo_read(handle, *rx_len, rx_buf);
252 rc632_read_eeprom(struct rfid_asic_handle *handle)
254 unsigned char recvbuf[60];
255 unsigned char sndbuf[3];
262 ret = rc632_fifo_write(handle, 3, sndbuf, 0x03);
266 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_READ_E2);
272 ret = rc632_fifo_read(handle, sizeof(recvbuf), recvbuf);
276 // FIXME: do something with eeprom contents
281 rc632_calc_crc16_from(struct rfid_asic_handle *handle)
283 unsigned char sndbuf[2] = { 0x01, 0x02 };
284 unsigned char crc_lsb = 0x00 , crc_msb = 0x00;
287 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0x12);
291 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0xe0);
295 ret = rc632_fifo_write(handle, sizeof(sndbuf), sndbuf, 3);
299 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_CALC_CRC);
303 usleep(10000); // FIXME: no checking for cmd completion?
305 ret = rc632_reg_read(handle, RC632_REG_CRC_RESULT_LSB, &crc_lsb);
309 ret = rc632_reg_read(handle, RC632_REG_CRC_RESULT_MSB, &crc_msb);
313 // FIXME: what to do with crc result?
319 rc632_register_dump(struct rfid_asic_handle *handle, unsigned char *buf)
324 for (i = 0; i <= 0x3f; i++) {
325 ret = rc632_reg_read(handle, i, &buf[i]);
326 // do we want error checks?
333 /* generic FIFO access functions (if no more efficient ones provided by
334 * transport driver) */
339 // FIXME: implementation (not needed for CM 5121)
346 // FIXME: implementation (not neded for CM 5121)
351 rc632_init(struct rfid_asic_handle *ah)
356 ret = rc632_power_up(ah);
360 /* disable register paging */
361 ret = rc632_reg_write(ah, 0x00, 0x00);
365 /* set some sane default values */
366 ret = rc632_reg_write(ah, 0x11, 0x5b);
371 ret = rc632_turn_on_rf(ah);
379 rc632_fini(struct rfid_asic_handle *ah)
384 ret = rc632_turn_off_rf(ah);
388 ret = rc632_power_down(ah);
395 struct rfid_asic_handle *
396 rc632_open(struct rfid_asic_transport_handle *th)
398 struct rfid_asic_handle *h;
400 h = malloc(sizeof(*h));
403 memset(h, 0, sizeof(*h));
408 h->mtu = h->mru = 40; /* FIXME */
410 if (rc632_init(h) < 0) {
419 rc632_close(struct rfid_asic_handle *h)
427 * Philips CL RC632 primitives for ISO 14443-A compliant PICC's
429 * (C) 2005 by Harald Welte <laforge@gnumonks.org>
434 rc632_iso14443a_init(struct rfid_asic_handle *handle)
438 // FIXME: some fifo work (drain fifo?)
440 /* flush fifo (our way) */
441 ret = rc632_reg_write(handle, RC632_REG_CONTROL, 0x01);
443 ret = rc632_reg_write(handle, RC632_REG_TX_CONTROL,
444 (RC632_TXCTRL_TX1_RF_EN |
445 RC632_TXCTRL_TX2_RF_EN |
446 RC632_TXCTRL_TX2_INV |
447 RC632_TXCTRL_FORCE_100_ASK |
448 RC632_TXCTRL_MOD_SRC_INT));
452 ret = rc632_reg_write(handle, RC632_REG_CW_CONDUCTANCE,
453 CM5121_CW_CONDUCTANCE);
457 ret = rc632_reg_write(handle, RC632_REG_MOD_CONDUCTANCE,
458 CM5121_MOD_CONDUCTANCE);
462 ret = rc632_reg_write(handle, RC632_REG_CODER_CONTROL,
463 (RC632_CDRCTRL_TXCD_14443A |
464 RC632_CDRCTRL_RATE_106K));
468 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH, 0x13);
472 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH_SOF, 0x3f);
476 ret = rc632_reg_write(handle, RC632_REG_TYPE_B_FRAMING, 0x00);
480 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL1,
481 (RC632_RXCTRL1_GAIN_35DB |
482 RC632_RXCTRL1_ISO14443 |
483 RC632_RXCTRL1_SUBCP_8));
487 ret = rc632_reg_write(handle, RC632_REG_DECODER_CONTROL,
488 (RC632_DECCTRL_MANCHESTER |
489 RC632_DECCTRL_RXFR_14443A));
493 ret = rc632_reg_write(handle, RC632_REG_BIT_PHASE,
494 CM5121_14443A_BITPHASE);
498 ret = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
499 CM5121_14443A_THRESHOLD);
503 ret = rc632_reg_write(handle, RC632_REG_BPSK_DEM_CONTROL, 0x00);
507 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL2,
508 (RC632_RXCTRL2_DECSRC_INT |
509 RC632_RXCTRL2_CLK_Q));
513 ret = rc632_reg_write(handle, RC632_REG_RX_WAIT, 0x03);
517 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
518 (RC632_CR_PARITY_ENABLE |
519 RC632_CR_PARITY_ODD));
523 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0x63);
527 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0x63);
535 rc632_iso14443a_fini(struct iso14443a_handle *handle_14443)
539 ret = rc632_turn_off_rf(handle);
549 /* issue a 14443-3 A PCD -> PICC command in a short frame, such as REQA, WUPA */
551 rc632_iso14443a_transcieve_sf(struct rfid_asic_handle *handle,
553 struct iso14443a_atqa *atqa)
556 unsigned char tx_buf[1];
557 unsigned char rx_len = 2;
559 memset(atqa, 0, sizeof(atqa));
563 /* transfer only 7 bits of last byte in frame */
564 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x07);
569 ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
570 RC632_CONTROL_CRYPTO1_ON);
575 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
576 (RC632_CR_PARITY_ENABLE |
577 RC632_CR_PARITY_ODD));
579 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
580 RC632_CR_TX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
586 ret = rc632_transcieve(handle, tx_buf, sizeof(tx_buf),
587 (unsigned char *)atqa, &rx_len, 0x32, 0);
589 DEBUGP("error during rc632_transcieve()\n");
593 /* switch back to normal 8bit last byte */
594 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x00);
599 DEBUGP("rx_len(%d) != 2\n", rx_len);
606 /* trasncieve regular frame */
608 rc632_iso14443a_transcieve(struct rfid_asic_handle *handle,
609 const unsigned char *tx_buf, unsigned int tx_len,
610 unsigned char *rx_buf, unsigned int *rx_len,
611 unsigned int timeout, unsigned int flags)
614 unsigned char rxl = *rx_len & 0xff;
616 memset(rx_buf, 0, *rx_len);
619 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
620 (RC632_CR_PARITY_ENABLE |
621 RC632_CR_PARITY_ODD |
622 RC632_CR_TX_CRC_ENABLE |
623 RC632_CR_RX_CRC_ENABLE));
625 ret = rc632_set_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
626 RC632_CR_TX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
630 ret = rc632_transcieve(handle, tx_buf, tx_len, rx_buf, &rxl, 0x32, 0);
639 /* transcieve anti collission bitframe */
641 rc632_iso14443a_transcieve_acf(struct rfid_asic_handle *handle,
642 struct iso14443a_anticol_cmd *acf,
643 unsigned int *bit_of_col)
646 unsigned char rx_buf[64];
647 unsigned char rx_len = sizeof(rx_buf);
648 unsigned char rx_align = 0, tx_last_bits, tx_bytes;
650 unsigned char error_flag;
651 *bit_of_col = ISO14443A_BITOFCOL_NONE;
652 memset(rx_buf, 0, sizeof(rx_buf));
654 /* disable mifare cryto */
655 ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
656 RC632_CONTROL_CRYPTO1_ON);
660 /* disable CRC summing */
662 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
663 (RC632_CR_PARITY_ENABLE |
664 RC632_CR_PARITY_ODD));
666 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
667 RC632_CR_TX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
672 tx_last_bits = acf->nvb & 0x0f; /* lower nibble indicates bits */
673 tx_bytes = acf->nvb >> 4;
676 rx_align = (tx_last_bits+1) % 8;/* rx frame complements tx */
679 //rx_align = 8 - tx_last_bits;/* rx frame complements tx */
681 /* set RxAlign and TxLastBits*/
682 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING,
683 (rx_align << 4) | (tx_last_bits));
687 ret = rc632_transcieve(handle, (unsigned char *)acf, tx_bytes,
688 rx_buf, &rx_len, 0x32, 0);
692 /* bitwise-OR the two halves of the split byte */
693 acf->uid_bits[tx_bytes-2] = (
694 (acf->uid_bits[tx_bytes-2] & (0xff >> (8-tx_last_bits)))
697 memcpy(&acf->uid_bits[tx_bytes+1-2], &rx_buf[1], rx_len-1);
699 /* determine whether there was a collission */
700 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
704 if (error_flag & RC632_ERR_FLAG_COL_ERR) {
705 /* retrieve bit of collission */
706 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
710 /* bit of collission relative to start of part 1 of
711 * anticollision frame (!) */
712 *bit_of_col = 2*8 + boc;
718 static int rc632_iso14443b_init(struct rfid_asic_handle *handle)
722 // FIXME: some FIFO work
724 /* flush fifo (our way) */
725 ret = rc632_reg_write(handle, RC632_REG_CONTROL, 0x01);
729 ret = rc632_reg_write(handle, RC632_REG_TX_CONTROL,
730 (RC632_TXCTRL_TX1_RF_EN |
731 RC632_TXCTRL_TX2_RF_EN |
732 RC632_TXCTRL_TX2_INV |
733 RC632_TXCTRL_MOD_SRC_INT));
737 ret = rc632_reg_write(handle, RC632_REG_CW_CONDUCTANCE, 0x3f);
741 ret = rc632_reg_write(handle, RC632_REG_MOD_CONDUCTANCE, 0x04);
745 ret = rc632_reg_write(handle, RC632_REG_CODER_CONTROL,
746 (RC632_CDRCTRL_TXCD_NRZ |
747 RC632_CDRCTRL_RATE_14443B));
751 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH, 0x13);
755 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH_SOF, 0x3f);
759 ret = rc632_reg_write(handle, RC632_REG_TYPE_B_FRAMING,
760 (RC632_TBFRAMING_SOF_11L_3H |
761 (6 << RC632_TBFRAMING_SPACE_SHIFT) |
762 RC632_TBFRAMING_EOF_11));
766 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL1,
767 (RC632_RXCTRL1_GAIN_35DB |
768 RC632_RXCTRL1_ISO14443 |
769 RC632_RXCTRL1_SUBCP_8));
773 ret = rc632_reg_write(handle, RC632_REG_DECODER_CONTROL,
774 (RC632_DECCTRL_BPSK |
775 RC632_DECCTRL_RXFR_14443B));
779 ret = rc632_reg_write(handle, RC632_REG_BIT_PHASE,
780 CM5121_14443B_BITPHASE);
784 ret = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
785 CM5121_14443B_THRESHOLD);
789 ret = rc632_reg_write(handle, RC632_REG_BPSK_DEM_CONTROL,
790 ((0x2 & RC632_BPSKD_TAUB_MASK)<<RC632_BPSKD_TAUB_SHIFT |
791 (0x3 & RC632_BPSKD_TAUD_MASK)<<RC632_BPSKD_TAUD_SHIFT |
792 RC632_BPSKD_FILTER_AMP_DETECT |
793 RC632_BPSKD_NO_RX_EOF |
794 RC632_BPSKD_NO_RX_EGT));
798 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL2,
799 (RC632_RXCTRL2_AUTO_PD |
800 RC632_RXCTRL2_DECSRC_INT));
804 ret = rc632_reg_write(handle, RC632_REG_RX_WAIT, 0x03);
808 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
809 (RC632_CR_TX_CRC_ENABLE |
810 RC632_CR_RX_CRC_ENABLE |
815 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0xff);
819 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0xff);
827 struct rfid_asic rc632 = {
828 .name = "Philips CL RC632",
829 .fc = ISO14443_FREQ_CARRIER,
831 .fn.power_up = &rc632_power_up,
832 .fn.power_down = &rc632_power_down,
833 .fn.turn_on_rf = &rc632_turn_on_rf,
834 .fn.turn_off_rf = &rc632_turn_off_rf,
835 .fn.transcieve = &rc632_iso14443a_transcieve,
837 .init = &rc632_iso14443a_init,
838 .transcieve_sf = &rc632_iso14443a_transcieve_sf,
839 .transcieve_acf = &rc632_iso14443a_transcieve_acf,
842 .init = &rc632_iso14443b_init,