nor/at91sam3: replace helper membuf
[fw/openocd] / src / flash / nor / at91sam3.c
index b27ea941c1391ad478ca5d78a87386a0bc810113..8fa6dd434a49bbbd8494c6cc956b4ba74d49ec3a 100644 (file)
 #endif
 
 
-#include <stdio.h>
-#include <string.h>
-#include <stddef.h>
-#include "types.h"
-#include "flash.h"
-#include <helper/membuf.h>
+#include "imp.h"
 #include "at91sam3.h"
 #include <helper/time_support.h>
 
@@ -215,8 +210,6 @@ struct sam3_chip {
        struct sam3_chip_details details;
        struct target *target;
        struct sam3_cfg cfg;
-
-       struct membuf *mbuf;
 };
 
 
@@ -1004,20 +997,6 @@ FLASHD_Lock(struct sam3_bank_private *pPrivate,
 /****** END SAM3 CODE ********/
 
 /* begin helpful debug code */
-
-static void
-sam3_sprintf(struct sam3_chip *pChip , const char *fmt, ...)
-{
-       va_list ap;
-       va_start(ap,fmt);
-       if (pChip->mbuf == NULL) {
-               return;
-       }
-
-       membuf_vsprintf(pChip->mbuf, fmt, ap);
-       va_end(ap);
-}
-
 // print the fieldname, the field value, in dec & hex, and return field value
 static uint32_t
 sam3_reg_fieldname(struct sam3_chip *pChip,
@@ -1042,7 +1021,7 @@ sam3_reg_fieldname(struct sam3_chip *pChip,
        }
 
        // show the basics
-       sam3_sprintf(pChip, "\t%*s: %*d [0x%0*x] ",
+       LOG_USER_N("\t%*s: %*d [0x%0*x] ",
                                  REG_NAME_WIDTH, regname,
                                  dwidth, v,
                                  hwidth, v);
@@ -1177,16 +1156,16 @@ sam3_explain_ckgr_mor(struct sam3_chip *pChip)
        uint32_t rcen;
 
        v = sam3_reg_fieldname(pChip, "MOSCXTEN", pChip->cfg.CKGR_MOR, 0, 1);
-       sam3_sprintf(pChip, "(main xtal enabled: %s)\n",
+       LOG_USER_N("(main xtal enabled: %s)\n",
                                  _yes_or_no(v));
        v = sam3_reg_fieldname(pChip, "MOSCXTBY", pChip->cfg.CKGR_MOR, 1, 1);
-       sam3_sprintf(pChip, "(main osc bypass: %s)\n",
+       LOG_USER_N("(main osc bypass: %s)\n",
                                  _yes_or_no(v));
        rcen = sam3_reg_fieldname(pChip, "MOSCRCEN", pChip->cfg.CKGR_MOR, 2, 1);
-       sam3_sprintf(pChip, "(onchip RC-OSC enabled: %s)\n",
+       LOG_USER_N("(onchip RC-OSC enabled: %s)\n",
                                  _yes_or_no(rcen));
        v = sam3_reg_fieldname(pChip, "MOSCRCF", pChip->cfg.CKGR_MOR, 4, 3);
-       sam3_sprintf(pChip, "(onchip RC-OSC freq: %s)\n",
+       LOG_USER_N("(onchip RC-OSC freq: %s)\n",
                                  _rc_freq[v]);
 
        pChip->cfg.rc_freq = 0;
@@ -1207,14 +1186,14 @@ sam3_explain_ckgr_mor(struct sam3_chip *pChip)
        }
 
        v = sam3_reg_fieldname(pChip,"MOSCXTST", pChip->cfg.CKGR_MOR, 8, 8);
-       sam3_sprintf(pChip, "(startup clks, time= %f uSecs)\n",
+       LOG_USER_N("(startup clks, time= %f uSecs)\n",
                                  ((float)(v * 1000000)) / ((float)(pChip->cfg.slow_freq)));
        v = sam3_reg_fieldname(pChip, "MOSCSEL", pChip->cfg.CKGR_MOR, 24, 1);
-       sam3_sprintf(pChip, "(mainosc source: %s)\n",
+       LOG_USER_N("(mainosc source: %s)\n",
                                  v ? "external xtal" : "internal RC");
 
        v = sam3_reg_fieldname(pChip,"CFDEN", pChip->cfg.CKGR_MOR, 25, 1);
-       sam3_sprintf(pChip, "(clock failure enabled: %s)\n",
+       LOG_USER_N("(clock failure enabled: %s)\n",
                                 _yes_or_no(v));
 }
 
@@ -1228,19 +1207,19 @@ sam3_explain_chipid_cidr(struct sam3_chip *pChip)
        const char *cp;
 
        sam3_reg_fieldname(pChip, "Version", pChip->cfg.CHIPID_CIDR, 0, 5);
-       sam3_sprintf(pChip,"\n");
+       LOG_USER_N("\n");
 
        v = sam3_reg_fieldname(pChip, "EPROC", pChip->cfg.CHIPID_CIDR, 5, 3);
-       sam3_sprintf(pChip, "%s\n", eproc_names[v]);
+       LOG_USER_N("%s\n", eproc_names[v]);
 
        v = sam3_reg_fieldname(pChip, "NVPSIZE", pChip->cfg.CHIPID_CIDR, 8, 4);
-       sam3_sprintf(pChip, "%s\n", nvpsize[v]);
+       LOG_USER_N("%s\n", nvpsize[v]);
 
        v = sam3_reg_fieldname(pChip, "NVPSIZE2", pChip->cfg.CHIPID_CIDR, 12, 4);
-       sam3_sprintf(pChip, "%s\n", nvpsize2[v]);
+       LOG_USER_N("%s\n", nvpsize2[v]);
 
        v = sam3_reg_fieldname(pChip, "SRAMSIZE", pChip->cfg.CHIPID_CIDR, 16,4);
-       sam3_sprintf(pChip, "%s\n", sramsize[ v ]);
+       LOG_USER_N("%s\n", sramsize[ v ]);
 
        v = sam3_reg_fieldname(pChip, "ARCH", pChip->cfg.CHIPID_CIDR, 20, 8);
        cp = _unknown;
@@ -1251,13 +1230,13 @@ sam3_explain_chipid_cidr(struct sam3_chip *pChip)
                }
        }
 
-       sam3_sprintf(pChip, "%s\n", cp);
+       LOG_USER_N("%s\n", cp);
 
        v = sam3_reg_fieldname(pChip, "NVPTYP", pChip->cfg.CHIPID_CIDR, 28, 3);
-       sam3_sprintf(pChip, "%s\n", nvptype[ v ]);
+       LOG_USER_N("%s\n", nvptype[ v ]);
 
        v = sam3_reg_fieldname(pChip, "EXTID", pChip->cfg.CHIPID_CIDR, 31, 1);
-       sam3_sprintf(pChip, "(exists: %s)\n", _yes_or_no(v));
+       LOG_USER_N("(exists: %s)\n", _yes_or_no(v));
 }
 
 static void
@@ -1267,14 +1246,14 @@ sam3_explain_ckgr_mcfr(struct sam3_chip *pChip)
 
 
        v = sam3_reg_fieldname(pChip, "MAINFRDY", pChip->cfg.CKGR_MCFR, 16, 1);
-       sam3_sprintf(pChip, "(main ready: %s)\n", _yes_or_no(v));
+       LOG_USER_N("(main ready: %s)\n", _yes_or_no(v));
 
        v = sam3_reg_fieldname(pChip, "MAINF", pChip->cfg.CKGR_MCFR, 0, 16);
 
        v = (v * pChip->cfg.slow_freq) / 16;
        pChip->cfg.mainosc_freq = v;
 
-       sam3_sprintf(pChip, "(%3.03f Mhz (%d.%03dkhz slowclk)\n",
+       LOG_USER_N("(%3.03f Mhz (%d.%03dkhz slowclk)\n",
                                 _tomhz(v),
                                 pChip->cfg.slow_freq / 1000,
                                 pChip->cfg.slow_freq % 1000);
@@ -1287,17 +1266,17 @@ sam3_explain_ckgr_plla(struct sam3_chip *pChip)
        uint32_t mula,diva;
 
        diva = sam3_reg_fieldname(pChip, "DIVA", pChip->cfg.CKGR_PLLAR, 0, 8);
-       sam3_sprintf(pChip,"\n");
+       LOG_USER_N("\n");
        mula = sam3_reg_fieldname(pChip, "MULA", pChip->cfg.CKGR_PLLAR, 16, 11);
-       sam3_sprintf(pChip,"\n");
+       LOG_USER_N("\n");
        pChip->cfg.plla_freq = 0;
        if (mula == 0) {
-               sam3_sprintf(pChip,"\tPLLA Freq: (Disabled,mula = 0)\n");
+               LOG_USER_N("\tPLLA Freq: (Disabled,mula = 0)\n");
        } else if (diva == 0) {
-               sam3_sprintf(pChip,"\tPLLA Freq: (Disabled,diva = 0)\n");
+               LOG_USER_N("\tPLLA Freq: (Disabled,diva = 0)\n");
        } else if (diva == 1) {
                pChip->cfg.plla_freq = (pChip->cfg.mainosc_freq * (mula + 1));
-               sam3_sprintf(pChip,"\tPLLA Freq: %3.03f MHz\n",
+               LOG_USER_N("\tPLLA Freq: %3.03f MHz\n",
                                         _tomhz(pChip->cfg.plla_freq));
        }
 }
@@ -1338,7 +1317,7 @@ sam3_explain_mckr(struct sam3_chip *pChip)
                break;
        }
 
-       sam3_sprintf(pChip, "%s (%3.03f Mhz)\n",
+       LOG_USER_N("%s (%3.03f Mhz)\n",
                                  cp,
                                  _tomhz(fin));
        pres = sam3_reg_fieldname(pChip, "PRES", pChip->cfg.PMC_MCKR, 4, 3);
@@ -1378,14 +1357,14 @@ sam3_explain_mckr(struct sam3_chip *pChip)
                assert(0);
                break;
        }
-       sam3_sprintf(pChip, "(%s)\n", cp);
+       LOG_USER_N("(%s)\n", cp);
        fin = fin / pdiv;
        // sam3 has a *SINGLE* clock -
        // other at91 series parts have divisors for these.
        pChip->cfg.cpu_freq = fin;
        pChip->cfg.mclk_freq = fin;
        pChip->cfg.fclk_freq = fin;
-       sam3_sprintf(pChip, "\t\tResult CPU Freq: %3.03f\n",
+       LOG_USER_N("\t\tResult CPU Freq: %3.03f\n",
                                  _tomhz(fin));
 }
 
@@ -1542,15 +1521,12 @@ sam3_GetInfo(struct sam3_chip *pChip)
        const struct sam3_reg_list *pReg;
        uint32_t regval;
 
-       membuf_reset(pChip->mbuf);
-
-
        pReg = &(sam3_all_regs[0]);
        while (pReg->name) {
                // display all regs
                LOG_DEBUG("Start: %s", pReg->name);
                regval = *sam3_get_reg_ptr(&(pChip->cfg), pReg);
-               sam3_sprintf(pChip, "%*s: [0x%08x] -> 0x%08x\n",
+               LOG_USER_N("%*s: [0x%08x] -> 0x%08x\n",
                                         REG_NAME_WIDTH,
                                         pReg->name,
                                         pReg->address,
@@ -1561,14 +1537,14 @@ sam3_GetInfo(struct sam3_chip *pChip)
                LOG_DEBUG("End: %s", pReg->name);
                pReg++;
        }
-       sam3_sprintf(pChip,"   rc-osc: %3.03f MHz\n", _tomhz(pChip->cfg.rc_freq));
-       sam3_sprintf(pChip,"  mainosc: %3.03f MHz\n", _tomhz(pChip->cfg.mainosc_freq));
-       sam3_sprintf(pChip,"     plla: %3.03f MHz\n", _tomhz(pChip->cfg.plla_freq));
-       sam3_sprintf(pChip," cpu-freq: %3.03f MHz\n", _tomhz(pChip->cfg.cpu_freq));
-       sam3_sprintf(pChip,"mclk-freq: %3.03f MHz\n", _tomhz(pChip->cfg.mclk_freq));
+       LOG_USER_N("   rc-osc: %3.03f MHz\n", _tomhz(pChip->cfg.rc_freq));
+       LOG_USER_N("  mainosc: %3.03f MHz\n", _tomhz(pChip->cfg.mainosc_freq));
+       LOG_USER_N("     plla: %3.03f MHz\n", _tomhz(pChip->cfg.plla_freq));
+       LOG_USER_N(" cpu-freq: %3.03f MHz\n", _tomhz(pChip->cfg.cpu_freq));
+       LOG_USER_N("mclk-freq: %3.03f MHz\n", _tomhz(pChip->cfg.mclk_freq));
 
 
-       sam3_sprintf(pChip, " UniqueId: 0x%08x 0x%08x 0x%08x 0x%08x\n",
+       LOG_USER_N(" UniqueId: 0x%08x 0x%08x 0x%08x 0x%08x\n",
                                  pChip->cfg.unique_id[0],
                                  pChip->cfg.unique_id[1],
                                  pChip->cfg.unique_id[2],
@@ -1668,11 +1644,6 @@ FLASH_BANK_COMMAND_HANDLER(sam3_flash_bank_command)
                // assumption is this runs at 32khz
                pChip->cfg.slow_freq = 32768;
                pChip->probed = 0;
-               pChip->mbuf = membuf_new();
-               if (!(pChip->mbuf)) {
-                       LOG_ERROR("no memory");
-                       return ERROR_FAIL;
-               }
        }
 
        switch (bank->base) {
@@ -1706,11 +1677,8 @@ sam3_GetDetails(struct sam3_bank_private *pPrivate)
 {
        const struct sam3_chip_details *pDetails;
        struct sam3_chip *pChip;
-       void *vp;
        struct flash_bank *saved_banks[SAM3_MAX_FLASH_BANKS];
-
        unsigned x;
-       const char *cp;
 
        LOG_DEBUG("Begin");
        pDetails = all_sam3_details;
@@ -1725,16 +1693,9 @@ sam3_GetDetails(struct sam3_bank_private *pPrivate)
                LOG_ERROR("SAM3 ChipID 0x%08x not found in table (perhaps you can this chip?)",
                                  (unsigned int)(pPrivate->pChip->cfg.CHIPID_CIDR));
                // Help the victim, print details about the chip
-               membuf_reset(pPrivate->pChip->mbuf);
-               membuf_sprintf(pPrivate->pChip->mbuf,
-                                               "SAM3 CHIPID_CIDR: 0x%08x decodes as follows\n",
+               LOG_INFO_N("SAM3 CHIPID_CIDR: 0x%08x decodes as follows\n",
                                                pPrivate->pChip->cfg.CHIPID_CIDR);
                sam3_explain_chipid_cidr(pPrivate->pChip);
-               cp = membuf_strtok(pPrivate->pChip->mbuf, "\n", &vp);
-               while (cp) {
-                       LOG_INFO("%s", cp);
-                       cp = membuf_strtok(NULL, "\n", &vp);
-               }
                return ERROR_FAIL;
        }
 
@@ -1802,18 +1763,14 @@ _sam3_probe(struct flash_bank *bank, int noise)
 
 
        LOG_DEBUG("Here");
-       r = sam3_GetInfo(pPrivate->pChip);
+       if (pPrivate->pChip->probed) {
+               r = sam3_GetInfo(pPrivate->pChip);
+       } else {
+               r = sam3_GetDetails(pPrivate);
+       }
        if (r != ERROR_OK) {
                return r;
        }
-       if (!(pPrivate->pChip->probed)) {
-               pPrivate->pChip->probed = 1;
-               LOG_DEBUG("Here");
-               r = sam3_GetDetails(pPrivate);
-               if (r != ERROR_OK) {
-                       return r;
-               }
-       }
 
        // update the flash bank size
        for (x = 0 ; x < SAM3_MAX_FLASH_BANKS ; x++) {
@@ -2050,9 +2007,7 @@ sam3_page_write_opcodes[] = {
        0x10,0xf0,0x01,0x0f,
        //  41 0024 FBD0                beq     .L4
        0xfb,0xd0,
-       //  42                  .done:
-       //  43 0026 FEE7                b       .done
-       0xfe,0xe7
+       0x00,0xBE                               /* bkpt #0 */
 };
 
 
@@ -2262,8 +2217,6 @@ sam3_write(struct flash_bank *bank,
 COMMAND_HANDLER(sam3_handle_info_command)
 {
        struct sam3_chip *pChip;
-       void *vp;
-       const char *cp;
        unsigned x;
        int r;
 
@@ -2292,7 +2245,7 @@ COMMAND_HANDLER(sam3_handle_info_command)
                        return ERROR_FAIL;
                }
        }
-       // above garentees the "chip details" structure is valid
+       // above guarantees the "chip details" structure is valid
        // and thus, bank private areas are valid
        // and we have a SAM3 chip, what a concept!
 
@@ -2325,13 +2278,6 @@ COMMAND_HANDLER(sam3_handle_info_command)
                return r;
        }
 
-
-       // print results
-       cp = membuf_strtok(pChip->mbuf, "\n", &vp);
-       while (cp) {
-               command_print(CMD_CTX,"%s", cp);
-               cp = membuf_strtok(NULL, "\n", &vp);
-       }
        return ERROR_OK;
 }
 
@@ -2390,7 +2336,7 @@ COMMAND_HANDLER(sam3_handle_gpnvm_command)
 
        if (0 == strcmp("show", CMD_ARGV[0])) {
                if (who == -1) {
-               showall:
+showall:
                        r = ERROR_OK;
                        for (x = 0 ; x < pChip->details.n_gpnvms ; x++) {
                                r = FLASHD_GetGPNVM(&(pChip->details.bank[0]), x, &v);
@@ -2470,24 +2416,27 @@ COMMAND_HANDLER(sam3_handle_slowclk_command)
 static const struct command_registration at91sam3_exec_command_handlers[] = {
        {
                .name = "gpnvm",
-               .handler = &sam3_handle_gpnvm_command,
+               .handler = sam3_handle_gpnvm_command,
                .mode = COMMAND_EXEC,
-               .usage = "[(set|clear) [<bit_id>]]",
-               .help = "Without arguments, shows the gpnvm register; "
-                       "otherwise, sets or clear the specified bit.",
+               .usage = "[('clr'|'set'|'show') bitnum]",
+               .help = "Without arguments, shows all bits in the gpnvm "
+                       "register.  Otherwise, clears, sets, or shows one "
+                       "General Purpose Non-Volatile Memory (gpnvm) bit.",
        },
        {
                .name = "info",
-               .handler = &sam3_handle_info_command,
+               .handler = sam3_handle_info_command,
                .mode = COMMAND_EXEC,
-               .help = "print information about the current sam3 chip",
+               .help = "Print information about the current at91sam3 chip"
+                       "and its flash configuration.",
        },
        {
                .name = "slowclk",
-               .handler = &sam3_handle_slowclk_command,
+               .handler = sam3_handle_slowclk_command,
                .mode = COMMAND_EXEC,
-               .usage = "<value>",
-               .help = "set the slowclock frequency (default 32768hz)",
+               .usage = "[clock_hz]",
+               .help = "Display or set the slowclock frequency "
+                       "(default 32768 Hz).",
        },
        COMMAND_REGISTRATION_DONE
 };
@@ -2502,15 +2451,16 @@ static const struct command_registration at91sam3_command_handlers[] = {
 };
 
 struct flash_driver at91sam3_flash = {
-               .name = "at91sam3",
-               .commands = at91sam3_command_handlers,
-               .flash_bank_command = &sam3_flash_bank_command,
-               .erase = &sam3_erase,
-               .protect = &sam3_protect,
-               .write = &sam3_write,
-               .probe = &sam3_probe,
-               .auto_probe = &sam3_auto_probe,
-               .erase_check = &sam3_erase_check,
-               .protect_check = &sam3_protect_check,
-               .info = &sam3_info,
-       };
+       .name = "at91sam3",
+       .commands = at91sam3_command_handlers,
+       .flash_bank_command = sam3_flash_bank_command,
+       .erase = sam3_erase,
+       .protect = sam3_protect,
+       .write = sam3_write,
+       .read = default_flash_read,
+       .probe = sam3_probe,
+       .auto_probe = sam3_auto_probe,
+       .erase_check = sam3_erase_check,
+       .protect_check = sam3_protect_check,
+       .info = sam3_info,
+};