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.

133 lines
3.6KB

  1. /***************************************************************************
  2. * Copyright (C) 2010 by Spencer Oliver *
  3. * spen@spen-soft.co.uk *
  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. .text
  21. .arch m4k
  22. .set noreorder
  23. .set noat
  24. /* params:
  25. * $a0 src adr - ram + result
  26. * $a1 dest adr - flash
  27. * $a2 count (32bit words)
  28. * vars
  29. *
  30. * temps:
  31. * $t0, $t1, $t2, $t3, $t4, $t5
  32. * $s0, $s1, $s3, $s4, $s5
  33. */
  34. .type main, @function
  35. .global main
  36. .ent main
  37. main:
  38. /* setup constants */
  39. lui $t0, 0xaa99
  40. ori $t0, 0x6655 /* NVMKEY1 */
  41. lui $t1, 0x5566
  42. ori $t1, 0x99AA /* NVMKEY2 */
  43. lui $t2, 0xBF80
  44. ori $t2, 0xF400 /* NVMCON */
  45. ori $t3, $zero, 0x4003 /* NVMCON row write cmd */
  46. ori $t4, $zero, 0x8000 /* NVMCON start cmd */
  47. write_row:
  48. /* can we perform a row write: 128 32bit words */
  49. sltiu $s3, $a2, 128
  50. bne $s3, $zero, write_word
  51. ori $t5, $zero, 0x4000 /* NVMCON clear cmd */
  52. /* perform row write 512 bytes */
  53. sw $a1, 32($t2) /* set NVMADDR with dest addr - real addr */
  54. sw $a0, 64($t2) /* set NVMSRCADDR with src addr - real addr */
  55. bal progflash
  56. addiu $a0, $a0, 512
  57. addiu $a1, $a1, 512
  58. beq $zero, $zero, write_row
  59. addiu $a2, $a2, -128
  60. write_word:
  61. /* write 32bit words */
  62. lui $s5, 0xa000
  63. ori $s5, 0x0000
  64. or $a0, $a0, $s5 /* convert to virtual addr */
  65. beq $zero, $zero, next_word
  66. ori $t3, $zero, 0x4001 /* NVMCON word write cmd */
  67. prog_word:
  68. lw $s4, 0($a0) /* load data - from virtual addr */
  69. sw $s4, 48($t2) /* set NVMDATA with data */
  70. sw $a1, 32($t2) /* set NVMADDR with dest addr - real addr */
  71. bal progflash
  72. addiu $a0, $a0, 4
  73. addiu $a1, $a1, 4
  74. addiu $a2, $a2, -1
  75. next_word:
  76. bne $a2, $zero, prog_word
  77. nop
  78. done:
  79. beq $zero, $zero, exit
  80. addiu $a0, $zero, 0
  81. error:
  82. /* save result to $a0 */
  83. addiu $a0, $s1, 0
  84. exit:
  85. sdbbp
  86. .end main
  87. .type progflash, @function
  88. .global progflash
  89. .ent progflash
  90. progflash:
  91. sw $t3, 0($t2) /* set NVMWREN */
  92. sw $t0, 16($t2) /* write NVMKEY1 */
  93. sw $t1, 16($t2) /* write NVMKEY2 */
  94. sw $t4, 8($t2) /* start operation */
  95. waitflash:
  96. lw $s0, 0($t2)
  97. and $s0, $s0, $t4
  98. bne $s0, $zero, waitflash
  99. nop
  100. /* following is to comply with errata #34
  101. * 500ns delay required */
  102. nop
  103. nop
  104. nop
  105. nop
  106. /* check for errors */
  107. lw $s1, 0($t2)
  108. andi $s1, $zero, 0x3000
  109. bne $s1, $zero, error
  110. sw $t5, 4($t2) /* clear NVMWREN */
  111. jr $ra
  112. nop
  113. .end progflash