8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/errno.h>
11 #include <linux/module.h>
17 #include <arch/system.h>
23 #define SETS(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
24 IO_STATE_(reg##_, field##_, _##val)
38 const unsigned int gpio_out_available,
39 const unsigned char pa_available,
40 const unsigned char pb_available);
86 static struct if_group if_groups[6] = {
168 .gpio_g_in = 0x00000000,
169 .gpio_g_out = 0x00000000,
184 .gpio_g_in = 0x000000c0,
185 .gpio_g_out = 0x000000c0,
200 .gpio_g_in = 0xc0000000,
201 .gpio_g_out = 0xc0000000,
206 .name =
"sync_serial_1",
216 .gpio_g_in = 0x00000000,
217 .gpio_g_out = 0x00000000,
222 .name =
"sync_serial_3",
232 .gpio_g_in = 0xc0000000,
233 .gpio_g_out = 0xc0000000,
238 .name =
"shared_ram",
248 .gpio_g_in = 0x0000ff3e,
249 .gpio_g_out = 0x0000ff38,
254 .name =
"shared_ram_w",
264 .gpio_g_in = 0x00ffff3e,
265 .gpio_g_out = 0x00ffff38,
280 .gpio_g_in = 0x0000ff3e,
281 .gpio_g_out = 0x0000ff3e,
296 .gpio_g_in = 0x3eff0000,
297 .gpio_g_out = 0x3eff0000,
312 .gpio_g_in = 0x00ffff3e,
313 .gpio_g_out = 0x00ffff3e,
328 .gpio_g_in = 0x0000ffff,
329 .gpio_g_out = 0x0000ffff,
344 .gpio_g_in = 0xffff0000,
345 .gpio_g_out = 0xffff0000,
360 .gpio_g_in = 0x01ffffff,
361 .gpio_g_out = 0x07ffffff,
376 .gpio_g_in = 0xf9ffffff,
377 .gpio_g_out = 0xffffffff,
392 .gpio_g_in = 0x00000000,
393 .gpio_g_out = 0x00000000,
408 .gpio_g_in = 0x00000000,
409 .gpio_g_out = 0x00000000,
424 .gpio_g_in = 0x00000000,
425 .gpio_g_out = 0x00000000,
440 .gpio_g_in = 0x3e000000,
441 .gpio_g_out = 0x0c000000,
457 .gpio_g_in = 0x0000ff3f,
458 .gpio_g_out = 0x0000ff3f,
473 .gpio_g_in = 0x000000c0,
474 .gpio_g_out = 0x000000c0,
489 .gpio_g_in = 0xc0000000,
490 .gpio_g_out = 0xc0000000,
505 .gpio_g_in = 0x3fff0000,
506 .gpio_g_out = 0x3fff0000,
521 .gpio_g_in = 0x00000000,
522 .gpio_g_out = 0x00000000,
537 .gpio_g_in = 0x00000000,
538 .gpio_g_out = 0x00000000,
547 static unsigned int gpio_in_pins = 0xffffffff;
548 static unsigned int gpio_out_pins = 0xffffffff;
549 static unsigned char gpio_pb_pins = 0xff;
550 static unsigned char gpio_pa_pins = 0xff;
557 static int cris_io_interface_init(
void);
559 static unsigned char clear_group_from_set(
const unsigned char groups,
struct if_group *
group)
561 return (groups & ~group->
group);
565 static struct if_group *get_group(
const unsigned char groups)
569 if (groups & if_groups[i].group) {
570 return &if_groups[
i];
577 static void notify_watchers(
void)
581 DBG(
printk(
"io_interface_mux: notifying watchers\n"));
584 w->
notify((
const unsigned int)gpio_in_pins,
585 (
const unsigned int)gpio_out_pins,
586 (
const unsigned char)gpio_pa_pins,
587 (
const unsigned char)gpio_pb_pins);
595 int set_gen_config = 0;
596 int set_gen_config_ii = 0;
597 unsigned long int gens;
598 unsigned long int gens_ii;
600 unsigned char group_set;
604 (
void)cris_io_interface_init();
606 DBG(
printk(
"cris_request_io_interface(%d, \"%s\")\n", ioif, device_id));
610 "%u submitted for %s\n",
618 if (interfaces[ioif].
used) {
620 "%s for %s, in use by %s\n",
621 interfaces[ioif].
name,
623 interfaces[ioif].
owner);
630 group_set = interfaces[ioif].
groups;
631 while (
NULL != (grp = get_group(group_set))) {
632 unsigned int if_group_use = 0;
634 switch (grp->
group) {
636 if_group_use = interfaces[ioif].
group_a;
639 if_group_use = interfaces[ioif].
group_b;
642 if_group_use = interfaces[ioif].
group_c;
645 if_group_use = interfaces[ioif].
group_d;
648 if_group_use = interfaces[ioif].
group_e;
651 if_group_use = interfaces[ioif].
group_f;
657 if (if_group_use & grp->
used) {
659 "%s needed by %s not available\n",
665 group_set = clear_group_from_set(group_set, grp);
669 if (((interfaces[ioif].gpio_g_in & gpio_in_pins) !=
670 interfaces[ioif].gpio_g_in) ||
671 ((interfaces[ioif].gpio_g_out & gpio_out_pins) !=
672 interfaces[ioif].gpio_g_out) ||
673 ((interfaces[ioif].gpio_b & gpio_pb_pins) !=
674 interfaces[ioif].gpio_b)) {
676 "required pins for interface %u\n", ioif);
696 set_gen_config_ii = 1;
697 SETS(gens_ii, R_GEN_CONFIG_II, sermode1,
async);
704 set_gen_config_ii = 1;
705 SETS(gens_ii, R_GEN_CONFIG_II, sermode3,
async);
708 set_gen_config_ii = 1;
709 SETS(gens_ii, R_GEN_CONFIG_II, sermode1,
sync);
713 set_gen_config_ii = 1;
714 SETS(gens_ii, R_GEN_CONFIG_II, sermode3,
sync);
772 "%u submitted for %s\n",
779 group_set = interfaces[ioif].
groups;
780 while (
NULL != (grp = get_group(group_set))) {
781 unsigned int if_group_use = 0;
783 switch (grp->
group) {
785 if_group_use = interfaces[ioif].
group_a;
788 if_group_use = interfaces[ioif].
group_b;
791 if_group_use = interfaces[ioif].
group_c;
794 if_group_use = interfaces[ioif].
group_d;
797 if_group_use = interfaces[ioif].
group_e;
800 if_group_use = interfaces[ioif].
group_f;
805 grp->
used |= if_group_use;
807 group_set = clear_group_from_set(group_set, grp);
810 interfaces[ioif].
used = 1;
811 interfaces[ioif].
owner = (
char*)device_id;
813 if (set_gen_config) {
818 for(i = 6; i > 0; i--)
821 if (set_gen_config_ii) {
827 "g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
828 gpio_in_pins, gpio_out_pins, gpio_pb_pins));
830 "grabbing pins: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
831 interfaces[ioif].gpio_g_in,
832 interfaces[ioif].gpio_g_out,
833 interfaces[ioif].gpio_b));
835 gpio_in_pins &= ~interfaces[ioif].
gpio_g_in;
836 gpio_out_pins &= ~interfaces[ioif].
gpio_g_out;
837 gpio_pb_pins &= ~interfaces[ioif].
gpio_b;
840 "g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
841 gpio_in_pins, gpio_out_pins, gpio_pb_pins));
854 unsigned char group_set;
857 (
void)cris_io_interface_init();
865 if (!interfaces[ioif].
used) {
866 printk(
KERN_CRIT "cris_free_io_interface: Freeing free interface %u\n",
871 group_set = interfaces[ioif].
groups;
872 while (
NULL != (grp = get_group(group_set))) {
873 unsigned int if_group_use = 0;
875 switch (grp->
group) {
877 if_group_use = interfaces[ioif].
group_a;
880 if_group_use = interfaces[ioif].
group_b;
883 if_group_use = interfaces[ioif].
group_c;
886 if_group_use = interfaces[ioif].
group_d;
889 if_group_use = interfaces[ioif].
group_e;
892 if_group_use = interfaces[ioif].
group_f;
898 if ((grp->
used & if_group_use) != if_group_use)
900 grp->
used = grp->
used & ~if_group_use;
902 group_set = clear_group_from_set(group_set, grp);
904 interfaces[ioif].
used = 0;
907 DBG(
printk(
"GPIO pins: available before: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
908 gpio_in_pins, gpio_out_pins, gpio_pb_pins));
909 DBG(
printk(
"freeing pins: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
910 interfaces[ioif].gpio_g_in,
911 interfaces[ioif].gpio_g_out,
912 interfaces[ioif].gpio_b));
914 gpio_in_pins |= interfaces[ioif].
gpio_g_in;
916 gpio_pb_pins |= interfaces[ioif].
gpio_b;
918 DBG(
printk(
"GPIO pins: available after: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
919 gpio_in_pins, gpio_out_pins, gpio_pb_pins));
928 static inline unsigned int create_mask(
const unsigned stop_bit)
931 if (stop_bit >= 32) {
934 return (1<<stop_bit)-1;
942 const unsigned stop_bit)
945 unsigned int mask = 0;
946 unsigned int tmp_mask;
947 unsigned long int flags;
950 (
void)cris_io_interface_init();
952 DBG(
printk(
"cris_io_interface_allocate_pins: if=%d port=%c start=%u stop=%u\n",
953 ioif, port, start_bit, stop_bit));
955 if (!((start_bit <= stop_bit) &&
956 ((((port ==
'a') || (port ==
'b')) && (stop_bit < 8)) ||
957 ((port ==
'g') && (stop_bit < 32))))) {
961 mask = create_mask(stop_bit + 1);
962 tmp_mask = create_mask(start_bit);
965 DBG(
printk(
"cris_io_interface_allocate_pins: port=%c start=%u stop=%u mask=0x%08x\n",
966 port, start_bit, stop_bit, mask));
972 if ((gpio_pa_pins & mask) != mask) {
976 owners = gpio_pa_owners;
977 gpio_pa_pins &= ~mask;
980 if ((gpio_pb_pins & mask) != mask) {
984 owners = gpio_pb_owners;
985 gpio_pb_pins &= ~mask;
988 if (((gpio_in_pins & mask) != mask) ||
989 ((gpio_out_pins & mask) != mask)) {
993 owners = gpio_pg_owners;
994 gpio_in_pins &= ~mask;
995 gpio_out_pins &= ~mask;
1002 for (i = start_bit; i <=
stop_bit; i++) {
1016 const unsigned stop_bit)
1019 unsigned int mask = 0;
1020 unsigned int tmp_mask;
1021 unsigned long int flags;
1024 (
void)cris_io_interface_init();
1026 if (!((start_bit <= stop_bit) &&
1027 ((((port ==
'a') || (port ==
'b')) && (stop_bit < 8)) ||
1028 ((port ==
'g') && (stop_bit < 32))))) {
1032 mask = create_mask(stop_bit + 1);
1033 tmp_mask = create_mask(start_bit);
1036 DBG(
printk(
"cris_io_interface_free_pins: port=%c start=%u stop=%u mask=0x%08x\n",
1037 port, start_bit, stop_bit, mask));
1043 if ((~gpio_pa_pins & mask) != mask) {
1047 owners = gpio_pa_owners;
1050 if ((~gpio_pb_pins & mask) != mask) {
1054 owners = gpio_pb_owners;
1057 if (((~gpio_in_pins & mask) != mask) ||
1058 ((~gpio_out_pins & mask) != mask)) {
1062 owners = gpio_pg_owners;
1068 for (i = start_bit; i <=
stop_bit; i++) {
1069 if (owners[i] != ioif) {
1070 printk(
KERN_CRIT "cris_io_interface_free_pins: Freeing unowned pins");
1077 gpio_pa_pins |=
mask;
1080 gpio_pb_pins |=
mask;
1083 gpio_in_pins |=
mask;
1084 gpio_out_pins |=
mask;
1088 for (i = start_bit; i <=
stop_bit; i++) {
1099 const unsigned int gpio_out_available,
1100 const unsigned char pa_available,
1101 const unsigned char pb_available))
1105 (
void)cris_io_interface_init();
1118 w->
notify((
const unsigned int)gpio_in_pins,
1119 (
const unsigned int)gpio_out_pins,
1120 (
const unsigned char)gpio_pa_pins,
1121 (
const unsigned char)gpio_pb_pins);
1127 const unsigned int gpio_out_available,
1128 const unsigned char pa_available,
1129 const unsigned char pb_available))
1133 (
void)cris_io_interface_init();
1141 prev->next = w->
next;
1152 static int cris_io_interface_init(
void)
1154 static int first = 1;
1162 for (i = 0; i<8; i++) {