1 /* IO interface mux allocator for ETRAX100LX.
2 * Copyright 2004-2007, Axis Communications AB
6 /* C.f. ETRAX100LX Designer's Reference chapter 19.9 */
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/errno.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
14 #include <arch/svinto.h>
16 #include <arch/io_interface_mux.h>
21 /* Macro to access ETRAX 100 registers */
22 #define SETS(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
23 IO_STATE_(reg##_, field##_, _##val)
36 void (*notify)(const unsigned int gpio_in_available,
37 const unsigned int gpio_out_available,
38 const unsigned char pa_available,
39 const unsigned char pb_available);
46 enum io_if_group group;
47 /* name - the name of the group 'A' to 'F' */
49 /* used - a bit mask of all pins in the group in the order listed
50 * in the tables in 19.9.1 to 19.9.6. Note that no
51 * distinction is made between in, out and in/out pins. */
58 enum cris_io_interface ioif;
59 /* name - the name of the interface */
61 /* groups - OR'ed together io_if_group flags describing what pin groups
62 * the interface uses pins in. */
64 /* used - set when the interface is allocated. */
67 /* group_a through group_f - bit masks describing what pins in the
68 * pin groups the interface uses. */
76 /* gpio_g_in, gpio_g_out, gpio_b - bit masks telling what pins in the
77 * GPIO ports the interface uses. This could be reconstucted using
78 * the group_X masks and a table of what pins the GPIO ports use,
79 * but that would be messy. */
80 unsigned int gpio_g_in;
81 unsigned int gpio_g_out;
85 static struct if_group if_groups[6] = {
118 /* The order in the array must match the order of enum
119 * cris_io_interface in io_interface_mux.h */
120 static struct interface interfaces[] = {
121 /* Begin Non-multiplexed interfaces */
154 /* End Non-multiplexed interfaces */
167 .gpio_g_in = 0x00000000,
168 .gpio_g_out = 0x00000000,
183 .gpio_g_in = 0x000000c0,
184 .gpio_g_out = 0x000000c0,
199 .gpio_g_in = 0xc0000000,
200 .gpio_g_out = 0xc0000000,
204 .ioif = if_sync_serial_1,
205 .name = "sync_serial_1",
206 .groups = group_e | group_f,
215 .gpio_g_in = 0x00000000,
216 .gpio_g_out = 0x00000000,
220 .ioif = if_sync_serial_3,
221 .name = "sync_serial_3",
222 .groups = group_c | group_f,
231 .gpio_g_in = 0xc0000000,
232 .gpio_g_out = 0xc0000000,
236 .ioif = if_shared_ram,
237 .name = "shared_ram",
247 .gpio_g_in = 0x0000ff3e,
248 .gpio_g_out = 0x0000ff38,
252 .ioif = if_shared_ram_w,
253 .name = "shared_ram_w",
254 .groups = group_a | group_d,
263 .gpio_g_in = 0x00ffff3e,
264 .gpio_g_out = 0x00ffff38,
279 .gpio_g_in = 0x0000ff3e,
280 .gpio_g_out = 0x0000ff3e,
295 .gpio_g_in = 0x3eff0000,
296 .gpio_g_out = 0x3eff0000,
302 .groups = group_a | group_d,
311 .gpio_g_in = 0x00ffff3e,
312 .gpio_g_out = 0x00ffff3e,
318 .groups = group_a | group_b | group_f,
327 .gpio_g_in = 0x0000ffff,
328 .gpio_g_out = 0x0000ffff,
334 .groups = group_c | group_d | group_f,
343 .gpio_g_in = 0xffff0000,
344 .gpio_g_out = 0xffff0000,
350 .groups = group_a | group_b | group_d | group_f,
359 .gpio_g_in = 0x01ffffff,
360 .gpio_g_out = 0x07ffffff,
366 .groups = group_a | group_b | group_c | group_d,
375 .gpio_g_in = 0xf9ffffff,
376 .gpio_g_out = 0xffffffff,
391 .gpio_g_in = 0x00000000,
392 .gpio_g_out = 0x00000000,
407 .gpio_g_in = 0x00000000,
408 .gpio_g_out = 0x00000000,
414 .groups = group_e | group_f,
423 .gpio_g_in = 0x00000000,
424 .gpio_g_out = 0x00000000,
439 .gpio_g_in = 0x3e000000,
440 .gpio_g_out = 0x0c000000,
445 .ioif = if_gpio_grp_a,
456 .gpio_g_in = 0x0000ff3f,
457 .gpio_g_out = 0x0000ff3f,
461 .ioif = if_gpio_grp_b,
472 .gpio_g_in = 0x000000c0,
473 .gpio_g_out = 0x000000c0,
477 .ioif = if_gpio_grp_c,
488 .gpio_g_in = 0xc0000000,
489 .gpio_g_out = 0xc0000000,
493 .ioif = if_gpio_grp_d,
504 .gpio_g_in = 0x3fff0000,
505 .gpio_g_out = 0x3fff0000,
509 .ioif = if_gpio_grp_e,
520 .gpio_g_in = 0x00000000,
521 .gpio_g_out = 0x00000000,
525 .ioif = if_gpio_grp_f,
536 .gpio_g_in = 0x00000000,
537 .gpio_g_out = 0x00000000,
543 static struct watcher *watchers = NULL;
545 /* The pins that are free to use in the GPIO ports. */
546 static unsigned int gpio_in_pins = 0xffffffff;
547 static unsigned int gpio_out_pins = 0xffffffff;
548 static unsigned char gpio_pb_pins = 0xff;
549 static unsigned char gpio_pa_pins = 0xff;
551 /* Identifiers for the owners of the GPIO pins. */
552 static enum cris_io_interface gpio_pa_owners[8];
553 static enum cris_io_interface gpio_pb_owners[8];
554 static enum cris_io_interface gpio_pg_owners[32];
556 static int cris_io_interface_init(void);
558 static unsigned char clear_group_from_set(const unsigned char groups, struct if_group *group)
560 return (groups & ~group->group);
564 static struct if_group *get_group(const unsigned char groups)
567 for (i = 0; i < ARRAY_SIZE(if_groups); i++) {
568 if (groups & if_groups[i].group) {
569 return &if_groups[i];
576 static void notify_watchers(void)
578 struct watcher *w = watchers;
580 DBG(printk("io_interface_mux: notifying watchers\n"));
583 w->notify((const unsigned int)gpio_in_pins,
584 (const unsigned int)gpio_out_pins,
585 (const unsigned char)gpio_pa_pins,
586 (const unsigned char)gpio_pb_pins);
592 int cris_request_io_interface(enum cris_io_interface ioif, const char *device_id)
594 int set_gen_config = 0;
595 int set_gen_config_ii = 0;
596 unsigned long int gens;
597 unsigned long int gens_ii;
598 struct if_group *grp;
599 unsigned char group_set;
603 (void)cris_io_interface_init();
605 DBG(printk("cris_request_io_interface(%d, \"%s\")\n", ioif, device_id));
607 if ((ioif >= if_max_interfaces) || (ioif < 0)) {
608 printk(KERN_CRIT "cris_request_io_interface: Bad interface "
609 "%u submitted for %s\n",
615 local_irq_save(flags);
617 if (interfaces[ioif].used) {
618 printk(KERN_CRIT "cris_io_interface: Cannot allocate interface "
619 "%s for %s, in use by %s\n",
620 interfaces[ioif].name,
622 interfaces[ioif].owner);
627 /* Check that all required pins in the used groups are free
628 * before allocating. */
629 group_set = interfaces[ioif].groups;
630 while (NULL != (grp = get_group(group_set))) {
631 unsigned int if_group_use = 0;
633 switch (grp->group) {
635 if_group_use = interfaces[ioif].group_a;
638 if_group_use = interfaces[ioif].group_b;
641 if_group_use = interfaces[ioif].group_c;
644 if_group_use = interfaces[ioif].group_d;
647 if_group_use = interfaces[ioif].group_e;
650 if_group_use = interfaces[ioif].group_f;
656 if (if_group_use & grp->used) {
657 printk(KERN_INFO "cris_request_io_interface: group "
658 "%s needed by %s not available\n",
659 grp->name, interfaces[ioif].name);
664 group_set = clear_group_from_set(group_set, grp);
667 /* Are the required GPIO pins available too? */
668 if (((interfaces[ioif].gpio_g_in & gpio_in_pins) !=
669 interfaces[ioif].gpio_g_in) ||
670 ((interfaces[ioif].gpio_g_out & gpio_out_pins) !=
671 interfaces[ioif].gpio_g_out) ||
672 ((interfaces[ioif].gpio_b & gpio_pb_pins) !=
673 interfaces[ioif].gpio_b)) {
674 printk(KERN_CRIT "cris_request_io_interface: Could not get "
675 "required pins for interface %u\n", ioif);
680 /* Check which registers need to be reconfigured. */
681 gens = genconfig_shadow;
682 gens_ii = gen_config_ii_shadow;
687 /* Begin Non-multiplexed interfaces */
693 /* End Non-multiplexed interfaces */
695 set_gen_config_ii = 1;
696 SETS(gens_ii, R_GEN_CONFIG_II, sermode1, async);
699 SETS(gens, R_GEN_CONFIG, ser2, select);
702 SETS(gens, R_GEN_CONFIG, ser3, select);
703 set_gen_config_ii = 1;
704 SETS(gens_ii, R_GEN_CONFIG_II, sermode3, async);
706 case if_sync_serial_1:
707 set_gen_config_ii = 1;
708 SETS(gens_ii, R_GEN_CONFIG_II, sermode1, sync);
710 case if_sync_serial_3:
711 SETS(gens, R_GEN_CONFIG, ser3, select);
712 set_gen_config_ii = 1;
713 SETS(gens_ii, R_GEN_CONFIG_II, sermode3, sync);
716 SETS(gens, R_GEN_CONFIG, mio, select);
718 case if_shared_ram_w:
719 SETS(gens, R_GEN_CONFIG, mio_w, select);
722 SETS(gens, R_GEN_CONFIG, par0, select);
725 SETS(gens, R_GEN_CONFIG, par1, select);
728 SETS(gens, R_GEN_CONFIG, par0, select);
729 SETS(gens, R_GEN_CONFIG, par_w, select);
732 SETS(gens, R_GEN_CONFIG, scsi0, select);
735 SETS(gens, R_GEN_CONFIG, scsi1, select);
738 SETS(gens, R_GEN_CONFIG, scsi0, select);
739 SETS(gens, R_GEN_CONFIG, scsi0w, select);
742 SETS(gens, R_GEN_CONFIG, ata, select);
750 SETS(gens, R_GEN_CONFIG, usb1, select);
753 SETS(gens, R_GEN_CONFIG, usb2, select);
756 /* GPIO groups are only accounted, don't do configuration changes. */
770 printk(KERN_INFO "cris_request_io_interface: Bad interface "
771 "%u submitted for %s\n",
777 /* All needed I/O pins and pin groups are free, allocate. */
778 group_set = interfaces[ioif].groups;
779 while (NULL != (grp = get_group(group_set))) {
780 unsigned int if_group_use = 0;
782 switch (grp->group) {
784 if_group_use = interfaces[ioif].group_a;
787 if_group_use = interfaces[ioif].group_b;
790 if_group_use = interfaces[ioif].group_c;
793 if_group_use = interfaces[ioif].group_d;
796 if_group_use = interfaces[ioif].group_e;
799 if_group_use = interfaces[ioif].group_f;
804 grp->used |= if_group_use;
806 group_set = clear_group_from_set(group_set, grp);
809 interfaces[ioif].used = 1;
810 interfaces[ioif].owner = (char*)device_id;
812 if (set_gen_config) {
814 genconfig_shadow = gens;
815 *R_GEN_CONFIG = genconfig_shadow;
816 /* Wait 12 cycles before doing any DMA command */
817 for(i = 6; i > 0; i--)
820 if (set_gen_config_ii) {
821 gen_config_ii_shadow = gens_ii;
822 *R_GEN_CONFIG_II = gen_config_ii_shadow;
825 DBG(printk(KERN_DEBUG "GPIO pins: available before: "
826 "g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
827 gpio_in_pins, gpio_out_pins, gpio_pb_pins));
828 DBG(printk(KERN_DEBUG
829 "grabbing pins: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
830 interfaces[ioif].gpio_g_in,
831 interfaces[ioif].gpio_g_out,
832 interfaces[ioif].gpio_b));
834 gpio_in_pins &= ~interfaces[ioif].gpio_g_in;
835 gpio_out_pins &= ~interfaces[ioif].gpio_g_out;
836 gpio_pb_pins &= ~interfaces[ioif].gpio_b;
838 DBG(printk(KERN_DEBUG "GPIO pins: available after: "
839 "g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
840 gpio_in_pins, gpio_out_pins, gpio_pb_pins));
843 local_irq_restore(flags);
850 void cris_free_io_interface(enum cris_io_interface ioif)
852 struct if_group *grp;
853 unsigned char group_set;
856 (void)cris_io_interface_init();
858 if ((ioif >= if_max_interfaces) || (ioif < 0)) {
859 printk(KERN_CRIT "cris_free_io_interface: Bad interface %u\n",
863 local_irq_save(flags);
864 if (!interfaces[ioif].used) {
865 printk(KERN_CRIT "cris_free_io_interface: Freeing free interface %u\n",
867 local_irq_restore(flags);
870 group_set = interfaces[ioif].groups;
871 while (NULL != (grp = get_group(group_set))) {
872 unsigned int if_group_use = 0;
874 switch (grp->group) {
876 if_group_use = interfaces[ioif].group_a;
879 if_group_use = interfaces[ioif].group_b;
882 if_group_use = interfaces[ioif].group_c;
885 if_group_use = interfaces[ioif].group_d;
888 if_group_use = interfaces[ioif].group_e;
891 if_group_use = interfaces[ioif].group_f;
897 if ((grp->used & if_group_use) != if_group_use)
899 grp->used = grp->used & ~if_group_use;
901 group_set = clear_group_from_set(group_set, grp);
903 interfaces[ioif].used = 0;
904 interfaces[ioif].owner = NULL;
906 DBG(printk("GPIO pins: available before: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
907 gpio_in_pins, gpio_out_pins, gpio_pb_pins));
908 DBG(printk("freeing pins: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
909 interfaces[ioif].gpio_g_in,
910 interfaces[ioif].gpio_g_out,
911 interfaces[ioif].gpio_b));
913 gpio_in_pins |= interfaces[ioif].gpio_g_in;
914 gpio_out_pins |= interfaces[ioif].gpio_g_out;
915 gpio_pb_pins |= interfaces[ioif].gpio_b;
917 DBG(printk("GPIO pins: available after: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
918 gpio_in_pins, gpio_out_pins, gpio_pb_pins));
920 local_irq_restore(flags);
925 /* Create a bitmask from bit 0 (inclusive) to bit stop_bit
926 (non-inclusive). stop_bit == 0 returns 0x0 */
927 static inline unsigned int create_mask(const unsigned stop_bit)
930 if (stop_bit >= 32) {
933 return (1<<stop_bit)-1;
937 /* port can be 'a', 'b' or 'g' */
938 int cris_io_interface_allocate_pins(const enum cris_io_interface ioif,
940 const unsigned start_bit,
941 const unsigned stop_bit)
944 unsigned int mask = 0;
945 unsigned int tmp_mask;
946 unsigned long int flags;
947 enum cris_io_interface *owners;
949 (void)cris_io_interface_init();
951 DBG(printk("cris_io_interface_allocate_pins: if=%d port=%c start=%u stop=%u\n",
952 ioif, port, start_bit, stop_bit));
954 if (!((start_bit <= stop_bit) &&
955 ((((port == 'a') || (port == 'b')) && (stop_bit < 8)) ||
956 ((port == 'g') && (stop_bit < 32))))) {
960 mask = create_mask(stop_bit + 1);
961 tmp_mask = create_mask(start_bit);
964 DBG(printk("cris_io_interface_allocate_pins: port=%c start=%u stop=%u mask=0x%08x\n",
965 port, start_bit, stop_bit, mask));
967 local_irq_save(flags);
971 if ((gpio_pa_pins & mask) != mask) {
972 local_irq_restore(flags);
975 owners = gpio_pa_owners;
976 gpio_pa_pins &= ~mask;
979 if ((gpio_pb_pins & mask) != mask) {
980 local_irq_restore(flags);
983 owners = gpio_pb_owners;
984 gpio_pb_pins &= ~mask;
987 if (((gpio_in_pins & mask) != mask) ||
988 ((gpio_out_pins & mask) != mask)) {
989 local_irq_restore(flags);
992 owners = gpio_pg_owners;
993 gpio_in_pins &= ~mask;
994 gpio_out_pins &= ~mask;
997 local_irq_restore(flags);
1001 for (i = start_bit; i <= stop_bit; i++) {
1004 local_irq_restore(flags);
1011 /* port can be 'a', 'b' or 'g' */
1012 int cris_io_interface_free_pins(const enum cris_io_interface ioif,
1014 const unsigned start_bit,
1015 const unsigned stop_bit)
1018 unsigned int mask = 0;
1019 unsigned int tmp_mask;
1020 unsigned long int flags;
1021 enum cris_io_interface *owners;
1023 (void)cris_io_interface_init();
1025 if (!((start_bit <= stop_bit) &&
1026 ((((port == 'a') || (port == 'b')) && (stop_bit < 8)) ||
1027 ((port == 'g') && (stop_bit < 32))))) {
1031 mask = create_mask(stop_bit + 1);
1032 tmp_mask = create_mask(start_bit);
1035 DBG(printk("cris_io_interface_free_pins: port=%c start=%u stop=%u mask=0x%08x\n",
1036 port, start_bit, stop_bit, mask));
1038 local_irq_save(flags);
1042 if ((~gpio_pa_pins & mask) != mask) {
1043 local_irq_restore(flags);
1044 printk(KERN_CRIT "cris_io_interface_free_pins: Freeing free pins");
1046 owners = gpio_pa_owners;
1049 if ((~gpio_pb_pins & mask) != mask) {
1050 local_irq_restore(flags);
1051 printk(KERN_CRIT "cris_io_interface_free_pins: Freeing free pins");
1053 owners = gpio_pb_owners;
1056 if (((~gpio_in_pins & mask) != mask) ||
1057 ((~gpio_out_pins & mask) != mask)) {
1058 local_irq_restore(flags);
1059 printk(KERN_CRIT "cris_io_interface_free_pins: Freeing free pins");
1061 owners = gpio_pg_owners;
1064 owners = NULL; /* Cannot happen. Shut up, gcc! */
1067 for (i = start_bit; i <= stop_bit; i++) {
1068 if (owners[i] != ioif) {
1069 printk(KERN_CRIT "cris_io_interface_free_pins: Freeing unowned pins");
1073 /* All was ok, change data. */
1076 gpio_pa_pins |= mask;
1079 gpio_pb_pins |= mask;
1082 gpio_in_pins |= mask;
1083 gpio_out_pins |= mask;
1087 for (i = start_bit; i <= stop_bit; i++) {
1088 owners[i] = if_unclaimed;
1090 local_irq_restore(flags);
1097 int cris_io_interface_register_watcher(void (*notify)(const unsigned int gpio_in_available,
1098 const unsigned int gpio_out_available,
1099 const unsigned char pa_available,
1100 const unsigned char pb_available))
1104 (void)cris_io_interface_init();
1106 if (NULL == notify) {
1109 w = kmalloc(sizeof(*w), GFP_KERNEL);
1117 w->notify((const unsigned int)gpio_in_pins,
1118 (const unsigned int)gpio_out_pins,
1119 (const unsigned char)gpio_pa_pins,
1120 (const unsigned char)gpio_pb_pins);
1125 void cris_io_interface_delete_watcher(void (*notify)(const unsigned int gpio_in_available,
1126 const unsigned int gpio_out_available,
1127 const unsigned char pa_available,
1128 const unsigned char pb_available))
1130 struct watcher *w = watchers, *prev = NULL;
1132 (void)cris_io_interface_init();
1134 while ((NULL != w) && (w->notify != notify)){
1140 prev->next = w->next;
1147 printk(KERN_WARNING "cris_io_interface_delete_watcher: Deleting unknown watcher 0x%p\n", notify);
1151 static int cris_io_interface_init(void)
1153 static int first = 1;
1161 for (i = 0; i<8; i++) {
1162 gpio_pa_owners[i] = if_unclaimed;
1163 gpio_pb_owners[i] = if_unclaimed;
1164 gpio_pg_owners[i] = if_unclaimed;
1167 gpio_pg_owners[i] = if_unclaimed;
1173 module_init(cris_io_interface_init);
1176 EXPORT_SYMBOL(cris_request_io_interface);
1177 EXPORT_SYMBOL(cris_free_io_interface);
1178 EXPORT_SYMBOL(cris_io_interface_allocate_pins);
1179 EXPORT_SYMBOL(cris_io_interface_free_pins);
1180 EXPORT_SYMBOL(cris_io_interface_register_watcher);
1181 EXPORT_SYMBOL(cris_io_interface_delete_watcher);