2 * Silicon Labs C2 port core Linux support
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>
23 #include <linux/c2port.h>
25 #define DRIVER_NAME "c2port"
26 #define DRIVER_VERSION "0.51.0"
28 static DEFINE_SPINLOCK(c2port_idr_lock);
29 static DEFINE_IDR(c2port_idr);
35 static struct class *c2port_class;
38 * C2 registers & commands defines
42 #define C2PORT_DEVICEID 0x00
43 #define C2PORT_REVID 0x01
44 #define C2PORT_FPCTL 0x02
45 #define C2PORT_FPDAT 0xB4
47 /* C2 interface commands */
48 #define C2PORT_GET_VERSION 0x01
49 #define C2PORT_DEVICE_ERASE 0x03
50 #define C2PORT_BLOCK_READ 0x06
51 #define C2PORT_BLOCK_WRITE 0x07
52 #define C2PORT_PAGE_ERASE 0x08
54 /* C2 status return codes */
55 #define C2PORT_INVALID_COMMAND 0x00
56 #define C2PORT_COMMAND_FAILED 0x02
57 #define C2PORT_COMMAND_OK 0x0d
60 * C2 port low level signal managements
63 static void c2port_reset(struct c2port_device *dev)
65 struct c2port_ops *ops = dev->ops;
67 /* To reset the device we have to keep clock line low for at least
71 ops->c2ck_set(dev, 0);
73 ops->c2ck_set(dev, 1);
79 static void c2port_strobe_ck(struct c2port_device *dev)
81 struct c2port_ops *ops = dev->ops;
83 /* During hi-low-hi transition we disable local IRQs to avoid
84 * interructions since C2 port specification says that it must be
85 * shorter than 5us, otherwise the microcontroller may consider
86 * it as a reset signal!
89 ops->c2ck_set(dev, 0);
91 ops->c2ck_set(dev, 1);
98 * C2 port basic functions
101 static void c2port_write_ar(struct c2port_device *dev, u8 addr)
103 struct c2port_ops *ops = dev->ops;
107 c2port_strobe_ck(dev);
109 /* INS field (11b, LSB first) */
110 ops->c2d_dir(dev, 0);
111 ops->c2d_set(dev, 1);
112 c2port_strobe_ck(dev);
113 ops->c2d_set(dev, 1);
114 c2port_strobe_ck(dev);
117 for (i = 0; i < 8; i++) {
118 ops->c2d_set(dev, addr & 0x01);
119 c2port_strobe_ck(dev);
125 ops->c2d_dir(dev, 1);
126 c2port_strobe_ck(dev);
129 static int c2port_read_ar(struct c2port_device *dev, u8 *addr)
131 struct c2port_ops *ops = dev->ops;
135 c2port_strobe_ck(dev);
137 /* INS field (10b, LSB first) */
138 ops->c2d_dir(dev, 0);
139 ops->c2d_set(dev, 0);
140 c2port_strobe_ck(dev);
141 ops->c2d_set(dev, 1);
142 c2port_strobe_ck(dev);
145 ops->c2d_dir(dev, 1);
147 for (i = 0; i < 8; i++) {
148 *addr >>= 1; /* shift in 8-bit ADDRESS field LSB first */
150 c2port_strobe_ck(dev);
151 if (ops->c2d_get(dev))
156 c2port_strobe_ck(dev);
161 static int c2port_write_dr(struct c2port_device *dev, u8 data)
163 struct c2port_ops *ops = dev->ops;
167 c2port_strobe_ck(dev);
169 /* INS field (01b, LSB first) */
170 ops->c2d_dir(dev, 0);
171 ops->c2d_set(dev, 1);
172 c2port_strobe_ck(dev);
173 ops->c2d_set(dev, 0);
174 c2port_strobe_ck(dev);
176 /* LENGTH field (00b, LSB first -> 1 byte) */
177 ops->c2d_set(dev, 0);
178 c2port_strobe_ck(dev);
179 ops->c2d_set(dev, 0);
180 c2port_strobe_ck(dev);
183 for (i = 0; i < 8; i++) {
184 ops->c2d_set(dev, data & 0x01);
185 c2port_strobe_ck(dev);
191 ops->c2d_dir(dev, 1);
194 c2port_strobe_ck(dev);
195 if (ops->c2d_get(dev))
199 } while (--timeout > 0);
204 c2port_strobe_ck(dev);
209 static int c2port_read_dr(struct c2port_device *dev, u8 *data)
211 struct c2port_ops *ops = dev->ops;
215 c2port_strobe_ck(dev);
217 /* INS field (00b, LSB first) */
218 ops->c2d_dir(dev, 0);
219 ops->c2d_set(dev, 0);
220 c2port_strobe_ck(dev);
221 ops->c2d_set(dev, 0);
222 c2port_strobe_ck(dev);
224 /* LENGTH field (00b, LSB first -> 1 byte) */
225 ops->c2d_set(dev, 0);
226 c2port_strobe_ck(dev);
227 ops->c2d_set(dev, 0);
228 c2port_strobe_ck(dev);
231 ops->c2d_dir(dev, 1);
234 c2port_strobe_ck(dev);
235 if (ops->c2d_get(dev))
239 } while (--timeout > 0);
245 for (i = 0; i < 8; i++) {
246 *data >>= 1; /* shift in 8-bit DATA field LSB first */
248 c2port_strobe_ck(dev);
249 if (ops->c2d_get(dev))
254 c2port_strobe_ck(dev);
259 static int c2port_poll_in_busy(struct c2port_device *dev)
262 int ret, timeout = 20;
265 ret = (c2port_read_ar(dev, &addr));
273 } while (--timeout > 0);
280 static int c2port_poll_out_ready(struct c2port_device *dev)
283 int ret, timeout = 10000; /* erase flash needs long time... */
286 ret = (c2port_read_ar(dev, &addr));
294 } while (--timeout > 0);
305 static ssize_t c2port_show_name(struct device *dev,
306 struct device_attribute *attr, char *buf)
308 struct c2port_device *c2dev = dev_get_drvdata(dev);
310 return sprintf(buf, "%s\n", c2dev->name);
313 static ssize_t c2port_show_flash_blocks_num(struct device *dev,
314 struct device_attribute *attr, char *buf)
316 struct c2port_device *c2dev = dev_get_drvdata(dev);
317 struct c2port_ops *ops = c2dev->ops;
319 return sprintf(buf, "%d\n", ops->blocks_num);
322 static ssize_t c2port_show_flash_block_size(struct device *dev,
323 struct device_attribute *attr, char *buf)
325 struct c2port_device *c2dev = dev_get_drvdata(dev);
326 struct c2port_ops *ops = c2dev->ops;
328 return sprintf(buf, "%d\n", ops->block_size);
331 static ssize_t c2port_show_flash_size(struct device *dev,
332 struct device_attribute *attr, char *buf)
334 struct c2port_device *c2dev = dev_get_drvdata(dev);
335 struct c2port_ops *ops = c2dev->ops;
337 return sprintf(buf, "%d\n", ops->blocks_num * ops->block_size);
340 static ssize_t c2port_show_access(struct device *dev,
341 struct device_attribute *attr, char *buf)
343 struct c2port_device *c2dev = dev_get_drvdata(dev);
345 return sprintf(buf, "%d\n", c2dev->access);
348 static ssize_t c2port_store_access(struct device *dev,
349 struct device_attribute *attr,
350 const char *buf, size_t count)
352 struct c2port_device *c2dev = dev_get_drvdata(dev);
353 struct c2port_ops *ops = c2dev->ops;
356 ret = sscanf(buf, "%d", &status);
360 mutex_lock(&c2dev->mutex);
362 c2dev->access = !!status;
364 /* If access is "on" clock should be HIGH _before_ setting the line
365 * as output and data line should be set as INPUT anyway */
367 ops->c2ck_set(c2dev, 1);
368 ops->access(c2dev, c2dev->access);
370 ops->c2d_dir(c2dev, 1);
372 mutex_unlock(&c2dev->mutex);
377 static ssize_t c2port_store_reset(struct device *dev,
378 struct device_attribute *attr,
379 const char *buf, size_t count)
381 struct c2port_device *c2dev = dev_get_drvdata(dev);
383 /* Check the device access status */
387 mutex_lock(&c2dev->mutex);
390 c2dev->flash_access = 0;
392 mutex_unlock(&c2dev->mutex);
397 static ssize_t __c2port_show_dev_id(struct c2port_device *dev, char *buf)
402 /* Select DEVICEID register for C2 data register accesses */
403 c2port_write_ar(dev, C2PORT_DEVICEID);
405 /* Read and return the device ID register */
406 ret = c2port_read_dr(dev, &data);
410 return sprintf(buf, "%d\n", data);
413 static ssize_t c2port_show_dev_id(struct device *dev,
414 struct device_attribute *attr, char *buf)
416 struct c2port_device *c2dev = dev_get_drvdata(dev);
419 /* Check the device access status */
423 mutex_lock(&c2dev->mutex);
424 ret = __c2port_show_dev_id(c2dev, buf);
425 mutex_unlock(&c2dev->mutex);
428 dev_err(dev, "cannot read from %s\n", c2dev->name);
433 static ssize_t __c2port_show_rev_id(struct c2port_device *dev, char *buf)
438 /* Select REVID register for C2 data register accesses */
439 c2port_write_ar(dev, C2PORT_REVID);
441 /* Read and return the revision ID register */
442 ret = c2port_read_dr(dev, &data);
446 return sprintf(buf, "%d\n", data);
449 static ssize_t c2port_show_rev_id(struct device *dev,
450 struct device_attribute *attr, char *buf)
452 struct c2port_device *c2dev = dev_get_drvdata(dev);
455 /* Check the device access status */
459 mutex_lock(&c2dev->mutex);
460 ret = __c2port_show_rev_id(c2dev, buf);
461 mutex_unlock(&c2dev->mutex);
464 dev_err(c2dev->dev, "cannot read from %s\n", c2dev->name);
469 static ssize_t c2port_show_flash_access(struct device *dev,
470 struct device_attribute *attr, char *buf)
472 struct c2port_device *c2dev = dev_get_drvdata(dev);
474 return sprintf(buf, "%d\n", c2dev->flash_access);
477 static ssize_t __c2port_store_flash_access(struct c2port_device *dev,
482 /* Check the device access status */
486 dev->flash_access = !!status;
488 /* If flash_access is off we have nothing to do... */
489 if (dev->flash_access == 0)
492 /* Target the C2 flash programming control register for C2 data
494 c2port_write_ar(dev, C2PORT_FPCTL);
496 /* Write the first keycode to enable C2 Flash programming */
497 ret = c2port_write_dr(dev, 0x02);
501 /* Write the second keycode to enable C2 Flash programming */
502 ret = c2port_write_dr(dev, 0x01);
506 /* Delay for at least 20ms to ensure the target is ready for
507 * C2 flash programming */
513 static ssize_t c2port_store_flash_access(struct device *dev,
514 struct device_attribute *attr,
515 const char *buf, size_t count)
517 struct c2port_device *c2dev = dev_get_drvdata(dev);
521 ret = sscanf(buf, "%d", &status);
525 mutex_lock(&c2dev->mutex);
526 ret = __c2port_store_flash_access(c2dev, status);
527 mutex_unlock(&c2dev->mutex);
530 dev_err(c2dev->dev, "cannot enable %s flash programming\n",
538 static ssize_t __c2port_write_flash_erase(struct c2port_device *dev)
543 /* Target the C2 flash programming data register for C2 data register
546 c2port_write_ar(dev, C2PORT_FPDAT);
548 /* Send device erase command */
549 c2port_write_dr(dev, C2PORT_DEVICE_ERASE);
551 /* Wait for input acknowledge */
552 ret = c2port_poll_in_busy(dev);
556 /* Should check status before starting FLASH access sequence */
558 /* Wait for status information */
559 ret = c2port_poll_out_ready(dev);
563 /* Read flash programming interface status */
564 ret = c2port_read_dr(dev, &status);
567 if (status != C2PORT_COMMAND_OK)
570 /* Send a three-byte arming sequence to enable the device erase.
571 * If the sequence is not received correctly, the command will be
573 * Sequence is: 0xde, 0xad, 0xa5.
575 c2port_write_dr(dev, 0xde);
576 ret = c2port_poll_in_busy(dev);
579 c2port_write_dr(dev, 0xad);
580 ret = c2port_poll_in_busy(dev);
583 c2port_write_dr(dev, 0xa5);
584 ret = c2port_poll_in_busy(dev);
588 ret = c2port_poll_out_ready(dev);
595 static ssize_t c2port_store_flash_erase(struct device *dev,
596 struct device_attribute *attr,
597 const char *buf, size_t count)
599 struct c2port_device *c2dev = dev_get_drvdata(dev);
602 /* Check the device and flash access status */
603 if (!c2dev->access || !c2dev->flash_access)
606 mutex_lock(&c2dev->mutex);
607 ret = __c2port_write_flash_erase(c2dev);
608 mutex_unlock(&c2dev->mutex);
611 dev_err(c2dev->dev, "cannot erase %s flash\n", c2dev->name);
618 static ssize_t __c2port_read_flash_data(struct c2port_device *dev,
619 char *buffer, loff_t offset, size_t count)
621 struct c2port_ops *ops = dev->ops;
622 u8 status, nread = 128;
625 /* Check for flash end */
626 if (offset >= ops->block_size * ops->blocks_num)
629 if (ops->block_size * ops->blocks_num - offset < nread)
630 nread = ops->block_size * ops->blocks_num - offset;
636 /* Target the C2 flash programming data register for C2 data register
638 c2port_write_ar(dev, C2PORT_FPDAT);
640 /* Send flash block read command */
641 c2port_write_dr(dev, C2PORT_BLOCK_READ);
643 /* Wait for input acknowledge */
644 ret = c2port_poll_in_busy(dev);
648 /* Should check status before starting FLASH access sequence */
650 /* Wait for status information */
651 ret = c2port_poll_out_ready(dev);
655 /* Read flash programming interface status */
656 ret = c2port_read_dr(dev, &status);
659 if (status != C2PORT_COMMAND_OK)
662 /* Send address high byte */
663 c2port_write_dr(dev, offset >> 8);
664 ret = c2port_poll_in_busy(dev);
668 /* Send address low byte */
669 c2port_write_dr(dev, offset & 0x00ff);
670 ret = c2port_poll_in_busy(dev);
674 /* Send address block size */
675 c2port_write_dr(dev, nread);
676 ret = c2port_poll_in_busy(dev);
680 /* Should check status before reading FLASH block */
682 /* Wait for status information */
683 ret = c2port_poll_out_ready(dev);
687 /* Read flash programming interface status */
688 ret = c2port_read_dr(dev, &status);
691 if (status != C2PORT_COMMAND_OK)
694 /* Read flash block */
695 for (i = 0; i < nread; i++) {
696 ret = c2port_poll_out_ready(dev);
700 ret = c2port_read_dr(dev, buffer+i);
708 static ssize_t c2port_read_flash_data(struct kobject *kobj,
709 struct bin_attribute *attr,
710 char *buffer, loff_t offset, size_t count)
712 struct c2port_device *c2dev =
713 dev_get_drvdata(container_of(kobj,
714 struct device, kobj));
717 /* Check the device and flash access status */
718 if (!c2dev->access || !c2dev->flash_access)
721 mutex_lock(&c2dev->mutex);
722 ret = __c2port_read_flash_data(c2dev, buffer, offset, count);
723 mutex_unlock(&c2dev->mutex);
726 dev_err(c2dev->dev, "cannot read %s flash\n", c2dev->name);
731 static ssize_t __c2port_write_flash_data(struct c2port_device *dev,
732 char *buffer, loff_t offset, size_t count)
734 struct c2port_ops *ops = dev->ops;
735 u8 status, nwrite = 128;
740 if (ops->block_size * ops->blocks_num - offset < nwrite)
741 nwrite = ops->block_size * ops->blocks_num - offset;
743 /* Check for flash end */
744 if (offset >= ops->block_size * ops->blocks_num)
747 /* Target the C2 flash programming data register for C2 data register
749 c2port_write_ar(dev, C2PORT_FPDAT);
751 /* Send flash block write command */
752 c2port_write_dr(dev, C2PORT_BLOCK_WRITE);
754 /* Wait for input acknowledge */
755 ret = c2port_poll_in_busy(dev);
759 /* Should check status before starting FLASH access sequence */
761 /* Wait for status information */
762 ret = c2port_poll_out_ready(dev);
766 /* Read flash programming interface status */
767 ret = c2port_read_dr(dev, &status);
770 if (status != C2PORT_COMMAND_OK)
773 /* Send address high byte */
774 c2port_write_dr(dev, offset >> 8);
775 ret = c2port_poll_in_busy(dev);
779 /* Send address low byte */
780 c2port_write_dr(dev, offset & 0x00ff);
781 ret = c2port_poll_in_busy(dev);
785 /* Send address block size */
786 c2port_write_dr(dev, nwrite);
787 ret = c2port_poll_in_busy(dev);
791 /* Should check status before writing FLASH block */
793 /* Wait for status information */
794 ret = c2port_poll_out_ready(dev);
798 /* Read flash programming interface status */
799 ret = c2port_read_dr(dev, &status);
802 if (status != C2PORT_COMMAND_OK)
805 /* Write flash block */
806 for (i = 0; i < nwrite; i++) {
807 ret = c2port_write_dr(dev, *(buffer+i));
811 ret = c2port_poll_in_busy(dev);
817 /* Wait for last flash write to complete */
818 ret = c2port_poll_out_ready(dev);
825 static ssize_t c2port_write_flash_data(struct kobject *kobj,
826 struct bin_attribute *attr,
827 char *buffer, loff_t offset, size_t count)
829 struct c2port_device *c2dev =
830 dev_get_drvdata(container_of(kobj,
831 struct device, kobj));
834 /* Check the device access status */
835 if (!c2dev->access || !c2dev->flash_access)
838 mutex_lock(&c2dev->mutex);
839 ret = __c2port_write_flash_data(c2dev, buffer, offset, count);
840 mutex_unlock(&c2dev->mutex);
843 dev_err(c2dev->dev, "cannot write %s flash\n", c2dev->name);
852 static struct device_attribute c2port_attrs[] = {
853 __ATTR(name, 0444, c2port_show_name, NULL),
854 __ATTR(flash_blocks_num, 0444, c2port_show_flash_blocks_num, NULL),
855 __ATTR(flash_block_size, 0444, c2port_show_flash_block_size, NULL),
856 __ATTR(flash_size, 0444, c2port_show_flash_size, NULL),
857 __ATTR(access, 0644, c2port_show_access, c2port_store_access),
858 __ATTR(reset, 0200, NULL, c2port_store_reset),
859 __ATTR(dev_id, 0444, c2port_show_dev_id, NULL),
860 __ATTR(rev_id, 0444, c2port_show_rev_id, NULL),
862 __ATTR(flash_access, 0644, c2port_show_flash_access,
863 c2port_store_flash_access),
864 __ATTR(flash_erase, 0200, NULL, c2port_store_flash_erase),
868 static struct bin_attribute c2port_bin_attrs = {
870 .name = "flash_data",
873 .read = c2port_read_flash_data,
874 .write = c2port_write_flash_data,
875 /* .size is computed at run-time */
882 struct c2port_device *c2port_device_register(char *name,
883 struct c2port_ops *ops, void *devdata)
885 struct c2port_device *c2dev;
888 if (unlikely(!ops) || unlikely(!ops->access) || \
889 unlikely(!ops->c2d_dir) || unlikely(!ops->c2ck_set) || \
890 unlikely(!ops->c2d_get) || unlikely(!ops->c2d_set))
891 return ERR_PTR(-EINVAL);
893 c2dev = kmalloc(sizeof(struct c2port_device), GFP_KERNEL);
894 if (unlikely(!c2dev))
895 return ERR_PTR(-ENOMEM);
897 ret = idr_pre_get(&c2port_idr, GFP_KERNEL);
900 goto error_idr_get_new;
903 spin_lock_irq(&c2port_idr_lock);
904 ret = idr_get_new(&c2port_idr, c2dev, &id);
905 spin_unlock_irq(&c2port_idr_lock);
908 goto error_idr_get_new;
911 c2dev->dev = device_create(c2port_class, NULL, 0, c2dev,
913 if (unlikely(!c2dev->dev)) {
915 goto error_device_create;
917 dev_set_drvdata(c2dev->dev, c2dev);
919 strncpy(c2dev->name, name, C2PORT_NAME_LEN);
921 mutex_init(&c2dev->mutex);
923 /* Create binary file */
924 c2port_bin_attrs.size = ops->blocks_num * ops->block_size;
925 ret = device_create_bin_file(c2dev->dev, &c2port_bin_attrs);
927 goto error_device_create_bin_file;
929 /* By default C2 port access is off */
930 c2dev->access = c2dev->flash_access = 0;
931 ops->access(c2dev, 0);
933 dev_info(c2dev->dev, "C2 port %s added\n", name);
934 dev_info(c2dev->dev, "%s flash has %d blocks x %d bytes "
935 "(%d bytes total)\n",
936 name, ops->blocks_num, ops->block_size,
937 ops->blocks_num * ops->block_size);
941 error_device_create_bin_file:
942 device_destroy(c2port_class, 0);
945 spin_lock_irq(&c2port_idr_lock);
946 idr_remove(&c2port_idr, id);
947 spin_unlock_irq(&c2port_idr_lock);
954 EXPORT_SYMBOL(c2port_device_register);
956 void c2port_device_unregister(struct c2port_device *c2dev)
961 dev_info(c2dev->dev, "C2 port %s removed\n", c2dev->name);
963 device_remove_bin_file(c2dev->dev, &c2port_bin_attrs);
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");
985 printk(KERN_ERR "c2port: failed to allocate class\n");
988 c2port_class->dev_attrs = c2port_attrs;
993 static void __exit c2port_exit(void)
995 class_destroy(c2port_class);
998 module_init(c2port_init);
999 module_exit(c2port_exit);
1002 MODULE_DESCRIPTION("Silicon Labs C2 port support v. " DRIVER_VERSION);
1003 MODULE_LICENSE("GPL");