From 9497265e72f1aa25649c8124c58a4afcfec49295 Mon Sep 17 00:00:00 2001 From: Jan Willeke Date: Tue, 3 Feb 2015 19:42:23 +0100 Subject: [PATCH] gpio driver prepare patch moves jtdev functions to pointers, to exchange late on. Signed-off-by: Jan Willeke --- drivers/jtaglib.c | 46 +++++++++++------------ drivers/jtdev.c | 93 +++++++++++++++++++++++++++++------------------ drivers/jtdev.h | 42 ++++++++++++--------- drivers/pif.c | 5 ++- 4 files changed, 107 insertions(+), 79 deletions(-) diff --git a/drivers/jtaglib.c b/drivers/jtaglib.c index 5616523..e433268 100644 --- a/drivers/jtaglib.c +++ b/drivers/jtaglib.c @@ -52,23 +52,23 @@ /* Bypass instruction */ #define IR_BYPASS 0xFF /* 0xFF */ -#define jtag_tms_set(p) jtdev_tms(p, 1) -#define jtag_tms_clr(p) jtdev_tms(p, 0) -#define jtag_tck_set(p) jtdev_tck(p, 1) -#define jtag_tck_clr(p) jtdev_tck(p, 0) -#define jtag_tdi_set(p) jtdev_tdi(p, 1) -#define jtag_tdi_clr(p) jtdev_tdi(p, 0) -#define jtag_tclk_set(p) jtdev_tclk(p, 1) -#define jtag_tclk_clr(p) jtdev_tclk(p, 0) -#define jtag_rst_set(p) jtdev_rst(p, 1) -#define jtag_rst_clr(p) jtdev_rst(p, 0) -#define jtag_tst_set(p) jtdev_tst(p, 1) -#define jtag_tst_clr(p) jtdev_tst(p, 0) +#define jtag_tms_set(p) p->f->jtdev_tms(p, 1) +#define jtag_tms_clr(p) p->f->jtdev_tms(p, 0) +#define jtag_tck_set(p) p->f->jtdev_tck(p, 1) +#define jtag_tck_clr(p) p->f->jtdev_tck(p, 0) +#define jtag_tdi_set(p) p->f->jtdev_tdi(p, 1) +#define jtag_tdi_clr(p) p->f->jtdev_tdi(p, 0) +#define jtag_tclk_set(p) p->f->jtdev_tclk(p, 1) +#define jtag_tclk_clr(p) p->f->jtdev_tclk(p, 0) +#define jtag_rst_set(p) p->f->jtdev_rst(p, 1) +#define jtag_rst_clr(p) p->f->jtdev_rst(p, 0) +#define jtag_tst_set(p) p->f->jtdev_tst(p, 1) +#define jtag_tst_clr(p) p->f->jtdev_tst(p, 0) -#define jtag_led_green_on(p) jtdev_led_green(p, 1) -#define jtag_led_green_off(p) jtdev_led_green(p, 0) -#define jtag_led_red_on(p) jtdev_led_red(p, 1) -#define jtag_led_red_off(p) jtdev_led_red(p, 0) +#define jtag_led_green_on(p) p->f->jtdev_led_green(p, 1) +#define jtag_led_green_off(p) p->f->jtdev_led_green(p, 0) +#define jtag_led_red_on(p) p->f->jtdev_led_red(p, 1) +#define jtag_led_red_off(p) p->f->jtdev_led_red(p, 0) /* Reset target JTAG interface and perform fuse-HW check */ static void jtag_reset_tap(struct jtdev *p) @@ -130,7 +130,7 @@ static unsigned int jtag_shift( struct jtdev *p, unsigned int mask; unsigned int tclk_save; - tclk_save = jtdev_tclk_get(p); + tclk_save = p->f->jtdev_tclk_get(p); data_in = 0; for (mask = 0x0001U << (num_bits - 1); mask != 0; mask >>= 1) { @@ -145,11 +145,11 @@ static unsigned int jtag_shift( struct jtdev *p, jtag_tck_clr(p); jtag_tck_set(p); - if (jtdev_tdo_get(p) == 1) + if (p->f->jtdev_tdo_get(p) == 1) data_in |= mask; } - jtdev_tclk(p, tclk_save); + p->f->jtdev_tclk(p, tclk_save); /* Set JTAG state back to Run-Test/Idle */ jtag_tclk_prep(p); @@ -370,14 +370,14 @@ unsigned int jtag_init(struct jtdev *p) unsigned int jtag_id; jtag_rst_clr(p); - jtdev_power_on(p); + p->f->jtdev_power_on(p); jtag_tst_set(p); jtag_tdi_set(p); jtag_tms_set(p); jtag_tck_set(p); jtag_tclk_set(p); jtag_rst_clr(p); - jtdev_connect(p); + p->f->jtdev_connect(p); jtag_rst_set(p); jtag_reset_tap(p); @@ -763,7 +763,7 @@ void jtag_write_flash(struct jtdev *p, /* provide TCLKs * min. 33 for F149 and F449 */ - jtdev_tclk_strobe(p, 35); + p->f->jtdev_tclk_strobe(p, 35); address += 2; if (p->failed) @@ -863,7 +863,7 @@ void jtag_erase_flash(struct jtdev *p, jtag_dr_shift(p, 0x2409); /* provide TCLKs */ - jtdev_tclk_strobe(p, number_of_strobes); + p->f->jtdev_tclk_strobe(p, number_of_strobes); /* Set RW to write */ jtag_ir_shift(p, IR_CNTRL_SIG_16BIT); diff --git a/drivers/jtdev.c b/drivers/jtdev.c index 106f0d5..5dea27d 100644 --- a/drivers/jtdev.c +++ b/drivers/jtdev.c @@ -131,7 +131,7 @@ static void do_ppwcontrol(struct jtdev *p) } } -int jtdev_open(struct jtdev *p, const char *device) +static int jtpif_open(struct jtdev *p, const char *device) { p->port = open(device, O_RDWR); if (p->port < 0) { @@ -157,7 +157,7 @@ int jtdev_open(struct jtdev *p, const char *device) return 0; } -void jtdev_close(struct jtdev *p) +static void jtpif_close(struct jtdev *p) { if (par_release(p->port) < 0) pr_error("warning: jtdev: failed to release port"); @@ -165,14 +165,14 @@ void jtdev_close(struct jtdev *p) close(p->port); } -void jtdev_power_on(struct jtdev *p) +static void jtpif_power_on(struct jtdev *p) { /* power supply on */ p->data_register |= POWER; do_ppwdata(p); } -void jtdev_power_off(struct jtdev *p) +static void jtpif_power_off(struct jtdev *p) { /* power supply off */ p->data_register &= ~POWER; @@ -184,19 +184,19 @@ void jtdev_power_off(struct jtdev *p) do_ppwcontrol(p); } -void jtdev_connect(struct jtdev *p) +static void jtpif_connect(struct jtdev *p) { p->control_register |= (TEST | ENABLE); do_ppwcontrol(p); } -void jtdev_release(struct jtdev *p) +static void jtpif_release(struct jtdev *p) { p->control_register &= ~(TEST | ENABLE); do_ppwcontrol(p); } -void jtdev_tck(struct jtdev *p, int out) +static void jtpif_tck(struct jtdev *p, int out) { if (out) p->data_register |= TCK; @@ -206,7 +206,7 @@ void jtdev_tck(struct jtdev *p, int out) do_ppwdata(p); } -void jtdev_tms(struct jtdev *p, int out) +static void jtpif_tms(struct jtdev *p, int out) { if (out) p->data_register |= TMS; @@ -216,7 +216,7 @@ void jtdev_tms(struct jtdev *p, int out) do_ppwdata(p); } -void jtdev_tdi(struct jtdev *p, int out) +static void jtpif_tdi(struct jtdev *p, int out) { if (out) p->data_register |= TDI; @@ -226,7 +226,7 @@ void jtdev_tdi(struct jtdev *p, int out) do_ppwdata(p); } -void jtdev_rst(struct jtdev *p, int out) +static void jtpif_rst(struct jtdev *p, int out) { /* reset pin is inverted by PC hardware */ if (out) @@ -237,7 +237,7 @@ void jtdev_rst(struct jtdev *p, int out) do_ppwcontrol(p); } -void jtdev_tst(struct jtdev *p, int out) +static void jtpif_tst(struct jtdev *p, int out) { if (out) p->control_register |= TEST; @@ -247,7 +247,7 @@ void jtdev_tst(struct jtdev *p, int out) do_ppwcontrol(p); } -int jtdev_tdo_get(struct jtdev *p) +static int jtpif_tdo_get(struct jtdev *p) { uint8_t input; @@ -260,7 +260,7 @@ int jtdev_tdo_get(struct jtdev *p) return (input & TDO) ? 1 : 0; } -void jtdev_tclk(struct jtdev *p, int out) +static void jtpif_tclk(struct jtdev *p, int out) { if (out) p->data_register |= TCLK; @@ -270,25 +270,25 @@ void jtdev_tclk(struct jtdev *p, int out) do_ppwdata(p); } -int jtdev_tclk_get(struct jtdev *p) +static int jtpif_tclk_get(struct jtdev *p) { return (p->data_register & TCLK) ? 1 : 0; } -void jtdev_tclk_strobe(struct jtdev *p, unsigned int count) +static void jtpif_tclk_strobe(struct jtdev *p, unsigned int count) { int i; for (i = 0; i < count; i++) { - jtdev_tclk(p, 1); - jtdev_tclk(p, 0); + jtpif_tclk(p, 1); + jtpif_tclk(p, 0); if (p->failed) return; } } -void jtdev_led_green(struct jtdev *p, int out) +static void jtpif_led_green(struct jtdev *p, int out) { if (out) p->data_register |= LED_GREEN; @@ -298,7 +298,7 @@ void jtdev_led_green(struct jtdev *p, int out) do_ppwdata(p); } -void jtdev_led_red(struct jtdev *p, int out) +static void jtpif_led_red(struct jtdev *p, int out) { if (out) p->data_register |= LED_RED; @@ -308,31 +308,52 @@ void jtdev_led_red(struct jtdev *p, int out) do_ppwdata(p); } #else /* __linux__ */ -int jtdev_open(struct jtdev *p, const char *device) +static int jtpif_open(struct jtdev *p, const char *device) { printc_err("jtdev: driver is not supported on this platform\n"); p->failed = 1; return -1; } -void jtdev_close(struct jtdev *p) { } +static void jtpif_close(struct jtdev *p) { } -void jtdev_power_on(struct jtdev *p) { } -void jtdev_power_off(struct jtdev *p) { } -void jtdev_connect(struct jtdev *p) { } -void jtdev_release(struct jtdev *p) { } +static void jtpif_power_on(struct jtdev *p) { } +static void jtpif_power_off(struct jtdev *p) { } +static void jtpif_connect(struct jtdev *p) { } +static void jtpif_release(struct jtdev *p) { } -void jtdev_tck(struct jtdev *p, int out) { } -void jtdev_tms(struct jtdev *p, int out) { } -void jtdev_tdi(struct jtdev *p, int out) { } -void jtdev_rst(struct jtdev *p, int out) { } -void jtdev_tst(struct jtdev *p, int out) { } -int jtdev_tdo_get(struct jtdev *p) { return 0; } +static void jtpif_tck(struct jtdev *p, int out) { } +static void jtpif_tms(struct jtdev *p, int out) { } +static void jtpif_tdi(struct jtdev *p, int out) { } +static void jtpif_rst(struct jtdev *p, int out) { } +static void jtpif_tst(struct jtdev *p, int out) { } +static int jtpif_tdo_get(struct jtdev *p) { return 0; } -void jtdev_tclk(struct jtdev *p, int out) { } -int jtdev_tclk_get(struct jtdev *p) { return 0; } -void jtdev_tclk_strobe(struct jtdev *p, unsigned int count) { } +static void jtpif_tclk(struct jtdev *p, int out) { } +static int jtpif_tclk_get(struct jtdev *p) { return 0; } +static void jtpif_tclk_strobe(struct jtdev *p, unsigned int count) { } -void jtdev_led_green(struct jtdev *p, int out) { } -void jtdev_led_red(struct jtdev *p, int out) { } +static void jtpif_led_green(struct jtdev *p, int out) { } +static void jtpif_led_red(struct jtdev *p, int out) { } #endif + +const struct jtdev_func jtdev_func_pif = { + .jtdev_open = jtpif_open, + .jtdev_close = jtpif_close, + .jtdev_power_on = jtpif_power_on, + .jtdev_power_off = jtpif_power_off, + .jtdev_connect = jtpif_connect, + .jtdev_release = jtpif_release, + .jtdev_tck = jtpif_tck, + .jtdev_tms = jtpif_tms, + .jtdev_tdi = jtpif_tdi, + .jtdev_rst = jtpif_rst, + .jtdev_tst = jtpif_tst, + .jtdev_tdo_get = jtpif_tdo_get, + .jtdev_tclk = jtpif_tclk, + .jtdev_tclk_get = jtpif_tclk_get, + .jtdev_tclk_strobe = jtpif_tclk_strobe, + .jtdev_led_green = jtpif_led_green, + .jtdev_led_red = jtpif_led_red +}; + diff --git a/drivers/jtdev.h b/drivers/jtdev.h index 16564cb..f5fdb90 100644 --- a/drivers/jtdev.h +++ b/drivers/jtdev.h @@ -19,44 +19,50 @@ #ifndef JTDEV_H_ #define JTDEV_H_ - +struct jtdev_func; struct jtdev { int port; uint8_t data_register; uint8_t control_register; int failed; + const struct jtdev_func * f; }; +struct jtdev_func{ /* Initialize/destroy a parallel-port JTAG interface. jtdev_open() * returns 0 on success or -1 if an error occurs. * * All other JTAG IO functions indicate errors by setting the failed * field in the jtdev structure. */ -int jtdev_open(struct jtdev *p, const char *device); -void jtdev_close(struct jtdev *p); + int (*jtdev_open)(struct jtdev *p, const char *device); + void (*jtdev_close)(struct jtdev *p); /* Connect/release JTAG */ -void jtdev_power_on(struct jtdev *p); -void jtdev_power_off(struct jtdev *p); -void jtdev_connect(struct jtdev *p); -void jtdev_release(struct jtdev *p); + void (*jtdev_power_on)(struct jtdev *p); + void (*jtdev_power_off)(struct jtdev *p); + void (*jtdev_connect)(struct jtdev *p); + void (*jtdev_release)(struct jtdev *p); /* Low-level IO */ -void jtdev_tck(struct jtdev *p, int out); -void jtdev_tms(struct jtdev *p, int out); -void jtdev_tdi(struct jtdev *p, int out); -void jtdev_rst(struct jtdev *p, int out); -void jtdev_tst(struct jtdev *p, int out); -int jtdev_tdo_get(struct jtdev *p); + void (*jtdev_tck)(struct jtdev *p, int out); + void (*jtdev_tms)(struct jtdev *p, int out); + void (*jtdev_tdi)(struct jtdev *p, int out); + void (*jtdev_rst)(struct jtdev *p, int out); + void (*jtdev_tst)(struct jtdev *p, int out); + int (*jtdev_tdo_get)(struct jtdev *p); /* TCLK management */ -void jtdev_tclk(struct jtdev *p, int out); -int jtdev_tclk_get(struct jtdev *p); -void jtdev_tclk_strobe(struct jtdev *p, unsigned int count); + void (*jtdev_tclk)(struct jtdev *p, int out); + int (*jtdev_tclk_get)(struct jtdev *p); + void (*jtdev_tclk_strobe)(struct jtdev *p, unsigned int count); /* LED indicators */ -void jtdev_led_green(struct jtdev *p, int out); -void jtdev_led_red(struct jtdev *p, int out); + void (*jtdev_led_green)(struct jtdev *p, int out); + void (*jtdev_led_red)(struct jtdev *p, int out); + +}; + +extern const struct jtdev_func jtdev_func_pif; #endif diff --git a/drivers/pif.c b/drivers/pif.c index 202ffa3..fcdabc8 100644 --- a/drivers/pif.c +++ b/drivers/pif.c @@ -353,8 +353,9 @@ static device_t pif_open(const struct device_args *args) memset(dev, 0, sizeof(*dev)); dev->base.type = &device_pif; dev->base.max_breakpoints = 0; + (&dev->jtag)->f = &jtdev_func_pif; - if (jtdev_open(&dev->jtag, args->path) < 0) { + if ((&dev->jtag)->f->jtdev_open(&dev->jtag, args->path) < 0) { printc_err("pif: can't open port\n"); free(dev); return NULL; @@ -377,7 +378,7 @@ static void pif_destroy(device_t dev_base) dev->jtag.failed = 0; jtag_release_device(&dev->jtag, 0xfffe); - jtdev_close(&dev->jtag); + (&dev->jtag)->f->jtdev_close(&dev->jtag); free(dev); }