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);
315 static ssize_t c2port_show_flash_blocks_num(struct device *dev,
316 struct device_attribute *attr, char *buf)
318 struct c2port_device *c2dev = dev_get_drvdata(dev);
319 struct c2port_ops *ops = c2dev->ops;
321 return sprintf(buf, "%d\n", ops->blocks_num);
324 static ssize_t c2port_show_flash_block_size(struct device *dev,
325 struct device_attribute *attr, char *buf)
327 struct c2port_device *c2dev = dev_get_drvdata(dev);
328 struct c2port_ops *ops = c2dev->ops;
330 return sprintf(buf, "%d\n", ops->block_size);
333 static ssize_t c2port_show_flash_size(struct device *dev,
334 struct device_attribute *attr, char *buf)
336 struct c2port_device *c2dev = dev_get_drvdata(dev);
337 struct c2port_ops *ops = c2dev->ops;
339 return sprintf(buf, "%d\n", ops->blocks_num * ops->block_size);
342 static ssize_t c2port_show_access(struct device *dev,
343 struct device_attribute *attr, char *buf)
345 struct c2port_device *c2dev = dev_get_drvdata(dev);
347 return sprintf(buf, "%d\n", c2dev->access);
350 static ssize_t c2port_store_access(struct device *dev,
351 struct device_attribute *attr,
352 const char *buf, size_t count)
354 struct c2port_device *c2dev = dev_get_drvdata(dev);
355 struct c2port_ops *ops = c2dev->ops;
358 ret = sscanf(buf, "%d", &status);
362 mutex_lock(&c2dev->mutex);
364 c2dev->access = !!status;
366 /* If access is "on" clock should be HIGH _before_ setting the line
367 * as output and data line should be set as INPUT anyway */
369 ops->c2ck_set(c2dev, 1);
370 ops->access(c2dev, c2dev->access);
372 ops->c2d_dir(c2dev, 1);
374 mutex_unlock(&c2dev->mutex);
379 static ssize_t c2port_store_reset(struct device *dev,
380 struct device_attribute *attr,
381 const char *buf, size_t count)
383 struct c2port_device *c2dev = dev_get_drvdata(dev);
385 /* Check the device access status */
389 mutex_lock(&c2dev->mutex);
392 c2dev->flash_access = 0;
394 mutex_unlock(&c2dev->mutex);
399 static ssize_t __c2port_show_dev_id(struct c2port_device *dev, char *buf)
404 /* Select DEVICEID register for C2 data register accesses */
405 c2port_write_ar(dev, C2PORT_DEVICEID);
407 /* Read and return the device ID register */
408 ret = c2port_read_dr(dev, &data);
412 return sprintf(buf, "%d\n", data);
415 static ssize_t c2port_show_dev_id(struct device *dev,
416 struct device_attribute *attr, char *buf)
418 struct c2port_device *c2dev = dev_get_drvdata(dev);
421 /* Check the device access status */
425 mutex_lock(&c2dev->mutex);
426 ret = __c2port_show_dev_id(c2dev, buf);
427 mutex_unlock(&c2dev->mutex);
430 dev_err(dev, "cannot read from %s\n", c2dev->name);
435 static ssize_t __c2port_show_rev_id(struct c2port_device *dev, char *buf)
440 /* Select REVID register for C2 data register accesses */
441 c2port_write_ar(dev, C2PORT_REVID);
443 /* Read and return the revision ID register */
444 ret = c2port_read_dr(dev, &data);
448 return sprintf(buf, "%d\n", data);
451 static ssize_t c2port_show_rev_id(struct device *dev,
452 struct device_attribute *attr, char *buf)
454 struct c2port_device *c2dev = dev_get_drvdata(dev);
457 /* Check the device access status */
461 mutex_lock(&c2dev->mutex);
462 ret = __c2port_show_rev_id(c2dev, buf);
463 mutex_unlock(&c2dev->mutex);
466 dev_err(c2dev->dev, "cannot read from %s\n", c2dev->name);
471 static ssize_t c2port_show_flash_access(struct device *dev,
472 struct device_attribute *attr, char *buf)
474 struct c2port_device *c2dev = dev_get_drvdata(dev);
476 return sprintf(buf, "%d\n", c2dev->flash_access);
479 static ssize_t __c2port_store_flash_access(struct c2port_device *dev,
484 /* Check the device access status */
488 dev->flash_access = !!status;
490 /* If flash_access is off we have nothing to do... */
491 if (dev->flash_access == 0)
494 /* Target the C2 flash programming control register for C2 data
496 c2port_write_ar(dev, C2PORT_FPCTL);
498 /* Write the first keycode to enable C2 Flash programming */
499 ret = c2port_write_dr(dev, 0x02);
503 /* Write the second keycode to enable C2 Flash programming */
504 ret = c2port_write_dr(dev, 0x01);
508 /* Delay for at least 20ms to ensure the target is ready for
509 * C2 flash programming */
515 static ssize_t c2port_store_flash_access(struct device *dev,
516 struct device_attribute *attr,
517 const char *buf, size_t count)
519 struct c2port_device *c2dev = dev_get_drvdata(dev);
523 ret = sscanf(buf, "%d", &status);
527 mutex_lock(&c2dev->mutex);
528 ret = __c2port_store_flash_access(c2dev, status);
529 mutex_unlock(&c2dev->mutex);
532 dev_err(c2dev->dev, "cannot enable %s flash programming\n",
540 static ssize_t __c2port_write_flash_erase(struct c2port_device *dev)
545 /* Target the C2 flash programming data register for C2 data register
548 c2port_write_ar(dev, C2PORT_FPDAT);
550 /* Send device erase command */
551 c2port_write_dr(dev, C2PORT_DEVICE_ERASE);
553 /* Wait for input acknowledge */
554 ret = c2port_poll_in_busy(dev);
558 /* Should check status before starting FLASH access sequence */
560 /* Wait for status information */
561 ret = c2port_poll_out_ready(dev);
565 /* Read flash programming interface status */
566 ret = c2port_read_dr(dev, &status);
569 if (status != C2PORT_COMMAND_OK)
572 /* Send a three-byte arming sequence to enable the device erase.
573 * If the sequence is not received correctly, the command will be
575 * Sequence is: 0xde, 0xad, 0xa5.
577 c2port_write_dr(dev, 0xde);
578 ret = c2port_poll_in_busy(dev);
581 c2port_write_dr(dev, 0xad);
582 ret = c2port_poll_in_busy(dev);
585 c2port_write_dr(dev, 0xa5);
586 ret = c2port_poll_in_busy(dev);
590 ret = c2port_poll_out_ready(dev);
597 static ssize_t c2port_store_flash_erase(struct device *dev,
598 struct device_attribute *attr,
599 const char *buf, size_t count)
601 struct c2port_device *c2dev = dev_get_drvdata(dev);
604 /* Check the device and flash access status */
605 if (!c2dev->access || !c2dev->flash_access)
608 mutex_lock(&c2dev->mutex);
609 ret = __c2port_write_flash_erase(c2dev);
610 mutex_unlock(&c2dev->mutex);
613 dev_err(c2dev->dev, "cannot erase %s flash\n", c2dev->name);
620 static ssize_t __c2port_read_flash_data(struct c2port_device *dev,
621 char *buffer, loff_t offset, size_t count)
623 struct c2port_ops *ops = dev->ops;
624 u8 status, nread = 128;
627 /* Check for flash end */
628 if (offset >= ops->block_size * ops->blocks_num)
631 if (ops->block_size * ops->blocks_num - offset < nread)
632 nread = ops->block_size * ops->blocks_num - offset;
638 /* Target the C2 flash programming data register for C2 data register
640 c2port_write_ar(dev, C2PORT_FPDAT);
642 /* Send flash block read command */
643 c2port_write_dr(dev, C2PORT_BLOCK_READ);
645 /* Wait for input acknowledge */
646 ret = c2port_poll_in_busy(dev);
650 /* Should check status before starting FLASH access sequence */
652 /* Wait for status information */
653 ret = c2port_poll_out_ready(dev);
657 /* Read flash programming interface status */
658 ret = c2port_read_dr(dev, &status);
661 if (status != C2PORT_COMMAND_OK)
664 /* Send address high byte */
665 c2port_write_dr(dev, offset >> 8);
666 ret = c2port_poll_in_busy(dev);
670 /* Send address low byte */
671 c2port_write_dr(dev, offset & 0x00ff);
672 ret = c2port_poll_in_busy(dev);
676 /* Send address block size */
677 c2port_write_dr(dev, nread);
678 ret = c2port_poll_in_busy(dev);
682 /* Should check status before reading FLASH block */
684 /* Wait for status information */
685 ret = c2port_poll_out_ready(dev);
689 /* Read flash programming interface status */
690 ret = c2port_read_dr(dev, &status);
693 if (status != C2PORT_COMMAND_OK)
696 /* Read flash block */
697 for (i = 0; i < nread; i++) {
698 ret = c2port_poll_out_ready(dev);
702 ret = c2port_read_dr(dev, buffer+i);
710 static ssize_t c2port_read_flash_data(struct file *filp, struct kobject *kobj,
711 struct bin_attribute *attr,
712 char *buffer, loff_t offset, size_t count)
714 struct c2port_device *c2dev =
715 dev_get_drvdata(container_of(kobj,
716 struct device, kobj));
719 /* Check the device and flash access status */
720 if (!c2dev->access || !c2dev->flash_access)
723 mutex_lock(&c2dev->mutex);
724 ret = __c2port_read_flash_data(c2dev, buffer, offset, count);
725 mutex_unlock(&c2dev->mutex);
728 dev_err(c2dev->dev, "cannot read %s flash\n", c2dev->name);
733 static ssize_t __c2port_write_flash_data(struct c2port_device *dev,
734 char *buffer, loff_t offset, size_t count)
736 struct c2port_ops *ops = dev->ops;
737 u8 status, nwrite = 128;
742 if (ops->block_size * ops->blocks_num - offset < nwrite)
743 nwrite = ops->block_size * ops->blocks_num - offset;
745 /* Check for flash end */
746 if (offset >= ops->block_size * ops->blocks_num)
749 /* Target the C2 flash programming data register for C2 data register
751 c2port_write_ar(dev, C2PORT_FPDAT);
753 /* Send flash block write command */
754 c2port_write_dr(dev, C2PORT_BLOCK_WRITE);
756 /* Wait for input acknowledge */
757 ret = c2port_poll_in_busy(dev);
761 /* Should check status before starting FLASH access sequence */
763 /* Wait for status information */
764 ret = c2port_poll_out_ready(dev);
768 /* Read flash programming interface status */
769 ret = c2port_read_dr(dev, &status);
772 if (status != C2PORT_COMMAND_OK)
775 /* Send address high byte */
776 c2port_write_dr(dev, offset >> 8);
777 ret = c2port_poll_in_busy(dev);
781 /* Send address low byte */
782 c2port_write_dr(dev, offset & 0x00ff);
783 ret = c2port_poll_in_busy(dev);
787 /* Send address block size */
788 c2port_write_dr(dev, nwrite);
789 ret = c2port_poll_in_busy(dev);
793 /* Should check status before writing FLASH block */
795 /* Wait for status information */
796 ret = c2port_poll_out_ready(dev);
800 /* Read flash programming interface status */
801 ret = c2port_read_dr(dev, &status);
804 if (status != C2PORT_COMMAND_OK)
807 /* Write flash block */
808 for (i = 0; i < nwrite; i++) {
809 ret = c2port_write_dr(dev, *(buffer+i));
813 ret = c2port_poll_in_busy(dev);
819 /* Wait for last flash write to complete */
820 ret = c2port_poll_out_ready(dev);
827 static ssize_t c2port_write_flash_data(struct file *filp, struct kobject *kobj,
828 struct bin_attribute *attr,
829 char *buffer, loff_t offset, size_t count)
831 struct c2port_device *c2dev =
832 dev_get_drvdata(container_of(kobj,
833 struct device, kobj));
836 /* Check the device access status */
837 if (!c2dev->access || !c2dev->flash_access)
840 mutex_lock(&c2dev->mutex);
841 ret = __c2port_write_flash_data(c2dev, buffer, offset, count);
842 mutex_unlock(&c2dev->mutex);
845 dev_err(c2dev->dev, "cannot write %s flash\n", c2dev->name);
854 static struct device_attribute c2port_attrs[] = {
855 __ATTR(name, 0444, c2port_show_name, NULL),
856 __ATTR(flash_blocks_num, 0444, c2port_show_flash_blocks_num, NULL),
857 __ATTR(flash_block_size, 0444, c2port_show_flash_block_size, NULL),
858 __ATTR(flash_size, 0444, c2port_show_flash_size, NULL),
859 __ATTR(access, 0644, c2port_show_access, c2port_store_access),
860 __ATTR(reset, 0200, NULL, c2port_store_reset),
861 __ATTR(dev_id, 0444, c2port_show_dev_id, NULL),
862 __ATTR(rev_id, 0444, c2port_show_rev_id, NULL),
864 __ATTR(flash_access, 0644, c2port_show_flash_access,
865 c2port_store_flash_access),
866 __ATTR(flash_erase, 0200, NULL, c2port_store_flash_erase),
870 static struct bin_attribute c2port_bin_attrs = {
872 .name = "flash_data",
875 .read = c2port_read_flash_data,
876 .write = c2port_write_flash_data,
877 /* .size is computed at run-time */
884 struct c2port_device *c2port_device_register(char *name,
885 struct c2port_ops *ops, void *devdata)
887 struct c2port_device *c2dev;
890 if (unlikely(!ops) || unlikely(!ops->access) || \
891 unlikely(!ops->c2d_dir) || unlikely(!ops->c2ck_set) || \
892 unlikely(!ops->c2d_get) || unlikely(!ops->c2d_set))
893 return ERR_PTR(-EINVAL);
895 c2dev = kmalloc(sizeof(struct c2port_device), GFP_KERNEL);
896 kmemcheck_annotate_bitfield(c2dev, flags);
897 if (unlikely(!c2dev))
898 return ERR_PTR(-ENOMEM);
900 ret = idr_pre_get(&c2port_idr, GFP_KERNEL);
903 goto error_idr_get_new;
906 spin_lock_irq(&c2port_idr_lock);
907 ret = idr_get_new(&c2port_idr, c2dev, &id);
908 spin_unlock_irq(&c2port_idr_lock);
911 goto error_idr_get_new;
914 c2dev->dev = device_create(c2port_class, NULL, 0, c2dev,
916 if (unlikely(IS_ERR(c2dev->dev))) {
917 ret = PTR_ERR(c2dev->dev);
918 goto error_device_create;
920 dev_set_drvdata(c2dev->dev, c2dev);
922 strncpy(c2dev->name, name, C2PORT_NAME_LEN);
924 mutex_init(&c2dev->mutex);
926 /* Create binary file */
927 c2port_bin_attrs.size = ops->blocks_num * ops->block_size;
928 ret = device_create_bin_file(c2dev->dev, &c2port_bin_attrs);
930 goto error_device_create_bin_file;
932 /* By default C2 port access is off */
933 c2dev->access = c2dev->flash_access = 0;
934 ops->access(c2dev, 0);
936 dev_info(c2dev->dev, "C2 port %s added\n", name);
937 dev_info(c2dev->dev, "%s flash has %d blocks x %d bytes "
938 "(%d bytes total)\n",
939 name, ops->blocks_num, ops->block_size,
940 ops->blocks_num * ops->block_size);
944 error_device_create_bin_file:
945 device_destroy(c2port_class, 0);
948 spin_lock_irq(&c2port_idr_lock);
949 idr_remove(&c2port_idr, id);
950 spin_unlock_irq(&c2port_idr_lock);
957 EXPORT_SYMBOL(c2port_device_register);
959 void c2port_device_unregister(struct c2port_device *c2dev)
964 dev_info(c2dev->dev, "C2 port %s removed\n", c2dev->name);
966 device_remove_bin_file(c2dev->dev, &c2port_bin_attrs);
967 spin_lock_irq(&c2port_idr_lock);
968 idr_remove(&c2port_idr, c2dev->id);
969 spin_unlock_irq(&c2port_idr_lock);
971 device_destroy(c2port_class, c2dev->id);
975 EXPORT_SYMBOL(c2port_device_unregister);
981 static int __init c2port_init(void)
983 printk(KERN_INFO "Silicon Labs C2 port support v. " DRIVER_VERSION
984 " - (C) 2007 Rodolfo Giometti\n");
986 c2port_class = class_create(THIS_MODULE, "c2port");
987 if (IS_ERR(c2port_class)) {
988 printk(KERN_ERR "c2port: failed to allocate class\n");
989 return PTR_ERR(c2port_class);
991 c2port_class->dev_attrs = c2port_attrs;
996 static void __exit c2port_exit(void)
998 class_destroy(c2port_class);
1001 module_init(c2port_init);
1002 module_exit(c2port_exit);
1004 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1005 MODULE_DESCRIPTION("Silicon Labs C2 port support v. " DRIVER_VERSION);
1006 MODULE_LICENSE("GPL");