2 * Silicon Labs C2 port core Linux support
4 * Copyright (c) 2007 Rodolfo Giometti <giometti@linux.it>
5 * Copyright (c) 2007 Eurotech S.p.A. <info@eurotech.it>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/device.h>
15 #include <linux/errno.h>
16 #include <linux/err.h>
17 #include <linux/kernel.h>
18 #include <linux/kmemcheck.h>
19 #include <linux/ctype.h>
20 #include <linux/delay.h>
21 #include <linux/idr.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
25 #include <linux/c2port.h>
27 #define DRIVER_NAME "c2port"
28 #define DRIVER_VERSION "0.51.0"
30 static DEFINE_SPINLOCK(c2port_idr_lock);
31 static DEFINE_IDR(c2port_idr);
37 static struct class *c2port_class;
40 * C2 registers & commands defines
44 #define C2PORT_DEVICEID 0x00
45 #define C2PORT_REVID 0x01
46 #define C2PORT_FPCTL 0x02
47 #define C2PORT_FPDAT 0xB4
49 /* C2 interface commands */
50 #define C2PORT_GET_VERSION 0x01
51 #define C2PORT_DEVICE_ERASE 0x03
52 #define C2PORT_BLOCK_READ 0x06
53 #define C2PORT_BLOCK_WRITE 0x07
54 #define C2PORT_PAGE_ERASE 0x08
56 /* C2 status return codes */
57 #define C2PORT_INVALID_COMMAND 0x00
58 #define C2PORT_COMMAND_FAILED 0x02
59 #define C2PORT_COMMAND_OK 0x0d
62 * C2 port low level signal managements
65 static void c2port_reset(struct c2port_device *dev)
67 struct c2port_ops *ops = dev->ops;
69 /* To reset the device we have to keep clock line low for at least
73 ops->c2ck_set(dev, 0);
75 ops->c2ck_set(dev, 1);
81 static void c2port_strobe_ck(struct c2port_device *dev)
83 struct c2port_ops *ops = dev->ops;
85 /* During hi-low-hi transition we disable local IRQs to avoid
86 * interructions since C2 port specification says that it must be
87 * shorter than 5us, otherwise the microcontroller may consider
88 * it as a reset signal!
91 ops->c2ck_set(dev, 0);
93 ops->c2ck_set(dev, 1);
100 * C2 port basic functions
103 static void c2port_write_ar(struct c2port_device *dev, u8 addr)
105 struct c2port_ops *ops = dev->ops;
109 c2port_strobe_ck(dev);
111 /* INS field (11b, LSB first) */
112 ops->c2d_dir(dev, 0);
113 ops->c2d_set(dev, 1);
114 c2port_strobe_ck(dev);
115 ops->c2d_set(dev, 1);
116 c2port_strobe_ck(dev);
119 for (i = 0; i < 8; i++) {
120 ops->c2d_set(dev, addr & 0x01);
121 c2port_strobe_ck(dev);
127 ops->c2d_dir(dev, 1);
128 c2port_strobe_ck(dev);
131 static int c2port_read_ar(struct c2port_device *dev, u8 *addr)
133 struct c2port_ops *ops = dev->ops;
137 c2port_strobe_ck(dev);
139 /* INS field (10b, LSB first) */
140 ops->c2d_dir(dev, 0);
141 ops->c2d_set(dev, 0);
142 c2port_strobe_ck(dev);
143 ops->c2d_set(dev, 1);
144 c2port_strobe_ck(dev);
147 ops->c2d_dir(dev, 1);
149 for (i = 0; i < 8; i++) {
150 *addr >>= 1; /* shift in 8-bit ADDRESS field LSB first */
152 c2port_strobe_ck(dev);
153 if (ops->c2d_get(dev))
158 c2port_strobe_ck(dev);
163 static int c2port_write_dr(struct c2port_device *dev, u8 data)
165 struct c2port_ops *ops = dev->ops;
169 c2port_strobe_ck(dev);
171 /* INS field (01b, LSB first) */
172 ops->c2d_dir(dev, 0);
173 ops->c2d_set(dev, 1);
174 c2port_strobe_ck(dev);
175 ops->c2d_set(dev, 0);
176 c2port_strobe_ck(dev);
178 /* LENGTH field (00b, LSB first -> 1 byte) */
179 ops->c2d_set(dev, 0);
180 c2port_strobe_ck(dev);
181 ops->c2d_set(dev, 0);
182 c2port_strobe_ck(dev);
185 for (i = 0; i < 8; i++) {
186 ops->c2d_set(dev, data & 0x01);
187 c2port_strobe_ck(dev);
193 ops->c2d_dir(dev, 1);
196 c2port_strobe_ck(dev);
197 if (ops->c2d_get(dev))
201 } while (--timeout > 0);
206 c2port_strobe_ck(dev);
211 static int c2port_read_dr(struct c2port_device *dev, u8 *data)
213 struct c2port_ops *ops = dev->ops;
217 c2port_strobe_ck(dev);
219 /* INS field (00b, LSB first) */
220 ops->c2d_dir(dev, 0);
221 ops->c2d_set(dev, 0);
222 c2port_strobe_ck(dev);
223 ops->c2d_set(dev, 0);
224 c2port_strobe_ck(dev);
226 /* LENGTH field (00b, LSB first -> 1 byte) */
227 ops->c2d_set(dev, 0);
228 c2port_strobe_ck(dev);
229 ops->c2d_set(dev, 0);
230 c2port_strobe_ck(dev);
233 ops->c2d_dir(dev, 1);
236 c2port_strobe_ck(dev);
237 if (ops->c2d_get(dev))
241 } while (--timeout > 0);
247 for (i = 0; i < 8; i++) {
248 *data >>= 1; /* shift in 8-bit DATA field LSB first */
250 c2port_strobe_ck(dev);
251 if (ops->c2d_get(dev))
256 c2port_strobe_ck(dev);
261 static int c2port_poll_in_busy(struct c2port_device *dev)
264 int ret, timeout = 20;
267 ret = (c2port_read_ar(dev, &addr));
275 } while (--timeout > 0);
282 static int c2port_poll_out_ready(struct c2port_device *dev)
285 int ret, timeout = 10000; /* erase flash needs long time... */
288 ret = (c2port_read_ar(dev, &addr));
296 } while (--timeout > 0);
307 static ssize_t c2port_show_name(struct device *dev,
308 struct device_attribute *attr, char *buf)
310 struct c2port_device *c2dev = dev_get_drvdata(dev);
312 return sprintf(buf, "%s\n", c2dev->name);
314 static DEVICE_ATTR(name, 0444, c2port_show_name, NULL);
316 static ssize_t c2port_show_flash_blocks_num(struct device *dev,
317 struct device_attribute *attr, char *buf)
319 struct c2port_device *c2dev = dev_get_drvdata(dev);
320 struct c2port_ops *ops = c2dev->ops;
322 return sprintf(buf, "%d\n", ops->blocks_num);
324 static DEVICE_ATTR(flash_blocks_num, 0444, c2port_show_flash_blocks_num, NULL);
326 static ssize_t c2port_show_flash_block_size(struct device *dev,
327 struct device_attribute *attr, char *buf)
329 struct c2port_device *c2dev = dev_get_drvdata(dev);
330 struct c2port_ops *ops = c2dev->ops;
332 return sprintf(buf, "%d\n", ops->block_size);
334 static DEVICE_ATTR(flash_block_size, 0444, c2port_show_flash_block_size, NULL);
336 static ssize_t c2port_show_flash_size(struct device *dev,
337 struct device_attribute *attr, char *buf)
339 struct c2port_device *c2dev = dev_get_drvdata(dev);
340 struct c2port_ops *ops = c2dev->ops;
342 return sprintf(buf, "%d\n", ops->blocks_num * ops->block_size);
344 static DEVICE_ATTR(flash_size, 0444, c2port_show_flash_size, NULL);
346 static ssize_t access_show(struct device *dev, struct device_attribute *attr,
349 struct c2port_device *c2dev = dev_get_drvdata(dev);
351 return sprintf(buf, "%d\n", c2dev->access);
354 static ssize_t access_store(struct device *dev, struct device_attribute *attr,
355 const char *buf, size_t count)
357 struct c2port_device *c2dev = dev_get_drvdata(dev);
358 struct c2port_ops *ops = c2dev->ops;
361 ret = sscanf(buf, "%d", &status);
365 mutex_lock(&c2dev->mutex);
367 c2dev->access = !!status;
369 /* If access is "on" clock should be HIGH _before_ setting the line
370 * as output and data line should be set as INPUT anyway */
372 ops->c2ck_set(c2dev, 1);
373 ops->access(c2dev, c2dev->access);
375 ops->c2d_dir(c2dev, 1);
377 mutex_unlock(&c2dev->mutex);
381 static DEVICE_ATTR_RW(access);
383 static ssize_t c2port_store_reset(struct device *dev,
384 struct device_attribute *attr,
385 const char *buf, size_t count)
387 struct c2port_device *c2dev = dev_get_drvdata(dev);
389 /* Check the device access status */
393 mutex_lock(&c2dev->mutex);
396 c2dev->flash_access = 0;
398 mutex_unlock(&c2dev->mutex);
402 static DEVICE_ATTR(reset, 0200, NULL, c2port_store_reset);
404 static ssize_t __c2port_show_dev_id(struct c2port_device *dev, char *buf)
409 /* Select DEVICEID register for C2 data register accesses */
410 c2port_write_ar(dev, C2PORT_DEVICEID);
412 /* Read and return the device ID register */
413 ret = c2port_read_dr(dev, &data);
417 return sprintf(buf, "%d\n", data);
420 static ssize_t c2port_show_dev_id(struct device *dev,
421 struct device_attribute *attr, char *buf)
423 struct c2port_device *c2dev = dev_get_drvdata(dev);
426 /* Check the device access status */
430 mutex_lock(&c2dev->mutex);
431 ret = __c2port_show_dev_id(c2dev, buf);
432 mutex_unlock(&c2dev->mutex);
435 dev_err(dev, "cannot read from %s\n", c2dev->name);
439 static DEVICE_ATTR(dev_id, 0444, c2port_show_dev_id, NULL);
441 static ssize_t __c2port_show_rev_id(struct c2port_device *dev, char *buf)
446 /* Select REVID register for C2 data register accesses */
447 c2port_write_ar(dev, C2PORT_REVID);
449 /* Read and return the revision ID register */
450 ret = c2port_read_dr(dev, &data);
454 return sprintf(buf, "%d\n", data);
457 static ssize_t c2port_show_rev_id(struct device *dev,
458 struct device_attribute *attr, char *buf)
460 struct c2port_device *c2dev = dev_get_drvdata(dev);
463 /* Check the device access status */
467 mutex_lock(&c2dev->mutex);
468 ret = __c2port_show_rev_id(c2dev, buf);
469 mutex_unlock(&c2dev->mutex);
472 dev_err(c2dev->dev, "cannot read from %s\n", c2dev->name);
476 static DEVICE_ATTR(rev_id, 0444, c2port_show_rev_id, NULL);
478 static ssize_t c2port_show_flash_access(struct device *dev,
479 struct device_attribute *attr, char *buf)
481 struct c2port_device *c2dev = dev_get_drvdata(dev);
483 return sprintf(buf, "%d\n", c2dev->flash_access);
486 static ssize_t __c2port_store_flash_access(struct c2port_device *dev,
491 /* Check the device access status */
495 dev->flash_access = !!status;
497 /* If flash_access is off we have nothing to do... */
498 if (dev->flash_access == 0)
501 /* Target the C2 flash programming control register for C2 data
503 c2port_write_ar(dev, C2PORT_FPCTL);
505 /* Write the first keycode to enable C2 Flash programming */
506 ret = c2port_write_dr(dev, 0x02);
510 /* Write the second keycode to enable C2 Flash programming */
511 ret = c2port_write_dr(dev, 0x01);
515 /* Delay for at least 20ms to ensure the target is ready for
516 * C2 flash programming */
522 static ssize_t c2port_store_flash_access(struct device *dev,
523 struct device_attribute *attr,
524 const char *buf, size_t count)
526 struct c2port_device *c2dev = dev_get_drvdata(dev);
530 ret = sscanf(buf, "%d", &status);
534 mutex_lock(&c2dev->mutex);
535 ret = __c2port_store_flash_access(c2dev, status);
536 mutex_unlock(&c2dev->mutex);
539 dev_err(c2dev->dev, "cannot enable %s flash programming\n",
546 static DEVICE_ATTR(flash_access, 0644, c2port_show_flash_access,
547 c2port_store_flash_access);
549 static ssize_t __c2port_write_flash_erase(struct c2port_device *dev)
554 /* Target the C2 flash programming data register for C2 data register
557 c2port_write_ar(dev, C2PORT_FPDAT);
559 /* Send device erase command */
560 c2port_write_dr(dev, C2PORT_DEVICE_ERASE);
562 /* Wait for input acknowledge */
563 ret = c2port_poll_in_busy(dev);
567 /* Should check status before starting FLASH access sequence */
569 /* Wait for status information */
570 ret = c2port_poll_out_ready(dev);
574 /* Read flash programming interface status */
575 ret = c2port_read_dr(dev, &status);
578 if (status != C2PORT_COMMAND_OK)
581 /* Send a three-byte arming sequence to enable the device erase.
582 * If the sequence is not received correctly, the command will be
584 * Sequence is: 0xde, 0xad, 0xa5.
586 c2port_write_dr(dev, 0xde);
587 ret = c2port_poll_in_busy(dev);
590 c2port_write_dr(dev, 0xad);
591 ret = c2port_poll_in_busy(dev);
594 c2port_write_dr(dev, 0xa5);
595 ret = c2port_poll_in_busy(dev);
599 ret = c2port_poll_out_ready(dev);
606 static ssize_t c2port_store_flash_erase(struct device *dev,
607 struct device_attribute *attr,
608 const char *buf, size_t count)
610 struct c2port_device *c2dev = dev_get_drvdata(dev);
613 /* Check the device and flash access status */
614 if (!c2dev->access || !c2dev->flash_access)
617 mutex_lock(&c2dev->mutex);
618 ret = __c2port_write_flash_erase(c2dev);
619 mutex_unlock(&c2dev->mutex);
622 dev_err(c2dev->dev, "cannot erase %s flash\n", c2dev->name);
628 static DEVICE_ATTR(flash_erase, 0200, NULL, c2port_store_flash_erase);
630 static ssize_t __c2port_read_flash_data(struct c2port_device *dev,
631 char *buffer, loff_t offset, size_t count)
633 struct c2port_ops *ops = dev->ops;
634 u8 status, nread = 128;
637 /* Check for flash end */
638 if (offset >= ops->block_size * ops->blocks_num)
641 if (ops->block_size * ops->blocks_num - offset < nread)
642 nread = ops->block_size * ops->blocks_num - offset;
648 /* Target the C2 flash programming data register for C2 data register
650 c2port_write_ar(dev, C2PORT_FPDAT);
652 /* Send flash block read command */
653 c2port_write_dr(dev, C2PORT_BLOCK_READ);
655 /* Wait for input acknowledge */
656 ret = c2port_poll_in_busy(dev);
660 /* Should check status before starting FLASH access sequence */
662 /* Wait for status information */
663 ret = c2port_poll_out_ready(dev);
667 /* Read flash programming interface status */
668 ret = c2port_read_dr(dev, &status);
671 if (status != C2PORT_COMMAND_OK)
674 /* Send address high byte */
675 c2port_write_dr(dev, offset >> 8);
676 ret = c2port_poll_in_busy(dev);
680 /* Send address low byte */
681 c2port_write_dr(dev, offset & 0x00ff);
682 ret = c2port_poll_in_busy(dev);
686 /* Send address block size */
687 c2port_write_dr(dev, nread);
688 ret = c2port_poll_in_busy(dev);
692 /* Should check status before reading FLASH block */
694 /* Wait for status information */
695 ret = c2port_poll_out_ready(dev);
699 /* Read flash programming interface status */
700 ret = c2port_read_dr(dev, &status);
703 if (status != C2PORT_COMMAND_OK)
706 /* Read flash block */
707 for (i = 0; i < nread; i++) {
708 ret = c2port_poll_out_ready(dev);
712 ret = c2port_read_dr(dev, buffer+i);
720 static ssize_t c2port_read_flash_data(struct file *filp, struct kobject *kobj,
721 struct bin_attribute *attr,
722 char *buffer, loff_t offset, size_t count)
724 struct c2port_device *c2dev =
725 dev_get_drvdata(container_of(kobj,
726 struct device, kobj));
729 /* Check the device and flash access status */
730 if (!c2dev->access || !c2dev->flash_access)
733 mutex_lock(&c2dev->mutex);
734 ret = __c2port_read_flash_data(c2dev, buffer, offset, count);
735 mutex_unlock(&c2dev->mutex);
738 dev_err(c2dev->dev, "cannot read %s flash\n", c2dev->name);
743 static ssize_t __c2port_write_flash_data(struct c2port_device *dev,
744 char *buffer, loff_t offset, size_t count)
746 struct c2port_ops *ops = dev->ops;
747 u8 status, nwrite = 128;
752 if (ops->block_size * ops->blocks_num - offset < nwrite)
753 nwrite = ops->block_size * ops->blocks_num - offset;
755 /* Check for flash end */
756 if (offset >= ops->block_size * ops->blocks_num)
759 /* Target the C2 flash programming data register for C2 data register
761 c2port_write_ar(dev, C2PORT_FPDAT);
763 /* Send flash block write command */
764 c2port_write_dr(dev, C2PORT_BLOCK_WRITE);
766 /* Wait for input acknowledge */
767 ret = c2port_poll_in_busy(dev);
771 /* Should check status before starting FLASH access sequence */
773 /* Wait for status information */
774 ret = c2port_poll_out_ready(dev);
778 /* Read flash programming interface status */
779 ret = c2port_read_dr(dev, &status);
782 if (status != C2PORT_COMMAND_OK)
785 /* Send address high byte */
786 c2port_write_dr(dev, offset >> 8);
787 ret = c2port_poll_in_busy(dev);
791 /* Send address low byte */
792 c2port_write_dr(dev, offset & 0x00ff);
793 ret = c2port_poll_in_busy(dev);
797 /* Send address block size */
798 c2port_write_dr(dev, nwrite);
799 ret = c2port_poll_in_busy(dev);
803 /* Should check status before writing FLASH block */
805 /* Wait for status information */
806 ret = c2port_poll_out_ready(dev);
810 /* Read flash programming interface status */
811 ret = c2port_read_dr(dev, &status);
814 if (status != C2PORT_COMMAND_OK)
817 /* Write flash block */
818 for (i = 0; i < nwrite; i++) {
819 ret = c2port_write_dr(dev, *(buffer+i));
823 ret = c2port_poll_in_busy(dev);
829 /* Wait for last flash write to complete */
830 ret = c2port_poll_out_ready(dev);
837 static ssize_t c2port_write_flash_data(struct file *filp, struct kobject *kobj,
838 struct bin_attribute *attr,
839 char *buffer, loff_t offset, size_t count)
841 struct c2port_device *c2dev =
842 dev_get_drvdata(container_of(kobj,
843 struct device, kobj));
846 /* Check the device access status */
847 if (!c2dev->access || !c2dev->flash_access)
850 mutex_lock(&c2dev->mutex);
851 ret = __c2port_write_flash_data(c2dev, buffer, offset, count);
852 mutex_unlock(&c2dev->mutex);
855 dev_err(c2dev->dev, "cannot write %s flash\n", c2dev->name);
859 /* size is computed at run-time */
860 static BIN_ATTR(flash_data, 0644, c2port_read_flash_data,
861 c2port_write_flash_data, 0);
866 static struct attribute *c2port_attrs[] = {
868 &dev_attr_flash_blocks_num.attr,
869 &dev_attr_flash_block_size.attr,
870 &dev_attr_flash_size.attr,
871 &dev_attr_access.attr,
872 &dev_attr_reset.attr,
873 &dev_attr_dev_id.attr,
874 &dev_attr_rev_id.attr,
875 &dev_attr_flash_access.attr,
876 &dev_attr_flash_erase.attr,
880 static struct bin_attribute *c2port_bin_attrs[] = {
881 &bin_attr_flash_data,
885 static const struct attribute_group c2port_group = {
886 .attrs = c2port_attrs,
887 .bin_attrs = c2port_bin_attrs,
890 static const struct attribute_group *c2port_groups[] = {
899 struct c2port_device *c2port_device_register(char *name,
900 struct c2port_ops *ops, void *devdata)
902 struct c2port_device *c2dev;
905 if (unlikely(!ops) || unlikely(!ops->access) || \
906 unlikely(!ops->c2d_dir) || unlikely(!ops->c2ck_set) || \
907 unlikely(!ops->c2d_get) || unlikely(!ops->c2d_set))
908 return ERR_PTR(-EINVAL);
910 c2dev = kmalloc(sizeof(struct c2port_device), GFP_KERNEL);
911 kmemcheck_annotate_bitfield(c2dev, flags);
912 if (unlikely(!c2dev))
913 return ERR_PTR(-ENOMEM);
915 idr_preload(GFP_KERNEL);
916 spin_lock_irq(&c2port_idr_lock);
917 ret = idr_alloc(&c2port_idr, c2dev, 0, 0, GFP_NOWAIT);
918 spin_unlock_irq(&c2port_idr_lock);
922 goto error_idr_alloc;
925 bin_attr_flash_data.size = ops->blocks_num * ops->block_size;
927 c2dev->dev = device_create(c2port_class, NULL, 0, c2dev,
928 "c2port%d", c2dev->id);
929 if (unlikely(IS_ERR(c2dev->dev))) {
930 ret = PTR_ERR(c2dev->dev);
931 goto error_device_create;
933 dev_set_drvdata(c2dev->dev, c2dev);
935 strncpy(c2dev->name, name, C2PORT_NAME_LEN);
937 mutex_init(&c2dev->mutex);
939 /* By default C2 port access is off */
940 c2dev->access = c2dev->flash_access = 0;
941 ops->access(c2dev, 0);
943 dev_info(c2dev->dev, "C2 port %s added\n", name);
944 dev_info(c2dev->dev, "%s flash has %d blocks x %d bytes "
945 "(%d bytes total)\n",
946 name, ops->blocks_num, ops->block_size,
947 ops->blocks_num * ops->block_size);
952 spin_lock_irq(&c2port_idr_lock);
953 idr_remove(&c2port_idr, c2dev->id);
954 spin_unlock_irq(&c2port_idr_lock);
961 EXPORT_SYMBOL(c2port_device_register);
963 void c2port_device_unregister(struct c2port_device *c2dev)
968 dev_info(c2dev->dev, "C2 port %s removed\n", c2dev->name);
970 spin_lock_irq(&c2port_idr_lock);
971 idr_remove(&c2port_idr, c2dev->id);
972 spin_unlock_irq(&c2port_idr_lock);
974 device_destroy(c2port_class, c2dev->id);
978 EXPORT_SYMBOL(c2port_device_unregister);
984 static int __init c2port_init(void)
986 printk(KERN_INFO "Silicon Labs C2 port support v. " DRIVER_VERSION
987 " - (C) 2007 Rodolfo Giometti\n");
989 c2port_class = class_create(THIS_MODULE, "c2port");
990 if (IS_ERR(c2port_class)) {
991 printk(KERN_ERR "c2port: failed to allocate class\n");
992 return PTR_ERR(c2port_class);
994 c2port_class->dev_groups = c2port_groups;
999 static void __exit c2port_exit(void)
1001 class_destroy(c2port_class);
1004 module_init(c2port_init);
1005 module_exit(c2port_exit);
1007 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1008 MODULE_DESCRIPTION("Silicon Labs C2 port support v. " DRIVER_VERSION);
1009 MODULE_LICENSE("GPL");