1 // SPDX-License-Identifier: GPL-2.0
2 /* IO interface mux allocator for ETRAX100LX.
3 * Copyright 2004-2007, Axis Communications AB
7 /* C.f. ETRAX100LX Designer's Reference chapter 19.9 */
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/errno.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
15 #include <arch/svinto.h>
17 #include <arch/io_interface_mux.h>
18 #include <arch/system.h>
23 /* Macro to access ETRAX 100 registers */
24 #define SETS(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
25 IO_STATE_(reg##_, field##_, _##val)
38 void (*notify)(const unsigned int gpio_in_available,
39 const unsigned int gpio_out_available,
40 const unsigned char pa_available,
41 const unsigned char pb_available);
48 enum io_if_group group;
49 /* name - the name of the group 'A' to 'F' */
51 /* used - a bit mask of all pins in the group in the order listed
52 * in the tables in 19.9.1 to 19.9.6. Note that no
53 * distinction is made between in, out and in/out pins. */
60 enum cris_io_interface ioif;
61 /* name - the name of the interface */
63 /* groups - OR'ed together io_if_group flags describing what pin groups
64 * the interface uses pins in. */
66 /* used - set when the interface is allocated. */
69 /* group_a through group_f - bit masks describing what pins in the
70 * pin groups the interface uses. */
78 /* gpio_g_in, gpio_g_out, gpio_b - bit masks telling what pins in the
79 * GPIO ports the interface uses. This could be reconstucted using
80 * the group_X masks and a table of what pins the GPIO ports use,
81 * but that would be messy. */
82 unsigned int gpio_g_in;
83 unsigned int gpio_g_out;
87 static struct if_group if_groups[6] = {
120 /* The order in the array must match the order of enum
121 * cris_io_interface in io_interface_mux.h */
122 static struct interface interfaces[] = {
123 /* Begin Non-multiplexed interfaces */
156 /* End Non-multiplexed interfaces */
169 .gpio_g_in = 0x00000000,
170 .gpio_g_out = 0x00000000,
185 .gpio_g_in = 0x000000c0,
186 .gpio_g_out = 0x000000c0,
201 .gpio_g_in = 0xc0000000,
202 .gpio_g_out = 0xc0000000,
206 .ioif = if_sync_serial_1,
207 .name = "sync_serial_1",
208 .groups = group_e | group_f,
217 .gpio_g_in = 0x00000000,
218 .gpio_g_out = 0x00000000,
222 .ioif = if_sync_serial_3,
223 .name = "sync_serial_3",
224 .groups = group_c | group_f,
233 .gpio_g_in = 0xc0000000,
234 .gpio_g_out = 0xc0000000,
238 .ioif = if_shared_ram,
239 .name = "shared_ram",
249 .gpio_g_in = 0x0000ff3e,
250 .gpio_g_out = 0x0000ff38,
254 .ioif = if_shared_ram_w,
255 .name = "shared_ram_w",
256 .groups = group_a | group_d,
265 .gpio_g_in = 0x00ffff3e,
266 .gpio_g_out = 0x00ffff38,
281 .gpio_g_in = 0x0000ff3e,
282 .gpio_g_out = 0x0000ff3e,
297 .gpio_g_in = 0x3eff0000,
298 .gpio_g_out = 0x3eff0000,
304 .groups = group_a | group_d,
313 .gpio_g_in = 0x00ffff3e,
314 .gpio_g_out = 0x00ffff3e,
320 .groups = group_a | group_b | group_f,
329 .gpio_g_in = 0x0000ffff,
330 .gpio_g_out = 0x0000ffff,
336 .groups = group_c | group_d | group_f,
345 .gpio_g_in = 0xffff0000,
346 .gpio_g_out = 0xffff0000,
352 .groups = group_a | group_b | group_d | group_f,
361 .gpio_g_in = 0x01ffffff,
362 .gpio_g_out = 0x07ffffff,
368 .groups = group_a | group_b | group_c | group_d,
377 .gpio_g_in = 0xf9ffffff,
378 .gpio_g_out = 0xffffffff,
393 .gpio_g_in = 0x00000000,
394 .gpio_g_out = 0x00000000,
409 .gpio_g_in = 0x00000000,
410 .gpio_g_out = 0x00000000,
416 .groups = group_e | group_f,
425 .gpio_g_in = 0x00000000,
426 .gpio_g_out = 0x00000000,
441 .gpio_g_in = 0x3e000000,
442 .gpio_g_out = 0x0c000000,
447 .ioif = if_gpio_grp_a,
458 .gpio_g_in = 0x0000ff3f,
459 .gpio_g_out = 0x0000ff3f,
463 .ioif = if_gpio_grp_b,
474 .gpio_g_in = 0x000000c0,
475 .gpio_g_out = 0x000000c0,
479 .ioif = if_gpio_grp_c,
490 .gpio_g_in = 0xc0000000,
491 .gpio_g_out = 0xc0000000,
495 .ioif = if_gpio_grp_d,
506 .gpio_g_in = 0x3fff0000,
507 .gpio_g_out = 0x3fff0000,
511 .ioif = if_gpio_grp_e,
522 .gpio_g_in = 0x00000000,
523 .gpio_g_out = 0x00000000,
527 .ioif = if_gpio_grp_f,
538 .gpio_g_in = 0x00000000,
539 .gpio_g_out = 0x00000000,
545 static struct watcher *watchers = NULL;
547 /* The pins that are free to use in the GPIO ports. */
548 static unsigned int gpio_in_pins = 0xffffffff;
549 static unsigned int gpio_out_pins = 0xffffffff;
550 static unsigned char gpio_pb_pins = 0xff;
551 static unsigned char gpio_pa_pins = 0xff;
553 /* Identifiers for the owners of the GPIO pins. */
554 static enum cris_io_interface gpio_pa_owners[8];
555 static enum cris_io_interface gpio_pb_owners[8];
556 static enum cris_io_interface gpio_pg_owners[32];
558 static int cris_io_interface_init(void);
560 static unsigned char clear_group_from_set(const unsigned char groups, struct if_group *group)
562 return (groups & ~group->group);
566 static struct if_group *get_group(const unsigned char groups)
569 for (i = 0; i < ARRAY_SIZE(if_groups); i++) {
570 if (groups & if_groups[i].group) {
571 return &if_groups[i];
578 static void notify_watchers(void)
580 struct watcher *w = watchers;
582 DBG(printk("io_interface_mux: notifying watchers\n"));
585 w->notify((const unsigned int)gpio_in_pins,
586 (const unsigned int)gpio_out_pins,
587 (const unsigned char)gpio_pa_pins,
588 (const unsigned char)gpio_pb_pins);
594 int cris_request_io_interface(enum cris_io_interface ioif, const char *device_id)
596 int set_gen_config = 0;
597 int set_gen_config_ii = 0;
598 unsigned long int gens;
599 unsigned long int gens_ii;
600 struct if_group *grp;
601 unsigned char group_set;
605 (void)cris_io_interface_init();
607 DBG(printk("cris_request_io_interface(%d, \"%s\")\n", ioif, device_id));
609 if ((ioif >= if_max_interfaces) || (ioif < 0)) {
610 printk(KERN_CRIT "cris_request_io_interface: Bad interface "
611 "%u submitted for %s\n",
617 local_irq_save(flags);
619 if (interfaces[ioif].used) {
620 printk(KERN_CRIT "cris_io_interface: Cannot allocate interface "
621 "%s for %s, in use by %s\n",
622 interfaces[ioif].name,
624 interfaces[ioif].owner);
629 /* Check that all required pins in the used groups are free
630 * before allocating. */
631 group_set = interfaces[ioif].groups;
632 while (NULL != (grp = get_group(group_set))) {
633 unsigned int if_group_use = 0;
635 switch (grp->group) {
637 if_group_use = interfaces[ioif].group_a;
640 if_group_use = interfaces[ioif].group_b;
643 if_group_use = interfaces[ioif].group_c;
646 if_group_use = interfaces[ioif].group_d;
649 if_group_use = interfaces[ioif].group_e;
652 if_group_use = interfaces[ioif].group_f;
658 if (if_group_use & grp->used) {
659 printk(KERN_INFO "cris_request_io_interface: group "
660 "%s needed by %s not available\n",
661 grp->name, interfaces[ioif].name);
666 group_set = clear_group_from_set(group_set, grp);
669 /* Are the required GPIO pins available too? */
670 if (((interfaces[ioif].gpio_g_in & gpio_in_pins) !=
671 interfaces[ioif].gpio_g_in) ||
672 ((interfaces[ioif].gpio_g_out & gpio_out_pins) !=
673 interfaces[ioif].gpio_g_out) ||
674 ((interfaces[ioif].gpio_b & gpio_pb_pins) !=
675 interfaces[ioif].gpio_b)) {
676 printk(KERN_CRIT "cris_request_io_interface: Could not get "
677 "required pins for interface %u\n", ioif);
682 /* Check which registers need to be reconfigured. */
683 gens = genconfig_shadow;
684 gens_ii = gen_config_ii_shadow;
689 /* Begin Non-multiplexed interfaces */
695 /* End Non-multiplexed interfaces */
697 set_gen_config_ii = 1;
698 SETS(gens_ii, R_GEN_CONFIG_II, sermode1, async);
701 SETS(gens, R_GEN_CONFIG, ser2, select);
704 SETS(gens, R_GEN_CONFIG, ser3, select);
705 set_gen_config_ii = 1;
706 SETS(gens_ii, R_GEN_CONFIG_II, sermode3, async);
708 case if_sync_serial_1:
709 set_gen_config_ii = 1;
710 SETS(gens_ii, R_GEN_CONFIG_II, sermode1, sync);
712 case if_sync_serial_3:
713 SETS(gens, R_GEN_CONFIG, ser3, select);
714 set_gen_config_ii = 1;
715 SETS(gens_ii, R_GEN_CONFIG_II, sermode3, sync);
718 SETS(gens, R_GEN_CONFIG, mio, select);
720 case if_shared_ram_w:
721 SETS(gens, R_GEN_CONFIG, mio_w, select);
724 SETS(gens, R_GEN_CONFIG, par0, select);
727 SETS(gens, R_GEN_CONFIG, par1, select);
730 SETS(gens, R_GEN_CONFIG, par0, select);
731 SETS(gens, R_GEN_CONFIG, par_w, select);
734 SETS(gens, R_GEN_CONFIG, scsi0, select);
737 SETS(gens, R_GEN_CONFIG, scsi1, select);
740 SETS(gens, R_GEN_CONFIG, scsi0, select);
741 SETS(gens, R_GEN_CONFIG, scsi0w, select);
744 SETS(gens, R_GEN_CONFIG, ata, select);
752 SETS(gens, R_GEN_CONFIG, usb1, select);
755 SETS(gens, R_GEN_CONFIG, usb2, select);
758 /* GPIO groups are only accounted, don't do configuration changes. */
772 printk(KERN_INFO "cris_request_io_interface: Bad interface "
773 "%u submitted for %s\n",
779 /* All needed I/O pins and pin groups are free, allocate. */
780 group_set = interfaces[ioif].groups;
781 while (NULL != (grp = get_group(group_set))) {
782 unsigned int if_group_use = 0;
784 switch (grp->group) {
786 if_group_use = interfaces[ioif].group_a;
789 if_group_use = interfaces[ioif].group_b;
792 if_group_use = interfaces[ioif].group_c;
795 if_group_use = interfaces[ioif].group_d;
798 if_group_use = interfaces[ioif].group_e;
801 if_group_use = interfaces[ioif].group_f;
806 grp->used |= if_group_use;
808 group_set = clear_group_from_set(group_set, grp);
811 interfaces[ioif].used = 1;
812 interfaces[ioif].owner = (char*)device_id;
814 if (set_gen_config) {
816 genconfig_shadow = gens;
817 *R_GEN_CONFIG = genconfig_shadow;
818 /* Wait 12 cycles before doing any DMA command */
819 for(i = 6; i > 0; i--)
822 if (set_gen_config_ii) {
823 gen_config_ii_shadow = gens_ii;
824 *R_GEN_CONFIG_II = gen_config_ii_shadow;
827 DBG(printk(KERN_DEBUG "GPIO pins: available before: "
828 "g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
829 gpio_in_pins, gpio_out_pins, gpio_pb_pins));
830 DBG(printk(KERN_DEBUG
831 "grabbing pins: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
832 interfaces[ioif].gpio_g_in,
833 interfaces[ioif].gpio_g_out,
834 interfaces[ioif].gpio_b));
836 gpio_in_pins &= ~interfaces[ioif].gpio_g_in;
837 gpio_out_pins &= ~interfaces[ioif].gpio_g_out;
838 gpio_pb_pins &= ~interfaces[ioif].gpio_b;
840 DBG(printk(KERN_DEBUG "GPIO pins: available after: "
841 "g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
842 gpio_in_pins, gpio_out_pins, gpio_pb_pins));
845 local_irq_restore(flags);
852 void cris_free_io_interface(enum cris_io_interface ioif)
854 struct if_group *grp;
855 unsigned char group_set;
858 (void)cris_io_interface_init();
860 if ((ioif >= if_max_interfaces) || (ioif < 0)) {
861 printk(KERN_CRIT "cris_free_io_interface: Bad interface %u\n",
865 local_irq_save(flags);
866 if (!interfaces[ioif].used) {
867 printk(KERN_CRIT "cris_free_io_interface: Freeing free interface %u\n",
869 local_irq_restore(flags);
872 group_set = interfaces[ioif].groups;
873 while (NULL != (grp = get_group(group_set))) {
874 unsigned int if_group_use = 0;
876 switch (grp->group) {
878 if_group_use = interfaces[ioif].group_a;
881 if_group_use = interfaces[ioif].group_b;
884 if_group_use = interfaces[ioif].group_c;
887 if_group_use = interfaces[ioif].group_d;
890 if_group_use = interfaces[ioif].group_e;
893 if_group_use = interfaces[ioif].group_f;
899 if ((grp->used & if_group_use) != if_group_use)
901 grp->used = grp->used & ~if_group_use;
903 group_set = clear_group_from_set(group_set, grp);
905 interfaces[ioif].used = 0;
906 interfaces[ioif].owner = NULL;
908 DBG(printk("GPIO pins: available before: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
909 gpio_in_pins, gpio_out_pins, gpio_pb_pins));
910 DBG(printk("freeing pins: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
911 interfaces[ioif].gpio_g_in,
912 interfaces[ioif].gpio_g_out,
913 interfaces[ioif].gpio_b));
915 gpio_in_pins |= interfaces[ioif].gpio_g_in;
916 gpio_out_pins |= interfaces[ioif].gpio_g_out;
917 gpio_pb_pins |= interfaces[ioif].gpio_b;
919 DBG(printk("GPIO pins: available after: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
920 gpio_in_pins, gpio_out_pins, gpio_pb_pins));
922 local_irq_restore(flags);
927 /* Create a bitmask from bit 0 (inclusive) to bit stop_bit
928 (non-inclusive). stop_bit == 0 returns 0x0 */
929 static inline unsigned int create_mask(const unsigned stop_bit)
932 if (stop_bit >= 32) {
935 return (1<<stop_bit)-1;
939 /* port can be 'a', 'b' or 'g' */
940 int cris_io_interface_allocate_pins(const enum cris_io_interface ioif,
942 const unsigned start_bit,
943 const unsigned stop_bit)
946 unsigned int mask = 0;
947 unsigned int tmp_mask;
948 unsigned long int flags;
949 enum cris_io_interface *owners;
951 (void)cris_io_interface_init();
953 DBG(printk("cris_io_interface_allocate_pins: if=%d port=%c start=%u stop=%u\n",
954 ioif, port, start_bit, stop_bit));
956 if (!((start_bit <= stop_bit) &&
957 ((((port == 'a') || (port == 'b')) && (stop_bit < 8)) ||
958 ((port == 'g') && (stop_bit < 32))))) {
962 mask = create_mask(stop_bit + 1);
963 tmp_mask = create_mask(start_bit);
966 DBG(printk("cris_io_interface_allocate_pins: port=%c start=%u stop=%u mask=0x%08x\n",
967 port, start_bit, stop_bit, mask));
969 local_irq_save(flags);
973 if ((gpio_pa_pins & mask) != mask) {
974 local_irq_restore(flags);
977 owners = gpio_pa_owners;
978 gpio_pa_pins &= ~mask;
981 if ((gpio_pb_pins & mask) != mask) {
982 local_irq_restore(flags);
985 owners = gpio_pb_owners;
986 gpio_pb_pins &= ~mask;
989 if (((gpio_in_pins & mask) != mask) ||
990 ((gpio_out_pins & mask) != mask)) {
991 local_irq_restore(flags);
994 owners = gpio_pg_owners;
995 gpio_in_pins &= ~mask;
996 gpio_out_pins &= ~mask;
999 local_irq_restore(flags);
1003 for (i = start_bit; i <= stop_bit; i++) {
1006 local_irq_restore(flags);
1013 /* port can be 'a', 'b' or 'g' */
1014 int cris_io_interface_free_pins(const enum cris_io_interface ioif,
1016 const unsigned start_bit,
1017 const unsigned stop_bit)
1020 unsigned int mask = 0;
1021 unsigned int tmp_mask;
1022 unsigned long int flags;
1023 enum cris_io_interface *owners;
1025 (void)cris_io_interface_init();
1027 if (!((start_bit <= stop_bit) &&
1028 ((((port == 'a') || (port == 'b')) && (stop_bit < 8)) ||
1029 ((port == 'g') && (stop_bit < 32))))) {
1033 mask = create_mask(stop_bit + 1);
1034 tmp_mask = create_mask(start_bit);
1037 DBG(printk("cris_io_interface_free_pins: port=%c start=%u stop=%u mask=0x%08x\n",
1038 port, start_bit, stop_bit, mask));
1040 local_irq_save(flags);
1044 if ((~gpio_pa_pins & mask) != mask) {
1045 local_irq_restore(flags);
1046 printk(KERN_CRIT "cris_io_interface_free_pins: Freeing free pins");
1048 owners = gpio_pa_owners;
1051 if ((~gpio_pb_pins & mask) != mask) {
1052 local_irq_restore(flags);
1053 printk(KERN_CRIT "cris_io_interface_free_pins: Freeing free pins");
1055 owners = gpio_pb_owners;
1058 if (((~gpio_in_pins & mask) != mask) ||
1059 ((~gpio_out_pins & mask) != mask)) {
1060 local_irq_restore(flags);
1061 printk(KERN_CRIT "cris_io_interface_free_pins: Freeing free pins");
1063 owners = gpio_pg_owners;
1066 owners = NULL; /* Cannot happen. Shut up, gcc! */
1069 for (i = start_bit; i <= stop_bit; i++) {
1070 if (owners[i] != ioif) {
1071 printk(KERN_CRIT "cris_io_interface_free_pins: Freeing unowned pins");
1075 /* All was ok, change data. */
1078 gpio_pa_pins |= mask;
1081 gpio_pb_pins |= mask;
1084 gpio_in_pins |= mask;
1085 gpio_out_pins |= mask;
1089 for (i = start_bit; i <= stop_bit; i++) {
1090 owners[i] = if_unclaimed;
1092 local_irq_restore(flags);
1099 int cris_io_interface_register_watcher(void (*notify)(const unsigned int gpio_in_available,
1100 const unsigned int gpio_out_available,
1101 const unsigned char pa_available,
1102 const unsigned char pb_available))
1106 (void)cris_io_interface_init();
1108 if (NULL == notify) {
1111 w = kmalloc(sizeof(*w), GFP_KERNEL);
1119 w->notify((const unsigned int)gpio_in_pins,
1120 (const unsigned int)gpio_out_pins,
1121 (const unsigned char)gpio_pa_pins,
1122 (const unsigned char)gpio_pb_pins);
1127 void cris_io_interface_delete_watcher(void (*notify)(const unsigned int gpio_in_available,
1128 const unsigned int gpio_out_available,
1129 const unsigned char pa_available,
1130 const unsigned char pb_available))
1132 struct watcher *w = watchers, *prev = NULL;
1134 (void)cris_io_interface_init();
1136 while ((NULL != w) && (w->notify != notify)){
1142 prev->next = w->next;
1149 printk(KERN_WARNING "cris_io_interface_delete_watcher: Deleting unknown watcher 0x%p\n", notify);
1153 static int cris_io_interface_init(void)
1155 static int first = 1;
1163 for (i = 0; i<8; i++) {
1164 gpio_pa_owners[i] = if_unclaimed;
1165 gpio_pb_owners[i] = if_unclaimed;
1166 gpio_pg_owners[i] = if_unclaimed;
1169 gpio_pg_owners[i] = if_unclaimed;
1175 module_init(cris_io_interface_init);
1178 EXPORT_SYMBOL(cris_request_io_interface);
1179 EXPORT_SYMBOL(cris_free_io_interface);
1180 EXPORT_SYMBOL(cris_io_interface_allocate_pins);
1181 EXPORT_SYMBOL(cris_io_interface_free_pins);
1182 EXPORT_SYMBOL(cris_io_interface_register_watcher);
1183 EXPORT_SYMBOL(cris_io_interface_delete_watcher);