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/ctype.h>
19 #include <linux/delay.h>
20 #include <linux/idr.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
24 #include <linux/c2port.h>
26 #define DRIVER_NAME "c2port"
27 #define DRIVER_VERSION "0.51.0"
29 static DEFINE_SPINLOCK(c2port_idr_lock);
30 static DEFINE_IDR(c2port_idr);
36 static struct class *c2port_class;
39 * C2 registers & commands defines
43 #define C2PORT_DEVICEID 0x00
44 #define C2PORT_REVID 0x01
45 #define C2PORT_FPCTL 0x02
46 #define C2PORT_FPDAT 0xB4
48 /* C2 interface commands */
49 #define C2PORT_GET_VERSION 0x01
50 #define C2PORT_DEVICE_ERASE 0x03
51 #define C2PORT_BLOCK_READ 0x06
52 #define C2PORT_BLOCK_WRITE 0x07
53 #define C2PORT_PAGE_ERASE 0x08
55 /* C2 status return codes */
56 #define C2PORT_INVALID_COMMAND 0x00
57 #define C2PORT_COMMAND_FAILED 0x02
58 #define C2PORT_COMMAND_OK 0x0d
61 * C2 port low level signal managements
64 static void c2port_reset(struct c2port_device *dev)
66 struct c2port_ops *ops = dev->ops;
68 /* To reset the device we have to keep clock line low for at least
72 ops->c2ck_set(dev, 0);
74 ops->c2ck_set(dev, 1);
80 static void c2port_strobe_ck(struct c2port_device *dev)
82 struct c2port_ops *ops = dev->ops;
84 /* During hi-low-hi transition we disable local IRQs to avoid
85 * interructions since C2 port specification says that it must be
86 * shorter than 5us, otherwise the microcontroller may consider
87 * it as a reset signal!
90 ops->c2ck_set(dev, 0);
92 ops->c2ck_set(dev, 1);
99 * C2 port basic functions
102 static void c2port_write_ar(struct c2port_device *dev, u8 addr)
104 struct c2port_ops *ops = dev->ops;
108 c2port_strobe_ck(dev);
110 /* INS field (11b, LSB first) */
111 ops->c2d_dir(dev, 0);
112 ops->c2d_set(dev, 1);
113 c2port_strobe_ck(dev);
114 ops->c2d_set(dev, 1);
115 c2port_strobe_ck(dev);
118 for (i = 0; i < 8; i++) {
119 ops->c2d_set(dev, addr & 0x01);
120 c2port_strobe_ck(dev);
126 ops->c2d_dir(dev, 1);
127 c2port_strobe_ck(dev);
130 static int c2port_read_ar(struct c2port_device *dev, u8 *addr)
132 struct c2port_ops *ops = dev->ops;
136 c2port_strobe_ck(dev);
138 /* INS field (10b, LSB first) */
139 ops->c2d_dir(dev, 0);
140 ops->c2d_set(dev, 0);
141 c2port_strobe_ck(dev);
142 ops->c2d_set(dev, 1);
143 c2port_strobe_ck(dev);
146 ops->c2d_dir(dev, 1);
148 for (i = 0; i < 8; i++) {
149 *addr >>= 1; /* shift in 8-bit ADDRESS field LSB first */
151 c2port_strobe_ck(dev);
152 if (ops->c2d_get(dev))
157 c2port_strobe_ck(dev);
162 static int c2port_write_dr(struct c2port_device *dev, u8 data)
164 struct c2port_ops *ops = dev->ops;
168 c2port_strobe_ck(dev);
170 /* INS field (01b, LSB first) */
171 ops->c2d_dir(dev, 0);
172 ops->c2d_set(dev, 1);
173 c2port_strobe_ck(dev);
174 ops->c2d_set(dev, 0);
175 c2port_strobe_ck(dev);
177 /* LENGTH field (00b, LSB first -> 1 byte) */
178 ops->c2d_set(dev, 0);
179 c2port_strobe_ck(dev);
180 ops->c2d_set(dev, 0);
181 c2port_strobe_ck(dev);
184 for (i = 0; i < 8; i++) {
185 ops->c2d_set(dev, data & 0x01);
186 c2port_strobe_ck(dev);
192 ops->c2d_dir(dev, 1);
195 c2port_strobe_ck(dev);
196 if (ops->c2d_get(dev))
200 } while (--timeout > 0);
205 c2port_strobe_ck(dev);
210 static int c2port_read_dr(struct c2port_device *dev, u8 *data)
212 struct c2port_ops *ops = dev->ops;
216 c2port_strobe_ck(dev);
218 /* INS field (00b, LSB first) */
219 ops->c2d_dir(dev, 0);
220 ops->c2d_set(dev, 0);
221 c2port_strobe_ck(dev);
222 ops->c2d_set(dev, 0);
223 c2port_strobe_ck(dev);
225 /* LENGTH field (00b, LSB first -> 1 byte) */
226 ops->c2d_set(dev, 0);
227 c2port_strobe_ck(dev);
228 ops->c2d_set(dev, 0);
229 c2port_strobe_ck(dev);
232 ops->c2d_dir(dev, 1);
235 c2port_strobe_ck(dev);
236 if (ops->c2d_get(dev))
240 } while (--timeout > 0);
246 for (i = 0; i < 8; i++) {
247 *data >>= 1; /* shift in 8-bit DATA field LSB first */
249 c2port_strobe_ck(dev);
250 if (ops->c2d_get(dev))
255 c2port_strobe_ck(dev);
260 static int c2port_poll_in_busy(struct c2port_device *dev)
263 int ret, timeout = 20;
266 ret = (c2port_read_ar(dev, &addr));
274 } while (--timeout > 0);
281 static int c2port_poll_out_ready(struct c2port_device *dev)
284 int ret, timeout = 10000; /* erase flash needs long time... */
287 ret = (c2port_read_ar(dev, &addr));
295 } while (--timeout > 0);
306 static ssize_t c2port_show_name(struct device *dev,
307 struct device_attribute *attr, char *buf)
309 struct c2port_device *c2dev = dev_get_drvdata(dev);
311 return sprintf(buf, "%s\n", c2dev->name);
313 static DEVICE_ATTR(name, 0444, c2port_show_name, NULL);
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);
323 static DEVICE_ATTR(flash_blocks_num, 0444, c2port_show_flash_blocks_num, NULL);
325 static ssize_t c2port_show_flash_block_size(struct device *dev,
326 struct device_attribute *attr, char *buf)
328 struct c2port_device *c2dev = dev_get_drvdata(dev);
329 struct c2port_ops *ops = c2dev->ops;
331 return sprintf(buf, "%d\n", ops->block_size);
333 static DEVICE_ATTR(flash_block_size, 0444, c2port_show_flash_block_size, NULL);
335 static ssize_t c2port_show_flash_size(struct device *dev,
336 struct device_attribute *attr, char *buf)
338 struct c2port_device *c2dev = dev_get_drvdata(dev);
339 struct c2port_ops *ops = c2dev->ops;
341 return sprintf(buf, "%d\n", ops->blocks_num * ops->block_size);
343 static DEVICE_ATTR(flash_size, 0444, c2port_show_flash_size, NULL);
345 static ssize_t access_show(struct device *dev, struct device_attribute *attr,
348 struct c2port_device *c2dev = dev_get_drvdata(dev);
350 return sprintf(buf, "%d\n", c2dev->access);
353 static ssize_t access_store(struct device *dev, struct device_attribute *attr,
354 const char *buf, size_t count)
356 struct c2port_device *c2dev = dev_get_drvdata(dev);
357 struct c2port_ops *ops = c2dev->ops;
360 ret = sscanf(buf, "%d", &status);
364 mutex_lock(&c2dev->mutex);
366 c2dev->access = !!status;
368 /* If access is "on" clock should be HIGH _before_ setting the line
369 * as output and data line should be set as INPUT anyway */
371 ops->c2ck_set(c2dev, 1);
372 ops->access(c2dev, c2dev->access);
374 ops->c2d_dir(c2dev, 1);
376 mutex_unlock(&c2dev->mutex);
380 static DEVICE_ATTR_RW(access);
382 static ssize_t c2port_store_reset(struct device *dev,
383 struct device_attribute *attr,
384 const char *buf, size_t count)
386 struct c2port_device *c2dev = dev_get_drvdata(dev);
388 /* Check the device access status */
392 mutex_lock(&c2dev->mutex);
395 c2dev->flash_access = 0;
397 mutex_unlock(&c2dev->mutex);
401 static DEVICE_ATTR(reset, 0200, NULL, c2port_store_reset);
403 static ssize_t __c2port_show_dev_id(struct c2port_device *dev, char *buf)
408 /* Select DEVICEID register for C2 data register accesses */
409 c2port_write_ar(dev, C2PORT_DEVICEID);
411 /* Read and return the device ID register */
412 ret = c2port_read_dr(dev, &data);
416 return sprintf(buf, "%d\n", data);
419 static ssize_t c2port_show_dev_id(struct device *dev,
420 struct device_attribute *attr, char *buf)
422 struct c2port_device *c2dev = dev_get_drvdata(dev);
425 /* Check the device access status */
429 mutex_lock(&c2dev->mutex);
430 ret = __c2port_show_dev_id(c2dev, buf);
431 mutex_unlock(&c2dev->mutex);
434 dev_err(dev, "cannot read from %s\n", c2dev->name);
438 static DEVICE_ATTR(dev_id, 0444, c2port_show_dev_id, NULL);
440 static ssize_t __c2port_show_rev_id(struct c2port_device *dev, char *buf)
445 /* Select REVID register for C2 data register accesses */
446 c2port_write_ar(dev, C2PORT_REVID);
448 /* Read and return the revision ID register */
449 ret = c2port_read_dr(dev, &data);
453 return sprintf(buf, "%d\n", data);
456 static ssize_t c2port_show_rev_id(struct device *dev,
457 struct device_attribute *attr, char *buf)
459 struct c2port_device *c2dev = dev_get_drvdata(dev);
462 /* Check the device access status */
466 mutex_lock(&c2dev->mutex);
467 ret = __c2port_show_rev_id(c2dev, buf);
468 mutex_unlock(&c2dev->mutex);
471 dev_err(c2dev->dev, "cannot read from %s\n", c2dev->name);
475 static DEVICE_ATTR(rev_id, 0444, c2port_show_rev_id, NULL);
477 static ssize_t c2port_show_flash_access(struct device *dev,
478 struct device_attribute *attr, char *buf)
480 struct c2port_device *c2dev = dev_get_drvdata(dev);
482 return sprintf(buf, "%d\n", c2dev->flash_access);
485 static ssize_t __c2port_store_flash_access(struct c2port_device *dev,
490 /* Check the device access status */
494 dev->flash_access = !!status;
496 /* If flash_access is off we have nothing to do... */
497 if (dev->flash_access == 0)
500 /* Target the C2 flash programming control register for C2 data
502 c2port_write_ar(dev, C2PORT_FPCTL);
504 /* Write the first keycode to enable C2 Flash programming */
505 ret = c2port_write_dr(dev, 0x02);
509 /* Write the second keycode to enable C2 Flash programming */
510 ret = c2port_write_dr(dev, 0x01);
514 /* Delay for at least 20ms to ensure the target is ready for
515 * C2 flash programming */
521 static ssize_t c2port_store_flash_access(struct device *dev,
522 struct device_attribute *attr,
523 const char *buf, size_t count)
525 struct c2port_device *c2dev = dev_get_drvdata(dev);
529 ret = sscanf(buf, "%d", &status);
533 mutex_lock(&c2dev->mutex);
534 ret = __c2port_store_flash_access(c2dev, status);
535 mutex_unlock(&c2dev->mutex);
538 dev_err(c2dev->dev, "cannot enable %s flash programming\n",
545 static DEVICE_ATTR(flash_access, 0644, c2port_show_flash_access,
546 c2port_store_flash_access);
548 static ssize_t __c2port_write_flash_erase(struct c2port_device *dev)
553 /* Target the C2 flash programming data register for C2 data register
556 c2port_write_ar(dev, C2PORT_FPDAT);
558 /* Send device erase command */
559 c2port_write_dr(dev, C2PORT_DEVICE_ERASE);
561 /* Wait for input acknowledge */
562 ret = c2port_poll_in_busy(dev);
566 /* Should check status before starting FLASH access sequence */
568 /* Wait for status information */
569 ret = c2port_poll_out_ready(dev);
573 /* Read flash programming interface status */
574 ret = c2port_read_dr(dev, &status);
577 if (status != C2PORT_COMMAND_OK)
580 /* Send a three-byte arming sequence to enable the device erase.
581 * If the sequence is not received correctly, the command will be
583 * Sequence is: 0xde, 0xad, 0xa5.
585 c2port_write_dr(dev, 0xde);
586 ret = c2port_poll_in_busy(dev);
589 c2port_write_dr(dev, 0xad);
590 ret = c2port_poll_in_busy(dev);
593 c2port_write_dr(dev, 0xa5);
594 ret = c2port_poll_in_busy(dev);
598 ret = c2port_poll_out_ready(dev);
605 static ssize_t c2port_store_flash_erase(struct device *dev,
606 struct device_attribute *attr,
607 const char *buf, size_t count)
609 struct c2port_device *c2dev = dev_get_drvdata(dev);
612 /* Check the device and flash access status */
613 if (!c2dev->access || !c2dev->flash_access)
616 mutex_lock(&c2dev->mutex);
617 ret = __c2port_write_flash_erase(c2dev);
618 mutex_unlock(&c2dev->mutex);
621 dev_err(c2dev->dev, "cannot erase %s flash\n", c2dev->name);
627 static DEVICE_ATTR(flash_erase, 0200, NULL, c2port_store_flash_erase);
629 static ssize_t __c2port_read_flash_data(struct c2port_device *dev,
630 char *buffer, loff_t offset, size_t count)
632 struct c2port_ops *ops = dev->ops;
633 u8 status, nread = 128;
636 /* Check for flash end */
637 if (offset >= ops->block_size * ops->blocks_num)
640 if (ops->block_size * ops->blocks_num - offset < nread)
641 nread = ops->block_size * ops->blocks_num - offset;
647 /* Target the C2 flash programming data register for C2 data register
649 c2port_write_ar(dev, C2PORT_FPDAT);
651 /* Send flash block read command */
652 c2port_write_dr(dev, C2PORT_BLOCK_READ);
654 /* Wait for input acknowledge */
655 ret = c2port_poll_in_busy(dev);
659 /* Should check status before starting FLASH access sequence */
661 /* Wait for status information */
662 ret = c2port_poll_out_ready(dev);
666 /* Read flash programming interface status */
667 ret = c2port_read_dr(dev, &status);
670 if (status != C2PORT_COMMAND_OK)
673 /* Send address high byte */
674 c2port_write_dr(dev, offset >> 8);
675 ret = c2port_poll_in_busy(dev);
679 /* Send address low byte */
680 c2port_write_dr(dev, offset & 0x00ff);
681 ret = c2port_poll_in_busy(dev);
685 /* Send address block size */
686 c2port_write_dr(dev, nread);
687 ret = c2port_poll_in_busy(dev);
691 /* Should check status before reading FLASH block */
693 /* Wait for status information */
694 ret = c2port_poll_out_ready(dev);
698 /* Read flash programming interface status */
699 ret = c2port_read_dr(dev, &status);
702 if (status != C2PORT_COMMAND_OK)
705 /* Read flash block */
706 for (i = 0; i < nread; i++) {
707 ret = c2port_poll_out_ready(dev);
711 ret = c2port_read_dr(dev, buffer+i);
719 static ssize_t c2port_read_flash_data(struct file *filp, struct kobject *kobj,
720 struct bin_attribute *attr,
721 char *buffer, loff_t offset, size_t count)
723 struct c2port_device *c2dev = dev_get_drvdata(kobj_to_dev(kobj));
726 /* Check the device and flash access status */
727 if (!c2dev->access || !c2dev->flash_access)
730 mutex_lock(&c2dev->mutex);
731 ret = __c2port_read_flash_data(c2dev, buffer, offset, count);
732 mutex_unlock(&c2dev->mutex);
735 dev_err(c2dev->dev, "cannot read %s flash\n", c2dev->name);
740 static ssize_t __c2port_write_flash_data(struct c2port_device *dev,
741 char *buffer, loff_t offset, size_t count)
743 struct c2port_ops *ops = dev->ops;
744 u8 status, nwrite = 128;
749 if (ops->block_size * ops->blocks_num - offset < nwrite)
750 nwrite = ops->block_size * ops->blocks_num - offset;
752 /* Check for flash end */
753 if (offset >= ops->block_size * ops->blocks_num)
756 /* Target the C2 flash programming data register for C2 data register
758 c2port_write_ar(dev, C2PORT_FPDAT);
760 /* Send flash block write command */
761 c2port_write_dr(dev, C2PORT_BLOCK_WRITE);
763 /* Wait for input acknowledge */
764 ret = c2port_poll_in_busy(dev);
768 /* Should check status before starting FLASH access sequence */
770 /* Wait for status information */
771 ret = c2port_poll_out_ready(dev);
775 /* Read flash programming interface status */
776 ret = c2port_read_dr(dev, &status);
779 if (status != C2PORT_COMMAND_OK)
782 /* Send address high byte */
783 c2port_write_dr(dev, offset >> 8);
784 ret = c2port_poll_in_busy(dev);
788 /* Send address low byte */
789 c2port_write_dr(dev, offset & 0x00ff);
790 ret = c2port_poll_in_busy(dev);
794 /* Send address block size */
795 c2port_write_dr(dev, nwrite);
796 ret = c2port_poll_in_busy(dev);
800 /* Should check status before writing FLASH block */
802 /* Wait for status information */
803 ret = c2port_poll_out_ready(dev);
807 /* Read flash programming interface status */
808 ret = c2port_read_dr(dev, &status);
811 if (status != C2PORT_COMMAND_OK)
814 /* Write flash block */
815 for (i = 0; i < nwrite; i++) {
816 ret = c2port_write_dr(dev, *(buffer+i));
820 ret = c2port_poll_in_busy(dev);
826 /* Wait for last flash write to complete */
827 ret = c2port_poll_out_ready(dev);
834 static ssize_t c2port_write_flash_data(struct file *filp, struct kobject *kobj,
835 struct bin_attribute *attr,
836 char *buffer, loff_t offset, size_t count)
838 struct c2port_device *c2dev = dev_get_drvdata(kobj_to_dev(kobj));
841 /* Check the device access status */
842 if (!c2dev->access || !c2dev->flash_access)
845 mutex_lock(&c2dev->mutex);
846 ret = __c2port_write_flash_data(c2dev, buffer, offset, count);
847 mutex_unlock(&c2dev->mutex);
850 dev_err(c2dev->dev, "cannot write %s flash\n", c2dev->name);
854 /* size is computed at run-time */
855 static BIN_ATTR(flash_data, 0644, c2port_read_flash_data,
856 c2port_write_flash_data, 0);
861 static struct attribute *c2port_attrs[] = {
863 &dev_attr_flash_blocks_num.attr,
864 &dev_attr_flash_block_size.attr,
865 &dev_attr_flash_size.attr,
866 &dev_attr_access.attr,
867 &dev_attr_reset.attr,
868 &dev_attr_dev_id.attr,
869 &dev_attr_rev_id.attr,
870 &dev_attr_flash_access.attr,
871 &dev_attr_flash_erase.attr,
875 static struct bin_attribute *c2port_bin_attrs[] = {
876 &bin_attr_flash_data,
880 static const struct attribute_group c2port_group = {
881 .attrs = c2port_attrs,
882 .bin_attrs = c2port_bin_attrs,
885 static const struct attribute_group *c2port_groups[] = {
894 struct c2port_device *c2port_device_register(char *name,
895 struct c2port_ops *ops, void *devdata)
897 struct c2port_device *c2dev;
900 if (unlikely(!ops) || unlikely(!ops->access) || \
901 unlikely(!ops->c2d_dir) || unlikely(!ops->c2ck_set) || \
902 unlikely(!ops->c2d_get) || unlikely(!ops->c2d_set))
903 return ERR_PTR(-EINVAL);
905 c2dev = kmalloc(sizeof(struct c2port_device), GFP_KERNEL);
906 if (unlikely(!c2dev))
907 return ERR_PTR(-ENOMEM);
909 idr_preload(GFP_KERNEL);
910 spin_lock_irq(&c2port_idr_lock);
911 ret = idr_alloc(&c2port_idr, c2dev, 0, 0, GFP_NOWAIT);
912 spin_unlock_irq(&c2port_idr_lock);
916 goto error_idr_alloc;
919 bin_attr_flash_data.size = ops->blocks_num * ops->block_size;
921 c2dev->dev = device_create(c2port_class, NULL, 0, c2dev,
922 "c2port%d", c2dev->id);
923 if (IS_ERR(c2dev->dev)) {
924 ret = PTR_ERR(c2dev->dev);
925 goto error_device_create;
927 dev_set_drvdata(c2dev->dev, c2dev);
929 strncpy(c2dev->name, name, C2PORT_NAME_LEN);
931 mutex_init(&c2dev->mutex);
933 /* By default C2 port access is off */
934 c2dev->access = c2dev->flash_access = 0;
935 ops->access(c2dev, 0);
937 dev_info(c2dev->dev, "C2 port %s added\n", name);
938 dev_info(c2dev->dev, "%s flash has %d blocks x %d bytes "
939 "(%d bytes total)\n",
940 name, ops->blocks_num, ops->block_size,
941 ops->blocks_num * ops->block_size);
946 spin_lock_irq(&c2port_idr_lock);
947 idr_remove(&c2port_idr, c2dev->id);
948 spin_unlock_irq(&c2port_idr_lock);
955 EXPORT_SYMBOL(c2port_device_register);
957 void c2port_device_unregister(struct c2port_device *c2dev)
962 dev_info(c2dev->dev, "C2 port %s removed\n", c2dev->name);
964 spin_lock_irq(&c2port_idr_lock);
965 idr_remove(&c2port_idr, c2dev->id);
966 spin_unlock_irq(&c2port_idr_lock);
968 device_destroy(c2port_class, c2dev->id);
972 EXPORT_SYMBOL(c2port_device_unregister);
978 static int __init c2port_init(void)
980 printk(KERN_INFO "Silicon Labs C2 port support v. " DRIVER_VERSION
981 " - (C) 2007 Rodolfo Giometti\n");
983 c2port_class = class_create(THIS_MODULE, "c2port");
984 if (IS_ERR(c2port_class)) {
985 printk(KERN_ERR "c2port: failed to allocate class\n");
986 return PTR_ERR(c2port_class);
988 c2port_class->dev_groups = c2port_groups;
993 static void __exit c2port_exit(void)
995 class_destroy(c2port_class);
998 module_init(c2port_init);
999 module_exit(c2port_exit);
1001 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1002 MODULE_DESCRIPTION("Silicon Labs C2 port support v. " DRIVER_VERSION);
1003 MODULE_LICENSE("GPL");