12 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/errno.h>
17 #include <linux/kernel.h>
19 #include <linux/ctype.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
27 #define DRIVER_NAME "c2port"
28 #define DRIVER_VERSION "0.51.0"
37 static struct class *c2port_class;
44 #define C2PORT_DEVICEID 0x00
45 #define C2PORT_REVID 0x01
46 #define C2PORT_FPCTL 0x02
47 #define C2PORT_FPDAT 0xB4
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
57 #define C2PORT_INVALID_COMMAND 0x00
58 #define C2PORT_COMMAND_FAILED 0x02
59 #define C2PORT_COMMAND_OK 0x0d
109 c2port_strobe_ck(dev);
114 c2port_strobe_ck(dev);
116 c2port_strobe_ck(dev);
119 for (i = 0; i < 8; i++) {
120 ops->
c2d_set(dev, addr & 0x01);
121 c2port_strobe_ck(dev);
128 c2port_strobe_ck(dev);
137 c2port_strobe_ck(dev);
142 c2port_strobe_ck(dev);
144 c2port_strobe_ck(dev);
149 for (i = 0; i < 8; i++) {
152 c2port_strobe_ck(dev);
158 c2port_strobe_ck(dev);
169 c2port_strobe_ck(dev);
174 c2port_strobe_ck(dev);
176 c2port_strobe_ck(dev);
180 c2port_strobe_ck(dev);
182 c2port_strobe_ck(dev);
185 for (i = 0; i < 8; i++) {
186 ops->
c2d_set(dev, data & 0x01);
187 c2port_strobe_ck(dev);
196 c2port_strobe_ck(dev);
201 }
while (--timeout > 0);
206 c2port_strobe_ck(dev);
217 c2port_strobe_ck(dev);
222 c2port_strobe_ck(dev);
224 c2port_strobe_ck(dev);
228 c2port_strobe_ck(dev);
230 c2port_strobe_ck(dev);
236 c2port_strobe_ck(dev);
241 }
while (--timeout > 0);
247 for (i = 0; i < 8; i++) {
250 c2port_strobe_ck(dev);
256 c2port_strobe_ck(dev);
264 int ret, timeout = 20;
267 ret = (c2port_read_ar(dev, &addr));
275 }
while (--timeout > 0);
285 int ret, timeout = 10000;
288 ret = (c2port_read_ar(dev, &addr));
296 }
while (--timeout > 0);
315 static ssize_t c2port_show_flash_blocks_num(
struct device *dev,
324 static ssize_t c2port_show_flash_block_size(
struct device *dev,
358 ret =
sscanf(buf,
"%d", &status);
381 const char *buf,
size_t count)
408 ret = c2port_read_dr(dev, &data);
412 return sprintf(buf,
"%d\n", data);
426 ret = __c2port_show_dev_id(c2dev, buf);
430 dev_err(dev,
"cannot read from %s\n", c2dev->
name);
444 ret = c2port_read_dr(dev, &data);
448 return sprintf(buf,
"%d\n", data);
462 ret = __c2port_show_rev_id(c2dev, buf);
499 ret = c2port_write_dr(dev, 0x02);
504 ret = c2port_write_dr(dev, 0x01);
517 const char *buf,
size_t count)
523 ret =
sscanf(buf,
"%d", &status);
528 ret = __c2port_store_flash_access(c2dev, status);
532 dev_err(c2dev->
dev,
"cannot enable %s flash programming\n",
554 ret = c2port_poll_in_busy(dev);
561 ret = c2port_poll_out_ready(dev);
566 ret = c2port_read_dr(dev, &status);
577 c2port_write_dr(dev, 0xde);
578 ret = c2port_poll_in_busy(dev);
581 c2port_write_dr(dev, 0
xad);
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);
599 const char *buf,
size_t count)
609 ret = __c2port_write_flash_erase(c2dev);
646 ret = c2port_poll_in_busy(dev);
653 ret = c2port_poll_out_ready(dev);
658 ret = c2port_read_dr(dev, &status);
665 c2port_write_dr(dev, offset >> 8);
666 ret = c2port_poll_in_busy(dev);
671 c2port_write_dr(dev, offset & 0x00ff);
672 ret = c2port_poll_in_busy(dev);
677 c2port_write_dr(dev, nread);
678 ret = c2port_poll_in_busy(dev);
685 ret = c2port_poll_out_ready(dev);
690 ret = c2port_read_dr(dev, &status);
697 for (i = 0; i < nread; i++) {
698 ret = c2port_poll_out_ready(dev);
702 ret = c2port_read_dr(dev, buffer+i);
712 char *buffer, loff_t offset,
size_t count)
724 ret = __c2port_read_flash_data(c2dev, buffer, offset, count);
734 char *buffer, loff_t offset,
size_t count)
757 ret = c2port_poll_in_busy(dev);
764 ret = c2port_poll_out_ready(dev);
769 ret = c2port_read_dr(dev, &status);
776 c2port_write_dr(dev, offset >> 8);
777 ret = c2port_poll_in_busy(dev);
782 c2port_write_dr(dev, offset & 0x00ff);
783 ret = c2port_poll_in_busy(dev);
788 c2port_write_dr(dev, nwrite);
789 ret = c2port_poll_in_busy(dev);
796 ret = c2port_poll_out_ready(dev);
801 ret = c2port_read_dr(dev, &status);
808 for (i = 0; i < nwrite; i++) {
809 ret = c2port_write_dr(dev, *(buffer+i));
813 ret = c2port_poll_in_busy(dev);
820 ret = c2port_poll_out_ready(dev);
829 char *buffer, loff_t offset,
size_t count)
841 ret = __c2port_write_flash_data(c2dev, buffer, offset, count);
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),
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),
872 .name =
"flash_data",
875 .read = c2port_read_flash_data,
876 .write = c2port_write_flash_data,
903 goto error_idr_get_new;
906 spin_lock_irq(&c2port_idr_lock);
908 spin_unlock_irq(&c2port_idr_lock);
911 goto error_idr_get_new;
917 ret = PTR_ERR(c2dev->
dev);
918 goto error_device_create;
930 goto error_device_create_bin_file;
937 dev_info(c2dev->
dev,
"%s flash has %d blocks x %d bytes "
938 "(%d bytes total)\n",
944 error_device_create_bin_file:
948 spin_lock_irq(&c2port_idr_lock);
950 spin_unlock_irq(&c2port_idr_lock);
967 spin_lock_irq(&c2port_idr_lock);
969 spin_unlock_irq(&c2port_idr_lock);
981 static int __init c2port_init(
void)
984 " - (C) 2007 Rodolfo Giometti\n");
987 if (IS_ERR(c2port_class)) {
989 return PTR_ERR(c2port_class);
996 static void __exit c2port_exit(
void)