From 99140ef1119cca0a162aeb1ada09a8fcdd5e578b Mon Sep 17 00:00:00 2001
From: Sebastian Kuzminsky <seb@highlab.com>
Date: Mon, 26 May 2008 05:39:24 +0000
Subject: Release 0.2:

    * Improved load-time sanity checking.

    * Improved handling of "zero instances enabled" situations.
---
 src/hal/drivers/mesa-hostmot2/ChangeLog  |  7 +++++++
 src/hal/drivers/mesa-hostmot2/TODO       | 23 ++++++++++++++++++++-
 src/hal/drivers/mesa-hostmot2/doc/README |  4 ++++
 src/hal/drivers/mesa-hostmot2/encoder.c  | 33 ++++++++++++++++++++++---------
 src/hal/drivers/mesa-hostmot2/hostmot2.h |  2 +-
 src/hal/drivers/mesa-hostmot2/ioport.c   | 13 +++++++++++-
 src/hal/drivers/mesa-hostmot2/pwmgen.c   | 34 +++++++++++++++++++++++---------
 src/hal/drivers/mesa-hostmot2/stepgen.c  | 33 ++++++++++++++++++++++---------
 src/hal/drivers/mesa-hostmot2/watchdog.c | 20 ++++++++++++++++++-
 9 files changed, 138 insertions(+), 31 deletions(-)

diff --git a/src/hal/drivers/mesa-hostmot2/ChangeLog b/src/hal/drivers/mesa-hostmot2/ChangeLog
index 215196cae..cf88744d5 100644
--- a/src/hal/drivers/mesa-hostmot2/ChangeLog
+++ b/src/hal/drivers/mesa-hostmot2/ChangeLog
@@ -1,4 +1,11 @@
 
+0.2 (released 2008-05-25)
+
+    Improved load-time sanity checking.
+
+    Improved handling of "zero instances enabled" situations.
+
+
 0.1 (released 2008-05-13)
 
     Split m7i43_hm2 0.4 into a generic hostmot2 driver and a low-level I/O
diff --git a/src/hal/drivers/mesa-hostmot2/TODO b/src/hal/drivers/mesa-hostmot2/TODO
index fbdf78540..1bc4491b0 100644
--- a/src/hal/drivers/mesa-hostmot2/TODO
+++ b/src/hal/drivers/mesa-hostmot2/TODO
@@ -1,10 +1,31 @@
 
 
+misc:
+
+    It's starting to feel like it's ready for some kind of module
+    abstraction...  To standardize the semantics and make it easier to
+    think about.  Pull out the common code.
+
+
+    add sample configs in configs/mesa-hostmot2
+
+        use env vars to choose board type?
+        http://www.linuxcnc.org/docs/devel/html/man/man1/halcmd.1.html#SUBSTITUTION
+
+
+    where do the firmwares belong?
+
+
+    add support for the 5i22 next
+
+
+
+
 bfload
 
     add "bfload reset"
 
-    5i22 has wrong subdevice ids?
+    5i22 has wrong subdevice ids?  SWP says it works on his 1.5M board
 
     add support for all the other PCI cards
 
diff --git a/src/hal/drivers/mesa-hostmot2/doc/README b/src/hal/drivers/mesa-hostmot2/doc/README
index 107afb984..d3558b9b4 100644
--- a/src/hal/drivers/mesa-hostmot2/doc/README
+++ b/src/hal/drivers/mesa-hostmot2/doc/README
@@ -1,4 +1,8 @@
 
+Docs for the Anything I/O boards are available from Mesa at
+<http://www.mesanet.com>.
+
+
 The docs for the PLX chipsets used on the PCI Anything I/O boards is
 available gratis but not libre from plxtech.com.
 
diff --git a/src/hal/drivers/mesa-hostmot2/encoder.c b/src/hal/drivers/mesa-hostmot2/encoder.c
index 1a902fe35..03d64b68b 100644
--- a/src/hal/drivers/mesa-hostmot2/encoder.c
+++ b/src/hal/drivers/mesa-hostmot2/encoder.c
@@ -35,10 +35,10 @@ int hm2_encoder_parse_md(hostmot2_t *hm2, int md_index) {
     hm2_module_descriptor_t *md = &hm2->md[md_index];
     int r;
 
-    if (hm2->config.num_encoders == 0) {
-        INFO("num_encoders=0, skipping encoder MD\n");
-        return 0;
-    }
+
+    // 
+    // some standard sanity checks
+    //
 
     if (!hm2_md_is_consistent(hm2, md_index, 2, 5, 4, 0x0003)) {
         ERR("inconsistent Module Descriptor!\n");
@@ -47,25 +47,39 @@ int hm2_encoder_parse_md(hostmot2_t *hm2, int md_index) {
 
     if (hm2->encoder.num_instances != 0) {
         ERR(
-            "Module Descriptor contains duplicate %s (inconsistent firmware), not loading driver\n",
+            "found duplicate Module Descriptor for %s (inconsistent firmware), not loading driver\n",
             hm2_get_general_function_name(md->gtag)
         );
         return -EINVAL;
     }
 
-    if (hm2->config.num_encoders == -1) {
-        hm2->encoder.num_instances = md->instances;
-    } else if (hm2->config.num_encoders > md->instances) {
+    if (hm2->config.num_encoders > md->instances) {
         ERR(
             "config.num_encoders=%d, but only %d are available, not loading driver\n",
             hm2->config.num_encoders,
             md->instances
         );
-        return -1;
+        return -EINVAL;
+    }
+
+    if (hm2->config.num_encoders == 0) {
+        INFO("num_encoders=0, skipping encoder MD\n");
+        return 0;
+    }
+
+
+    // 
+    // looks good, start initializing
+    // 
+
+
+    if (hm2->config.num_encoders == -1) {
+        hm2->encoder.num_instances = md->instances;
     } else {
         hm2->encoder.num_instances = hm2->config.num_encoders;
     }
 
+
     hm2->encoder.instance = (hm2_encoder_instance_t *)hal_malloc(hm2->encoder.num_instances * sizeof(hm2_encoder_instance_t));
     if (hm2->encoder.instance == NULL) {
         ERR("out of memory!\n");
@@ -161,6 +175,7 @@ fail0:
 
 
 void hm2_encoder_force_write(hostmot2_t *hm2) {
+    if (hm2->encoder.num_instances == 0) return;
     hm2->llio->write(hm2->llio, hm2->encoder.latch_control_addr, hm2->encoder.control_reg, (hm2->encoder.num_instances * sizeof(u32)));
 }
 
diff --git a/src/hal/drivers/mesa-hostmot2/hostmot2.h b/src/hal/drivers/mesa-hostmot2/hostmot2.h
index 8106f45fe..a206aae2a 100644
--- a/src/hal/drivers/mesa-hostmot2/hostmot2.h
+++ b/src/hal/drivers/mesa-hostmot2/hostmot2.h
@@ -28,7 +28,7 @@
 #include "hostmot2-lowlevel.h"
 
 
-#define HM2_VERSION "0.1"
+#define HM2_VERSION "0.2"
 #define HM2_NAME    "hm2"
 
 #define PRINT_NO_LL(level, fmt, args...)  rtapi_print_msg(level, HM2_NAME ": " fmt, ## args);
diff --git a/src/hal/drivers/mesa-hostmot2/ioport.c b/src/hal/drivers/mesa-hostmot2/ioport.c
index 3e2a91464..1cec796ea 100644
--- a/src/hal/drivers/mesa-hostmot2/ioport.c
+++ b/src/hal/drivers/mesa-hostmot2/ioport.c
@@ -35,6 +35,11 @@ int hm2_ioport_parse_md(hostmot2_t *hm2, int md_index) {
     hm2_module_descriptor_t *md = &hm2->md[md_index];
     int i, r;
 
+
+    // 
+    // some standard sanity checks
+    //
+
     if (!hm2_md_is_consistent(hm2, md_index, 0, 5, 4, 0x001F)) {
         ERR("inconsistent Module Descriptor!\n");
         return -EINVAL;
@@ -42,12 +47,17 @@ int hm2_ioport_parse_md(hostmot2_t *hm2, int md_index) {
 
     if (hm2->ioport.num_instances != 0) {
         ERR(
-            "Module Descriptor contains duplicate %s (inconsistent firmware), not loading driver\n",
+            "found duplicate Module Descriptor for %s (inconsistent firmware), not loading driver\n",
             hm2_get_general_function_name(md->gtag)
         );
         return -EINVAL;
     }
 
+
+    // 
+    // special sanity check for io_ports
+    // 
+
     if (hm2->idrom.io_ports != md->instances) {
         ERR(
             "IDROM IOPorts is %d but MD IOPort NumInstances is %d, inconsistent firmware, aborting driver load\n",
@@ -59,6 +69,7 @@ int hm2_ioport_parse_md(hostmot2_t *hm2, int md_index) {
 
     hm2->ioport.num_instances = md->instances;
 
+
     hm2->ioport.clock_frequency = md->clock_freq;
     hm2->ioport.version = md->version;
 
diff --git a/src/hal/drivers/mesa-hostmot2/pwmgen.c b/src/hal/drivers/mesa-hostmot2/pwmgen.c
index c1ee4676a..e8c57927d 100644
--- a/src/hal/drivers/mesa-hostmot2/pwmgen.c
+++ b/src/hal/drivers/mesa-hostmot2/pwmgen.c
@@ -34,6 +34,8 @@
 void hm2_pwmgen_force_write(hostmot2_t *hm2) {
     int i;
 
+    if (hm2->pwmgen.num_instances == 0) return;
+
     for (i = 0; i < hm2->pwmgen.num_instances; i ++) {
         hm2->pwmgen.pwm_mode_reg[i] = hm2->pwmgen.instance[i].pwm_width_select;
         hm2->pwmgen.pwm_mode_reg[i] |= hm2->pwmgen.instance[i].pwm_mode_select << 2;
@@ -124,10 +126,10 @@ int hm2_pwmgen_parse_md(hostmot2_t *hm2, int md_index) {
     hm2_module_descriptor_t *md = &hm2->md[md_index];
     int r;
 
-    if (hm2->config.num_pwmgens == 0) {
-        INFO("num_pwmgens=0, skipping pwmgen MD\n");
-        return 0;
-    }
+
+    // 
+    // some standard sanity checks
+    //
 
     if (!hm2_md_is_consistent(hm2, md_index, 0, 5, 4, 0x0003)) {
         ERR("inconsistent Module Descriptor!\n");
@@ -136,25 +138,39 @@ int hm2_pwmgen_parse_md(hostmot2_t *hm2, int md_index) {
 
     if (hm2->pwmgen.num_instances != 0) {
         ERR(
-            "Module Descriptor contains duplicate %s (inconsistent firmware), not loading driver\n",
+            "found duplicate Module Descriptor for %s (inconsistent firmware), not loading driver\n",
             hm2_get_general_function_name(md->gtag)
         );
         return -EINVAL;
     }
 
-    if (hm2->config.num_pwmgens == -1) {
-        hm2->pwmgen.num_instances = md->instances;
-    } else if (hm2->config.num_pwmgens > md->instances) {
+    if (hm2->config.num_pwmgens > md->instances) {
         ERR(
             "config.num_pwmgens=%d, but only %d are available, not loading driver\n",
             hm2->config.num_pwmgens,
             md->instances
         );
-        return -1;
+        return -EINVAL;
+    }
+
+    if (hm2->config.num_pwmgens == 0) {
+        INFO("num_pwmgens=0, skipping pwmgen MD\n");
+        return 0;
+    }
+
+
+    // 
+    // looks good, start initializing
+    // 
+
+
+    if (hm2->config.num_pwmgens == -1) {
+        hm2->pwmgen.num_instances = md->instances;
     } else {
         hm2->pwmgen.num_instances = hm2->config.num_pwmgens;
     }
 
+
     hm2->pwmgen.instance = (hm2_pwmgen_instance_t *)hal_malloc(hm2->pwmgen.num_instances * sizeof(hm2_pwmgen_instance_t));
     if (hm2->pwmgen.instance == NULL) {
         ERR("out of memory!\n");
diff --git a/src/hal/drivers/mesa-hostmot2/stepgen.c b/src/hal/drivers/mesa-hostmot2/stepgen.c
index 8ee84d253..fe19a5a96 100644
--- a/src/hal/drivers/mesa-hostmot2/stepgen.c
+++ b/src/hal/drivers/mesa-hostmot2/stepgen.c
@@ -231,6 +231,7 @@ static void hm2_stepgen_force_write_pulse_width_time(hostmot2_t *hm2) {
 
 
 void hm2_stepgen_force_write(hostmot2_t *hm2) {
+    if (hm2->stepgen.num_instances == 0) return;
     hm2_stepgen_force_write_mode(hm2);
     hm2_stepgen_force_write_dir_setup_time(hm2);
     hm2_stepgen_force_write_dir_hold_time(hm2);
@@ -256,10 +257,10 @@ int hm2_stepgen_parse_md(hostmot2_t *hm2, int md_index) {
     hm2_module_descriptor_t *md = &hm2->md[md_index];
     int r;
 
-    if (hm2->config.num_stepgens == 0) {
-        INFO("num_stepgens=0, skipping stepgen MD\n");
-        return 0;
-    }
+
+    // 
+    // some standard sanity checks
+    //
 
     if (!hm2_md_is_consistent(hm2, md_index, 0, 10, 4, 0x01FF)) {
         ERR("inconsistent Module Descriptor!\n");
@@ -268,25 +269,39 @@ int hm2_stepgen_parse_md(hostmot2_t *hm2, int md_index) {
 
     if (hm2->stepgen.num_instances != 0) {
         ERR(
-            "Module Descriptor contains duplicate %s (inconsistent firmware), not loading driver\n",
+            "found duplicate Module Descriptor for %s (inconsistent firmware), not loading driver\n",
             hm2_get_general_function_name(md->gtag)
         );
         return -EINVAL;
     }
 
-    if (hm2->config.num_stepgens == -1) {
-        hm2->stepgen.num_instances = md->instances;
-    } else if (hm2->config.num_stepgens > md->instances) {
+    if (hm2->config.num_stepgens > md->instances) {
         ERR(
             "config.num_stepgens=%d, but only %d are available, not loading driver\n",
             hm2->config.num_stepgens,
             md->instances
         );
-        return -1;
+        return -EINVAL;
+    }
+
+    if (hm2->config.num_stepgens == 0) {
+        INFO("num_stepgens=0, skipping stepgen MD\n");
+        return 0;
+    }
+
+
+    // 
+    // looks good, start initializing
+    // 
+
+
+    if (hm2->config.num_stepgens == -1) {
+        hm2->stepgen.num_instances = md->instances;
     } else {
         hm2->stepgen.num_instances = hm2->config.num_stepgens;
     }
 
+
     hm2->stepgen.instance = (hm2_stepgen_instance_t *)hal_malloc(hm2->stepgen.num_instances * sizeof(hm2_stepgen_instance_t));
     if (hm2->stepgen.instance == NULL) {
         ERR("out of memory!\n");
diff --git a/src/hal/drivers/mesa-hostmot2/watchdog.c b/src/hal/drivers/mesa-hostmot2/watchdog.c
index 8cad81c94..6a83ac30d 100644
--- a/src/hal/drivers/mesa-hostmot2/watchdog.c
+++ b/src/hal/drivers/mesa-hostmot2/watchdog.c
@@ -83,6 +83,11 @@ int hm2_watchdog_parse_md(hostmot2_t *hm2, int md_index) {
     hm2_module_descriptor_t *md = &hm2->md[md_index];
     int r;
 
+
+    // 
+    // some standard sanity checks
+    //
+
     if (!hm2_md_is_consistent(hm2, md_index, 0, 3, 4, 0)) {
         ERR("inconsistent Module Descriptor!\n");
         return -EINVAL;
@@ -90,16 +95,27 @@ int hm2_watchdog_parse_md(hostmot2_t *hm2, int md_index) {
 
     if (hm2->watchdog.num_instances != 0) {
         ERR(
-            "Module Descriptor contains duplicate %s (inconsistent firmware), not loading driver\n",
+            "found duplicate Module Descriptor for %s (inconsistent firmware), not loading driver\n",
             hm2_get_general_function_name(md->gtag)
         );
         return -EINVAL;
     }
 
+
+    // 
+    // special sanity checks for watchdog
+    //
+
     if (md->instances != 1) {
         WARN("MD declares %d watchdogs!  only using the first one...\n", md->instances);
     }
 
+
+    // 
+    // looks good, start initializing
+    // 
+
+
     hm2->watchdog.num_instances = 1;
 
     hm2->watchdog.instance = (hm2_watchdog_instance_t *)hal_malloc(hm2->watchdog.num_instances * sizeof(hm2_watchdog_instance_t));
@@ -251,6 +267,8 @@ void hm2_watchdog_cleanup(hostmot2_t *hm2) {
 void hm2_watchdog_force_write(hostmot2_t *hm2) {
     u64 tmp;
 
+    if (hm2->watchdog.num_instances != 1) return;
+
     tmp = (hm2->watchdog.instance[0].hal.param.timeout_ns * ((double)hm2->watchdog.clock_frequency / (double)(1000 * 1000 * 1000))) - 1;
     if (tmp < 0x80000000) {
         hm2->watchdog.timer_reg[0] = tmp;
-- 
cgit v1.2.3