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>
22 #include <linux/c2port.h>
24 #define DRIVER_NAME "c2port"
25 #define DRIVER_VERSION "0.51.0"
27 static DEFINE_SPINLOCK(c2port_idr_lock);
28 static DEFINE_IDR(c2port_idr);
34 static struct class *c2port_class;
37 * C2 registers & commands defines
41 #define C2PORT_DEVICEID 0x00
42 #define C2PORT_REVID 0x01
43 #define C2PORT_FPCTL 0x02
44 #define C2PORT_FPDAT 0xB4
46 /* C2 interface commands */
47 #define C2PORT_GET_VERSION 0x01
48 #define C2PORT_DEVICE_ERASE 0x03
49 #define C2PORT_BLOCK_READ 0x06
50 #define C2PORT_BLOCK_WRITE 0x07
51 #define C2PORT_PAGE_ERASE 0x08
53 /* C2 status return codes */
54 #define C2PORT_INVALID_COMMAND 0x00
55 #define C2PORT_COMMAND_FAILED 0x02
56 #define C2PORT_COMMAND_OK 0x0d
59 * C2 port low level signal managements
62 static void c2port_reset(struct c2port_device *dev)
64 struct c2port_ops *ops = dev->ops;
66 /* To reset the device we have to keep clock line low for at least
70 ops->c2ck_set(dev, 0);
72 ops->c2ck_set(dev, 1);
78 static void c2port_strobe_ck(struct c2port_device *dev)
80 struct c2port_ops *ops = dev->ops;
82 /* During hi-low-hi transition we disable local IRQs to avoid
83 * interructions since C2 port specification says that it must be
84 * shorter than 5us, otherwise the microcontroller may consider
85 * it as a reset signal!
88 ops->c2ck_set(dev, 0);
90 ops->c2ck_set(dev, 1);
97 * C2 port basic functions
100 static void c2port_write_ar(struct c2port_device *dev, u8 addr)
102 struct c2port_ops *ops = dev->ops;
106 c2port_strobe_ck(dev);
108 /* INS field (11b, LSB first) */
109 ops->c2d_dir(dev, 0);
110 ops->c2d_set(dev, 1);
111 c2port_strobe_ck(dev);
112 ops->c2d_set(dev, 1);
113 c2port_strobe_ck(dev);
116 for (i = 0; i < 8; i++) {
117 ops->c2d_set(dev, addr & 0x01);
118 c2port_strobe_ck(dev);
124 ops->c2d_dir(dev, 1);
125 c2port_strobe_ck(dev);
128 static int c2port_read_ar(struct c2port_device *dev, u8 *addr)
130 struct c2port_ops *ops = dev->ops;
134 c2port_strobe_ck(dev);
136 /* INS field (10b, LSB first) */
137 ops->c2d_dir(dev, 0);
138 ops->c2d_set(dev, 0);
139 c2port_strobe_ck(dev);
140 ops->c2d_set(dev, 1);
141 c2port_strobe_ck(dev);
144 ops->c2d_dir(dev, 1);
146 for (i = 0; i < 8; i++) {
147 *addr >>= 1; /* shift in 8-bit ADDRESS field LSB first */
149 c2port_strobe_ck(dev);
150 if (ops->c2d_get(dev))
155 c2port_strobe_ck(dev);
160 static int c2port_write_dr(struct c2port_device *dev, u8 data)
162 struct c2port_ops *ops = dev->ops;
166 c2port_strobe_ck(dev);
168 /* INS field (01b, LSB first) */
169 ops->c2d_dir(dev, 0);
170 ops->c2d_set(dev, 1);
171 c2port_strobe_ck(dev);
172 ops->c2d_set(dev, 0);
173 c2port_strobe_ck(dev);
175 /* LENGTH field (00b, LSB first -> 1 byte) */
176 ops->c2d_set(dev, 0);
177 c2port_strobe_ck(dev);
178 ops->c2d_set(dev, 0);
179 c2port_strobe_ck(dev);
182 for (i = 0; i < 8; i++) {
183 ops->c2d_set(dev, data & 0x01);
184 c2port_strobe_ck(dev);
190 ops->c2d_dir(dev, 1);
193 c2port_strobe_ck(dev);
194 if (ops->c2d_get(dev))
198 } while (--timeout > 0);
203 c2port_strobe_ck(dev);
208 static int c2port_read_dr(struct c2port_device *dev, u8 *data)
210 struct c2port_ops *ops = dev->ops;
214 c2port_strobe_ck(dev);
216 /* INS field (00b, LSB first) */
217 ops->c2d_dir(dev, 0);
218 ops->c2d_set(dev, 0);
219 c2port_strobe_ck(dev);
220 ops->c2d_set(dev, 0);
221 c2port_strobe_ck(dev);
223 /* LENGTH field (00b, LSB first -> 1 byte) */
224 ops->c2d_set(dev, 0);
225 c2port_strobe_ck(dev);
226 ops->c2d_set(dev, 0);
227 c2port_strobe_ck(dev);
230 ops->c2d_dir(dev, 1);
233 c2port_strobe_ck(dev);
234 if (ops->c2d_get(dev))
238 } while (--timeout > 0);
244 for (i = 0; i < 8; i++) {
245 *data >>= 1; /* shift in 8-bit DATA field LSB first */
247 c2port_strobe_ck(dev);
248 if (ops->c2d_get(dev))
253 c2port_strobe_ck(dev);
258 static int c2port_poll_in_busy(struct c2port_device *dev)
261 int ret, timeout = 20;
264 ret = (c2port_read_ar(dev, &addr));
272 } while (--timeout > 0);
279 static int c2port_poll_out_ready(struct c2port_device *dev)
282 int ret, timeout = 10000; /* erase flash needs long time... */
285 ret = (c2port_read_ar(dev, &addr));
293 } while (--timeout > 0);
304 static ssize_t c2port_show_name(struct device *dev,
305 struct device_attribute *attr, char *buf)
307 struct c2port_device *c2dev = dev_get_drvdata(dev);
309 return sprintf(buf, "%s\n", c2dev->name);
312 static ssize_t c2port_show_flash_blocks_num(struct device *dev,
313 struct device_attribute *attr, char *buf)
315 struct c2port_device *c2dev = dev_get_drvdata(dev);
316 struct c2port_ops *ops = c2dev->ops;
318 return sprintf(buf, "%d\n", ops->blocks_num);
321 static ssize_t c2port_show_flash_block_size(struct device *dev,
322 struct device_attribute *attr, char *buf)
324 struct c2port_device *c2dev = dev_get_drvdata(dev);
325 struct c2port_ops *ops = c2dev->ops;
327 return sprintf(buf, "%d\n", ops->block_size);
330 static ssize_t c2port_show_flash_size(struct device *dev,
331 struct device_attribute *attr, char *buf)
333 struct c2port_device *c2dev = dev_get_drvdata(dev);
334 struct c2port_ops *ops = c2dev->ops;
336 return sprintf(buf, "%d\n", ops->blocks_num * ops->block_size);
339 static ssize_t c2port_show_access(struct device *dev,
340 struct device_attribute *attr, char *buf)
342 struct c2port_device *c2dev = dev_get_drvdata(dev);
344 return sprintf(buf, "%d\n", c2dev->access);
347 static ssize_t c2port_store_access(struct device *dev,
348 struct device_attribute *attr,
349 const char *buf, size_t count)
351 struct c2port_device *c2dev = dev_get_drvdata(dev);
352 struct c2port_ops *ops = c2dev->ops;
355 ret = sscanf(buf, "%d", &status);
359 mutex_lock(&c2dev->mutex);
361 c2dev->access = !!status;
363 /* If access is "on" clock should be HIGH _before_ setting the line
364 * as output and data line should be set as INPUT anyway */
366 ops->c2ck_set(c2dev, 1);
367 ops->access(c2dev, c2dev->access);
369 ops->c2d_dir(c2dev, 1);
371 mutex_unlock(&c2dev->mutex);
376 static ssize_t c2port_store_reset(struct device *dev,
377 struct device_attribute *attr,
378 const char *buf, size_t count)
380 struct c2port_device *c2dev = dev_get_drvdata(dev);
382 /* Check the device access status */
386 mutex_lock(&c2dev->mutex);
389 c2dev->flash_access = 0;
391 mutex_unlock(&c2dev->mutex);
396 static ssize_t __c2port_show_dev_id(struct c2port_device *dev, char *buf)
401 /* Select DEVICEID register for C2 data register accesses */
402 c2port_write_ar(dev, C2PORT_DEVICEID);
404 /* Read and return the device ID register */
405 ret = c2port_read_dr(dev, &data);
409 return sprintf(buf, "%d\n", data);
412 static ssize_t c2port_show_dev_id(struct device *dev,
413 struct device_attribute *attr, char *buf)
415 struct c2port_device *c2dev = dev_get_drvdata(dev);
418 /* Check the device access status */
422 mutex_lock(&c2dev->mutex);
423 ret = __c2port_show_dev_id(c2dev, buf);
424 mutex_unlock(&c2dev->mutex);
427 dev_err(dev, "cannot read from %s\n", c2dev->name);
432 static ssize_t __c2port_show_rev_id(struct c2port_device *dev, char *buf)
437 /* Select REVID register for C2 data register accesses */
438 c2port_write_ar(dev, C2PORT_REVID);
440 /* Read and return the revision ID register */
441 ret = c2port_read_dr(dev, &data);
445 return sprintf(buf, "%d\n", data);
448 static ssize_t c2port_show_rev_id(struct device *dev,
449 struct device_attribute *attr, char *buf)
451 struct c2port_device *c2dev = dev_get_drvdata(dev);
454 /* Check the device access status */
458 mutex_lock(&c2dev->mutex);
459 ret = __c2port_show_rev_id(c2dev, buf);
460 mutex_unlock(&c2dev->mutex);
463 dev_err(c2dev->dev, "cannot read from %s\n", c2dev->name);
468 static ssize_t c2port_show_flash_access(struct device *dev,
469 struct device_attribute *attr, char *buf)
471 struct c2port_device *c2dev = dev_get_drvdata(dev);
473 return sprintf(buf, "%d\n", c2dev->flash_access);
476 static ssize_t __c2port_store_flash_access(struct c2port_device *dev,
481 /* Check the device access status */
485 dev->flash_access = !!status;
487 /* If flash_access is off we have nothing to do... */
488 if (dev->flash_access == 0)
491 /* Target the C2 flash programming control register for C2 data
493 c2port_write_ar(dev, C2PORT_FPCTL);
495 /* Write the first keycode to enable C2 Flash programming */
496 ret = c2port_write_dr(dev, 0x02);
500 /* Write the second keycode to enable C2 Flash programming */
501 ret = c2port_write_dr(dev, 0x01);
505 /* Delay for at least 20ms to ensure the target is ready for
506 * C2 flash programming */
512 static ssize_t c2port_store_flash_access(struct device *dev,
513 struct device_attribute *attr,
514 const char *buf, size_t count)
516 struct c2port_device *c2dev = dev_get_drvdata(dev);
520 ret = sscanf(buf, "%d", &status);
524 mutex_lock(&c2dev->mutex);
525 ret = __c2port_store_flash_access(c2dev, status);
526 mutex_unlock(&c2dev->mutex);
529 dev_err(c2dev->dev, "cannot enable %s flash programming\n",
537 static ssize_t __c2port_write_flash_erase(struct c2port_device *dev)
542 /* Target the C2 flash programming data register for C2 data register
545 c2port_write_ar(dev, C2PORT_FPDAT);
547 /* Send device erase command */
548 c2port_write_dr(dev, C2PORT_DEVICE_ERASE);
550 /* Wait for input acknowledge */
551 ret = c2port_poll_in_busy(dev);
555 /* Should check status before starting FLASH access sequence */
557 /* Wait for status information */
558 ret = c2port_poll_out_ready(dev);
562 /* Read flash programming interface status */
563 ret = c2port_read_dr(dev, &status);
566 if (status != C2PORT_COMMAND_OK)
569 /* Send a three-byte arming sequence to enable the device erase.
570 * If the sequence is not received correctly, the command will be
572 * Sequence is: 0xde, 0xad, 0xa5.
574 c2port_write_dr(dev, 0xde);
575 ret = c2port_poll_in_busy(dev);
578 c2port_write_dr(dev, 0xad);
579 ret = c2port_poll_in_busy(dev);
582 c2port_write_dr(dev, 0xa5);
583 ret = c2port_poll_in_busy(dev);
587 ret = c2port_poll_out_ready(dev);
594 static ssize_t c2port_store_flash_erase(struct device *dev,
595 struct device_attribute *attr,
596 const char *buf, size_t count)
598 struct c2port_device *c2dev = dev_get_drvdata(dev);
601 /* Check the device and flash access status */
602 if (!c2dev->access || !c2dev->flash_access)
605 mutex_lock(&c2dev->mutex);
606 ret = __c2port_write_flash_erase(c2dev);
607 mutex_unlock(&c2dev->mutex);
610 dev_err(c2dev->dev, "cannot erase %s flash\n", c2dev->name);
617 static ssize_t __c2port_read_flash_data(struct c2port_device *dev,
618 char *buffer, loff_t offset, size_t count)
620 struct c2port_ops *ops = dev->ops;
621 u8 status, nread = 128;
624 /* Check for flash end */
625 if (offset >= ops->block_size * ops->blocks_num)
628 if (ops->block_size * ops->blocks_num - offset < nread)
629 nread = ops->block_size * ops->blocks_num - offset;
635 /* Target the C2 flash programming data register for C2 data register
637 c2port_write_ar(dev, C2PORT_FPDAT);
639 /* Send flash block read command */
640 c2port_write_dr(dev, C2PORT_BLOCK_READ);
642 /* Wait for input acknowledge */
643 ret = c2port_poll_in_busy(dev);
647 /* Should check status before starting FLASH access sequence */
649 /* Wait for status information */
650 ret = c2port_poll_out_ready(dev);
654 /* Read flash programming interface status */
655 ret = c2port_read_dr(dev, &status);
658 if (status != C2PORT_COMMAND_OK)
661 /* Send address high byte */
662 c2port_write_dr(dev, offset >> 8);
663 ret = c2port_poll_in_busy(dev);
667 /* Send address low byte */
668 c2port_write_dr(dev, offset & 0x00ff);
669 ret = c2port_poll_in_busy(dev);
673 /* Send address block size */
674 c2port_write_dr(dev, nread);
675 ret = c2port_poll_in_busy(dev);
679 /* Should check status before reading FLASH block */
681 /* Wait for status information */
682 ret = c2port_poll_out_ready(dev);
686 /* Read flash programming interface status */
687 ret = c2port_read_dr(dev, &status);
690 if (status != C2PORT_COMMAND_OK)
693 /* Read flash block */
694 for (i = 0; i < nread; i++) {
695 ret = c2port_poll_out_ready(dev);
699 ret = c2port_read_dr(dev, buffer+i);
707 static ssize_t c2port_read_flash_data(struct kobject *kobj,
708 struct bin_attribute *attr,
709 char *buffer, loff_t offset, size_t count)
711 struct c2port_device *c2dev =
712 dev_get_drvdata(container_of(kobj,
713 struct device, kobj));
716 /* Check the device and flash access status */
717 if (!c2dev->access || !c2dev->flash_access)
720 mutex_lock(&c2dev->mutex);
721 ret = __c2port_read_flash_data(c2dev, buffer, offset, count);
722 mutex_unlock(&c2dev->mutex);
725 dev_err(c2dev->dev, "cannot read %s flash\n", c2dev->name);
730 static ssize_t __c2port_write_flash_data(struct c2port_device *dev,
731 char *buffer, loff_t offset, size_t count)
733 struct c2port_ops *ops = dev->ops;
734 u8 status, nwrite = 128;
739 if (ops->block_size * ops->blocks_num - offset < nwrite)
740 nwrite = ops->block_size * ops->blocks_num - offset;
742 /* Check for flash end */
743 if (offset >= ops->block_size * ops->blocks_num)
746 /* Target the C2 flash programming data register for C2 data register
748 c2port_write_ar(dev, C2PORT_FPDAT);
750 /* Send flash block write command */
751 c2port_write_dr(dev, C2PORT_BLOCK_WRITE);
753 /* Wait for input acknowledge */
754 ret = c2port_poll_in_busy(dev);
758 /* Should check status before starting FLASH access sequence */
760 /* Wait for status information */
761 ret = c2port_poll_out_ready(dev);
765 /* Read flash programming interface status */
766 ret = c2port_read_dr(dev, &status);
769 if (status != C2PORT_COMMAND_OK)
772 /* Send address high byte */
773 c2port_write_dr(dev, offset >> 8);
774 ret = c2port_poll_in_busy(dev);
778 /* Send address low byte */
779 c2port_write_dr(dev, offset & 0x00ff);
780 ret = c2port_poll_in_busy(dev);
784 /* Send address block size */
785 c2port_write_dr(dev, nwrite);
786 ret = c2port_poll_in_busy(dev);
790 /* Should check status before writing FLASH block */
792 /* Wait for status information */
793 ret = c2port_poll_out_ready(dev);
797 /* Read flash programming interface status */
798 ret = c2port_read_dr(dev, &status);
801 if (status != C2PORT_COMMAND_OK)
804 /* Write flash block */
805 for (i = 0; i < nwrite; i++) {
806 ret = c2port_write_dr(dev, *(buffer+i));
810 ret = c2port_poll_in_busy(dev);
816 /* Wait for last flash write to complete */
817 ret = c2port_poll_out_ready(dev);
824 static ssize_t c2port_write_flash_data(struct kobject *kobj,
825 struct bin_attribute *attr,
826 char *buffer, loff_t offset, size_t count)
828 struct c2port_device *c2dev =
829 dev_get_drvdata(container_of(kobj,
830 struct device, kobj));
833 /* Check the device access status */
834 if (!c2dev->access || !c2dev->flash_access)
837 mutex_lock(&c2dev->mutex);
838 ret = __c2port_write_flash_data(c2dev, buffer, offset, count);
839 mutex_unlock(&c2dev->mutex);
842 dev_err(c2dev->dev, "cannot write %s flash\n", c2dev->name);
851 static struct device_attribute c2port_attrs[] = {
852 __ATTR(name, 0444, c2port_show_name, NULL),
853 __ATTR(flash_blocks_num, 0444, c2port_show_flash_blocks_num, NULL),
854 __ATTR(flash_block_size, 0444, c2port_show_flash_block_size, NULL),
855 __ATTR(flash_size, 0444, c2port_show_flash_size, NULL),
856 __ATTR(access, 0644, c2port_show_access, c2port_store_access),
857 __ATTR(reset, 0200, NULL, c2port_store_reset),
858 __ATTR(dev_id, 0444, c2port_show_dev_id, NULL),
859 __ATTR(rev_id, 0444, c2port_show_rev_id, NULL),
861 __ATTR(flash_access, 0644, c2port_show_flash_access,
862 c2port_store_flash_access),
863 __ATTR(flash_erase, 0200, NULL, c2port_store_flash_erase),
867 static struct bin_attribute c2port_bin_attrs = {
869 .name = "flash_data",
872 .read = c2port_read_flash_data,
873 .write = c2port_write_flash_data,
874 /* .size is computed at run-time */
881 struct c2port_device *c2port_device_register(char *name,
882 struct c2port_ops *ops, void *devdata)
884 struct c2port_device *c2dev;
887 if (unlikely(!ops) || unlikely(!ops->access) || \
888 unlikely(!ops->c2d_dir) || unlikely(!ops->c2ck_set) || \
889 unlikely(!ops->c2d_get) || unlikely(!ops->c2d_set))
890 return ERR_PTR(-EINVAL);
892 c2dev = kmalloc(sizeof(struct c2port_device), GFP_KERNEL);
893 if (unlikely(!c2dev))
894 return ERR_PTR(-ENOMEM);
896 ret = idr_pre_get(&c2port_idr, GFP_KERNEL);
899 goto error_idr_get_new;
902 spin_lock_irq(&c2port_idr_lock);
903 ret = idr_get_new(&c2port_idr, c2dev, &id);
904 spin_unlock_irq(&c2port_idr_lock);
907 goto error_idr_get_new;
910 c2dev->dev = device_create(c2port_class, NULL, 0, c2dev,
912 if (unlikely(!c2dev->dev)) {
914 goto error_device_create;
916 dev_set_drvdata(c2dev->dev, c2dev);
918 strncpy(c2dev->name, name, C2PORT_NAME_LEN);
920 mutex_init(&c2dev->mutex);
922 /* Create binary file */
923 c2port_bin_attrs.size = ops->blocks_num * ops->block_size;
924 ret = device_create_bin_file(c2dev->dev, &c2port_bin_attrs);
926 goto error_device_create_bin_file;
928 /* By default C2 port access is off */
929 c2dev->access = c2dev->flash_access = 0;
930 ops->access(c2dev, 0);
932 dev_info(c2dev->dev, "C2 port %s added\n", name);
933 dev_info(c2dev->dev, "%s flash has %d blocks x %d bytes "
934 "(%d bytes total)\n",
935 name, ops->blocks_num, ops->block_size,
936 ops->blocks_num * ops->block_size);
940 error_device_create_bin_file:
941 device_destroy(c2port_class, 0);
944 spin_lock_irq(&c2port_idr_lock);
945 idr_remove(&c2port_idr, id);
946 spin_unlock_irq(&c2port_idr_lock);
953 EXPORT_SYMBOL(c2port_device_register);
955 void c2port_device_unregister(struct c2port_device *c2dev)
960 dev_info(c2dev->dev, "C2 port %s removed\n", c2dev->name);
962 device_remove_bin_file(c2dev->dev, &c2port_bin_attrs);
963 spin_lock_irq(&c2port_idr_lock);
964 idr_remove(&c2port_idr, c2dev->id);
965 spin_unlock_irq(&c2port_idr_lock);
967 device_destroy(c2port_class, c2dev->id);
971 EXPORT_SYMBOL(c2port_device_unregister);
977 static int __init c2port_init(void)
979 printk(KERN_INFO "Silicon Labs C2 port support v. " DRIVER_VERSION
980 " - (C) 2007 Rodolfo Giometti\n");
982 c2port_class = class_create(THIS_MODULE, "c2port");
984 printk(KERN_ERR "c2port: failed to allocate class\n");
987 c2port_class->dev_attrs = c2port_attrs;
992 static void __exit c2port_exit(void)
994 class_destroy(c2port_class);
997 module_init(c2port_init);
998 module_exit(c2port_exit);
1000 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1001 MODULE_DESCRIPTION("Silicon Labs C2 port support v. " DRIVER_VERSION);
1002 MODULE_LICENSE("GPL");