You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

at91samd.c 31 KiB

at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
at91samd: fix protect, add EEPROM and boot commands There were two problems with the _protect() feature: 1. The address written was off by a factor of two because the address register takes 16-bit rather than 8-bit addresses. As a result the wrong sectors were (un)protected with the protect command. This has been fixed. 2. The protection settings issued via the lock or unlock region commands don't persist after reset. Making them persist requires modifying the LOCK bits in the User Row using the infrastructure described below. The Atmel SAMD2x MCUs provide a User Row (the size of which is one page). This contains a few settings that users may wish to modify from the debugger, especially during production. This change adds commands to inspect and set: - EEPROM size, the size in bytes of the emulated EEPROM region of the Flash. - Bootloader size, the size in bytes of the protected "boot" section of the Flash. This is done by a careful read-modify-write of the special User Row page, avoiding erasing when possible and disallowing the changing of documented reserved bits. The Atmel SAMD20 datasheet was used for bit positions and descriptions, size tables, etc. and testing was done on a SAMD20 Xplained Pro board. It's technically possible to store arbitrary user data (ex: serial numbers, MAC addresses, etc) in the remaining portion of the User Row page (that is, beyond the first 64 bits of it). The infrastructure used by the eeprom and bootloader commands can be used to access this as well, and this seems safer than exposing the User Row as a normal Flash sector that openocd understands due to the delicate nature of some of the data stored there. Change-Id: I29ca1bdbdc7884bc0ba0ad18af1b6bab78c7ad38 Signed-off-by: Andrey Yurovsky <yurovsky@gmail.com> Reviewed-on: http://openocd.zylin.com/2326 Tested-by: jenkins Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. /***************************************************************************
  2. * Copyright (C) 2013 by Andrey Yurovsky *
  3. * Andrey Yurovsky <yurovsky@gmail.com> *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "imp.h"
  24. #include "helper/binarybuffer.h"
  25. #include <target/cortex_m.h>
  26. #define SAMD_NUM_SECTORS 16
  27. #define SAMD_PAGE_SIZE_MAX 1024
  28. #define SAMD_FLASH ((uint32_t)0x00000000) /* physical Flash memory */
  29. #define SAMD_USER_ROW ((uint32_t)0x00804000) /* User Row of Flash */
  30. #define SAMD_PAC1 0x41000000 /* Peripheral Access Control 1 */
  31. #define SAMD_DSU 0x41002000 /* Device Service Unit */
  32. #define SAMD_NVMCTRL 0x41004000 /* Non-volatile memory controller */
  33. #define SAMD_DSU_STATUSA 1 /* DSU status register */
  34. #define SAMD_DSU_DID 0x18 /* Device ID register */
  35. #define SAMD_NVMCTRL_CTRLA 0x00 /* NVM control A register */
  36. #define SAMD_NVMCTRL_CTRLB 0x04 /* NVM control B register */
  37. #define SAMD_NVMCTRL_PARAM 0x08 /* NVM parameters register */
  38. #define SAMD_NVMCTRL_INTFLAG 0x18 /* NVM Interupt Flag Status & Clear */
  39. #define SAMD_NVMCTRL_STATUS 0x18 /* NVM status register */
  40. #define SAMD_NVMCTRL_ADDR 0x1C /* NVM address register */
  41. #define SAMD_NVMCTRL_LOCK 0x20 /* NVM Lock section register */
  42. #define SAMD_CMDEX_KEY 0xA5UL
  43. #define SAMD_NVM_CMD(n) ((SAMD_CMDEX_KEY << 8) | (n & 0x7F))
  44. /* NVMCTRL commands. See Table 20-4 in 42129F–SAM–10/2013 */
  45. #define SAMD_NVM_CMD_ER 0x02 /* Erase Row */
  46. #define SAMD_NVM_CMD_WP 0x04 /* Write Page */
  47. #define SAMD_NVM_CMD_EAR 0x05 /* Erase Auxilary Row */
  48. #define SAMD_NVM_CMD_WAP 0x06 /* Write Auxilary Page */
  49. #define SAMD_NVM_CMD_LR 0x40 /* Lock Region */
  50. #define SAMD_NVM_CMD_UR 0x41 /* Unlock Region */
  51. #define SAMD_NVM_CMD_SPRM 0x42 /* Set Power Reduction Mode */
  52. #define SAMD_NVM_CMD_CPRM 0x43 /* Clear Power Reduction Mode */
  53. #define SAMD_NVM_CMD_PBC 0x44 /* Page Buffer Clear */
  54. #define SAMD_NVM_CMD_SSB 0x45 /* Set Security Bit */
  55. #define SAMD_NVM_CMD_INVALL 0x46 /* Invalidate all caches */
  56. /* NVMCTRL bits */
  57. #define SAMD_NVM_CTRLB_MANW 0x80
  58. /* Known identifiers */
  59. #define SAMD_PROCESSOR_M0 0x01
  60. #define SAMD_FAMILY_D 0x00
  61. #define SAMD_FAMILY_L 0x01
  62. #define SAMD_FAMILY_C 0x02
  63. #define SAMD_SERIES_20 0x00
  64. #define SAMD_SERIES_21 0x01
  65. #define SAMD_SERIES_22 0x02
  66. #define SAMD_SERIES_10 0x02
  67. #define SAMD_SERIES_11 0x03
  68. /* Device ID macros */
  69. #define SAMD_GET_PROCESSOR(id) (id >> 28)
  70. #define SAMD_GET_FAMILY(id) (((id >> 23) & 0x1F))
  71. #define SAMD_GET_SERIES(id) (((id >> 16) & 0x3F))
  72. #define SAMD_GET_DEVSEL(id) (id & 0xFF)
  73. struct samd_part {
  74. uint8_t id;
  75. const char *name;
  76. uint32_t flash_kb;
  77. uint32_t ram_kb;
  78. };
  79. /* Known SAMD10 parts */
  80. static const struct samd_part samd10_parts[] = {
  81. { 0x0, "SAMD10D14AMU", 16, 4 },
  82. { 0x1, "SAMD10D13AMU", 8, 4 },
  83. { 0x2, "SAMD10D12AMU", 4, 4 },
  84. { 0x3, "SAMD10D14ASU", 16, 4 },
  85. { 0x4, "SAMD10D13ASU", 8, 4 },
  86. { 0x5, "SAMD10D12ASU", 4, 4 },
  87. { 0x6, "SAMD10C14A", 16, 4 },
  88. { 0x7, "SAMD10C13A", 8, 4 },
  89. { 0x8, "SAMD10C12A", 4, 4 },
  90. };
  91. /* Known SAMD11 parts */
  92. static const struct samd_part samd11_parts[] = {
  93. { 0x0, "SAMD11D14AMU", 16, 4 },
  94. { 0x1, "SAMD11D13AMU", 8, 4 },
  95. { 0x2, "SAMD11D12AMU", 4, 4 },
  96. { 0x3, "SAMD11D14ASU", 16, 4 },
  97. { 0x4, "SAMD11D13ASU", 8, 4 },
  98. { 0x5, "SAMD11D12ASU", 4, 4 },
  99. { 0x6, "SAMD11C14A", 16, 4 },
  100. { 0x7, "SAMD11C13A", 8, 4 },
  101. { 0x8, "SAMD11C12A", 4, 4 },
  102. };
  103. /* Known SAMD20 parts. See Table 12-8 in 42129F–SAM–10/2013 */
  104. static const struct samd_part samd20_parts[] = {
  105. { 0x0, "SAMD20J18A", 256, 32 },
  106. { 0x1, "SAMD20J17A", 128, 16 },
  107. { 0x2, "SAMD20J16A", 64, 8 },
  108. { 0x3, "SAMD20J15A", 32, 4 },
  109. { 0x4, "SAMD20J14A", 16, 2 },
  110. { 0x5, "SAMD20G18A", 256, 32 },
  111. { 0x6, "SAMD20G17A", 128, 16 },
  112. { 0x7, "SAMD20G16A", 64, 8 },
  113. { 0x8, "SAMD20G15A", 32, 4 },
  114. { 0x9, "SAMD20G14A", 16, 2 },
  115. { 0xA, "SAMD20E18A", 256, 32 },
  116. { 0xB, "SAMD20E17A", 128, 16 },
  117. { 0xC, "SAMD20E16A", 64, 8 },
  118. { 0xD, "SAMD20E15A", 32, 4 },
  119. { 0xE, "SAMD20E14A", 16, 2 },
  120. };
  121. /* Known SAMD21 parts. */
  122. static const struct samd_part samd21_parts[] = {
  123. { 0x0, "SAMD21J18A", 256, 32 },
  124. { 0x1, "SAMD21J17A", 128, 16 },
  125. { 0x2, "SAMD21J16A", 64, 8 },
  126. { 0x3, "SAMD21J15A", 32, 4 },
  127. { 0x4, "SAMD21J14A", 16, 2 },
  128. { 0x5, "SAMD21G18A", 256, 32 },
  129. { 0x6, "SAMD21G17A", 128, 16 },
  130. { 0x7, "SAMD21G16A", 64, 8 },
  131. { 0x8, "SAMD21G15A", 32, 4 },
  132. { 0x9, "SAMD21G14A", 16, 2 },
  133. { 0xA, "SAMD21E18A", 256, 32 },
  134. { 0xB, "SAMD21E17A", 128, 16 },
  135. { 0xC, "SAMD21E16A", 64, 8 },
  136. { 0xD, "SAMD21E15A", 32, 4 },
  137. { 0xE, "SAMD21E14A", 16, 2 },
  138. /* Below are B Variants (Table 3-7 from rev I of datasheet) */
  139. { 0x20, "SAMD21J16B", 64, 8 },
  140. { 0x21, "SAMD21J15B", 32, 4 },
  141. { 0x23, "SAMD21G16B", 64, 8 },
  142. { 0x24, "SAMD21G15B", 32, 4 },
  143. { 0x26, "SAMD21E16B", 64, 8 },
  144. { 0x27, "SAMD21E15B", 32, 4 },
  145. };
  146. /* Known SAMR21 parts. */
  147. static const struct samd_part samr21_parts[] = {
  148. { 0x19, "SAMR21G18A", 256, 32 },
  149. { 0x1A, "SAMR21G17A", 128, 32 },
  150. { 0x1B, "SAMR21G16A", 64, 32 },
  151. { 0x1C, "SAMR21E18A", 256, 32 },
  152. { 0x1D, "SAMR21E17A", 128, 32 },
  153. { 0x1E, "SAMR21E16A", 64, 32 },
  154. };
  155. /* Known SAML21 parts. */
  156. static const struct samd_part saml21_parts[] = {
  157. { 0x00, "SAML21J18A", 256, 32 },
  158. { 0x01, "SAML21J17A", 128, 16 },
  159. { 0x02, "SAML21J16A", 64, 8 },
  160. { 0x05, "SAML21G18A", 256, 32 },
  161. { 0x06, "SAML21G17A", 128, 16 },
  162. { 0x07, "SAML21G16A", 64, 8 },
  163. { 0x0A, "SAML21E18A", 256, 32 },
  164. { 0x0B, "SAML21E17A", 128, 16 },
  165. { 0x0C, "SAML21E16A", 64, 8 },
  166. { 0x0D, "SAML21E15A", 32, 4 },
  167. { 0x0F, "SAML21J18B", 256, 32 },
  168. { 0x10, "SAML21J17B", 128, 16 },
  169. { 0x11, "SAML21J16B", 64, 8 },
  170. { 0x14, "SAML21G18B", 256, 32 },
  171. { 0x15, "SAML21G17B", 128, 16 },
  172. { 0x16, "SAML21G16B", 64, 8 },
  173. { 0x19, "SAML21E18B", 256, 32 },
  174. { 0x1A, "SAML21E17B", 128, 16 },
  175. { 0x1B, "SAML21E16B", 64, 8 },
  176. { 0x1C, "SAML21E15B", 32, 4 },
  177. };
  178. /* Known SAML22 parts. */
  179. static const struct samd_part saml22_parts[] = {
  180. { 0x00, "SAML22N18A", 256, 32 },
  181. { 0x01, "SAML22N17A", 128, 16 },
  182. { 0x02, "SAML22N16A", 64, 8 },
  183. { 0x05, "SAML22J18A", 256, 32 },
  184. { 0x06, "SAML22J17A", 128, 16 },
  185. { 0x07, "SAML22J16A", 64, 8 },
  186. { 0x0A, "SAML22G18A", 256, 32 },
  187. { 0x0B, "SAML22G17A", 128, 16 },
  188. { 0x0C, "SAML22G16A", 64, 8 },
  189. };
  190. /* Known SAMC20 parts. */
  191. static const struct samd_part samc20_parts[] = {
  192. { 0x00, "SAMC20J18A", 256, 32 },
  193. { 0x01, "SAMC20J17A", 128, 16 },
  194. { 0x02, "SAMC20J16A", 64, 8 },
  195. { 0x03, "SAMC20J15A", 32, 4 },
  196. { 0x05, "SAMC20G18A", 256, 32 },
  197. { 0x06, "SAMC20G17A", 128, 16 },
  198. { 0x07, "SAMC20G16A", 64, 8 },
  199. { 0x08, "SAMC20G15A", 32, 4 },
  200. { 0x0A, "SAMC20E18A", 256, 32 },
  201. { 0x0B, "SAMC20E17A", 128, 16 },
  202. { 0x0C, "SAMC20E16A", 64, 8 },
  203. { 0x0D, "SAMC20E15A", 32, 4 },
  204. };
  205. /* Known SAMC21 parts. */
  206. static const struct samd_part samc21_parts[] = {
  207. { 0x00, "SAMC21J18A", 256, 32 },
  208. { 0x01, "SAMC21J17A", 128, 16 },
  209. { 0x02, "SAMC21J16A", 64, 8 },
  210. { 0x03, "SAMC21J15A", 32, 4 },
  211. { 0x05, "SAMC21G18A", 256, 32 },
  212. { 0x06, "SAMC21G17A", 128, 16 },
  213. { 0x07, "SAMC21G16A", 64, 8 },
  214. { 0x08, "SAMC21G15A", 32, 4 },
  215. { 0x0A, "SAMC21E18A", 256, 32 },
  216. { 0x0B, "SAMC21E17A", 128, 16 },
  217. { 0x0C, "SAMC21E16A", 64, 8 },
  218. { 0x0D, "SAMC21E15A", 32, 4 },
  219. };
  220. /* Each family of parts contains a parts table in the DEVSEL field of DID. The
  221. * processor ID, family ID, and series ID are used to determine which exact
  222. * family this is and then we can use the corresponding table. */
  223. struct samd_family {
  224. uint8_t processor;
  225. uint8_t family;
  226. uint8_t series;
  227. const struct samd_part *parts;
  228. size_t num_parts;
  229. };
  230. /* Known SAMD families */
  231. static const struct samd_family samd_families[] = {
  232. { SAMD_PROCESSOR_M0, SAMD_FAMILY_D, SAMD_SERIES_20,
  233. samd20_parts, ARRAY_SIZE(samd20_parts) },
  234. { SAMD_PROCESSOR_M0, SAMD_FAMILY_D, SAMD_SERIES_21,
  235. samd21_parts, ARRAY_SIZE(samd21_parts) },
  236. { SAMD_PROCESSOR_M0, SAMD_FAMILY_D, SAMD_SERIES_21,
  237. samr21_parts, ARRAY_SIZE(samr21_parts) },
  238. { SAMD_PROCESSOR_M0, SAMD_FAMILY_D, SAMD_SERIES_10,
  239. samd10_parts, ARRAY_SIZE(samd10_parts) },
  240. { SAMD_PROCESSOR_M0, SAMD_FAMILY_D, SAMD_SERIES_11,
  241. samd11_parts, ARRAY_SIZE(samd11_parts) },
  242. { SAMD_PROCESSOR_M0, SAMD_FAMILY_L, SAMD_SERIES_21,
  243. saml21_parts, ARRAY_SIZE(saml21_parts) },
  244. { SAMD_PROCESSOR_M0, SAMD_FAMILY_L, SAMD_SERIES_22,
  245. saml22_parts, ARRAY_SIZE(saml22_parts) },
  246. { SAMD_PROCESSOR_M0, SAMD_FAMILY_C, SAMD_SERIES_20,
  247. samc20_parts, ARRAY_SIZE(samc20_parts) },
  248. { SAMD_PROCESSOR_M0, SAMD_FAMILY_C, SAMD_SERIES_21,
  249. samc21_parts, ARRAY_SIZE(samc21_parts) },
  250. };
  251. struct samd_info {
  252. uint32_t page_size;
  253. int num_pages;
  254. int sector_size;
  255. bool probed;
  256. struct target *target;
  257. struct samd_info *next;
  258. };
  259. static struct samd_info *samd_chips;
  260. static const struct samd_part *samd_find_part(uint32_t id)
  261. {
  262. uint8_t processor = SAMD_GET_PROCESSOR(id);
  263. uint8_t family = SAMD_GET_FAMILY(id);
  264. uint8_t series = SAMD_GET_SERIES(id);
  265. uint8_t devsel = SAMD_GET_DEVSEL(id);
  266. for (unsigned i = 0; i < ARRAY_SIZE(samd_families); i++) {
  267. if (samd_families[i].processor == processor &&
  268. samd_families[i].series == series &&
  269. samd_families[i].family == family) {
  270. for (unsigned j = 0; j < samd_families[i].num_parts; j++) {
  271. if (samd_families[i].parts[j].id == devsel)
  272. return &samd_families[i].parts[j];
  273. }
  274. }
  275. }
  276. return NULL;
  277. }
  278. static int samd_protect_check(struct flash_bank *bank)
  279. {
  280. int res;
  281. uint16_t lock;
  282. res = target_read_u16(bank->target,
  283. SAMD_NVMCTRL + SAMD_NVMCTRL_LOCK, &lock);
  284. if (res != ERROR_OK)
  285. return res;
  286. /* Lock bits are active-low */
  287. for (int i = 0; i < bank->num_sectors; i++)
  288. bank->sectors[i].is_protected = !(lock & (1<<i));
  289. return ERROR_OK;
  290. }
  291. static int samd_get_flash_page_info(struct target *target,
  292. uint32_t *sizep, int *nump)
  293. {
  294. int res;
  295. uint32_t param;
  296. res = target_read_u32(target, SAMD_NVMCTRL + SAMD_NVMCTRL_PARAM, &param);
  297. if (res == ERROR_OK) {
  298. /* The PSZ field (bits 18:16) indicate the page size bytes as 2^(3+n)
  299. * so 0 is 8KB and 7 is 1024KB. */
  300. if (sizep)
  301. *sizep = (8 << ((param >> 16) & 0x7));
  302. /* The NVMP field (bits 15:0) indicates the total number of pages */
  303. if (nump)
  304. *nump = param & 0xFFFF;
  305. } else {
  306. LOG_ERROR("Couldn't read NVM Parameters register");
  307. }
  308. return res;
  309. }
  310. static int samd_probe(struct flash_bank *bank)
  311. {
  312. uint32_t id;
  313. int res;
  314. struct samd_info *chip = (struct samd_info *)bank->driver_priv;
  315. const struct samd_part *part;
  316. if (chip->probed)
  317. return ERROR_OK;
  318. res = target_read_u32(bank->target, SAMD_DSU + SAMD_DSU_DID, &id);
  319. if (res != ERROR_OK) {
  320. LOG_ERROR("Couldn't read Device ID register");
  321. return res;
  322. }
  323. part = samd_find_part(id);
  324. if (part == NULL) {
  325. LOG_ERROR("Couldn't find part corresponding to DID %08" PRIx32, id);
  326. return ERROR_FAIL;
  327. }
  328. bank->size = part->flash_kb * 1024;
  329. chip->sector_size = bank->size / SAMD_NUM_SECTORS;
  330. res = samd_get_flash_page_info(bank->target, &chip->page_size,
  331. &chip->num_pages);
  332. if (res != ERROR_OK) {
  333. LOG_ERROR("Couldn't determine Flash page size");
  334. return res;
  335. }
  336. /* Sanity check: the total flash size in the DSU should match the page size
  337. * multiplied by the number of pages. */
  338. if (bank->size != chip->num_pages * chip->page_size) {
  339. LOG_WARNING("SAMD: bank size doesn't match NVM parameters. "
  340. "Identified %" PRIu32 "KB Flash but NVMCTRL reports %u %" PRIu32 "B pages",
  341. part->flash_kb, chip->num_pages, chip->page_size);
  342. }
  343. /* Allocate the sector table */
  344. bank->num_sectors = SAMD_NUM_SECTORS;
  345. bank->sectors = calloc(bank->num_sectors, sizeof((bank->sectors)[0]));
  346. if (!bank->sectors)
  347. return ERROR_FAIL;
  348. /* Fill out the sector information: all SAMD sectors are the same size and
  349. * there is always a fixed number of them. */
  350. for (int i = 0; i < bank->num_sectors; i++) {
  351. bank->sectors[i].size = chip->sector_size;
  352. bank->sectors[i].offset = i * chip->sector_size;
  353. /* mark as unknown */
  354. bank->sectors[i].is_erased = -1;
  355. bank->sectors[i].is_protected = -1;
  356. }
  357. samd_protect_check(bank);
  358. /* Done */
  359. chip->probed = true;
  360. LOG_INFO("SAMD MCU: %s (%" PRIu32 "KB Flash, %" PRIu32 "KB RAM)", part->name,
  361. part->flash_kb, part->ram_kb);
  362. return ERROR_OK;
  363. }
  364. static bool samd_check_error(struct target *target)
  365. {
  366. int ret;
  367. bool error;
  368. uint16_t status;
  369. ret = target_read_u16(target,
  370. SAMD_NVMCTRL + SAMD_NVMCTRL_STATUS, &status);
  371. if (ret != ERROR_OK) {
  372. LOG_ERROR("Can't read NVM status");
  373. return true;
  374. }
  375. if (status & 0x001C) {
  376. if (status & (1 << 4)) /* NVME */
  377. LOG_ERROR("SAMD: NVM Error");
  378. if (status & (1 << 3)) /* LOCKE */
  379. LOG_ERROR("SAMD: NVM lock error");
  380. if (status & (1 << 2)) /* PROGE */
  381. LOG_ERROR("SAMD: NVM programming error");
  382. error = true;
  383. } else {
  384. error = false;
  385. }
  386. /* Clear the error conditions by writing a one to them */
  387. ret = target_write_u16(target,
  388. SAMD_NVMCTRL + SAMD_NVMCTRL_STATUS, status);
  389. if (ret != ERROR_OK)
  390. LOG_ERROR("Can't clear NVM error conditions");
  391. return error;
  392. }
  393. static int samd_issue_nvmctrl_command(struct target *target, uint16_t cmd)
  394. {
  395. int res;
  396. if (target->state != TARGET_HALTED) {
  397. LOG_ERROR("Target not halted");
  398. return ERROR_TARGET_NOT_HALTED;
  399. }
  400. /* Issue the NVM command */
  401. res = target_write_u16(target,
  402. SAMD_NVMCTRL + SAMD_NVMCTRL_CTRLA, SAMD_NVM_CMD(cmd));
  403. if (res != ERROR_OK)
  404. return res;
  405. /* Check to see if the NVM command resulted in an error condition. */
  406. if (samd_check_error(target))
  407. return ERROR_FAIL;
  408. return ERROR_OK;
  409. }
  410. static int samd_erase_row(struct target *target, uint32_t address)
  411. {
  412. int res;
  413. /* Set an address contained in the row to be erased */
  414. res = target_write_u32(target,
  415. SAMD_NVMCTRL + SAMD_NVMCTRL_ADDR, address >> 1);
  416. /* Issue the Erase Row command to erase that row. */
  417. if (res == ERROR_OK)
  418. res = samd_issue_nvmctrl_command(target,
  419. address == SAMD_USER_ROW ? SAMD_NVM_CMD_EAR : SAMD_NVM_CMD_ER);
  420. if (res != ERROR_OK) {
  421. LOG_ERROR("Failed to erase row containing %08" PRIx32, address);
  422. return ERROR_FAIL;
  423. }
  424. return ERROR_OK;
  425. }
  426. static bool is_user_row_reserved_bit(uint8_t bit)
  427. {
  428. /* See Table 9-3 in the SAMD20 datasheet for more information. */
  429. switch (bit) {
  430. /* Reserved bits */
  431. case 3:
  432. case 7:
  433. /* Voltage regulator internal configuration with default value of 0x70,
  434. * may not be changed. */
  435. case 17 ... 24:
  436. /* 41 is voltage regulator internal configuration and must not be
  437. * changed. 42 through 47 are reserved. */
  438. case 41 ... 47:
  439. return true;
  440. default:
  441. break;
  442. }
  443. return false;
  444. }
  445. /* Modify the contents of the User Row in Flash. These are described in Table
  446. * 9-3 of the SAMD20 datasheet. The User Row itself has a size of one page
  447. * and contains a combination of "fuses" and calibration data in bits 24:17.
  448. * We therefore try not to erase the row's contents unless we absolutely have
  449. * to and we don't permit modifying reserved bits. */
  450. static int samd_modify_user_row(struct target *target, uint32_t value,
  451. uint8_t startb, uint8_t endb)
  452. {
  453. int res;
  454. if (is_user_row_reserved_bit(startb) || is_user_row_reserved_bit(endb)) {
  455. LOG_ERROR("Can't modify bits in the requested range");
  456. return ERROR_FAIL;
  457. }
  458. /* Retrieve the MCU's page size, in bytes. This is also the size of the
  459. * entire User Row. */
  460. uint32_t page_size;
  461. res = samd_get_flash_page_info(target, &page_size, NULL);
  462. if (res != ERROR_OK) {
  463. LOG_ERROR("Couldn't determine Flash page size");
  464. return res;
  465. }
  466. /* Make sure the size is sane before we allocate. */
  467. assert(page_size > 0 && page_size <= SAMD_PAGE_SIZE_MAX);
  468. /* Make sure we're within the single page that comprises the User Row. */
  469. if (startb >= (page_size * 8) || endb >= (page_size * 8)) {
  470. LOG_ERROR("Can't modify bits outside the User Row page range");
  471. return ERROR_FAIL;
  472. }
  473. uint8_t *buf = malloc(page_size);
  474. if (!buf)
  475. return ERROR_FAIL;
  476. /* Read the user row (comprising one page) by half-words. */
  477. res = target_read_memory(target, SAMD_USER_ROW, 2, page_size / 2, buf);
  478. if (res != ERROR_OK)
  479. goto out_user_row;
  480. /* We will need to erase before writing if the new value needs a '1' in any
  481. * position for which the current value had a '0'. Otherwise we can avoid
  482. * erasing. */
  483. uint32_t cur = buf_get_u32(buf, startb, endb - startb + 1);
  484. if ((~cur) & value) {
  485. res = samd_erase_row(target, SAMD_USER_ROW);
  486. if (res != ERROR_OK) {
  487. LOG_ERROR("Couldn't erase user row");
  488. goto out_user_row;
  489. }
  490. }
  491. /* Modify */
  492. buf_set_u32(buf, startb, endb - startb + 1, value);
  493. /* Write the page buffer back out to the target. A Flash write will be
  494. * triggered automatically. */
  495. res = target_write_memory(target, SAMD_USER_ROW, 4, page_size / 4, buf);
  496. if (res != ERROR_OK)
  497. goto out_user_row;
  498. if (samd_check_error(target)) {
  499. res = ERROR_FAIL;
  500. goto out_user_row;
  501. }
  502. /* Success */
  503. res = ERROR_OK;
  504. out_user_row:
  505. free(buf);
  506. return res;
  507. }
  508. static int samd_protect(struct flash_bank *bank, int set, int first, int last)
  509. {
  510. struct samd_info *chip = (struct samd_info *)bank->driver_priv;
  511. /* We can issue lock/unlock region commands with the target running but
  512. * the settings won't persist unless we're able to modify the LOCK regions
  513. * and that requires the target to be halted. */
  514. if (bank->target->state != TARGET_HALTED) {
  515. LOG_ERROR("Target not halted");
  516. return ERROR_TARGET_NOT_HALTED;
  517. }
  518. int res = ERROR_OK;
  519. for (int s = first; s <= last; s++) {
  520. if (set != bank->sectors[s].is_protected) {
  521. /* Load an address that is within this sector (we use offset 0) */
  522. res = target_write_u32(bank->target,
  523. SAMD_NVMCTRL + SAMD_NVMCTRL_ADDR,
  524. ((s * chip->sector_size) >> 1));
  525. if (res != ERROR_OK)
  526. goto exit;
  527. /* Tell the controller to lock that sector */
  528. res = samd_issue_nvmctrl_command(bank->target,
  529. set ? SAMD_NVM_CMD_LR : SAMD_NVM_CMD_UR);
  530. if (res != ERROR_OK)
  531. goto exit;
  532. }
  533. }
  534. /* We've now applied our changes, however they will be undone by the next
  535. * reset unless we also apply them to the LOCK bits in the User Page. The
  536. * LOCK bits start at bit 48, corresponding to Sector 0 and end with bit 63,
  537. * corresponding to Sector 15. A '1' means unlocked and a '0' means
  538. * locked. See Table 9-3 in the SAMD20 datasheet for more details. */
  539. res = samd_modify_user_row(bank->target, set ? 0x0000 : 0xFFFF,
  540. 48 + first, 48 + last);
  541. if (res != ERROR_OK)
  542. LOG_WARNING("SAMD: protect settings were not made persistent!");
  543. res = ERROR_OK;
  544. exit:
  545. samd_protect_check(bank);
  546. return res;
  547. }
  548. static int samd_erase(struct flash_bank *bank, int first, int last)
  549. {
  550. int res;
  551. int rows_in_sector;
  552. struct samd_info *chip = (struct samd_info *)bank->driver_priv;
  553. if (bank->target->state != TARGET_HALTED) {
  554. LOG_ERROR("Target not halted");
  555. return ERROR_TARGET_NOT_HALTED;
  556. }
  557. if (!chip->probed) {
  558. if (samd_probe(bank) != ERROR_OK)
  559. return ERROR_FLASH_BANK_NOT_PROBED;
  560. }
  561. /* The SAMD NVM has row erase granularity. There are four pages in a row
  562. * and the number of rows in a sector depends on the sector size, which in
  563. * turn depends on the Flash capacity as there is a fixed number of
  564. * sectors. */
  565. rows_in_sector = chip->sector_size / (chip->page_size * 4);
  566. /* For each sector to be erased */
  567. for (int s = first; s <= last; s++) {
  568. if (bank->sectors[s].is_protected) {
  569. LOG_ERROR("SAMD: failed to erase sector %d. That sector is write-protected", s);
  570. return ERROR_FLASH_OPERATION_FAILED;
  571. }
  572. /* For each row in that sector */
  573. for (int r = s * rows_in_sector; r < (s + 1) * rows_in_sector; r++) {
  574. res = samd_erase_row(bank->target, r * chip->page_size * 4);
  575. if (res != ERROR_OK) {
  576. LOG_ERROR("SAMD: failed to erase sector %d", s);
  577. return res;
  578. }
  579. }
  580. }
  581. return ERROR_OK;
  582. }
  583. static int samd_write(struct flash_bank *bank, const uint8_t *buffer,
  584. uint32_t offset, uint32_t count)
  585. {
  586. int res;
  587. uint32_t nvm_ctrlb;
  588. uint32_t address;
  589. uint32_t pg_offset;
  590. uint32_t nb;
  591. uint32_t nw;
  592. struct samd_info *chip = (struct samd_info *)bank->driver_priv;
  593. uint8_t *pb = NULL;
  594. bool manual_wp;
  595. if (bank->target->state != TARGET_HALTED) {
  596. LOG_ERROR("Target not halted");
  597. return ERROR_TARGET_NOT_HALTED;
  598. }
  599. if (!chip->probed) {
  600. if (samd_probe(bank) != ERROR_OK)
  601. return ERROR_FLASH_BANK_NOT_PROBED;
  602. }
  603. /* Check if we need to do manual page write commands */
  604. res = target_read_u32(bank->target, SAMD_NVMCTRL + SAMD_NVMCTRL_CTRLB, &nvm_ctrlb);
  605. if (res != ERROR_OK)
  606. return res;
  607. if (nvm_ctrlb & SAMD_NVM_CTRLB_MANW)
  608. manual_wp = true;
  609. else
  610. manual_wp = false;
  611. res = samd_issue_nvmctrl_command(bank->target, SAMD_NVM_CMD_PBC);
  612. if (res != ERROR_OK) {
  613. LOG_ERROR("%s: %d", __func__, __LINE__);
  614. return res;
  615. }
  616. while (count) {
  617. nb = chip->page_size - offset % chip->page_size;
  618. if (count < nb)
  619. nb = count;
  620. address = bank->base + offset;
  621. pg_offset = offset % chip->page_size;
  622. if (offset % 4 || (offset + nb) % 4) {
  623. /* Either start or end of write is not word aligned */
  624. if (!pb) {
  625. pb = malloc(chip->page_size);
  626. if (!pb)
  627. return ERROR_FAIL;
  628. }
  629. /* Set temporary page buffer to 0xff and overwrite the relevant part */
  630. memset(pb, 0xff, chip->page_size);
  631. memcpy(pb + pg_offset, buffer, nb);
  632. /* Align start address to a word boundary */
  633. address -= offset % 4;
  634. pg_offset -= offset % 4;
  635. assert(pg_offset % 4 == 0);
  636. /* Extend length to whole words */
  637. nw = (nb + offset % 4 + 3) / 4;
  638. assert(pg_offset + 4 * nw <= chip->page_size);
  639. /* Now we have original data extended by 0xff bytes
  640. * to the nearest word boundary on both start and end */
  641. res = target_write_memory(bank->target, address, 4, nw, pb + pg_offset);
  642. } else {
  643. assert(nb % 4 == 0);
  644. nw = nb / 4;
  645. assert(pg_offset + 4 * nw <= chip->page_size);
  646. /* Word aligned data, use direct write from buffer */
  647. res = target_write_memory(bank->target, address, 4, nw, buffer);
  648. }
  649. if (res != ERROR_OK) {
  650. LOG_ERROR("%s: %d", __func__, __LINE__);
  651. goto free_pb;
  652. }
  653. /* Devices with errata 13134 have automatic page write enabled by default
  654. * For other devices issue a write page CMD to the NVM
  655. * If the page has not been written up to the last word
  656. * then issue CMD_WP always */
  657. if (manual_wp || pg_offset + 4 * nw < chip->page_size) {
  658. res = samd_issue_nvmctrl_command(bank->target, SAMD_NVM_CMD_WP);
  659. if (res != ERROR_OK) {
  660. LOG_ERROR("%s: %d", __func__, __LINE__);
  661. goto free_pb;
  662. }
  663. }
  664. /* Access through AHB is stalled while flash is being programmed */
  665. usleep(200);
  666. if (samd_check_error(bank->target)) {
  667. LOG_ERROR("%s: write failed at address 0x%08" PRIx32, __func__, address);
  668. res = ERROR_FAIL;
  669. goto free_pb;
  670. }
  671. /* We're done with the page contents */
  672. count -= nb;
  673. offset += nb;
  674. buffer += nb;
  675. }
  676. free_pb:
  677. if (pb)
  678. free(pb);
  679. return res;
  680. }
  681. FLASH_BANK_COMMAND_HANDLER(samd_flash_bank_command)
  682. {
  683. struct samd_info *chip = samd_chips;
  684. while (chip) {
  685. if (chip->target == bank->target)
  686. break;
  687. chip = chip->next;
  688. }
  689. if (!chip) {
  690. /* Create a new chip */
  691. chip = calloc(1, sizeof(*chip));
  692. if (!chip)
  693. return ERROR_FAIL;
  694. chip->target = bank->target;
  695. chip->probed = false;
  696. bank->driver_priv = chip;
  697. /* Insert it into the chips list (at head) */
  698. chip->next = samd_chips;
  699. samd_chips = chip;
  700. }
  701. if (bank->base != SAMD_FLASH) {
  702. LOG_ERROR("Address 0x%08" PRIx32 " invalid bank address (try 0x%08" PRIx32
  703. "[at91samd series] )",
  704. bank->base, SAMD_FLASH);
  705. return ERROR_FAIL;
  706. }
  707. return ERROR_OK;
  708. }
  709. COMMAND_HANDLER(samd_handle_info_command)
  710. {
  711. return ERROR_OK;
  712. }
  713. COMMAND_HANDLER(samd_handle_chip_erase_command)
  714. {
  715. struct target *target = get_current_target(CMD_CTX);
  716. if (target) {
  717. /* Enable access to the DSU by disabling the write protect bit */
  718. target_write_u32(target, SAMD_PAC1, (1<<1));
  719. /* Tell the DSU to perform a full chip erase. It takes about 240ms to
  720. * perform the erase. */
  721. target_write_u8(target, SAMD_DSU, (1<<4));
  722. command_print(CMD_CTX, "chip erased");
  723. }
  724. return ERROR_OK;
  725. }
  726. COMMAND_HANDLER(samd_handle_set_security_command)
  727. {
  728. int res = ERROR_OK;
  729. struct target *target = get_current_target(CMD_CTX);
  730. if (CMD_ARGC < 1 || (CMD_ARGC >= 1 && (strcmp(CMD_ARGV[0], "enable")))) {
  731. command_print(CMD_CTX, "supply the \"enable\" argument to proceed.");
  732. return ERROR_COMMAND_SYNTAX_ERROR;
  733. }
  734. if (target) {
  735. if (target->state != TARGET_HALTED) {
  736. LOG_ERROR("Target not halted");
  737. return ERROR_TARGET_NOT_HALTED;
  738. }
  739. res = samd_issue_nvmctrl_command(target, SAMD_NVM_CMD_SSB);
  740. /* Check (and clear) error conditions */
  741. if (res == ERROR_OK)
  742. command_print(CMD_CTX, "chip secured on next power-cycle");
  743. else
  744. command_print(CMD_CTX, "failed to secure chip");
  745. }
  746. return res;
  747. }
  748. COMMAND_HANDLER(samd_handle_eeprom_command)
  749. {
  750. int res = ERROR_OK;
  751. struct target *target = get_current_target(CMD_CTX);
  752. if (target) {
  753. if (target->state != TARGET_HALTED) {
  754. LOG_ERROR("Target not halted");
  755. return ERROR_TARGET_NOT_HALTED;
  756. }
  757. if (CMD_ARGC >= 1) {
  758. int val = atoi(CMD_ARGV[0]);
  759. uint32_t code;
  760. if (val == 0)
  761. code = 7;
  762. else {
  763. /* Try to match size in bytes with corresponding size code */
  764. for (code = 0; code <= 6; code++) {
  765. if (val == (2 << (13 - code)))
  766. break;
  767. }
  768. if (code > 6) {
  769. command_print(CMD_CTX, "Invalid EEPROM size. Please see "
  770. "datasheet for a list valid sizes.");
  771. return ERROR_COMMAND_SYNTAX_ERROR;
  772. }
  773. }
  774. res = samd_modify_user_row(target, code, 4, 6);
  775. } else {
  776. uint16_t val;
  777. res = target_read_u16(target, SAMD_USER_ROW, &val);
  778. if (res == ERROR_OK) {
  779. uint32_t size = ((val >> 4) & 0x7); /* grab size code */
  780. if (size == 0x7)
  781. command_print(CMD_CTX, "EEPROM is disabled");
  782. else {
  783. /* Otherwise, 6 is 256B, 0 is 16KB */
  784. command_print(CMD_CTX, "EEPROM size is %u bytes",
  785. (2 << (13 - size)));
  786. }
  787. }
  788. }
  789. }
  790. return res;
  791. }
  792. COMMAND_HANDLER(samd_handle_bootloader_command)
  793. {
  794. int res = ERROR_OK;
  795. struct target *target = get_current_target(CMD_CTX);
  796. if (target) {
  797. if (target->state != TARGET_HALTED) {
  798. LOG_ERROR("Target not halted");
  799. return ERROR_TARGET_NOT_HALTED;
  800. }
  801. /* Retrieve the MCU's page size, in bytes. */
  802. uint32_t page_size;
  803. res = samd_get_flash_page_info(target, &page_size, NULL);
  804. if (res != ERROR_OK) {
  805. LOG_ERROR("Couldn't determine Flash page size");
  806. return res;
  807. }
  808. if (CMD_ARGC >= 1) {
  809. int val = atoi(CMD_ARGV[0]);
  810. uint32_t code;
  811. if (val == 0)
  812. code = 7;
  813. else {
  814. /* Try to match size in bytes with corresponding size code */
  815. for (code = 0; code <= 6; code++) {
  816. if ((unsigned int)val == (2UL << (8UL - code)) * page_size)
  817. break;
  818. }
  819. if (code > 6) {
  820. command_print(CMD_CTX, "Invalid bootloader size. Please "
  821. "see datasheet for a list valid sizes.");
  822. return ERROR_COMMAND_SYNTAX_ERROR;
  823. }
  824. }
  825. res = samd_modify_user_row(target, code, 0, 2);
  826. } else {
  827. uint16_t val;
  828. res = target_read_u16(target, SAMD_USER_ROW, &val);
  829. if (res == ERROR_OK) {
  830. uint32_t size = (val & 0x7); /* grab size code */
  831. uint32_t nb;
  832. if (size == 0x7)
  833. nb = 0;
  834. else
  835. nb = (2 << (8 - size)) * page_size;
  836. /* There are 4 pages per row */
  837. command_print(CMD_CTX, "Bootloader size is %" PRIu32 " bytes (%" PRIu32 " rows)",
  838. nb, (uint32_t)(nb / (page_size * 4)));
  839. }
  840. }
  841. }
  842. return res;
  843. }
  844. COMMAND_HANDLER(samd_handle_reset_deassert)
  845. {
  846. struct target *target = get_current_target(CMD_CTX);
  847. struct armv7m_common *armv7m = target_to_armv7m(target);
  848. int retval = ERROR_OK;
  849. enum reset_types jtag_reset_config = jtag_get_reset_config();
  850. /* In case of sysresetreq, debug retains state set in cortex_m_assert_reset()
  851. * so we just release reset held by DSU
  852. *
  853. * n_RESET (srst) clears the DP, so reenable debug and set vector catch here
  854. *
  855. * After vectreset DSU release is not needed however makes no harm
  856. */
  857. if (target->reset_halt && (jtag_reset_config & RESET_HAS_SRST)) {
  858. retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DHCSR, DBGKEY | C_HALT | C_DEBUGEN);
  859. if (retval == ERROR_OK)
  860. retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DEMCR,
  861. TRCENA | VC_HARDERR | VC_BUSERR | VC_CORERESET);
  862. /* do not return on error here, releasing DSU reset is more important */
  863. }
  864. /* clear CPU Reset Phase Extension bit */
  865. int retval2 = target_write_u8(target, SAMD_DSU + SAMD_DSU_STATUSA, (1<<1));
  866. if (retval2 != ERROR_OK)
  867. return retval2;
  868. return retval;
  869. }
  870. static const struct command_registration at91samd_exec_command_handlers[] = {
  871. {
  872. .name = "dsu_reset_deassert",
  873. .handler = samd_handle_reset_deassert,
  874. .mode = COMMAND_EXEC,
  875. .help = "deasert internal reset held by DSU"
  876. },
  877. {
  878. .name = "info",
  879. .handler = samd_handle_info_command,
  880. .mode = COMMAND_EXEC,
  881. .help = "Print information about the current at91samd chip"
  882. "and its flash configuration.",
  883. },
  884. {
  885. .name = "chip-erase",
  886. .handler = samd_handle_chip_erase_command,
  887. .mode = COMMAND_EXEC,
  888. .help = "Erase the entire Flash by using the Chip"
  889. "Erase feature in the Device Service Unit (DSU).",
  890. },
  891. {
  892. .name = "set-security",
  893. .handler = samd_handle_set_security_command,
  894. .mode = COMMAND_EXEC,
  895. .help = "Secure the chip's Flash by setting the Security Bit."
  896. "This makes it impossible to read the Flash contents."
  897. "The only way to undo this is to issue the chip-erase"
  898. "command.",
  899. },
  900. {
  901. .name = "eeprom",
  902. .usage = "[size_in_bytes]",
  903. .handler = samd_handle_eeprom_command,
  904. .mode = COMMAND_EXEC,
  905. .help = "Show or set the EEPROM size setting, stored in the User Row."
  906. "Please see Table 20-3 of the SAMD20 datasheet for allowed values."
  907. "Changes are stored immediately but take affect after the MCU is"
  908. "reset.",
  909. },
  910. {
  911. .name = "bootloader",
  912. .usage = "[size_in_bytes]",
  913. .handler = samd_handle_bootloader_command,
  914. .mode = COMMAND_EXEC,
  915. .help = "Show or set the bootloader size, stored in the User Row."
  916. "Please see Table 20-2 of the SAMD20 datasheet for allowed values."
  917. "Changes are stored immediately but take affect after the MCU is"
  918. "reset.",
  919. },
  920. COMMAND_REGISTRATION_DONE
  921. };
  922. static const struct command_registration at91samd_command_handlers[] = {
  923. {
  924. .name = "at91samd",
  925. .mode = COMMAND_ANY,
  926. .help = "at91samd flash command group",
  927. .usage = "",
  928. .chain = at91samd_exec_command_handlers,
  929. },
  930. COMMAND_REGISTRATION_DONE
  931. };
  932. struct flash_driver at91samd_flash = {
  933. .name = "at91samd",
  934. .commands = at91samd_command_handlers,
  935. .flash_bank_command = samd_flash_bank_command,
  936. .erase = samd_erase,
  937. .protect = samd_protect,
  938. .write = samd_write,
  939. .read = default_flash_read,
  940. .probe = samd_probe,
  941. .auto_probe = samd_probe,
  942. .erase_check = default_flash_blank_check,
  943. .protect_check = samd_protect_check,
  944. };