st_spi_fsm.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177
  1. /*
  2. * st_spi_fsm.c - ST Fast Sequence Mode (FSM) Serial Flash Controller
  3. *
  4. * Author: Angus Clark <angus.clark@st.com>
  5. *
  6. * Copyright (C) 2010-2014 STMicroelectronics Limited
  7. *
  8. * JEDEC probe based on drivers/mtd/devices/m25p80.c
  9. *
  10. * This code is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/regmap.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/mtd/mtd.h>
  21. #include <linux/mtd/partitions.h>
  22. #include <linux/mtd/spi-nor.h>
  23. #include <linux/sched.h>
  24. #include <linux/delay.h>
  25. #include <linux/io.h>
  26. #include <linux/of.h>
  27. #include <linux/clk.h>
  28. #include "serial_flash_cmds.h"
  29. /*
  30. * FSM SPI Controller Registers
  31. */
  32. #define SPI_CLOCKDIV 0x0010
  33. #define SPI_MODESELECT 0x0018
  34. #define SPI_CONFIGDATA 0x0020
  35. #define SPI_STA_MODE_CHANGE 0x0028
  36. #define SPI_FAST_SEQ_TRANSFER_SIZE 0x0100
  37. #define SPI_FAST_SEQ_ADD1 0x0104
  38. #define SPI_FAST_SEQ_ADD2 0x0108
  39. #define SPI_FAST_SEQ_ADD_CFG 0x010c
  40. #define SPI_FAST_SEQ_OPC1 0x0110
  41. #define SPI_FAST_SEQ_OPC2 0x0114
  42. #define SPI_FAST_SEQ_OPC3 0x0118
  43. #define SPI_FAST_SEQ_OPC4 0x011c
  44. #define SPI_FAST_SEQ_OPC5 0x0120
  45. #define SPI_MODE_BITS 0x0124
  46. #define SPI_DUMMY_BITS 0x0128
  47. #define SPI_FAST_SEQ_FLASH_STA_DATA 0x012c
  48. #define SPI_FAST_SEQ_1 0x0130
  49. #define SPI_FAST_SEQ_2 0x0134
  50. #define SPI_FAST_SEQ_3 0x0138
  51. #define SPI_FAST_SEQ_4 0x013c
  52. #define SPI_FAST_SEQ_CFG 0x0140
  53. #define SPI_FAST_SEQ_STA 0x0144
  54. #define SPI_QUAD_BOOT_SEQ_INIT_1 0x0148
  55. #define SPI_QUAD_BOOT_SEQ_INIT_2 0x014c
  56. #define SPI_QUAD_BOOT_READ_SEQ_1 0x0150
  57. #define SPI_QUAD_BOOT_READ_SEQ_2 0x0154
  58. #define SPI_PROGRAM_ERASE_TIME 0x0158
  59. #define SPI_MULT_PAGE_REPEAT_SEQ_1 0x015c
  60. #define SPI_MULT_PAGE_REPEAT_SEQ_2 0x0160
  61. #define SPI_STATUS_WR_TIME_REG 0x0164
  62. #define SPI_FAST_SEQ_DATA_REG 0x0300
  63. /*
  64. * Register: SPI_MODESELECT
  65. */
  66. #define SPI_MODESELECT_CONTIG 0x01
  67. #define SPI_MODESELECT_FASTREAD 0x02
  68. #define SPI_MODESELECT_DUALIO 0x04
  69. #define SPI_MODESELECT_FSM 0x08
  70. #define SPI_MODESELECT_QUADBOOT 0x10
  71. /*
  72. * Register: SPI_CONFIGDATA
  73. */
  74. #define SPI_CFG_DEVICE_ST 0x1
  75. #define SPI_CFG_DEVICE_ATMEL 0x4
  76. #define SPI_CFG_MIN_CS_HIGH(x) (((x) & 0xfff) << 4)
  77. #define SPI_CFG_CS_SETUPHOLD(x) (((x) & 0xff) << 16)
  78. #define SPI_CFG_DATA_HOLD(x) (((x) & 0xff) << 24)
  79. #define SPI_CFG_DEFAULT_MIN_CS_HIGH SPI_CFG_MIN_CS_HIGH(0x0AA)
  80. #define SPI_CFG_DEFAULT_CS_SETUPHOLD SPI_CFG_CS_SETUPHOLD(0xA0)
  81. #define SPI_CFG_DEFAULT_DATA_HOLD SPI_CFG_DATA_HOLD(0x00)
  82. /*
  83. * Register: SPI_FAST_SEQ_TRANSFER_SIZE
  84. */
  85. #define TRANSFER_SIZE(x) ((x) * 8)
  86. /*
  87. * Register: SPI_FAST_SEQ_ADD_CFG
  88. */
  89. #define ADR_CFG_CYCLES_ADD1(x) ((x) << 0)
  90. #define ADR_CFG_PADS_1_ADD1 (0x0 << 6)
  91. #define ADR_CFG_PADS_2_ADD1 (0x1 << 6)
  92. #define ADR_CFG_PADS_4_ADD1 (0x3 << 6)
  93. #define ADR_CFG_CSDEASSERT_ADD1 (1 << 8)
  94. #define ADR_CFG_CYCLES_ADD2(x) ((x) << (0+16))
  95. #define ADR_CFG_PADS_1_ADD2 (0x0 << (6+16))
  96. #define ADR_CFG_PADS_2_ADD2 (0x1 << (6+16))
  97. #define ADR_CFG_PADS_4_ADD2 (0x3 << (6+16))
  98. #define ADR_CFG_CSDEASSERT_ADD2 (1 << (8+16))
  99. /*
  100. * Register: SPI_FAST_SEQ_n
  101. */
  102. #define SEQ_OPC_OPCODE(x) ((x) << 0)
  103. #define SEQ_OPC_CYCLES(x) ((x) << 8)
  104. #define SEQ_OPC_PADS_1 (0x0 << 14)
  105. #define SEQ_OPC_PADS_2 (0x1 << 14)
  106. #define SEQ_OPC_PADS_4 (0x3 << 14)
  107. #define SEQ_OPC_CSDEASSERT (1 << 16)
  108. /*
  109. * Register: SPI_FAST_SEQ_CFG
  110. */
  111. #define SEQ_CFG_STARTSEQ (1 << 0)
  112. #define SEQ_CFG_SWRESET (1 << 5)
  113. #define SEQ_CFG_CSDEASSERT (1 << 6)
  114. #define SEQ_CFG_READNOTWRITE (1 << 7)
  115. #define SEQ_CFG_ERASE (1 << 8)
  116. #define SEQ_CFG_PADS_1 (0x0 << 16)
  117. #define SEQ_CFG_PADS_2 (0x1 << 16)
  118. #define SEQ_CFG_PADS_4 (0x3 << 16)
  119. /*
  120. * Register: SPI_MODE_BITS
  121. */
  122. #define MODE_DATA(x) (x & 0xff)
  123. #define MODE_CYCLES(x) ((x & 0x3f) << 16)
  124. #define MODE_PADS_1 (0x0 << 22)
  125. #define MODE_PADS_2 (0x1 << 22)
  126. #define MODE_PADS_4 (0x3 << 22)
  127. #define DUMMY_CSDEASSERT (1 << 24)
  128. /*
  129. * Register: SPI_DUMMY_BITS
  130. */
  131. #define DUMMY_CYCLES(x) ((x & 0x3f) << 16)
  132. #define DUMMY_PADS_1 (0x0 << 22)
  133. #define DUMMY_PADS_2 (0x1 << 22)
  134. #define DUMMY_PADS_4 (0x3 << 22)
  135. #define DUMMY_CSDEASSERT (1 << 24)
  136. /*
  137. * Register: SPI_FAST_SEQ_FLASH_STA_DATA
  138. */
  139. #define STA_DATA_BYTE1(x) ((x & 0xff) << 0)
  140. #define STA_DATA_BYTE2(x) ((x & 0xff) << 8)
  141. #define STA_PADS_1 (0x0 << 16)
  142. #define STA_PADS_2 (0x1 << 16)
  143. #define STA_PADS_4 (0x3 << 16)
  144. #define STA_CSDEASSERT (0x1 << 20)
  145. #define STA_RDNOTWR (0x1 << 21)
  146. /*
  147. * FSM SPI Instruction Opcodes
  148. */
  149. #define STFSM_OPC_CMD 0x1
  150. #define STFSM_OPC_ADD 0x2
  151. #define STFSM_OPC_STA 0x3
  152. #define STFSM_OPC_MODE 0x4
  153. #define STFSM_OPC_DUMMY 0x5
  154. #define STFSM_OPC_DATA 0x6
  155. #define STFSM_OPC_WAIT 0x7
  156. #define STFSM_OPC_JUMP 0x8
  157. #define STFSM_OPC_GOTO 0x9
  158. #define STFSM_OPC_STOP 0xF
  159. /*
  160. * FSM SPI Instructions (== opcode + operand).
  161. */
  162. #define STFSM_INSTR(cmd, op) ((cmd) | ((op) << 4))
  163. #define STFSM_INST_CMD1 STFSM_INSTR(STFSM_OPC_CMD, 1)
  164. #define STFSM_INST_CMD2 STFSM_INSTR(STFSM_OPC_CMD, 2)
  165. #define STFSM_INST_CMD3 STFSM_INSTR(STFSM_OPC_CMD, 3)
  166. #define STFSM_INST_CMD4 STFSM_INSTR(STFSM_OPC_CMD, 4)
  167. #define STFSM_INST_CMD5 STFSM_INSTR(STFSM_OPC_CMD, 5)
  168. #define STFSM_INST_ADD1 STFSM_INSTR(STFSM_OPC_ADD, 1)
  169. #define STFSM_INST_ADD2 STFSM_INSTR(STFSM_OPC_ADD, 2)
  170. #define STFSM_INST_DATA_WRITE STFSM_INSTR(STFSM_OPC_DATA, 1)
  171. #define STFSM_INST_DATA_READ STFSM_INSTR(STFSM_OPC_DATA, 2)
  172. #define STFSM_INST_STA_RD1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
  173. #define STFSM_INST_STA_WR1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
  174. #define STFSM_INST_STA_RD2 STFSM_INSTR(STFSM_OPC_STA, 0x2)
  175. #define STFSM_INST_STA_WR1_2 STFSM_INSTR(STFSM_OPC_STA, 0x3)
  176. #define STFSM_INST_MODE STFSM_INSTR(STFSM_OPC_MODE, 0)
  177. #define STFSM_INST_DUMMY STFSM_INSTR(STFSM_OPC_DUMMY, 0)
  178. #define STFSM_INST_WAIT STFSM_INSTR(STFSM_OPC_WAIT, 0)
  179. #define STFSM_INST_STOP STFSM_INSTR(STFSM_OPC_STOP, 0)
  180. #define STFSM_DEFAULT_EMI_FREQ 100000000UL /* 100 MHz */
  181. #define STFSM_DEFAULT_WR_TIME (STFSM_DEFAULT_EMI_FREQ * (15/1000)) /* 15ms */
  182. #define STFSM_FLASH_SAFE_FREQ 10000000UL /* 10 MHz */
  183. #define STFSM_MAX_WAIT_SEQ_MS 1000 /* FSM execution time */
  184. /* S25FLxxxS commands */
  185. #define S25FL_CMD_WRITE4_1_1_4 0x34
  186. #define S25FL_CMD_SE4 0xdc
  187. #define S25FL_CMD_CLSR 0x30
  188. #define S25FL_CMD_DYBWR 0xe1
  189. #define S25FL_CMD_DYBRD 0xe0
  190. #define S25FL_CMD_WRITE4 0x12 /* Note, opcode clashes with
  191. * 'SPINOR_OP_WRITE_1_4_4'
  192. * as found on N25Qxxx devices! */
  193. /* Status register */
  194. #define FLASH_STATUS_BUSY 0x01
  195. #define FLASH_STATUS_WEL 0x02
  196. #define FLASH_STATUS_BP0 0x04
  197. #define FLASH_STATUS_BP1 0x08
  198. #define FLASH_STATUS_BP2 0x10
  199. #define FLASH_STATUS_SRWP0 0x80
  200. #define FLASH_STATUS_TIMEOUT 0xff
  201. /* S25FL Error Flags */
  202. #define S25FL_STATUS_E_ERR 0x20
  203. #define S25FL_STATUS_P_ERR 0x40
  204. #define N25Q_CMD_WRVCR 0x81
  205. #define N25Q_CMD_RDVCR 0x85
  206. #define N25Q_CMD_RDVECR 0x65
  207. #define N25Q_CMD_RDNVCR 0xb5
  208. #define N25Q_CMD_WRNVCR 0xb1
  209. #define FLASH_PAGESIZE 256 /* In Bytes */
  210. #define FLASH_PAGESIZE_32 (FLASH_PAGESIZE / 4) /* In uint32_t */
  211. #define FLASH_MAX_BUSY_WAIT (300 * HZ) /* Maximum 'CHIPERASE' time */
  212. /*
  213. * Flags to tweak operation of default read/write/erase routines
  214. */
  215. #define CFG_READ_TOGGLE_32BIT_ADDR 0x00000001
  216. #define CFG_WRITE_TOGGLE_32BIT_ADDR 0x00000002
  217. #define CFG_ERASESEC_TOGGLE_32BIT_ADDR 0x00000008
  218. #define CFG_S25FL_CHECK_ERROR_FLAGS 0x00000010
  219. struct stfsm_seq {
  220. uint32_t data_size;
  221. uint32_t addr1;
  222. uint32_t addr2;
  223. uint32_t addr_cfg;
  224. uint32_t seq_opc[5];
  225. uint32_t mode;
  226. uint32_t dummy;
  227. uint32_t status;
  228. uint8_t seq[16];
  229. uint32_t seq_cfg;
  230. } __packed __aligned(4);
  231. struct stfsm {
  232. struct device *dev;
  233. void __iomem *base;
  234. struct resource *region;
  235. struct mtd_info mtd;
  236. struct mutex lock;
  237. struct flash_info *info;
  238. struct clk *clk;
  239. uint32_t configuration;
  240. uint32_t fifo_dir_delay;
  241. bool booted_from_spi;
  242. bool reset_signal;
  243. bool reset_por;
  244. struct stfsm_seq stfsm_seq_read;
  245. struct stfsm_seq stfsm_seq_write;
  246. struct stfsm_seq stfsm_seq_en_32bit_addr;
  247. };
  248. /* Parameters to configure a READ or WRITE FSM sequence */
  249. struct seq_rw_config {
  250. uint32_t flags; /* flags to support config */
  251. uint8_t cmd; /* FLASH command */
  252. int write; /* Write Sequence */
  253. uint8_t addr_pads; /* No. of addr pads (MODE & DUMMY) */
  254. uint8_t data_pads; /* No. of data pads */
  255. uint8_t mode_data; /* MODE data */
  256. uint8_t mode_cycles; /* No. of MODE cycles */
  257. uint8_t dummy_cycles; /* No. of DUMMY cycles */
  258. };
  259. /* SPI Flash Device Table */
  260. struct flash_info {
  261. char *name;
  262. /*
  263. * JEDEC id zero means "no ID" (most older chips); otherwise it has
  264. * a high byte of zero plus three data bytes: the manufacturer id,
  265. * then a two byte device id.
  266. */
  267. u32 jedec_id;
  268. u16 ext_id;
  269. /*
  270. * The size listed here is what works with SPINOR_OP_SE, which isn't
  271. * necessarily called a "sector" by the vendor.
  272. */
  273. unsigned sector_size;
  274. u16 n_sectors;
  275. u32 flags;
  276. /*
  277. * Note, where FAST_READ is supported, freq_max specifies the
  278. * FAST_READ frequency, not the READ frequency.
  279. */
  280. u32 max_freq;
  281. int (*config)(struct stfsm *);
  282. };
  283. static int stfsm_n25q_config(struct stfsm *fsm);
  284. static int stfsm_mx25_config(struct stfsm *fsm);
  285. static int stfsm_s25fl_config(struct stfsm *fsm);
  286. static int stfsm_w25q_config(struct stfsm *fsm);
  287. static struct flash_info flash_types[] = {
  288. /*
  289. * ST Microelectronics/Numonyx --
  290. * (newer production versions may have feature updates
  291. * (eg faster operating frequency)
  292. */
  293. #define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
  294. { "m25p40", 0x202013, 0, 64 * 1024, 8, M25P_FLAG, 25, NULL },
  295. { "m25p80", 0x202014, 0, 64 * 1024, 16, M25P_FLAG, 25, NULL },
  296. { "m25p16", 0x202015, 0, 64 * 1024, 32, M25P_FLAG, 25, NULL },
  297. { "m25p32", 0x202016, 0, 64 * 1024, 64, M25P_FLAG, 50, NULL },
  298. { "m25p64", 0x202017, 0, 64 * 1024, 128, M25P_FLAG, 50, NULL },
  299. { "m25p128", 0x202018, 0, 256 * 1024, 64, M25P_FLAG, 50, NULL },
  300. #define M25PX_FLAG (FLASH_FLAG_READ_WRITE | \
  301. FLASH_FLAG_READ_FAST | \
  302. FLASH_FLAG_READ_1_1_2 | \
  303. FLASH_FLAG_WRITE_1_1_2)
  304. { "m25px32", 0x207116, 0, 64 * 1024, 64, M25PX_FLAG, 75, NULL },
  305. { "m25px64", 0x207117, 0, 64 * 1024, 128, M25PX_FLAG, 75, NULL },
  306. /* Macronix MX25xxx
  307. * - Support for 'FLASH_FLAG_WRITE_1_4_4' is omitted for devices
  308. * where operating frequency must be reduced.
  309. */
  310. #define MX25_FLAG (FLASH_FLAG_READ_WRITE | \
  311. FLASH_FLAG_READ_FAST | \
  312. FLASH_FLAG_READ_1_1_2 | \
  313. FLASH_FLAG_READ_1_2_2 | \
  314. FLASH_FLAG_READ_1_1_4 | \
  315. FLASH_FLAG_SE_4K | \
  316. FLASH_FLAG_SE_32K)
  317. { "mx25l3255e", 0xc29e16, 0, 64 * 1024, 64,
  318. (MX25_FLAG | FLASH_FLAG_WRITE_1_4_4), 86,
  319. stfsm_mx25_config},
  320. { "mx25l25635e", 0xc22019, 0, 64*1024, 512,
  321. (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
  322. stfsm_mx25_config },
  323. { "mx25l25655e", 0xc22619, 0, 64*1024, 512,
  324. (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
  325. stfsm_mx25_config},
  326. #define N25Q_FLAG (FLASH_FLAG_READ_WRITE | \
  327. FLASH_FLAG_READ_FAST | \
  328. FLASH_FLAG_READ_1_1_2 | \
  329. FLASH_FLAG_READ_1_2_2 | \
  330. FLASH_FLAG_READ_1_1_4 | \
  331. FLASH_FLAG_READ_1_4_4 | \
  332. FLASH_FLAG_WRITE_1_1_2 | \
  333. FLASH_FLAG_WRITE_1_2_2 | \
  334. FLASH_FLAG_WRITE_1_1_4 | \
  335. FLASH_FLAG_WRITE_1_4_4)
  336. { "n25q128", 0x20ba18, 0, 64 * 1024, 256, N25Q_FLAG, 108,
  337. stfsm_n25q_config },
  338. { "n25q256", 0x20ba19, 0, 64 * 1024, 512,
  339. N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, stfsm_n25q_config },
  340. /*
  341. * Spansion S25FLxxxP
  342. * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
  343. */
  344. #define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE | \
  345. FLASH_FLAG_READ_1_1_2 | \
  346. FLASH_FLAG_READ_1_2_2 | \
  347. FLASH_FLAG_READ_1_1_4 | \
  348. FLASH_FLAG_READ_1_4_4 | \
  349. FLASH_FLAG_WRITE_1_1_4 | \
  350. FLASH_FLAG_READ_FAST)
  351. { "s25fl032p", 0x010215, 0x4d00, 64 * 1024, 64, S25FLXXXP_FLAG, 80,
  352. stfsm_s25fl_config},
  353. { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024, 64, S25FLXXXP_FLAG, 80,
  354. stfsm_s25fl_config },
  355. { "s25fl129p1", 0x012018, 0x4d01, 64 * 1024, 256, S25FLXXXP_FLAG, 80,
  356. stfsm_s25fl_config },
  357. /*
  358. * Spansion S25FLxxxS
  359. * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
  360. * - RESET# signal supported by die but not bristled out on all
  361. * package types. The package type is a function of board design,
  362. * so this information is captured in the board's flags.
  363. * - Supports 'DYB' sector protection. Depending on variant, sectors
  364. * may default to locked state on power-on.
  365. */
  366. #define S25FLXXXS_FLAG (S25FLXXXP_FLAG | \
  367. FLASH_FLAG_RESET | \
  368. FLASH_FLAG_DYB_LOCKING)
  369. { "s25fl128s0", 0x012018, 0x0300, 256 * 1024, 64, S25FLXXXS_FLAG, 80,
  370. stfsm_s25fl_config },
  371. { "s25fl128s1", 0x012018, 0x0301, 64 * 1024, 256, S25FLXXXS_FLAG, 80,
  372. stfsm_s25fl_config },
  373. { "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
  374. S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
  375. { "s25fl256s1", 0x010219, 0x4d01, 64 * 1024, 512,
  376. S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
  377. /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
  378. #define W25X_FLAG (FLASH_FLAG_READ_WRITE | \
  379. FLASH_FLAG_READ_FAST | \
  380. FLASH_FLAG_READ_1_1_2 | \
  381. FLASH_FLAG_WRITE_1_1_2)
  382. { "w25x40", 0xef3013, 0, 64 * 1024, 8, W25X_FLAG, 75, NULL },
  383. { "w25x80", 0xef3014, 0, 64 * 1024, 16, W25X_FLAG, 75, NULL },
  384. { "w25x16", 0xef3015, 0, 64 * 1024, 32, W25X_FLAG, 75, NULL },
  385. { "w25x32", 0xef3016, 0, 64 * 1024, 64, W25X_FLAG, 75, NULL },
  386. { "w25x64", 0xef3017, 0, 64 * 1024, 128, W25X_FLAG, 75, NULL },
  387. /* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
  388. #define W25Q_FLAG (FLASH_FLAG_READ_WRITE | \
  389. FLASH_FLAG_READ_FAST | \
  390. FLASH_FLAG_READ_1_1_2 | \
  391. FLASH_FLAG_READ_1_2_2 | \
  392. FLASH_FLAG_READ_1_1_4 | \
  393. FLASH_FLAG_READ_1_4_4 | \
  394. FLASH_FLAG_WRITE_1_1_4)
  395. { "w25q80", 0xef4014, 0, 64 * 1024, 16, W25Q_FLAG, 80,
  396. stfsm_w25q_config },
  397. { "w25q16", 0xef4015, 0, 64 * 1024, 32, W25Q_FLAG, 80,
  398. stfsm_w25q_config },
  399. { "w25q32", 0xef4016, 0, 64 * 1024, 64, W25Q_FLAG, 80,
  400. stfsm_w25q_config },
  401. { "w25q64", 0xef4017, 0, 64 * 1024, 128, W25Q_FLAG, 80,
  402. stfsm_w25q_config },
  403. /* Sentinel */
  404. { NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
  405. };
  406. /*
  407. * FSM message sequence configurations:
  408. *
  409. * All configs are presented in order of preference
  410. */
  411. /* Default READ configurations, in order of preference */
  412. static struct seq_rw_config default_read_configs[] = {
  413. {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4, 0, 4, 4, 0x00, 2, 4},
  414. {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4, 0, 1, 4, 0x00, 4, 0},
  415. {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2, 0, 2, 2, 0x00, 4, 0},
  416. {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
  417. {FLASH_FLAG_READ_FAST, SPINOR_OP_READ_FAST, 0, 1, 1, 0x00, 0, 8},
  418. {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ, 0, 1, 1, 0x00, 0, 0},
  419. {0x00, 0, 0, 0, 0, 0x00, 0, 0},
  420. };
  421. /* Default WRITE configurations */
  422. static struct seq_rw_config default_write_configs[] = {
  423. {FLASH_FLAG_WRITE_1_4_4, SPINOR_OP_WRITE_1_4_4, 1, 4, 4, 0x00, 0, 0},
  424. {FLASH_FLAG_WRITE_1_1_4, SPINOR_OP_WRITE_1_1_4, 1, 1, 4, 0x00, 0, 0},
  425. {FLASH_FLAG_WRITE_1_2_2, SPINOR_OP_WRITE_1_2_2, 1, 2, 2, 0x00, 0, 0},
  426. {FLASH_FLAG_WRITE_1_1_2, SPINOR_OP_WRITE_1_1_2, 1, 1, 2, 0x00, 0, 0},
  427. {FLASH_FLAG_READ_WRITE, SPINOR_OP_WRITE, 1, 1, 1, 0x00, 0, 0},
  428. {0x00, 0, 0, 0, 0, 0x00, 0, 0},
  429. };
  430. /*
  431. * [N25Qxxx] Configuration
  432. */
  433. #define N25Q_VCR_DUMMY_CYCLES(x) (((x) & 0xf) << 4)
  434. #define N25Q_VCR_XIP_DISABLED ((uint8_t)0x1 << 3)
  435. #define N25Q_VCR_WRAP_CONT 0x3
  436. /* N25Q 3-byte Address READ configurations
  437. * - 'FAST' variants configured for 8 dummy cycles.
  438. *
  439. * Note, the number of dummy cycles used for 'FAST' READ operations is
  440. * configurable and would normally be tuned according to the READ command and
  441. * operating frequency. However, this applies universally to all 'FAST' READ
  442. * commands, including those used by the SPIBoot controller, and remains in
  443. * force until the device is power-cycled. Since the SPIBoot controller is
  444. * hard-wired to use 8 dummy cycles, we must configure the device to also use 8
  445. * cycles.
  446. */
  447. static struct seq_rw_config n25q_read3_configs[] = {
  448. {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4, 0, 4, 4, 0x00, 0, 8},
  449. {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4, 0, 1, 4, 0x00, 0, 8},
  450. {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2, 0, 2, 2, 0x00, 0, 8},
  451. {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
  452. {FLASH_FLAG_READ_FAST, SPINOR_OP_READ_FAST, 0, 1, 1, 0x00, 0, 8},
  453. {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ, 0, 1, 1, 0x00, 0, 0},
  454. {0x00, 0, 0, 0, 0, 0x00, 0, 0},
  455. };
  456. /* N25Q 4-byte Address READ configurations
  457. * - use special 4-byte address READ commands (reduces overheads, and
  458. * reduces risk of hitting watchdog reset issues).
  459. * - 'FAST' variants configured for 8 dummy cycles (see note above.)
  460. */
  461. static struct seq_rw_config n25q_read4_configs[] = {
  462. {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B, 0, 4, 4, 0x00, 0, 8},
  463. {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B, 0, 1, 4, 0x00, 0, 8},
  464. {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B, 0, 2, 2, 0x00, 0, 8},
  465. {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B, 0, 1, 2, 0x00, 0, 8},
  466. {FLASH_FLAG_READ_FAST, SPINOR_OP_READ_FAST_4B, 0, 1, 1, 0x00, 0, 8},
  467. {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ_4B, 0, 1, 1, 0x00, 0, 0},
  468. {0x00, 0, 0, 0, 0, 0x00, 0, 0},
  469. };
  470. /*
  471. * [MX25xxx] Configuration
  472. */
  473. #define MX25_STATUS_QE (0x1 << 6)
  474. static int stfsm_mx25_en_32bit_addr_seq(struct stfsm_seq *seq)
  475. {
  476. seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
  477. SEQ_OPC_CYCLES(8) |
  478. SEQ_OPC_OPCODE(SPINOR_OP_EN4B) |
  479. SEQ_OPC_CSDEASSERT);
  480. seq->seq[0] = STFSM_INST_CMD1;
  481. seq->seq[1] = STFSM_INST_WAIT;
  482. seq->seq[2] = STFSM_INST_STOP;
  483. seq->seq_cfg = (SEQ_CFG_PADS_1 |
  484. SEQ_CFG_ERASE |
  485. SEQ_CFG_READNOTWRITE |
  486. SEQ_CFG_CSDEASSERT |
  487. SEQ_CFG_STARTSEQ);
  488. return 0;
  489. }
  490. /*
  491. * [S25FLxxx] Configuration
  492. */
  493. #define STFSM_S25FL_CONFIG_QE (0x1 << 1)
  494. /*
  495. * S25FLxxxS devices provide three ways of supporting 32-bit addressing: Bank
  496. * Register, Extended Address Modes, and a 32-bit address command set. The
  497. * 32-bit address command set is used here, since it avoids any problems with
  498. * entering a state that is incompatible with the SPIBoot Controller.
  499. */
  500. static struct seq_rw_config stfsm_s25fl_read4_configs[] = {
  501. {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B, 0, 4, 4, 0x00, 2, 4},
  502. {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B, 0, 1, 4, 0x00, 0, 8},
  503. {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B, 0, 2, 2, 0x00, 4, 0},
  504. {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B, 0, 1, 2, 0x00, 0, 8},
  505. {FLASH_FLAG_READ_FAST, SPINOR_OP_READ_FAST_4B, 0, 1, 1, 0x00, 0, 8},
  506. {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ_4B, 0, 1, 1, 0x00, 0, 0},
  507. {0x00, 0, 0, 0, 0, 0x00, 0, 0},
  508. };
  509. static struct seq_rw_config stfsm_s25fl_write4_configs[] = {
  510. {FLASH_FLAG_WRITE_1_1_4, S25FL_CMD_WRITE4_1_1_4, 1, 1, 4, 0x00, 0, 0},
  511. {FLASH_FLAG_READ_WRITE, S25FL_CMD_WRITE4, 1, 1, 1, 0x00, 0, 0},
  512. {0x00, 0, 0, 0, 0, 0x00, 0, 0},
  513. };
  514. /*
  515. * [W25Qxxx] Configuration
  516. */
  517. #define W25Q_STATUS_QE (0x1 << 1)
  518. static struct stfsm_seq stfsm_seq_read_jedec = {
  519. .data_size = TRANSFER_SIZE(8),
  520. .seq_opc[0] = (SEQ_OPC_PADS_1 |
  521. SEQ_OPC_CYCLES(8) |
  522. SEQ_OPC_OPCODE(SPINOR_OP_RDID)),
  523. .seq = {
  524. STFSM_INST_CMD1,
  525. STFSM_INST_DATA_READ,
  526. STFSM_INST_STOP,
  527. },
  528. .seq_cfg = (SEQ_CFG_PADS_1 |
  529. SEQ_CFG_READNOTWRITE |
  530. SEQ_CFG_CSDEASSERT |
  531. SEQ_CFG_STARTSEQ),
  532. };
  533. static struct stfsm_seq stfsm_seq_read_status_fifo = {
  534. .data_size = TRANSFER_SIZE(4),
  535. .seq_opc[0] = (SEQ_OPC_PADS_1 |
  536. SEQ_OPC_CYCLES(8) |
  537. SEQ_OPC_OPCODE(SPINOR_OP_RDSR)),
  538. .seq = {
  539. STFSM_INST_CMD1,
  540. STFSM_INST_DATA_READ,
  541. STFSM_INST_STOP,
  542. },
  543. .seq_cfg = (SEQ_CFG_PADS_1 |
  544. SEQ_CFG_READNOTWRITE |
  545. SEQ_CFG_CSDEASSERT |
  546. SEQ_CFG_STARTSEQ),
  547. };
  548. static struct stfsm_seq stfsm_seq_erase_sector = {
  549. /* 'addr_cfg' configured during initialisation */
  550. .seq_opc = {
  551. (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  552. SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
  553. (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  554. SEQ_OPC_OPCODE(SPINOR_OP_SE)),
  555. },
  556. .seq = {
  557. STFSM_INST_CMD1,
  558. STFSM_INST_CMD2,
  559. STFSM_INST_ADD1,
  560. STFSM_INST_ADD2,
  561. STFSM_INST_STOP,
  562. },
  563. .seq_cfg = (SEQ_CFG_PADS_1 |
  564. SEQ_CFG_READNOTWRITE |
  565. SEQ_CFG_CSDEASSERT |
  566. SEQ_CFG_STARTSEQ),
  567. };
  568. static struct stfsm_seq stfsm_seq_erase_chip = {
  569. .seq_opc = {
  570. (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  571. SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
  572. (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  573. SEQ_OPC_OPCODE(SPINOR_OP_CHIP_ERASE) | SEQ_OPC_CSDEASSERT),
  574. },
  575. .seq = {
  576. STFSM_INST_CMD1,
  577. STFSM_INST_CMD2,
  578. STFSM_INST_WAIT,
  579. STFSM_INST_STOP,
  580. },
  581. .seq_cfg = (SEQ_CFG_PADS_1 |
  582. SEQ_CFG_ERASE |
  583. SEQ_CFG_READNOTWRITE |
  584. SEQ_CFG_CSDEASSERT |
  585. SEQ_CFG_STARTSEQ),
  586. };
  587. static struct stfsm_seq stfsm_seq_write_status = {
  588. .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  589. SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
  590. .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  591. SEQ_OPC_OPCODE(SPINOR_OP_WRSR)),
  592. .seq = {
  593. STFSM_INST_CMD1,
  594. STFSM_INST_CMD2,
  595. STFSM_INST_STA_WR1,
  596. STFSM_INST_STOP,
  597. },
  598. .seq_cfg = (SEQ_CFG_PADS_1 |
  599. SEQ_CFG_READNOTWRITE |
  600. SEQ_CFG_CSDEASSERT |
  601. SEQ_CFG_STARTSEQ),
  602. };
  603. /* Dummy sequence to read one byte of data from flash into the FIFO */
  604. static const struct stfsm_seq stfsm_seq_load_fifo_byte = {
  605. .data_size = TRANSFER_SIZE(1),
  606. .seq_opc[0] = (SEQ_OPC_PADS_1 |
  607. SEQ_OPC_CYCLES(8) |
  608. SEQ_OPC_OPCODE(SPINOR_OP_RDID)),
  609. .seq = {
  610. STFSM_INST_CMD1,
  611. STFSM_INST_DATA_READ,
  612. STFSM_INST_STOP,
  613. },
  614. .seq_cfg = (SEQ_CFG_PADS_1 |
  615. SEQ_CFG_READNOTWRITE |
  616. SEQ_CFG_CSDEASSERT |
  617. SEQ_CFG_STARTSEQ),
  618. };
  619. static int stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq *seq)
  620. {
  621. seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  622. SEQ_OPC_OPCODE(SPINOR_OP_EN4B));
  623. seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  624. SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
  625. SEQ_OPC_CSDEASSERT);
  626. seq->seq[0] = STFSM_INST_CMD2;
  627. seq->seq[1] = STFSM_INST_CMD1;
  628. seq->seq[2] = STFSM_INST_WAIT;
  629. seq->seq[3] = STFSM_INST_STOP;
  630. seq->seq_cfg = (SEQ_CFG_PADS_1 |
  631. SEQ_CFG_ERASE |
  632. SEQ_CFG_READNOTWRITE |
  633. SEQ_CFG_CSDEASSERT |
  634. SEQ_CFG_STARTSEQ);
  635. return 0;
  636. }
  637. static inline int stfsm_is_idle(struct stfsm *fsm)
  638. {
  639. return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
  640. }
  641. static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
  642. {
  643. return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
  644. }
  645. static inline void stfsm_load_seq(struct stfsm *fsm,
  646. const struct stfsm_seq *seq)
  647. {
  648. void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
  649. const uint32_t *src = (const uint32_t *)seq;
  650. int words = sizeof(*seq) / sizeof(*src);
  651. BUG_ON(!stfsm_is_idle(fsm));
  652. while (words--) {
  653. writel(*src, dst);
  654. src++;
  655. dst += 4;
  656. }
  657. }
  658. static void stfsm_wait_seq(struct stfsm *fsm)
  659. {
  660. unsigned long deadline;
  661. int timeout = 0;
  662. deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
  663. while (!timeout) {
  664. if (time_after_eq(jiffies, deadline))
  665. timeout = 1;
  666. if (stfsm_is_idle(fsm))
  667. return;
  668. cond_resched();
  669. }
  670. dev_err(fsm->dev, "timeout on sequence completion\n");
  671. }
  672. static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf, uint32_t size)
  673. {
  674. uint32_t remaining = size >> 2;
  675. uint32_t avail;
  676. uint32_t words;
  677. dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
  678. BUG_ON((((uintptr_t)buf) & 0x3) || (size & 0x3));
  679. while (remaining) {
  680. for (;;) {
  681. avail = stfsm_fifo_available(fsm);
  682. if (avail)
  683. break;
  684. udelay(1);
  685. }
  686. words = min(avail, remaining);
  687. remaining -= words;
  688. readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
  689. buf += words;
  690. }
  691. }
  692. /*
  693. * Clear the data FIFO
  694. *
  695. * Typically, this is only required during driver initialisation, where no
  696. * assumptions can be made regarding the state of the FIFO.
  697. *
  698. * The process of clearing the FIFO is complicated by fact that while it is
  699. * possible for the FIFO to contain an arbitrary number of bytes [1], the
  700. * SPI_FAST_SEQ_STA register only reports the number of complete 32-bit words
  701. * present. Furthermore, data can only be drained from the FIFO by reading
  702. * complete 32-bit words.
  703. *
  704. * With this in mind, a two stage process is used to the clear the FIFO:
  705. *
  706. * 1. Read any complete 32-bit words from the FIFO, as reported by the
  707. * SPI_FAST_SEQ_STA register.
  708. *
  709. * 2. Mop up any remaining bytes. At this point, it is not known if there
  710. * are 0, 1, 2, or 3 bytes in the FIFO. To handle all cases, a dummy FSM
  711. * sequence is used to load one byte at a time, until a complete 32-bit
  712. * word is formed; at most, 4 bytes will need to be loaded.
  713. *
  714. * [1] It is theoretically possible for the FIFO to contain an arbitrary number
  715. * of bits. However, since there are no known use-cases that leave
  716. * incomplete bytes in the FIFO, only words and bytes are considered here.
  717. */
  718. static void stfsm_clear_fifo(struct stfsm *fsm)
  719. {
  720. const struct stfsm_seq *seq = &stfsm_seq_load_fifo_byte;
  721. uint32_t words, i;
  722. /* 1. Clear any 32-bit words */
  723. words = stfsm_fifo_available(fsm);
  724. if (words) {
  725. for (i = 0; i < words; i++)
  726. readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
  727. dev_dbg(fsm->dev, "cleared %d words from FIFO\n", words);
  728. }
  729. /*
  730. * 2. Clear any remaining bytes
  731. * - Load the FIFO, one byte at a time, until a complete 32-bit word
  732. * is available.
  733. */
  734. for (i = 0, words = 0; i < 4 && !words; i++) {
  735. stfsm_load_seq(fsm, seq);
  736. stfsm_wait_seq(fsm);
  737. words = stfsm_fifo_available(fsm);
  738. }
  739. /* - A single word must be available now */
  740. if (words != 1) {
  741. dev_err(fsm->dev, "failed to clear bytes from the data FIFO\n");
  742. return;
  743. }
  744. /* - Read the 32-bit word */
  745. readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
  746. dev_dbg(fsm->dev, "cleared %d byte(s) from the data FIFO\n", 4 - i);
  747. }
  748. static int stfsm_write_fifo(struct stfsm *fsm, const uint32_t *buf,
  749. uint32_t size)
  750. {
  751. uint32_t words = size >> 2;
  752. dev_dbg(fsm->dev, "writing %d bytes to FIFO\n", size);
  753. BUG_ON((((uintptr_t)buf) & 0x3) || (size & 0x3));
  754. writesl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
  755. return size;
  756. }
  757. static int stfsm_enter_32bit_addr(struct stfsm *fsm, int enter)
  758. {
  759. struct stfsm_seq *seq = &fsm->stfsm_seq_en_32bit_addr;
  760. uint32_t cmd = enter ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
  761. seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
  762. SEQ_OPC_CYCLES(8) |
  763. SEQ_OPC_OPCODE(cmd) |
  764. SEQ_OPC_CSDEASSERT);
  765. stfsm_load_seq(fsm, seq);
  766. stfsm_wait_seq(fsm);
  767. return 0;
  768. }
  769. static uint8_t stfsm_wait_busy(struct stfsm *fsm)
  770. {
  771. struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
  772. unsigned long deadline;
  773. uint32_t status;
  774. int timeout = 0;
  775. /* Use RDRS1 */
  776. seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
  777. SEQ_OPC_CYCLES(8) |
  778. SEQ_OPC_OPCODE(SPINOR_OP_RDSR));
  779. /* Load read_status sequence */
  780. stfsm_load_seq(fsm, seq);
  781. /*
  782. * Repeat until busy bit is deasserted, or timeout, or error (S25FLxxxS)
  783. */
  784. deadline = jiffies + FLASH_MAX_BUSY_WAIT;
  785. while (!timeout) {
  786. if (time_after_eq(jiffies, deadline))
  787. timeout = 1;
  788. stfsm_wait_seq(fsm);
  789. stfsm_read_fifo(fsm, &status, 4);
  790. if ((status & FLASH_STATUS_BUSY) == 0)
  791. return 0;
  792. if ((fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS) &&
  793. ((status & S25FL_STATUS_P_ERR) ||
  794. (status & S25FL_STATUS_E_ERR)))
  795. return (uint8_t)(status & 0xff);
  796. if (!timeout)
  797. /* Restart */
  798. writel(seq->seq_cfg, fsm->base + SPI_FAST_SEQ_CFG);
  799. cond_resched();
  800. }
  801. dev_err(fsm->dev, "timeout on wait_busy\n");
  802. return FLASH_STATUS_TIMEOUT;
  803. }
  804. static int stfsm_read_status(struct stfsm *fsm, uint8_t cmd,
  805. uint8_t *data, int bytes)
  806. {
  807. struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
  808. uint32_t tmp;
  809. uint8_t *t = (uint8_t *)&tmp;
  810. int i;
  811. dev_dbg(fsm->dev, "read 'status' register [0x%02x], %d byte(s)\n",
  812. cmd, bytes);
  813. BUG_ON(bytes != 1 && bytes != 2);
  814. seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  815. SEQ_OPC_OPCODE(cmd)),
  816. stfsm_load_seq(fsm, seq);
  817. stfsm_read_fifo(fsm, &tmp, 4);
  818. for (i = 0; i < bytes; i++)
  819. data[i] = t[i];
  820. stfsm_wait_seq(fsm);
  821. return 0;
  822. }
  823. static int stfsm_write_status(struct stfsm *fsm, uint8_t cmd,
  824. uint16_t data, int bytes, int wait_busy)
  825. {
  826. struct stfsm_seq *seq = &stfsm_seq_write_status;
  827. dev_dbg(fsm->dev,
  828. "write 'status' register [0x%02x], %d byte(s), 0x%04x\n"
  829. " %s wait-busy\n", cmd, bytes, data, wait_busy ? "with" : "no");
  830. BUG_ON(bytes != 1 && bytes != 2);
  831. seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  832. SEQ_OPC_OPCODE(cmd));
  833. seq->status = (uint32_t)data | STA_PADS_1 | STA_CSDEASSERT;
  834. seq->seq[2] = (bytes == 1) ? STFSM_INST_STA_WR1 : STFSM_INST_STA_WR1_2;
  835. stfsm_load_seq(fsm, seq);
  836. stfsm_wait_seq(fsm);
  837. if (wait_busy)
  838. stfsm_wait_busy(fsm);
  839. return 0;
  840. }
  841. /*
  842. * SoC reset on 'boot-from-spi' systems
  843. *
  844. * Certain modes of operation cause the Flash device to enter a particular state
  845. * for a period of time (e.g. 'Erase Sector', 'Quad Enable', and 'Enter 32-bit
  846. * Addr' commands). On boot-from-spi systems, it is important to consider what
  847. * happens if a warm reset occurs during this period. The SPIBoot controller
  848. * assumes that Flash device is in its default reset state, 24-bit address mode,
  849. * and ready to accept commands. This can be achieved using some form of
  850. * on-board logic/controller to force a device POR in response to a SoC-level
  851. * reset or by making use of the device reset signal if available (limited
  852. * number of devices only).
  853. *
  854. * Failure to take such precautions can cause problems following a warm reset.
  855. * For some operations (e.g. ERASE), there is little that can be done. For
  856. * other modes of operation (e.g. 32-bit addressing), options are often
  857. * available that can help minimise the window in which a reset could cause a
  858. * problem.
  859. *
  860. */
  861. static bool stfsm_can_handle_soc_reset(struct stfsm *fsm)
  862. {
  863. /* Reset signal is available on the board and supported by the device */
  864. if (fsm->reset_signal && fsm->info->flags & FLASH_FLAG_RESET)
  865. return true;
  866. /* Board-level logic forces a power-on-reset */
  867. if (fsm->reset_por)
  868. return true;
  869. /* Reset is not properly handled and may result in failure to reboot */
  870. return false;
  871. }
  872. /* Configure 'addr_cfg' according to addressing mode */
  873. static void stfsm_prepare_erasesec_seq(struct stfsm *fsm,
  874. struct stfsm_seq *seq)
  875. {
  876. int addr1_cycles = fsm->info->flags & FLASH_FLAG_32BIT_ADDR ? 16 : 8;
  877. seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(addr1_cycles) |
  878. ADR_CFG_PADS_1_ADD1 |
  879. ADR_CFG_CYCLES_ADD2(16) |
  880. ADR_CFG_PADS_1_ADD2 |
  881. ADR_CFG_CSDEASSERT_ADD2);
  882. }
  883. /* Search for preferred configuration based on available flags */
  884. static struct seq_rw_config *
  885. stfsm_search_seq_rw_configs(struct stfsm *fsm,
  886. struct seq_rw_config cfgs[])
  887. {
  888. struct seq_rw_config *config;
  889. int flags = fsm->info->flags;
  890. for (config = cfgs; config->cmd != 0; config++)
  891. if ((config->flags & flags) == config->flags)
  892. return config;
  893. return NULL;
  894. }
  895. /* Prepare a READ/WRITE sequence according to configuration parameters */
  896. static void stfsm_prepare_rw_seq(struct stfsm *fsm,
  897. struct stfsm_seq *seq,
  898. struct seq_rw_config *cfg)
  899. {
  900. int addr1_cycles, addr2_cycles;
  901. int i = 0;
  902. memset(seq, 0, sizeof(*seq));
  903. /* Add READ/WRITE OPC */
  904. seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
  905. SEQ_OPC_CYCLES(8) |
  906. SEQ_OPC_OPCODE(cfg->cmd));
  907. /* Add WREN OPC for a WRITE sequence */
  908. if (cfg->write)
  909. seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
  910. SEQ_OPC_CYCLES(8) |
  911. SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
  912. SEQ_OPC_CSDEASSERT);
  913. /* Address configuration (24 or 32-bit addresses) */
  914. addr1_cycles = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
  915. addr1_cycles /= cfg->addr_pads;
  916. addr2_cycles = 16 / cfg->addr_pads;
  917. seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 | /* ADD1 cycles */
  918. (cfg->addr_pads - 1) << 6 | /* ADD1 pads */
  919. (addr2_cycles & 0x3f) << 16 | /* ADD2 cycles */
  920. ((cfg->addr_pads - 1) << 22)); /* ADD2 pads */
  921. /* Data/Sequence configuration */
  922. seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
  923. SEQ_CFG_STARTSEQ |
  924. SEQ_CFG_CSDEASSERT);
  925. if (!cfg->write)
  926. seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
  927. /* Mode configuration (no. of pads taken from addr cfg) */
  928. seq->mode = ((cfg->mode_data & 0xff) << 0 | /* data */
  929. (cfg->mode_cycles & 0x3f) << 16 | /* cycles */
  930. (cfg->addr_pads - 1) << 22); /* pads */
  931. /* Dummy configuration (no. of pads taken from addr cfg) */
  932. seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 | /* cycles */
  933. (cfg->addr_pads - 1) << 22); /* pads */
  934. /* Instruction sequence */
  935. i = 0;
  936. if (cfg->write)
  937. seq->seq[i++] = STFSM_INST_CMD2;
  938. seq->seq[i++] = STFSM_INST_CMD1;
  939. seq->seq[i++] = STFSM_INST_ADD1;
  940. seq->seq[i++] = STFSM_INST_ADD2;
  941. if (cfg->mode_cycles)
  942. seq->seq[i++] = STFSM_INST_MODE;
  943. if (cfg->dummy_cycles)
  944. seq->seq[i++] = STFSM_INST_DUMMY;
  945. seq->seq[i++] =
  946. cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
  947. seq->seq[i++] = STFSM_INST_STOP;
  948. }
  949. static int stfsm_search_prepare_rw_seq(struct stfsm *fsm,
  950. struct stfsm_seq *seq,
  951. struct seq_rw_config *cfgs)
  952. {
  953. struct seq_rw_config *config;
  954. config = stfsm_search_seq_rw_configs(fsm, cfgs);
  955. if (!config) {
  956. dev_err(fsm->dev, "failed to find suitable config\n");
  957. return -EINVAL;
  958. }
  959. stfsm_prepare_rw_seq(fsm, seq, config);
  960. return 0;
  961. }
  962. /* Prepare a READ/WRITE/ERASE 'default' sequences */
  963. static int stfsm_prepare_rwe_seqs_default(struct stfsm *fsm)
  964. {
  965. uint32_t flags = fsm->info->flags;
  966. int ret;
  967. /* Configure 'READ' sequence */
  968. ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
  969. default_read_configs);
  970. if (ret) {
  971. dev_err(fsm->dev,
  972. "failed to prep READ sequence with flags [0x%08x]\n",
  973. flags);
  974. return ret;
  975. }
  976. /* Configure 'WRITE' sequence */
  977. ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
  978. default_write_configs);
  979. if (ret) {
  980. dev_err(fsm->dev,
  981. "failed to prep WRITE sequence with flags [0x%08x]\n",
  982. flags);
  983. return ret;
  984. }
  985. /* Configure 'ERASE_SECTOR' sequence */
  986. stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
  987. return 0;
  988. }
  989. static int stfsm_mx25_config(struct stfsm *fsm)
  990. {
  991. uint32_t flags = fsm->info->flags;
  992. uint32_t data_pads;
  993. uint8_t sta;
  994. int ret;
  995. bool soc_reset;
  996. /*
  997. * Use default READ/WRITE sequences
  998. */
  999. ret = stfsm_prepare_rwe_seqs_default(fsm);
  1000. if (ret)
  1001. return ret;
  1002. /*
  1003. * Configure 32-bit Address Support
  1004. */
  1005. if (flags & FLASH_FLAG_32BIT_ADDR) {
  1006. /* Configure 'enter_32bitaddr' FSM sequence */
  1007. stfsm_mx25_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
  1008. soc_reset = stfsm_can_handle_soc_reset(fsm);
  1009. if (soc_reset || !fsm->booted_from_spi)
  1010. /* If we can handle SoC resets, we enable 32-bit address
  1011. * mode pervasively */
  1012. stfsm_enter_32bit_addr(fsm, 1);
  1013. else
  1014. /* Else, enable/disable 32-bit addressing before/after
  1015. * each operation */
  1016. fsm->configuration = (CFG_READ_TOGGLE_32BIT_ADDR |
  1017. CFG_WRITE_TOGGLE_32BIT_ADDR |
  1018. CFG_ERASESEC_TOGGLE_32BIT_ADDR);
  1019. }
  1020. /* Check status of 'QE' bit, update if required. */
  1021. stfsm_read_status(fsm, SPINOR_OP_RDSR, &sta, 1);
  1022. data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
  1023. if (data_pads == 4) {
  1024. if (!(sta & MX25_STATUS_QE)) {
  1025. /* Set 'QE' */
  1026. sta |= MX25_STATUS_QE;
  1027. stfsm_write_status(fsm, SPINOR_OP_WRSR, sta, 1, 1);
  1028. }
  1029. } else {
  1030. if (sta & MX25_STATUS_QE) {
  1031. /* Clear 'QE' */
  1032. sta &= ~MX25_STATUS_QE;
  1033. stfsm_write_status(fsm, SPINOR_OP_WRSR, sta, 1, 1);
  1034. }
  1035. }
  1036. return 0;
  1037. }
  1038. static int stfsm_n25q_config(struct stfsm *fsm)
  1039. {
  1040. uint32_t flags = fsm->info->flags;
  1041. uint8_t vcr;
  1042. int ret = 0;
  1043. bool soc_reset;
  1044. /* Configure 'READ' sequence */
  1045. if (flags & FLASH_FLAG_32BIT_ADDR)
  1046. ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
  1047. n25q_read4_configs);
  1048. else
  1049. ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
  1050. n25q_read3_configs);
  1051. if (ret) {
  1052. dev_err(fsm->dev,
  1053. "failed to prepare READ sequence with flags [0x%08x]\n",
  1054. flags);
  1055. return ret;
  1056. }
  1057. /* Configure 'WRITE' sequence (default configs) */
  1058. ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
  1059. default_write_configs);
  1060. if (ret) {
  1061. dev_err(fsm->dev,
  1062. "preparing WRITE sequence using flags [0x%08x] failed\n",
  1063. flags);
  1064. return ret;
  1065. }
  1066. /* * Configure 'ERASE_SECTOR' sequence */
  1067. stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
  1068. /* Configure 32-bit address support */
  1069. if (flags & FLASH_FLAG_32BIT_ADDR) {
  1070. stfsm_n25q_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
  1071. soc_reset = stfsm_can_handle_soc_reset(fsm);
  1072. if (soc_reset || !fsm->booted_from_spi) {
  1073. /*
  1074. * If we can handle SoC resets, we enable 32-bit
  1075. * address mode pervasively
  1076. */
  1077. stfsm_enter_32bit_addr(fsm, 1);
  1078. } else {
  1079. /*
  1080. * If not, enable/disable for WRITE and ERASE
  1081. * operations (READ uses special commands)
  1082. */
  1083. fsm->configuration = (CFG_WRITE_TOGGLE_32BIT_ADDR |
  1084. CFG_ERASESEC_TOGGLE_32BIT_ADDR);
  1085. }
  1086. }
  1087. /*
  1088. * Configure device to use 8 dummy cycles
  1089. */
  1090. vcr = (N25Q_VCR_DUMMY_CYCLES(8) | N25Q_VCR_XIP_DISABLED |
  1091. N25Q_VCR_WRAP_CONT);
  1092. stfsm_write_status(fsm, N25Q_CMD_WRVCR, vcr, 1, 0);
  1093. return 0;
  1094. }
  1095. static void stfsm_s25fl_prepare_erasesec_seq_32(struct stfsm_seq *seq)
  1096. {
  1097. seq->seq_opc[1] = (SEQ_OPC_PADS_1 |
  1098. SEQ_OPC_CYCLES(8) |
  1099. SEQ_OPC_OPCODE(S25FL_CMD_SE4));
  1100. seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
  1101. ADR_CFG_PADS_1_ADD1 |
  1102. ADR_CFG_CYCLES_ADD2(16) |
  1103. ADR_CFG_PADS_1_ADD2 |
  1104. ADR_CFG_CSDEASSERT_ADD2);
  1105. }
  1106. static void stfsm_s25fl_read_dyb(struct stfsm *fsm, uint32_t offs, uint8_t *dby)
  1107. {
  1108. uint32_t tmp;
  1109. struct stfsm_seq seq = {
  1110. .data_size = TRANSFER_SIZE(4),
  1111. .seq_opc[0] = (SEQ_OPC_PADS_1 |
  1112. SEQ_OPC_CYCLES(8) |
  1113. SEQ_OPC_OPCODE(S25FL_CMD_DYBRD)),
  1114. .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
  1115. ADR_CFG_PADS_1_ADD1 |
  1116. ADR_CFG_CYCLES_ADD2(16) |
  1117. ADR_CFG_PADS_1_ADD2),
  1118. .addr1 = (offs >> 16) & 0xffff,
  1119. .addr2 = offs & 0xffff,
  1120. .seq = {
  1121. STFSM_INST_CMD1,
  1122. STFSM_INST_ADD1,
  1123. STFSM_INST_ADD2,
  1124. STFSM_INST_DATA_READ,
  1125. STFSM_INST_STOP,
  1126. },
  1127. .seq_cfg = (SEQ_CFG_PADS_1 |
  1128. SEQ_CFG_READNOTWRITE |
  1129. SEQ_CFG_CSDEASSERT |
  1130. SEQ_CFG_STARTSEQ),
  1131. };
  1132. stfsm_load_seq(fsm, &seq);
  1133. stfsm_read_fifo(fsm, &tmp, 4);
  1134. *dby = (uint8_t)(tmp >> 24);
  1135. stfsm_wait_seq(fsm);
  1136. }
  1137. static void stfsm_s25fl_write_dyb(struct stfsm *fsm, uint32_t offs, uint8_t dby)
  1138. {
  1139. struct stfsm_seq seq = {
  1140. .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  1141. SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
  1142. SEQ_OPC_CSDEASSERT),
  1143. .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  1144. SEQ_OPC_OPCODE(S25FL_CMD_DYBWR)),
  1145. .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
  1146. ADR_CFG_PADS_1_ADD1 |
  1147. ADR_CFG_CYCLES_ADD2(16) |
  1148. ADR_CFG_PADS_1_ADD2),
  1149. .status = (uint32_t)dby | STA_PADS_1 | STA_CSDEASSERT,
  1150. .addr1 = (offs >> 16) & 0xffff,
  1151. .addr2 = offs & 0xffff,
  1152. .seq = {
  1153. STFSM_INST_CMD1,
  1154. STFSM_INST_CMD2,
  1155. STFSM_INST_ADD1,
  1156. STFSM_INST_ADD2,
  1157. STFSM_INST_STA_WR1,
  1158. STFSM_INST_STOP,
  1159. },
  1160. .seq_cfg = (SEQ_CFG_PADS_1 |
  1161. SEQ_CFG_READNOTWRITE |
  1162. SEQ_CFG_CSDEASSERT |
  1163. SEQ_CFG_STARTSEQ),
  1164. };
  1165. stfsm_load_seq(fsm, &seq);
  1166. stfsm_wait_seq(fsm);
  1167. stfsm_wait_busy(fsm);
  1168. }
  1169. static int stfsm_s25fl_clear_status_reg(struct stfsm *fsm)
  1170. {
  1171. struct stfsm_seq seq = {
  1172. .seq_opc[0] = (SEQ_OPC_PADS_1 |
  1173. SEQ_OPC_CYCLES(8) |
  1174. SEQ_OPC_OPCODE(S25FL_CMD_CLSR) |
  1175. SEQ_OPC_CSDEASSERT),
  1176. .seq_opc[1] = (SEQ_OPC_PADS_1 |
  1177. SEQ_OPC_CYCLES(8) |
  1178. SEQ_OPC_OPCODE(SPINOR_OP_WRDI) |
  1179. SEQ_OPC_CSDEASSERT),
  1180. .seq = {
  1181. STFSM_INST_CMD1,
  1182. STFSM_INST_CMD2,
  1183. STFSM_INST_WAIT,
  1184. STFSM_INST_STOP,
  1185. },
  1186. .seq_cfg = (SEQ_CFG_PADS_1 |
  1187. SEQ_CFG_ERASE |
  1188. SEQ_CFG_READNOTWRITE |
  1189. SEQ_CFG_CSDEASSERT |
  1190. SEQ_CFG_STARTSEQ),
  1191. };
  1192. stfsm_load_seq(fsm, &seq);
  1193. stfsm_wait_seq(fsm);
  1194. return 0;
  1195. }
  1196. static int stfsm_s25fl_config(struct stfsm *fsm)
  1197. {
  1198. struct flash_info *info = fsm->info;
  1199. uint32_t flags = info->flags;
  1200. uint32_t data_pads;
  1201. uint32_t offs;
  1202. uint16_t sta_wr;
  1203. uint8_t sr1, cr1, dyb;
  1204. int update_sr = 0;
  1205. int ret;
  1206. if (flags & FLASH_FLAG_32BIT_ADDR) {
  1207. /*
  1208. * Prepare Read/Write/Erase sequences according to S25FLxxx
  1209. * 32-bit address command set
  1210. */
  1211. ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
  1212. stfsm_s25fl_read4_configs);
  1213. if (ret)
  1214. return ret;
  1215. ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
  1216. stfsm_s25fl_write4_configs);
  1217. if (ret)
  1218. return ret;
  1219. stfsm_s25fl_prepare_erasesec_seq_32(&stfsm_seq_erase_sector);
  1220. } else {
  1221. /* Use default configurations for 24-bit addressing */
  1222. ret = stfsm_prepare_rwe_seqs_default(fsm);
  1223. if (ret)
  1224. return ret;
  1225. }
  1226. /*
  1227. * For devices that support 'DYB' sector locking, check lock status and
  1228. * unlock sectors if necessary (some variants power-on with sectors
  1229. * locked by default)
  1230. */
  1231. if (flags & FLASH_FLAG_DYB_LOCKING) {
  1232. offs = 0;
  1233. for (offs = 0; offs < info->sector_size * info->n_sectors;) {
  1234. stfsm_s25fl_read_dyb(fsm, offs, &dyb);
  1235. if (dyb == 0x00)
  1236. stfsm_s25fl_write_dyb(fsm, offs, 0xff);
  1237. /* Handle bottom/top 4KiB parameter sectors */
  1238. if ((offs < info->sector_size * 2) ||
  1239. (offs >= (info->sector_size - info->n_sectors * 4)))
  1240. offs += 0x1000;
  1241. else
  1242. offs += 0x10000;
  1243. }
  1244. }
  1245. /* Check status of 'QE' bit, update if required. */
  1246. stfsm_read_status(fsm, SPINOR_OP_RDCR, &cr1, 1);
  1247. data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
  1248. if (data_pads == 4) {
  1249. if (!(cr1 & STFSM_S25FL_CONFIG_QE)) {
  1250. /* Set 'QE' */
  1251. cr1 |= STFSM_S25FL_CONFIG_QE;
  1252. update_sr = 1;
  1253. }
  1254. } else {
  1255. if (cr1 & STFSM_S25FL_CONFIG_QE) {
  1256. /* Clear 'QE' */
  1257. cr1 &= ~STFSM_S25FL_CONFIG_QE;
  1258. update_sr = 1;
  1259. }
  1260. }
  1261. if (update_sr) {
  1262. stfsm_read_status(fsm, SPINOR_OP_RDSR, &sr1, 1);
  1263. sta_wr = ((uint16_t)cr1 << 8) | sr1;
  1264. stfsm_write_status(fsm, SPINOR_OP_WRSR, sta_wr, 2, 1);
  1265. }
  1266. /*
  1267. * S25FLxxx devices support Program and Error error flags.
  1268. * Configure driver to check flags and clear if necessary.
  1269. */
  1270. fsm->configuration |= CFG_S25FL_CHECK_ERROR_FLAGS;
  1271. return 0;
  1272. }
  1273. static int stfsm_w25q_config(struct stfsm *fsm)
  1274. {
  1275. uint32_t data_pads;
  1276. uint8_t sr1, sr2;
  1277. uint16_t sr_wr;
  1278. int update_sr = 0;
  1279. int ret;
  1280. ret = stfsm_prepare_rwe_seqs_default(fsm);
  1281. if (ret)
  1282. return ret;
  1283. /* Check status of 'QE' bit, update if required. */
  1284. stfsm_read_status(fsm, SPINOR_OP_RDCR, &sr2, 1);
  1285. data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
  1286. if (data_pads == 4) {
  1287. if (!(sr2 & W25Q_STATUS_QE)) {
  1288. /* Set 'QE' */
  1289. sr2 |= W25Q_STATUS_QE;
  1290. update_sr = 1;
  1291. }
  1292. } else {
  1293. if (sr2 & W25Q_STATUS_QE) {
  1294. /* Clear 'QE' */
  1295. sr2 &= ~W25Q_STATUS_QE;
  1296. update_sr = 1;
  1297. }
  1298. }
  1299. if (update_sr) {
  1300. /* Write status register */
  1301. stfsm_read_status(fsm, SPINOR_OP_RDSR, &sr1, 1);
  1302. sr_wr = ((uint16_t)sr2 << 8) | sr1;
  1303. stfsm_write_status(fsm, SPINOR_OP_WRSR, sr_wr, 2, 1);
  1304. }
  1305. return 0;
  1306. }
  1307. static int stfsm_read(struct stfsm *fsm, uint8_t *buf, uint32_t size,
  1308. uint32_t offset)
  1309. {
  1310. struct stfsm_seq *seq = &fsm->stfsm_seq_read;
  1311. uint32_t data_pads;
  1312. uint32_t read_mask;
  1313. uint32_t size_ub;
  1314. uint32_t size_lb;
  1315. uint32_t size_mop;
  1316. uint32_t tmp[4];
  1317. uint32_t page_buf[FLASH_PAGESIZE_32];
  1318. uint8_t *p;
  1319. dev_dbg(fsm->dev, "reading %d bytes from 0x%08x\n", size, offset);
  1320. /* Enter 32-bit address mode, if required */
  1321. if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
  1322. stfsm_enter_32bit_addr(fsm, 1);
  1323. /* Must read in multiples of 32 cycles (or 32*pads/8 Bytes) */
  1324. data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
  1325. read_mask = (data_pads << 2) - 1;
  1326. /* Handle non-aligned buf */
  1327. p = ((uintptr_t)buf & 0x3) ? (uint8_t *)page_buf : buf;
  1328. /* Handle non-aligned size */
  1329. size_ub = (size + read_mask) & ~read_mask;
  1330. size_lb = size & ~read_mask;
  1331. size_mop = size & read_mask;
  1332. seq->data_size = TRANSFER_SIZE(size_ub);
  1333. seq->addr1 = (offset >> 16) & 0xffff;
  1334. seq->addr2 = offset & 0xffff;
  1335. stfsm_load_seq(fsm, seq);
  1336. if (size_lb)
  1337. stfsm_read_fifo(fsm, (uint32_t *)p, size_lb);
  1338. if (size_mop) {
  1339. stfsm_read_fifo(fsm, tmp, read_mask + 1);
  1340. memcpy(p + size_lb, &tmp, size_mop);
  1341. }
  1342. /* Handle non-aligned buf */
  1343. if ((uintptr_t)buf & 0x3)
  1344. memcpy(buf, page_buf, size);
  1345. /* Wait for sequence to finish */
  1346. stfsm_wait_seq(fsm);
  1347. stfsm_clear_fifo(fsm);
  1348. /* Exit 32-bit address mode, if required */
  1349. if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
  1350. stfsm_enter_32bit_addr(fsm, 0);
  1351. return 0;
  1352. }
  1353. static int stfsm_write(struct stfsm *fsm, const uint8_t *buf,
  1354. uint32_t size, uint32_t offset)
  1355. {
  1356. struct stfsm_seq *seq = &fsm->stfsm_seq_write;
  1357. uint32_t data_pads;
  1358. uint32_t write_mask;
  1359. uint32_t size_ub;
  1360. uint32_t size_lb;
  1361. uint32_t size_mop;
  1362. uint32_t tmp[4];
  1363. uint32_t i;
  1364. uint32_t page_buf[FLASH_PAGESIZE_32];
  1365. uint8_t *t = (uint8_t *)&tmp;
  1366. const uint8_t *p;
  1367. int ret;
  1368. dev_dbg(fsm->dev, "writing %d bytes to 0x%08x\n", size, offset);
  1369. /* Enter 32-bit address mode, if required */
  1370. if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
  1371. stfsm_enter_32bit_addr(fsm, 1);
  1372. /* Must write in multiples of 32 cycles (or 32*pads/8 bytes) */
  1373. data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
  1374. write_mask = (data_pads << 2) - 1;
  1375. /* Handle non-aligned buf */
  1376. if ((uintptr_t)buf & 0x3) {
  1377. memcpy(page_buf, buf, size);
  1378. p = (uint8_t *)page_buf;
  1379. } else {
  1380. p = buf;
  1381. }
  1382. /* Handle non-aligned size */
  1383. size_ub = (size + write_mask) & ~write_mask;
  1384. size_lb = size & ~write_mask;
  1385. size_mop = size & write_mask;
  1386. seq->data_size = TRANSFER_SIZE(size_ub);
  1387. seq->addr1 = (offset >> 16) & 0xffff;
  1388. seq->addr2 = offset & 0xffff;
  1389. /* Need to set FIFO to write mode, before writing data to FIFO (see
  1390. * GNBvb79594)
  1391. */
  1392. writel(0x00040000, fsm->base + SPI_FAST_SEQ_CFG);
  1393. /*
  1394. * Before writing data to the FIFO, apply a small delay to allow a
  1395. * potential change of FIFO direction to complete.
  1396. */
  1397. if (fsm->fifo_dir_delay == 0)
  1398. readl(fsm->base + SPI_FAST_SEQ_CFG);
  1399. else
  1400. udelay(fsm->fifo_dir_delay);
  1401. /* Write data to FIFO, before starting sequence (see GNBvd79593) */
  1402. if (size_lb) {
  1403. stfsm_write_fifo(fsm, (uint32_t *)p, size_lb);
  1404. p += size_lb;
  1405. }
  1406. /* Handle non-aligned size */
  1407. if (size_mop) {
  1408. memset(t, 0xff, write_mask + 1); /* fill with 0xff's */
  1409. for (i = 0; i < size_mop; i++)
  1410. t[i] = *p++;
  1411. stfsm_write_fifo(fsm, tmp, write_mask + 1);
  1412. }
  1413. /* Start sequence */
  1414. stfsm_load_seq(fsm, seq);
  1415. /* Wait for sequence to finish */
  1416. stfsm_wait_seq(fsm);
  1417. /* Wait for completion */
  1418. ret = stfsm_wait_busy(fsm);
  1419. if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
  1420. stfsm_s25fl_clear_status_reg(fsm);
  1421. /* Exit 32-bit address mode, if required */
  1422. if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
  1423. stfsm_enter_32bit_addr(fsm, 0);
  1424. return 0;
  1425. }
  1426. /*
  1427. * Read an address range from the flash chip. The address range
  1428. * may be any size provided it is within the physical boundaries.
  1429. */
  1430. static int stfsm_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
  1431. size_t *retlen, u_char *buf)
  1432. {
  1433. struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
  1434. uint32_t bytes;
  1435. dev_dbg(fsm->dev, "%s from 0x%08x, len %zd\n",
  1436. __func__, (u32)from, len);
  1437. mutex_lock(&fsm->lock);
  1438. while (len > 0) {
  1439. bytes = min_t(size_t, len, FLASH_PAGESIZE);
  1440. stfsm_read(fsm, buf, bytes, from);
  1441. buf += bytes;
  1442. from += bytes;
  1443. len -= bytes;
  1444. *retlen += bytes;
  1445. }
  1446. mutex_unlock(&fsm->lock);
  1447. return 0;
  1448. }
  1449. static int stfsm_erase_sector(struct stfsm *fsm, uint32_t offset)
  1450. {
  1451. struct stfsm_seq *seq = &stfsm_seq_erase_sector;
  1452. int ret;
  1453. dev_dbg(fsm->dev, "erasing sector at 0x%08x\n", offset);
  1454. /* Enter 32-bit address mode, if required */
  1455. if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
  1456. stfsm_enter_32bit_addr(fsm, 1);
  1457. seq->addr1 = (offset >> 16) & 0xffff;
  1458. seq->addr2 = offset & 0xffff;
  1459. stfsm_load_seq(fsm, seq);
  1460. stfsm_wait_seq(fsm);
  1461. /* Wait for completion */
  1462. ret = stfsm_wait_busy(fsm);
  1463. if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
  1464. stfsm_s25fl_clear_status_reg(fsm);
  1465. /* Exit 32-bit address mode, if required */
  1466. if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
  1467. stfsm_enter_32bit_addr(fsm, 0);
  1468. return ret;
  1469. }
  1470. static int stfsm_erase_chip(struct stfsm *fsm)
  1471. {
  1472. const struct stfsm_seq *seq = &stfsm_seq_erase_chip;
  1473. dev_dbg(fsm->dev, "erasing chip\n");
  1474. stfsm_load_seq(fsm, seq);
  1475. stfsm_wait_seq(fsm);
  1476. return stfsm_wait_busy(fsm);
  1477. }
  1478. /*
  1479. * Write an address range to the flash chip. Data must be written in
  1480. * FLASH_PAGESIZE chunks. The address range may be any size provided
  1481. * it is within the physical boundaries.
  1482. */
  1483. static int stfsm_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
  1484. size_t *retlen, const u_char *buf)
  1485. {
  1486. struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
  1487. u32 page_offs;
  1488. u32 bytes;
  1489. uint8_t *b = (uint8_t *)buf;
  1490. int ret = 0;
  1491. dev_dbg(fsm->dev, "%s to 0x%08x, len %zd\n", __func__, (u32)to, len);
  1492. /* Offset within page */
  1493. page_offs = to % FLASH_PAGESIZE;
  1494. mutex_lock(&fsm->lock);
  1495. while (len) {
  1496. /* Write up to page boundary */
  1497. bytes = min_t(size_t, FLASH_PAGESIZE - page_offs, len);
  1498. ret = stfsm_write(fsm, b, bytes, to);
  1499. if (ret)
  1500. goto out1;
  1501. b += bytes;
  1502. len -= bytes;
  1503. to += bytes;
  1504. /* We are now page-aligned */
  1505. page_offs = 0;
  1506. *retlen += bytes;
  1507. }
  1508. out1:
  1509. mutex_unlock(&fsm->lock);
  1510. return ret;
  1511. }
  1512. /*
  1513. * Erase an address range on the flash chip. The address range may extend
  1514. * one or more erase sectors. Return an error is there is a problem erasing.
  1515. */
  1516. static int stfsm_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  1517. {
  1518. struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
  1519. u32 addr, len;
  1520. int ret;
  1521. dev_dbg(fsm->dev, "%s at 0x%llx, len %lld\n", __func__,
  1522. (long long)instr->addr, (long long)instr->len);
  1523. addr = instr->addr;
  1524. len = instr->len;
  1525. mutex_lock(&fsm->lock);
  1526. /* Whole-chip erase? */
  1527. if (len == mtd->size) {
  1528. ret = stfsm_erase_chip(fsm);
  1529. if (ret)
  1530. goto out1;
  1531. } else {
  1532. while (len) {
  1533. ret = stfsm_erase_sector(fsm, addr);
  1534. if (ret)
  1535. goto out1;
  1536. addr += mtd->erasesize;
  1537. len -= mtd->erasesize;
  1538. }
  1539. }
  1540. mutex_unlock(&fsm->lock);
  1541. return 0;
  1542. out1:
  1543. mutex_unlock(&fsm->lock);
  1544. return ret;
  1545. }
  1546. static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *jedec)
  1547. {
  1548. const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
  1549. uint32_t tmp[2];
  1550. stfsm_load_seq(fsm, seq);
  1551. stfsm_read_fifo(fsm, tmp, 8);
  1552. memcpy(jedec, tmp, 5);
  1553. stfsm_wait_seq(fsm);
  1554. }
  1555. static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
  1556. {
  1557. struct flash_info *info;
  1558. u16 ext_jedec;
  1559. u32 jedec;
  1560. u8 id[5];
  1561. stfsm_read_jedec(fsm, id);
  1562. jedec = id[0] << 16 | id[1] << 8 | id[2];
  1563. /*
  1564. * JEDEC also defines an optional "extended device information"
  1565. * string for after vendor-specific data, after the three bytes
  1566. * we use here. Supporting some chips might require using it.
  1567. */
  1568. ext_jedec = id[3] << 8 | id[4];
  1569. dev_dbg(fsm->dev, "JEDEC = 0x%08x [%5ph]\n", jedec, id);
  1570. for (info = flash_types; info->name; info++) {
  1571. if (info->jedec_id == jedec) {
  1572. if (info->ext_id && info->ext_id != ext_jedec)
  1573. continue;
  1574. return info;
  1575. }
  1576. }
  1577. dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
  1578. return NULL;
  1579. }
  1580. static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
  1581. {
  1582. int ret, timeout = 10;
  1583. /* Wait for controller to accept mode change */
  1584. while (--timeout) {
  1585. ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
  1586. if (ret & 0x1)
  1587. break;
  1588. udelay(1);
  1589. }
  1590. if (!timeout)
  1591. return -EBUSY;
  1592. writel(mode, fsm->base + SPI_MODESELECT);
  1593. return 0;
  1594. }
  1595. static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
  1596. {
  1597. uint32_t emi_freq;
  1598. uint32_t clk_div;
  1599. emi_freq = clk_get_rate(fsm->clk);
  1600. /*
  1601. * Calculate clk_div - values between 2 and 128
  1602. * Multiple of 2, rounded up
  1603. */
  1604. clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
  1605. if (clk_div < 2)
  1606. clk_div = 2;
  1607. else if (clk_div > 128)
  1608. clk_div = 128;
  1609. /*
  1610. * Determine a suitable delay for the IP to complete a change of
  1611. * direction of the FIFO. The required delay is related to the clock
  1612. * divider used. The following heuristics are based on empirical tests,
  1613. * using a 100MHz EMI clock.
  1614. */
  1615. if (clk_div <= 4)
  1616. fsm->fifo_dir_delay = 0;
  1617. else if (clk_div <= 10)
  1618. fsm->fifo_dir_delay = 1;
  1619. else
  1620. fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
  1621. dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
  1622. emi_freq, spi_freq, clk_div);
  1623. writel(clk_div, fsm->base + SPI_CLOCKDIV);
  1624. }
  1625. static int stfsm_init(struct stfsm *fsm)
  1626. {
  1627. int ret;
  1628. /* Perform a soft reset of the FSM controller */
  1629. writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
  1630. udelay(1);
  1631. writel(0, fsm->base + SPI_FAST_SEQ_CFG);
  1632. /* Set clock to 'safe' frequency initially */
  1633. stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
  1634. /* Switch to FSM */
  1635. ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
  1636. if (ret)
  1637. return ret;
  1638. /* Set timing parameters */
  1639. writel(SPI_CFG_DEVICE_ST |
  1640. SPI_CFG_DEFAULT_MIN_CS_HIGH |
  1641. SPI_CFG_DEFAULT_CS_SETUPHOLD |
  1642. SPI_CFG_DEFAULT_DATA_HOLD,
  1643. fsm->base + SPI_CONFIGDATA);
  1644. writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
  1645. /*
  1646. * Set the FSM 'WAIT' delay to the minimum workable value. Note, for
  1647. * our purposes, the WAIT instruction is used purely to achieve
  1648. * "sequence validity" rather than actually implement a delay.
  1649. */
  1650. writel(0x00000001, fsm->base + SPI_PROGRAM_ERASE_TIME);
  1651. /* Clear FIFO, just in case */
  1652. stfsm_clear_fifo(fsm);
  1653. return 0;
  1654. }
  1655. static void stfsm_fetch_platform_configs(struct platform_device *pdev)
  1656. {
  1657. struct stfsm *fsm = platform_get_drvdata(pdev);
  1658. struct device_node *np = pdev->dev.of_node;
  1659. struct regmap *regmap;
  1660. uint32_t boot_device_reg;
  1661. uint32_t boot_device_spi;
  1662. uint32_t boot_device; /* Value we read from *boot_device_reg */
  1663. int ret;
  1664. /* Booting from SPI NOR Flash is the default */
  1665. fsm->booted_from_spi = true;
  1666. regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  1667. if (IS_ERR(regmap))
  1668. goto boot_device_fail;
  1669. fsm->reset_signal = of_property_read_bool(np, "st,reset-signal");
  1670. fsm->reset_por = of_property_read_bool(np, "st,reset-por");
  1671. /* Where in the syscon the boot device information lives */
  1672. ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
  1673. if (ret)
  1674. goto boot_device_fail;
  1675. /* Boot device value when booted from SPI NOR */
  1676. ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
  1677. if (ret)
  1678. goto boot_device_fail;
  1679. ret = regmap_read(regmap, boot_device_reg, &boot_device);
  1680. if (ret)
  1681. goto boot_device_fail;
  1682. if (boot_device != boot_device_spi)
  1683. fsm->booted_from_spi = false;
  1684. return;
  1685. boot_device_fail:
  1686. dev_warn(&pdev->dev,
  1687. "failed to fetch boot device, assuming boot from SPI\n");
  1688. }
  1689. static int stfsm_probe(struct platform_device *pdev)
  1690. {
  1691. struct device_node *np = pdev->dev.of_node;
  1692. struct flash_info *info;
  1693. struct resource *res;
  1694. struct stfsm *fsm;
  1695. int ret;
  1696. if (!np) {
  1697. dev_err(&pdev->dev, "No DT found\n");
  1698. return -EINVAL;
  1699. }
  1700. fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
  1701. if (!fsm)
  1702. return -ENOMEM;
  1703. fsm->dev = &pdev->dev;
  1704. platform_set_drvdata(pdev, fsm);
  1705. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1706. if (!res) {
  1707. dev_err(&pdev->dev, "Resource not found\n");
  1708. return -ENODEV;
  1709. }
  1710. fsm->base = devm_ioremap_resource(&pdev->dev, res);
  1711. if (IS_ERR(fsm->base)) {
  1712. dev_err(&pdev->dev,
  1713. "Failed to reserve memory region %pR\n", res);
  1714. return PTR_ERR(fsm->base);
  1715. }
  1716. fsm->clk = devm_clk_get(&pdev->dev, NULL);
  1717. if (IS_ERR(fsm->clk)) {
  1718. dev_err(fsm->dev, "Couldn't find EMI clock.\n");
  1719. return PTR_ERR(fsm->clk);
  1720. }
  1721. ret = clk_prepare_enable(fsm->clk);
  1722. if (ret) {
  1723. dev_err(fsm->dev, "Failed to enable EMI clock.\n");
  1724. return ret;
  1725. }
  1726. mutex_init(&fsm->lock);
  1727. ret = stfsm_init(fsm);
  1728. if (ret) {
  1729. dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
  1730. goto err_clk_unprepare;
  1731. }
  1732. stfsm_fetch_platform_configs(pdev);
  1733. /* Detect SPI FLASH device */
  1734. info = stfsm_jedec_probe(fsm);
  1735. if (!info) {
  1736. ret = -ENODEV;
  1737. goto err_clk_unprepare;
  1738. }
  1739. fsm->info = info;
  1740. /* Use device size to determine address width */
  1741. if (info->sector_size * info->n_sectors > 0x1000000)
  1742. info->flags |= FLASH_FLAG_32BIT_ADDR;
  1743. /*
  1744. * Configure READ/WRITE/ERASE sequences according to platform and
  1745. * device flags.
  1746. */
  1747. if (info->config) {
  1748. ret = info->config(fsm);
  1749. if (ret)
  1750. goto err_clk_unprepare;
  1751. } else {
  1752. ret = stfsm_prepare_rwe_seqs_default(fsm);
  1753. if (ret)
  1754. goto err_clk_unprepare;
  1755. }
  1756. fsm->mtd.name = info->name;
  1757. fsm->mtd.dev.parent = &pdev->dev;
  1758. mtd_set_of_node(&fsm->mtd, np);
  1759. fsm->mtd.type = MTD_NORFLASH;
  1760. fsm->mtd.writesize = 4;
  1761. fsm->mtd.writebufsize = fsm->mtd.writesize;
  1762. fsm->mtd.flags = MTD_CAP_NORFLASH;
  1763. fsm->mtd.size = info->sector_size * info->n_sectors;
  1764. fsm->mtd.erasesize = info->sector_size;
  1765. fsm->mtd._read = stfsm_mtd_read;
  1766. fsm->mtd._write = stfsm_mtd_write;
  1767. fsm->mtd._erase = stfsm_mtd_erase;
  1768. dev_info(&pdev->dev,
  1769. "Found serial flash device: %s\n"
  1770. " size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
  1771. info->name,
  1772. (long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
  1773. fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
  1774. return mtd_device_register(&fsm->mtd, NULL, 0);
  1775. err_clk_unprepare:
  1776. clk_disable_unprepare(fsm->clk);
  1777. return ret;
  1778. }
  1779. static int stfsm_remove(struct platform_device *pdev)
  1780. {
  1781. struct stfsm *fsm = platform_get_drvdata(pdev);
  1782. return mtd_device_unregister(&fsm->mtd);
  1783. }
  1784. #ifdef CONFIG_PM_SLEEP
  1785. static int stfsmfsm_suspend(struct device *dev)
  1786. {
  1787. struct stfsm *fsm = dev_get_drvdata(dev);
  1788. clk_disable_unprepare(fsm->clk);
  1789. return 0;
  1790. }
  1791. static int stfsmfsm_resume(struct device *dev)
  1792. {
  1793. struct stfsm *fsm = dev_get_drvdata(dev);
  1794. return clk_prepare_enable(fsm->clk);
  1795. }
  1796. #endif
  1797. static SIMPLE_DEV_PM_OPS(stfsm_pm_ops, stfsmfsm_suspend, stfsmfsm_resume);
  1798. static const struct of_device_id stfsm_match[] = {
  1799. { .compatible = "st,spi-fsm", },
  1800. {},
  1801. };
  1802. MODULE_DEVICE_TABLE(of, stfsm_match);
  1803. static struct platform_driver stfsm_driver = {
  1804. .probe = stfsm_probe,
  1805. .remove = stfsm_remove,
  1806. .driver = {
  1807. .name = "st-spi-fsm",
  1808. .of_match_table = stfsm_match,
  1809. .pm = &stfsm_pm_ops,
  1810. },
  1811. };
  1812. module_platform_driver(stfsm_driver);
  1813. MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
  1814. MODULE_DESCRIPTION("ST SPI FSM driver");
  1815. MODULE_LICENSE("GPL");