st_spi_fsm.c 58 KB

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