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.
 
 
 
 
 
 

223 lines
7.5 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2012 by George Harris *
  3. * george@luminairecoffee.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. .text
  21. .syntax unified
  22. .cpu cortex-m3
  23. .thumb
  24. .thumb_func
  25. /*
  26. * Params :
  27. * r0 = workarea start, status (out)
  28. * r1 = workarea end
  29. * r2 = target address (offset from flash base)
  30. * r3 = count (bytes)
  31. * r4 = page size
  32. * Clobbered:
  33. * r7 - rp
  34. * r8 - wp, tmp
  35. * r9 - send/receive data
  36. * r10 - temp
  37. * r11 - current page end address
  38. */
  39. /*
  40. * This code is embedded within: src/flash/nor/lpcspifi.c as a "C" array.
  41. *
  42. * To rebuild:
  43. * arm-none-eabi-gcc -c lpcspifi_write.S
  44. * arm-none-eabi-objcopy -O binary lpcspifi_write.o lpcspifi_write.bin
  45. * xxd -c 8 -i lpcspifi_write.bin > lpcspifi_write.txt
  46. *
  47. * Then read and edit this result into the "C" source.
  48. */
  49. #define SSP_BASE_HIGH 0x4008
  50. #define SSP_BASE_LOW 0x3000
  51. #define SSP_CR0_OFFSET 0x00
  52. #define SSP_CR1_OFFSET 0x04
  53. #define SSP_DATA_OFFSET 0x08
  54. #define SSP_CPSR_OFFSET 0x10
  55. #define SSP_SR_OFFSET 0x0c
  56. #define SSP_CLOCK_BASE_HIGH 0x4005
  57. #define SSP_CLOCK_BASE_LOW 0x0000
  58. #define SSP_BRANCH_CLOCK_BASE_HIGH 0x4005
  59. #define SSP_BRANCH_CLOCK_BASE_LOW 0x2000
  60. #define SSP_BASE_CLOCK_OFFSET 0x94
  61. #define SSP_BRANCH_CLOCK_OFFSET 0x700
  62. #define IOCONFIG_BASE_HIGH 0x4008
  63. #define IOCONFIG_BASE_LOW 0x6000
  64. #define IOCONFIG_SCK_OFFSET 0x18c
  65. #define IOCONFIG_HOLD_OFFSET 0x190
  66. #define IOCONFIG_WP_OFFSET 0x194
  67. #define IOCONFIG_MISO_OFFSET 0x198
  68. #define IOCONFIG_MOSI_OFFSET 0x19c
  69. #define IOCONFIG_CS_OFFSET 0x1a0
  70. #define IO_BASE_HIGH 0x400f
  71. #define IO_BASE_LOW 0x4000
  72. #define IO_CS_OFFSET 0xab
  73. #define IODIR_BASE_HIGH 0x400f
  74. #define IODIR_BASE_LOW 0x6000
  75. #define IO_CS_DIR_OFFSET 0x14
  76. setup: /* Initialize SSP pins and module */
  77. mov.w r10, #IOCONFIG_BASE_LOW
  78. movt r10, #IOCONFIG_BASE_HIGH
  79. mov.w r8, #0xea
  80. str.w r8, [r10, #IOCONFIG_SCK_OFFSET] /* Configure SCK pin function */
  81. mov.w r8, #0x40
  82. str.w r8, [r10, #IOCONFIG_HOLD_OFFSET] /* Configure /HOLD pin function */
  83. mov.w r8, #0x40
  84. str.w r8, [r10, #IOCONFIG_WP_OFFSET] /* Configure /WP pin function */
  85. mov.w r8, #0xed
  86. str.w r8, [r10, #IOCONFIG_MISO_OFFSET] /* Configure MISO pin function */
  87. mov.w r8, #0xed
  88. str.w r8, [r10, #IOCONFIG_MOSI_OFFSET] /* Configure MOSI pin function */
  89. mov.w r8, #0x44
  90. str.w r8, [r10, #IOCONFIG_CS_OFFSET] /* Configure CS pin function */
  91. mov.w r10, #IODIR_BASE_LOW
  92. movt r10, #IODIR_BASE_HIGH
  93. mov.w r8, #0x800
  94. str r8, [r10, #IO_CS_DIR_OFFSET] /* Set CS as output */
  95. mov.w r10, #IO_BASE_LOW
  96. movt r10, #IO_BASE_HIGH
  97. mov.w r8, #0xff
  98. str.w r8, [r10, #IO_CS_OFFSET] /* Set CS high */
  99. mov.w r10, #SSP_CLOCK_BASE_LOW
  100. movt r10, #SSP_CLOCK_BASE_HIGH
  101. mov.w r8, #0x0000
  102. movt r8, #0x0100
  103. str.w r8, [r10, #SSP_BASE_CLOCK_OFFSET] /* Configure SSP0 base clock (use 12 MHz IRC) */
  104. mov.w r10, #SSP_BRANCH_CLOCK_BASE_LOW
  105. movt r10, #SSP_BRANCH_CLOCK_BASE_HIGH
  106. mov.w r8, #0x01
  107. str.w r8, [r10, #SSP_BRANCH_CLOCK_OFFSET] /* Configure (enable) SSP0 branch clock */
  108. mov.w r10, #SSP_BASE_LOW
  109. movt r10, #SSP_BASE_HIGH
  110. mov.w r8, #0x07
  111. str.w r8, [r10, #SSP_CR0_OFFSET] /* Set clock postscale */
  112. mov.w r8, #0x02
  113. str.w r8, [r10, #SSP_CPSR_OFFSET] /* Set clock prescale */
  114. str.w r8, [r10, #SSP_CR1_OFFSET] /* Enable SSP in SPI mode */
  115. mov.w r11, #0x00
  116. find_next_page_boundary:
  117. add r11, r4 /* Increment to the next page */
  118. cmp r11, r2
  119. /* If we have not reached the next page boundary after the target address, keep going */
  120. bls find_next_page_boundary
  121. write_enable:
  122. bl cs_down
  123. mov.w r9, #0x06 /* Send the write enable command */
  124. bl write_data
  125. bl cs_up
  126. bl cs_down
  127. mov.w r9, #0x05 /* Get status register */
  128. bl write_data
  129. mov.w r9, #0x00 /* Dummy data to clock in status */
  130. bl write_data
  131. bl cs_up
  132. tst r9, #0x02 /* If the WE bit isn't set, we have a problem. */
  133. beq error
  134. page_program:
  135. bl cs_down
  136. mov.w r9, #0x02 /* Send the page program command */
  137. bl write_data
  138. write_address:
  139. lsr r9, r2, #16 /* Send the current 24-bit write address, MSB first */
  140. bl write_data
  141. lsr r9, r2, #8
  142. bl write_data
  143. mov.w r9, r2
  144. bl write_data
  145. wait_fifo:
  146. ldr r8, [r0] /* read the write pointer */
  147. cmp r8, #0 /* if it's zero, we're gonzo */
  148. beq exit
  149. ldr r7, [r0, #4] /* read the read pointer */
  150. cmp r7, r8 /* wait until they are not equal */
  151. beq wait_fifo
  152. write:
  153. ldrb r9, [r7], #0x01 /* Load one byte from the FIFO, increment the read pointer by 1 */
  154. bl write_data /* send the byte to the flash chip */
  155. cmp r7, r1 /* wrap the read pointer if it is at the end */
  156. it cs
  157. addcs r7, r0, #8 /* skip loader args */
  158. str r7, [r0, #4] /* store the new read pointer */
  159. subs r3, r3, #1 /* decrement count */
  160. cbz r3, exit /* Exit if we have written everything */
  161. add r2, #1 /* Increment flash address by 1 */
  162. cmp r11, r2 /* See if we have reached the end of a page */
  163. bne wait_fifo /* If not, keep writing bytes */
  164. bl cs_up /* Otherwise, end the command and keep going w/ the next page */
  165. add r11, r4 /* Move up the end-of-page address by the page size*/
  166. wait_flash_busy: /* Wait for the flash to finish the previous page write */
  167. bl cs_down
  168. mov.w r9, #0x05 /* Get status register */
  169. bl write_data
  170. mov.w r9, #0x00 /* Dummy data to clock in status */
  171. bl write_data
  172. bl cs_up
  173. tst r9, #0x01 /* If it isn't done, keep waiting */
  174. bne wait_flash_busy
  175. b write_enable /* If it is done, start a new page write */
  176. write_data: /* Send/receive 1 byte of data over SSP */
  177. mov.w r10, #SSP_BASE_LOW
  178. movt r10, #SSP_BASE_HIGH
  179. str.w r9, [r10, #SSP_DATA_OFFSET] /* Write supplied data to the SSP data reg */
  180. wait_transmit:
  181. ldr r9, [r10, #SSP_SR_OFFSET] /* Check SSP status */
  182. tst r9, #0x0010 /* Check if BSY bit is set */
  183. bne wait_transmit /* If still transmitting, keep waiting */
  184. ldr r9, [r10, #SSP_DATA_OFFSET] /* Load received data */
  185. bx lr /* Exit subroutine */
  186. cs_up:
  187. mov.w r8, #0xff
  188. b cs_write
  189. cs_down:
  190. mov.w r8, #0x0000
  191. cs_write:
  192. mov.w r10, #IO_BASE_LOW
  193. movt r10, #IO_BASE_HIGH
  194. str.w r8, [r10, #IO_CS_OFFSET]
  195. bx lr
  196. error:
  197. movs r0, #0
  198. str r0, [r2, #4] /* set rp = 0 on error */
  199. exit:
  200. bl cs_up /* end the command before returning */
  201. mov r0, r6
  202. bkpt #0x00
  203. .end