brcmnand.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306
  1. /*
  2. * Copyright © 2010-2015 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/version.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/device.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/err.h>
  20. #include <linux/completion.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/ioport.h>
  25. #include <linux/bug.h>
  26. #include <linux/kernel.h>
  27. #include <linux/bitops.h>
  28. #include <linux/mm.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/nand.h>
  31. #include <linux/mtd/partitions.h>
  32. #include <linux/of.h>
  33. #include <linux/of_mtd.h>
  34. #include <linux/of_platform.h>
  35. #include <linux/slab.h>
  36. #include <linux/list.h>
  37. #include <linux/log2.h>
  38. #include "brcmnand.h"
  39. /*
  40. * This flag controls if WP stays on between erase/write commands to mitigate
  41. * flash corruption due to power glitches. Values:
  42. * 0: NAND_WP is not used or not available
  43. * 1: NAND_WP is set by default, cleared for erase/write operations
  44. * 2: NAND_WP is always cleared
  45. */
  46. static int wp_on = 1;
  47. module_param(wp_on, int, 0444);
  48. /***********************************************************************
  49. * Definitions
  50. ***********************************************************************/
  51. #define DRV_NAME "brcmnand"
  52. #define CMD_NULL 0x00
  53. #define CMD_PAGE_READ 0x01
  54. #define CMD_SPARE_AREA_READ 0x02
  55. #define CMD_STATUS_READ 0x03
  56. #define CMD_PROGRAM_PAGE 0x04
  57. #define CMD_PROGRAM_SPARE_AREA 0x05
  58. #define CMD_COPY_BACK 0x06
  59. #define CMD_DEVICE_ID_READ 0x07
  60. #define CMD_BLOCK_ERASE 0x08
  61. #define CMD_FLASH_RESET 0x09
  62. #define CMD_BLOCKS_LOCK 0x0a
  63. #define CMD_BLOCKS_LOCK_DOWN 0x0b
  64. #define CMD_BLOCKS_UNLOCK 0x0c
  65. #define CMD_READ_BLOCKS_LOCK_STATUS 0x0d
  66. #define CMD_PARAMETER_READ 0x0e
  67. #define CMD_PARAMETER_CHANGE_COL 0x0f
  68. #define CMD_LOW_LEVEL_OP 0x10
  69. struct brcm_nand_dma_desc {
  70. u32 next_desc;
  71. u32 next_desc_ext;
  72. u32 cmd_irq;
  73. u32 dram_addr;
  74. u32 dram_addr_ext;
  75. u32 tfr_len;
  76. u32 total_len;
  77. u32 flash_addr;
  78. u32 flash_addr_ext;
  79. u32 cs;
  80. u32 pad2[5];
  81. u32 status_valid;
  82. } __packed;
  83. /* Bitfields for brcm_nand_dma_desc::status_valid */
  84. #define FLASH_DMA_ECC_ERROR (1 << 8)
  85. #define FLASH_DMA_CORR_ERROR (1 << 9)
  86. /* 512B flash cache in the NAND controller HW */
  87. #define FC_SHIFT 9U
  88. #define FC_BYTES 512U
  89. #define FC_WORDS (FC_BYTES >> 2)
  90. #define BRCMNAND_MIN_PAGESIZE 512
  91. #define BRCMNAND_MIN_BLOCKSIZE (8 * 1024)
  92. #define BRCMNAND_MIN_DEVSIZE (4ULL * 1024 * 1024)
  93. /* Controller feature flags */
  94. enum {
  95. BRCMNAND_HAS_1K_SECTORS = BIT(0),
  96. BRCMNAND_HAS_PREFETCH = BIT(1),
  97. BRCMNAND_HAS_CACHE_MODE = BIT(2),
  98. BRCMNAND_HAS_WP = BIT(3),
  99. };
  100. struct brcmnand_controller {
  101. struct device *dev;
  102. struct nand_hw_control controller;
  103. void __iomem *nand_base;
  104. void __iomem *nand_fc; /* flash cache */
  105. void __iomem *flash_dma_base;
  106. unsigned int irq;
  107. unsigned int dma_irq;
  108. int nand_version;
  109. /* Some SoCs provide custom interrupt status register(s) */
  110. struct brcmnand_soc *soc;
  111. int cmd_pending;
  112. bool dma_pending;
  113. struct completion done;
  114. struct completion dma_done;
  115. /* List of NAND hosts (one for each chip-select) */
  116. struct list_head host_list;
  117. struct brcm_nand_dma_desc *dma_desc;
  118. dma_addr_t dma_pa;
  119. /* in-memory cache of the FLASH_CACHE, used only for some commands */
  120. u32 flash_cache[FC_WORDS];
  121. /* Controller revision details */
  122. const u16 *reg_offsets;
  123. unsigned int reg_spacing; /* between CS1, CS2, ... regs */
  124. const u8 *cs_offsets; /* within each chip-select */
  125. const u8 *cs0_offsets; /* within CS0, if different */
  126. unsigned int max_block_size;
  127. const unsigned int *block_sizes;
  128. unsigned int max_page_size;
  129. const unsigned int *page_sizes;
  130. unsigned int max_oob;
  131. u32 features;
  132. /* for low-power standby/resume only */
  133. u32 nand_cs_nand_select;
  134. u32 nand_cs_nand_xor;
  135. u32 corr_stat_threshold;
  136. u32 flash_dma_mode;
  137. };
  138. struct brcmnand_cfg {
  139. u64 device_size;
  140. unsigned int block_size;
  141. unsigned int page_size;
  142. unsigned int spare_area_size;
  143. unsigned int device_width;
  144. unsigned int col_adr_bytes;
  145. unsigned int blk_adr_bytes;
  146. unsigned int ful_adr_bytes;
  147. unsigned int sector_size_1k;
  148. unsigned int ecc_level;
  149. /* use for low-power standby/resume only */
  150. u32 acc_control;
  151. u32 config;
  152. u32 config_ext;
  153. u32 timing_1;
  154. u32 timing_2;
  155. };
  156. struct brcmnand_host {
  157. struct list_head node;
  158. struct device_node *of_node;
  159. struct nand_chip chip;
  160. struct mtd_info mtd;
  161. struct platform_device *pdev;
  162. int cs;
  163. unsigned int last_cmd;
  164. unsigned int last_byte;
  165. u64 last_addr;
  166. struct brcmnand_cfg hwcfg;
  167. struct brcmnand_controller *ctrl;
  168. };
  169. enum brcmnand_reg {
  170. BRCMNAND_CMD_START = 0,
  171. BRCMNAND_CMD_EXT_ADDRESS,
  172. BRCMNAND_CMD_ADDRESS,
  173. BRCMNAND_INTFC_STATUS,
  174. BRCMNAND_CS_SELECT,
  175. BRCMNAND_CS_XOR,
  176. BRCMNAND_LL_OP,
  177. BRCMNAND_CS0_BASE,
  178. BRCMNAND_CS1_BASE, /* CS1 regs, if non-contiguous */
  179. BRCMNAND_CORR_THRESHOLD,
  180. BRCMNAND_CORR_THRESHOLD_EXT,
  181. BRCMNAND_UNCORR_COUNT,
  182. BRCMNAND_CORR_COUNT,
  183. BRCMNAND_CORR_EXT_ADDR,
  184. BRCMNAND_CORR_ADDR,
  185. BRCMNAND_UNCORR_EXT_ADDR,
  186. BRCMNAND_UNCORR_ADDR,
  187. BRCMNAND_SEMAPHORE,
  188. BRCMNAND_ID,
  189. BRCMNAND_ID_EXT,
  190. BRCMNAND_LL_RDATA,
  191. BRCMNAND_OOB_READ_BASE,
  192. BRCMNAND_OOB_READ_10_BASE, /* offset 0x10, if non-contiguous */
  193. BRCMNAND_OOB_WRITE_BASE,
  194. BRCMNAND_OOB_WRITE_10_BASE, /* offset 0x10, if non-contiguous */
  195. BRCMNAND_FC_BASE,
  196. };
  197. /* BRCMNAND v4.0 */
  198. static const u16 brcmnand_regs_v40[] = {
  199. [BRCMNAND_CMD_START] = 0x04,
  200. [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
  201. [BRCMNAND_CMD_ADDRESS] = 0x0c,
  202. [BRCMNAND_INTFC_STATUS] = 0x6c,
  203. [BRCMNAND_CS_SELECT] = 0x14,
  204. [BRCMNAND_CS_XOR] = 0x18,
  205. [BRCMNAND_LL_OP] = 0x178,
  206. [BRCMNAND_CS0_BASE] = 0x40,
  207. [BRCMNAND_CS1_BASE] = 0xd0,
  208. [BRCMNAND_CORR_THRESHOLD] = 0x84,
  209. [BRCMNAND_CORR_THRESHOLD_EXT] = 0,
  210. [BRCMNAND_UNCORR_COUNT] = 0,
  211. [BRCMNAND_CORR_COUNT] = 0,
  212. [BRCMNAND_CORR_EXT_ADDR] = 0x70,
  213. [BRCMNAND_CORR_ADDR] = 0x74,
  214. [BRCMNAND_UNCORR_EXT_ADDR] = 0x78,
  215. [BRCMNAND_UNCORR_ADDR] = 0x7c,
  216. [BRCMNAND_SEMAPHORE] = 0x58,
  217. [BRCMNAND_ID] = 0x60,
  218. [BRCMNAND_ID_EXT] = 0x64,
  219. [BRCMNAND_LL_RDATA] = 0x17c,
  220. [BRCMNAND_OOB_READ_BASE] = 0x20,
  221. [BRCMNAND_OOB_READ_10_BASE] = 0x130,
  222. [BRCMNAND_OOB_WRITE_BASE] = 0x30,
  223. [BRCMNAND_OOB_WRITE_10_BASE] = 0,
  224. [BRCMNAND_FC_BASE] = 0x200,
  225. };
  226. /* BRCMNAND v5.0 */
  227. static const u16 brcmnand_regs_v50[] = {
  228. [BRCMNAND_CMD_START] = 0x04,
  229. [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
  230. [BRCMNAND_CMD_ADDRESS] = 0x0c,
  231. [BRCMNAND_INTFC_STATUS] = 0x6c,
  232. [BRCMNAND_CS_SELECT] = 0x14,
  233. [BRCMNAND_CS_XOR] = 0x18,
  234. [BRCMNAND_LL_OP] = 0x178,
  235. [BRCMNAND_CS0_BASE] = 0x40,
  236. [BRCMNAND_CS1_BASE] = 0xd0,
  237. [BRCMNAND_CORR_THRESHOLD] = 0x84,
  238. [BRCMNAND_CORR_THRESHOLD_EXT] = 0,
  239. [BRCMNAND_UNCORR_COUNT] = 0,
  240. [BRCMNAND_CORR_COUNT] = 0,
  241. [BRCMNAND_CORR_EXT_ADDR] = 0x70,
  242. [BRCMNAND_CORR_ADDR] = 0x74,
  243. [BRCMNAND_UNCORR_EXT_ADDR] = 0x78,
  244. [BRCMNAND_UNCORR_ADDR] = 0x7c,
  245. [BRCMNAND_SEMAPHORE] = 0x58,
  246. [BRCMNAND_ID] = 0x60,
  247. [BRCMNAND_ID_EXT] = 0x64,
  248. [BRCMNAND_LL_RDATA] = 0x17c,
  249. [BRCMNAND_OOB_READ_BASE] = 0x20,
  250. [BRCMNAND_OOB_READ_10_BASE] = 0x130,
  251. [BRCMNAND_OOB_WRITE_BASE] = 0x30,
  252. [BRCMNAND_OOB_WRITE_10_BASE] = 0x140,
  253. [BRCMNAND_FC_BASE] = 0x200,
  254. };
  255. /* BRCMNAND v6.0 - v7.1 */
  256. static const u16 brcmnand_regs_v60[] = {
  257. [BRCMNAND_CMD_START] = 0x04,
  258. [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
  259. [BRCMNAND_CMD_ADDRESS] = 0x0c,
  260. [BRCMNAND_INTFC_STATUS] = 0x14,
  261. [BRCMNAND_CS_SELECT] = 0x18,
  262. [BRCMNAND_CS_XOR] = 0x1c,
  263. [BRCMNAND_LL_OP] = 0x20,
  264. [BRCMNAND_CS0_BASE] = 0x50,
  265. [BRCMNAND_CS1_BASE] = 0,
  266. [BRCMNAND_CORR_THRESHOLD] = 0xc0,
  267. [BRCMNAND_CORR_THRESHOLD_EXT] = 0xc4,
  268. [BRCMNAND_UNCORR_COUNT] = 0xfc,
  269. [BRCMNAND_CORR_COUNT] = 0x100,
  270. [BRCMNAND_CORR_EXT_ADDR] = 0x10c,
  271. [BRCMNAND_CORR_ADDR] = 0x110,
  272. [BRCMNAND_UNCORR_EXT_ADDR] = 0x114,
  273. [BRCMNAND_UNCORR_ADDR] = 0x118,
  274. [BRCMNAND_SEMAPHORE] = 0x150,
  275. [BRCMNAND_ID] = 0x194,
  276. [BRCMNAND_ID_EXT] = 0x198,
  277. [BRCMNAND_LL_RDATA] = 0x19c,
  278. [BRCMNAND_OOB_READ_BASE] = 0x200,
  279. [BRCMNAND_OOB_READ_10_BASE] = 0,
  280. [BRCMNAND_OOB_WRITE_BASE] = 0x280,
  281. [BRCMNAND_OOB_WRITE_10_BASE] = 0,
  282. [BRCMNAND_FC_BASE] = 0x400,
  283. };
  284. /* BRCMNAND v7.1 */
  285. static const u16 brcmnand_regs_v71[] = {
  286. [BRCMNAND_CMD_START] = 0x04,
  287. [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
  288. [BRCMNAND_CMD_ADDRESS] = 0x0c,
  289. [BRCMNAND_INTFC_STATUS] = 0x14,
  290. [BRCMNAND_CS_SELECT] = 0x18,
  291. [BRCMNAND_CS_XOR] = 0x1c,
  292. [BRCMNAND_LL_OP] = 0x20,
  293. [BRCMNAND_CS0_BASE] = 0x50,
  294. [BRCMNAND_CS1_BASE] = 0,
  295. [BRCMNAND_CORR_THRESHOLD] = 0xdc,
  296. [BRCMNAND_CORR_THRESHOLD_EXT] = 0xe0,
  297. [BRCMNAND_UNCORR_COUNT] = 0xfc,
  298. [BRCMNAND_CORR_COUNT] = 0x100,
  299. [BRCMNAND_CORR_EXT_ADDR] = 0x10c,
  300. [BRCMNAND_CORR_ADDR] = 0x110,
  301. [BRCMNAND_UNCORR_EXT_ADDR] = 0x114,
  302. [BRCMNAND_UNCORR_ADDR] = 0x118,
  303. [BRCMNAND_SEMAPHORE] = 0x150,
  304. [BRCMNAND_ID] = 0x194,
  305. [BRCMNAND_ID_EXT] = 0x198,
  306. [BRCMNAND_LL_RDATA] = 0x19c,
  307. [BRCMNAND_OOB_READ_BASE] = 0x200,
  308. [BRCMNAND_OOB_READ_10_BASE] = 0,
  309. [BRCMNAND_OOB_WRITE_BASE] = 0x280,
  310. [BRCMNAND_OOB_WRITE_10_BASE] = 0,
  311. [BRCMNAND_FC_BASE] = 0x400,
  312. };
  313. enum brcmnand_cs_reg {
  314. BRCMNAND_CS_CFG_EXT = 0,
  315. BRCMNAND_CS_CFG,
  316. BRCMNAND_CS_ACC_CONTROL,
  317. BRCMNAND_CS_TIMING1,
  318. BRCMNAND_CS_TIMING2,
  319. };
  320. /* Per chip-select offsets for v7.1 */
  321. static const u8 brcmnand_cs_offsets_v71[] = {
  322. [BRCMNAND_CS_ACC_CONTROL] = 0x00,
  323. [BRCMNAND_CS_CFG_EXT] = 0x04,
  324. [BRCMNAND_CS_CFG] = 0x08,
  325. [BRCMNAND_CS_TIMING1] = 0x0c,
  326. [BRCMNAND_CS_TIMING2] = 0x10,
  327. };
  328. /* Per chip-select offsets for pre v7.1, except CS0 on <= v5.0 */
  329. static const u8 brcmnand_cs_offsets[] = {
  330. [BRCMNAND_CS_ACC_CONTROL] = 0x00,
  331. [BRCMNAND_CS_CFG_EXT] = 0x04,
  332. [BRCMNAND_CS_CFG] = 0x04,
  333. [BRCMNAND_CS_TIMING1] = 0x08,
  334. [BRCMNAND_CS_TIMING2] = 0x0c,
  335. };
  336. /* Per chip-select offset for <= v5.0 on CS0 only */
  337. static const u8 brcmnand_cs_offsets_cs0[] = {
  338. [BRCMNAND_CS_ACC_CONTROL] = 0x00,
  339. [BRCMNAND_CS_CFG_EXT] = 0x08,
  340. [BRCMNAND_CS_CFG] = 0x08,
  341. [BRCMNAND_CS_TIMING1] = 0x10,
  342. [BRCMNAND_CS_TIMING2] = 0x14,
  343. };
  344. /*
  345. * Bitfields for the CFG and CFG_EXT registers. Pre-v7.1 controllers only had
  346. * one config register, but once the bitfields overflowed, newer controllers
  347. * (v7.1 and newer) added a CFG_EXT register and shuffled a few fields around.
  348. */
  349. enum {
  350. CFG_BLK_ADR_BYTES_SHIFT = 8,
  351. CFG_COL_ADR_BYTES_SHIFT = 12,
  352. CFG_FUL_ADR_BYTES_SHIFT = 16,
  353. CFG_BUS_WIDTH_SHIFT = 23,
  354. CFG_BUS_WIDTH = BIT(CFG_BUS_WIDTH_SHIFT),
  355. CFG_DEVICE_SIZE_SHIFT = 24,
  356. /* Only for pre-v7.1 (with no CFG_EXT register) */
  357. CFG_PAGE_SIZE_SHIFT = 20,
  358. CFG_BLK_SIZE_SHIFT = 28,
  359. /* Only for v7.1+ (with CFG_EXT register) */
  360. CFG_EXT_PAGE_SIZE_SHIFT = 0,
  361. CFG_EXT_BLK_SIZE_SHIFT = 4,
  362. };
  363. /* BRCMNAND_INTFC_STATUS */
  364. enum {
  365. INTFC_FLASH_STATUS = GENMASK(7, 0),
  366. INTFC_ERASED = BIT(27),
  367. INTFC_OOB_VALID = BIT(28),
  368. INTFC_CACHE_VALID = BIT(29),
  369. INTFC_FLASH_READY = BIT(30),
  370. INTFC_CTLR_READY = BIT(31),
  371. };
  372. static inline u32 nand_readreg(struct brcmnand_controller *ctrl, u32 offs)
  373. {
  374. return brcmnand_readl(ctrl->nand_base + offs);
  375. }
  376. static inline void nand_writereg(struct brcmnand_controller *ctrl, u32 offs,
  377. u32 val)
  378. {
  379. brcmnand_writel(val, ctrl->nand_base + offs);
  380. }
  381. static int brcmnand_revision_init(struct brcmnand_controller *ctrl)
  382. {
  383. static const unsigned int block_sizes_v6[] = { 8, 16, 128, 256, 512, 1024, 2048, 0 };
  384. static const unsigned int block_sizes_v4[] = { 16, 128, 8, 512, 256, 1024, 2048, 0 };
  385. static const unsigned int page_sizes[] = { 512, 2048, 4096, 8192, 0 };
  386. ctrl->nand_version = nand_readreg(ctrl, 0) & 0xffff;
  387. /* Only support v4.0+? */
  388. if (ctrl->nand_version < 0x0400) {
  389. dev_err(ctrl->dev, "version %#x not supported\n",
  390. ctrl->nand_version);
  391. return -ENODEV;
  392. }
  393. /* Register offsets */
  394. if (ctrl->nand_version >= 0x0701)
  395. ctrl->reg_offsets = brcmnand_regs_v71;
  396. else if (ctrl->nand_version >= 0x0600)
  397. ctrl->reg_offsets = brcmnand_regs_v60;
  398. else if (ctrl->nand_version >= 0x0500)
  399. ctrl->reg_offsets = brcmnand_regs_v50;
  400. else if (ctrl->nand_version >= 0x0400)
  401. ctrl->reg_offsets = brcmnand_regs_v40;
  402. /* Chip-select stride */
  403. if (ctrl->nand_version >= 0x0701)
  404. ctrl->reg_spacing = 0x14;
  405. else
  406. ctrl->reg_spacing = 0x10;
  407. /* Per chip-select registers */
  408. if (ctrl->nand_version >= 0x0701) {
  409. ctrl->cs_offsets = brcmnand_cs_offsets_v71;
  410. } else {
  411. ctrl->cs_offsets = brcmnand_cs_offsets;
  412. /* v5.0 and earlier has a different CS0 offset layout */
  413. if (ctrl->nand_version <= 0x0500)
  414. ctrl->cs0_offsets = brcmnand_cs_offsets_cs0;
  415. }
  416. /* Page / block sizes */
  417. if (ctrl->nand_version >= 0x0701) {
  418. /* >= v7.1 use nice power-of-2 values! */
  419. ctrl->max_page_size = 16 * 1024;
  420. ctrl->max_block_size = 2 * 1024 * 1024;
  421. } else {
  422. ctrl->page_sizes = page_sizes;
  423. if (ctrl->nand_version >= 0x0600)
  424. ctrl->block_sizes = block_sizes_v6;
  425. else
  426. ctrl->block_sizes = block_sizes_v4;
  427. if (ctrl->nand_version < 0x0400) {
  428. ctrl->max_page_size = 4096;
  429. ctrl->max_block_size = 512 * 1024;
  430. }
  431. }
  432. /* Maximum spare area sector size (per 512B) */
  433. if (ctrl->nand_version >= 0x0600)
  434. ctrl->max_oob = 64;
  435. else if (ctrl->nand_version >= 0x0500)
  436. ctrl->max_oob = 32;
  437. else
  438. ctrl->max_oob = 16;
  439. /* v6.0 and newer (except v6.1) have prefetch support */
  440. if (ctrl->nand_version >= 0x0600 && ctrl->nand_version != 0x0601)
  441. ctrl->features |= BRCMNAND_HAS_PREFETCH;
  442. /*
  443. * v6.x has cache mode, but it's implemented differently. Ignore it for
  444. * now.
  445. */
  446. if (ctrl->nand_version >= 0x0700)
  447. ctrl->features |= BRCMNAND_HAS_CACHE_MODE;
  448. if (ctrl->nand_version >= 0x0500)
  449. ctrl->features |= BRCMNAND_HAS_1K_SECTORS;
  450. if (ctrl->nand_version >= 0x0700)
  451. ctrl->features |= BRCMNAND_HAS_WP;
  452. else if (of_property_read_bool(ctrl->dev->of_node, "brcm,nand-has-wp"))
  453. ctrl->features |= BRCMNAND_HAS_WP;
  454. return 0;
  455. }
  456. static inline u32 brcmnand_read_reg(struct brcmnand_controller *ctrl,
  457. enum brcmnand_reg reg)
  458. {
  459. u16 offs = ctrl->reg_offsets[reg];
  460. if (offs)
  461. return nand_readreg(ctrl, offs);
  462. else
  463. return 0;
  464. }
  465. static inline void brcmnand_write_reg(struct brcmnand_controller *ctrl,
  466. enum brcmnand_reg reg, u32 val)
  467. {
  468. u16 offs = ctrl->reg_offsets[reg];
  469. if (offs)
  470. nand_writereg(ctrl, offs, val);
  471. }
  472. static inline void brcmnand_rmw_reg(struct brcmnand_controller *ctrl,
  473. enum brcmnand_reg reg, u32 mask, unsigned
  474. int shift, u32 val)
  475. {
  476. u32 tmp = brcmnand_read_reg(ctrl, reg);
  477. tmp &= ~mask;
  478. tmp |= val << shift;
  479. brcmnand_write_reg(ctrl, reg, tmp);
  480. }
  481. static inline u32 brcmnand_read_fc(struct brcmnand_controller *ctrl, int word)
  482. {
  483. return __raw_readl(ctrl->nand_fc + word * 4);
  484. }
  485. static inline void brcmnand_write_fc(struct brcmnand_controller *ctrl,
  486. int word, u32 val)
  487. {
  488. __raw_writel(val, ctrl->nand_fc + word * 4);
  489. }
  490. static inline u16 brcmnand_cs_offset(struct brcmnand_controller *ctrl, int cs,
  491. enum brcmnand_cs_reg reg)
  492. {
  493. u16 offs_cs0 = ctrl->reg_offsets[BRCMNAND_CS0_BASE];
  494. u16 offs_cs1 = ctrl->reg_offsets[BRCMNAND_CS1_BASE];
  495. u8 cs_offs;
  496. if (cs == 0 && ctrl->cs0_offsets)
  497. cs_offs = ctrl->cs0_offsets[reg];
  498. else
  499. cs_offs = ctrl->cs_offsets[reg];
  500. if (cs && offs_cs1)
  501. return offs_cs1 + (cs - 1) * ctrl->reg_spacing + cs_offs;
  502. return offs_cs0 + cs * ctrl->reg_spacing + cs_offs;
  503. }
  504. static inline u32 brcmnand_count_corrected(struct brcmnand_controller *ctrl)
  505. {
  506. if (ctrl->nand_version < 0x0600)
  507. return 1;
  508. return brcmnand_read_reg(ctrl, BRCMNAND_CORR_COUNT);
  509. }
  510. static void brcmnand_wr_corr_thresh(struct brcmnand_host *host, u8 val)
  511. {
  512. struct brcmnand_controller *ctrl = host->ctrl;
  513. unsigned int shift = 0, bits;
  514. enum brcmnand_reg reg = BRCMNAND_CORR_THRESHOLD;
  515. int cs = host->cs;
  516. if (ctrl->nand_version >= 0x0600)
  517. bits = 6;
  518. else if (ctrl->nand_version >= 0x0500)
  519. bits = 5;
  520. else
  521. bits = 4;
  522. if (ctrl->nand_version >= 0x0600) {
  523. if (cs >= 5)
  524. reg = BRCMNAND_CORR_THRESHOLD_EXT;
  525. shift = (cs % 5) * bits;
  526. }
  527. brcmnand_rmw_reg(ctrl, reg, (bits - 1) << shift, shift, val);
  528. }
  529. static inline int brcmnand_cmd_shift(struct brcmnand_controller *ctrl)
  530. {
  531. if (ctrl->nand_version < 0x0700)
  532. return 24;
  533. return 0;
  534. }
  535. /***********************************************************************
  536. * NAND ACC CONTROL bitfield
  537. *
  538. * Some bits have remained constant throughout hardware revision, while
  539. * others have shifted around.
  540. ***********************************************************************/
  541. /* Constant for all versions (where supported) */
  542. enum {
  543. /* See BRCMNAND_HAS_CACHE_MODE */
  544. ACC_CONTROL_CACHE_MODE = BIT(22),
  545. /* See BRCMNAND_HAS_PREFETCH */
  546. ACC_CONTROL_PREFETCH = BIT(23),
  547. ACC_CONTROL_PAGE_HIT = BIT(24),
  548. ACC_CONTROL_WR_PREEMPT = BIT(25),
  549. ACC_CONTROL_PARTIAL_PAGE = BIT(26),
  550. ACC_CONTROL_RD_ERASED = BIT(27),
  551. ACC_CONTROL_FAST_PGM_RDIN = BIT(28),
  552. ACC_CONTROL_WR_ECC = BIT(30),
  553. ACC_CONTROL_RD_ECC = BIT(31),
  554. };
  555. static inline u32 brcmnand_spare_area_mask(struct brcmnand_controller *ctrl)
  556. {
  557. if (ctrl->nand_version >= 0x0600)
  558. return GENMASK(6, 0);
  559. else
  560. return GENMASK(5, 0);
  561. }
  562. #define NAND_ACC_CONTROL_ECC_SHIFT 16
  563. static inline u32 brcmnand_ecc_level_mask(struct brcmnand_controller *ctrl)
  564. {
  565. u32 mask = (ctrl->nand_version >= 0x0600) ? 0x1f : 0x0f;
  566. return mask << NAND_ACC_CONTROL_ECC_SHIFT;
  567. }
  568. static void brcmnand_set_ecc_enabled(struct brcmnand_host *host, int en)
  569. {
  570. struct brcmnand_controller *ctrl = host->ctrl;
  571. u16 offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_ACC_CONTROL);
  572. u32 acc_control = nand_readreg(ctrl, offs);
  573. u32 ecc_flags = ACC_CONTROL_WR_ECC | ACC_CONTROL_RD_ECC;
  574. if (en) {
  575. acc_control |= ecc_flags; /* enable RD/WR ECC */
  576. acc_control |= host->hwcfg.ecc_level
  577. << NAND_ACC_CONTROL_ECC_SHIFT;
  578. } else {
  579. acc_control &= ~ecc_flags; /* disable RD/WR ECC */
  580. acc_control &= ~brcmnand_ecc_level_mask(ctrl);
  581. }
  582. nand_writereg(ctrl, offs, acc_control);
  583. }
  584. static inline int brcmnand_sector_1k_shift(struct brcmnand_controller *ctrl)
  585. {
  586. if (ctrl->nand_version >= 0x0600)
  587. return 7;
  588. else if (ctrl->nand_version >= 0x0500)
  589. return 6;
  590. else
  591. return -1;
  592. }
  593. static int brcmnand_get_sector_size_1k(struct brcmnand_host *host)
  594. {
  595. struct brcmnand_controller *ctrl = host->ctrl;
  596. int shift = brcmnand_sector_1k_shift(ctrl);
  597. u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
  598. BRCMNAND_CS_ACC_CONTROL);
  599. if (shift < 0)
  600. return 0;
  601. return (nand_readreg(ctrl, acc_control_offs) >> shift) & 0x1;
  602. }
  603. static void brcmnand_set_sector_size_1k(struct brcmnand_host *host, int val)
  604. {
  605. struct brcmnand_controller *ctrl = host->ctrl;
  606. int shift = brcmnand_sector_1k_shift(ctrl);
  607. u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
  608. BRCMNAND_CS_ACC_CONTROL);
  609. u32 tmp;
  610. if (shift < 0)
  611. return;
  612. tmp = nand_readreg(ctrl, acc_control_offs);
  613. tmp &= ~(1 << shift);
  614. tmp |= (!!val) << shift;
  615. nand_writereg(ctrl, acc_control_offs, tmp);
  616. }
  617. /***********************************************************************
  618. * CS_NAND_SELECT
  619. ***********************************************************************/
  620. enum {
  621. CS_SELECT_NAND_WP = BIT(29),
  622. CS_SELECT_AUTO_DEVICE_ID_CFG = BIT(30),
  623. };
  624. static inline void brcmnand_set_wp(struct brcmnand_controller *ctrl, bool en)
  625. {
  626. u32 val = en ? CS_SELECT_NAND_WP : 0;
  627. brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT, CS_SELECT_NAND_WP, 0, val);
  628. }
  629. /***********************************************************************
  630. * Flash DMA
  631. ***********************************************************************/
  632. enum flash_dma_reg {
  633. FLASH_DMA_REVISION = 0x00,
  634. FLASH_DMA_FIRST_DESC = 0x04,
  635. FLASH_DMA_FIRST_DESC_EXT = 0x08,
  636. FLASH_DMA_CTRL = 0x0c,
  637. FLASH_DMA_MODE = 0x10,
  638. FLASH_DMA_STATUS = 0x14,
  639. FLASH_DMA_INTERRUPT_DESC = 0x18,
  640. FLASH_DMA_INTERRUPT_DESC_EXT = 0x1c,
  641. FLASH_DMA_ERROR_STATUS = 0x20,
  642. FLASH_DMA_CURRENT_DESC = 0x24,
  643. FLASH_DMA_CURRENT_DESC_EXT = 0x28,
  644. };
  645. static inline bool has_flash_dma(struct brcmnand_controller *ctrl)
  646. {
  647. return ctrl->flash_dma_base;
  648. }
  649. static inline bool flash_dma_buf_ok(const void *buf)
  650. {
  651. return buf && !is_vmalloc_addr(buf) &&
  652. likely(IS_ALIGNED((uintptr_t)buf, 4));
  653. }
  654. static inline void flash_dma_writel(struct brcmnand_controller *ctrl, u8 offs,
  655. u32 val)
  656. {
  657. brcmnand_writel(val, ctrl->flash_dma_base + offs);
  658. }
  659. static inline u32 flash_dma_readl(struct brcmnand_controller *ctrl, u8 offs)
  660. {
  661. return brcmnand_readl(ctrl->flash_dma_base + offs);
  662. }
  663. /* Low-level operation types: command, address, write, or read */
  664. enum brcmnand_llop_type {
  665. LL_OP_CMD,
  666. LL_OP_ADDR,
  667. LL_OP_WR,
  668. LL_OP_RD,
  669. };
  670. /***********************************************************************
  671. * Internal support functions
  672. ***********************************************************************/
  673. static inline bool is_hamming_ecc(struct brcmnand_cfg *cfg)
  674. {
  675. return cfg->sector_size_1k == 0 && cfg->spare_area_size == 16 &&
  676. cfg->ecc_level == 15;
  677. }
  678. /*
  679. * Returns a nand_ecclayout strucutre for the given layout/configuration.
  680. * Returns NULL on failure.
  681. */
  682. static struct nand_ecclayout *brcmnand_create_layout(int ecc_level,
  683. struct brcmnand_host *host)
  684. {
  685. struct brcmnand_cfg *cfg = &host->hwcfg;
  686. int i, j;
  687. struct nand_ecclayout *layout;
  688. int req;
  689. int sectors;
  690. int sas;
  691. int idx1, idx2;
  692. layout = devm_kzalloc(&host->pdev->dev, sizeof(*layout), GFP_KERNEL);
  693. if (!layout)
  694. return NULL;
  695. sectors = cfg->page_size / (512 << cfg->sector_size_1k);
  696. sas = cfg->spare_area_size << cfg->sector_size_1k;
  697. /* Hamming */
  698. if (is_hamming_ecc(cfg)) {
  699. for (i = 0, idx1 = 0, idx2 = 0; i < sectors; i++) {
  700. /* First sector of each page may have BBI */
  701. if (i == 0) {
  702. layout->oobfree[idx2].offset = i * sas + 1;
  703. /* Small-page NAND use byte 6 for BBI */
  704. if (cfg->page_size == 512)
  705. layout->oobfree[idx2].offset--;
  706. layout->oobfree[idx2].length = 5;
  707. } else {
  708. layout->oobfree[idx2].offset = i * sas;
  709. layout->oobfree[idx2].length = 6;
  710. }
  711. idx2++;
  712. layout->eccpos[idx1++] = i * sas + 6;
  713. layout->eccpos[idx1++] = i * sas + 7;
  714. layout->eccpos[idx1++] = i * sas + 8;
  715. layout->oobfree[idx2].offset = i * sas + 9;
  716. layout->oobfree[idx2].length = 7;
  717. idx2++;
  718. /* Leave zero-terminated entry for OOBFREE */
  719. if (idx1 >= MTD_MAX_ECCPOS_ENTRIES_LARGE ||
  720. idx2 >= MTD_MAX_OOBFREE_ENTRIES_LARGE - 1)
  721. break;
  722. }
  723. goto out;
  724. }
  725. /*
  726. * CONTROLLER_VERSION:
  727. * < v5.0: ECC_REQ = ceil(BCH_T * 13/8)
  728. * >= v5.0: ECC_REQ = ceil(BCH_T * 14/8)
  729. * But we will just be conservative.
  730. */
  731. req = DIV_ROUND_UP(ecc_level * 14, 8);
  732. if (req >= sas) {
  733. dev_err(&host->pdev->dev,
  734. "error: ECC too large for OOB (ECC bytes %d, spare sector %d)\n",
  735. req, sas);
  736. return NULL;
  737. }
  738. layout->eccbytes = req * sectors;
  739. for (i = 0, idx1 = 0, idx2 = 0; i < sectors; i++) {
  740. for (j = sas - req; j < sas && idx1 <
  741. MTD_MAX_ECCPOS_ENTRIES_LARGE; j++, idx1++)
  742. layout->eccpos[idx1] = i * sas + j;
  743. /* First sector of each page may have BBI */
  744. if (i == 0) {
  745. if (cfg->page_size == 512 && (sas - req >= 6)) {
  746. /* Small-page NAND use byte 6 for BBI */
  747. layout->oobfree[idx2].offset = 0;
  748. layout->oobfree[idx2].length = 5;
  749. idx2++;
  750. if (sas - req > 6) {
  751. layout->oobfree[idx2].offset = 6;
  752. layout->oobfree[idx2].length =
  753. sas - req - 6;
  754. idx2++;
  755. }
  756. } else if (sas > req + 1) {
  757. layout->oobfree[idx2].offset = i * sas + 1;
  758. layout->oobfree[idx2].length = sas - req - 1;
  759. idx2++;
  760. }
  761. } else if (sas > req) {
  762. layout->oobfree[idx2].offset = i * sas;
  763. layout->oobfree[idx2].length = sas - req;
  764. idx2++;
  765. }
  766. /* Leave zero-terminated entry for OOBFREE */
  767. if (idx1 >= MTD_MAX_ECCPOS_ENTRIES_LARGE ||
  768. idx2 >= MTD_MAX_OOBFREE_ENTRIES_LARGE - 1)
  769. break;
  770. }
  771. out:
  772. /* Sum available OOB */
  773. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES_LARGE; i++)
  774. layout->oobavail += layout->oobfree[i].length;
  775. return layout;
  776. }
  777. static struct nand_ecclayout *brcmstb_choose_ecc_layout(
  778. struct brcmnand_host *host)
  779. {
  780. struct nand_ecclayout *layout;
  781. struct brcmnand_cfg *p = &host->hwcfg;
  782. unsigned int ecc_level = p->ecc_level;
  783. if (p->sector_size_1k)
  784. ecc_level <<= 1;
  785. layout = brcmnand_create_layout(ecc_level, host);
  786. if (!layout) {
  787. dev_err(&host->pdev->dev,
  788. "no proper ecc_layout for this NAND cfg\n");
  789. return NULL;
  790. }
  791. return layout;
  792. }
  793. static void brcmnand_wp(struct mtd_info *mtd, int wp)
  794. {
  795. struct nand_chip *chip = mtd->priv;
  796. struct brcmnand_host *host = chip->priv;
  797. struct brcmnand_controller *ctrl = host->ctrl;
  798. if ((ctrl->features & BRCMNAND_HAS_WP) && wp_on == 1) {
  799. static int old_wp = -1;
  800. if (old_wp != wp) {
  801. dev_dbg(ctrl->dev, "WP %s\n", wp ? "on" : "off");
  802. old_wp = wp;
  803. }
  804. brcmnand_set_wp(ctrl, wp);
  805. }
  806. }
  807. /* Helper functions for reading and writing OOB registers */
  808. static inline u8 oob_reg_read(struct brcmnand_controller *ctrl, u32 offs)
  809. {
  810. u16 offset0, offset10, reg_offs;
  811. offset0 = ctrl->reg_offsets[BRCMNAND_OOB_READ_BASE];
  812. offset10 = ctrl->reg_offsets[BRCMNAND_OOB_READ_10_BASE];
  813. if (offs >= ctrl->max_oob)
  814. return 0x77;
  815. if (offs >= 16 && offset10)
  816. reg_offs = offset10 + ((offs - 0x10) & ~0x03);
  817. else
  818. reg_offs = offset0 + (offs & ~0x03);
  819. return nand_readreg(ctrl, reg_offs) >> (24 - ((offs & 0x03) << 3));
  820. }
  821. static inline void oob_reg_write(struct brcmnand_controller *ctrl, u32 offs,
  822. u32 data)
  823. {
  824. u16 offset0, offset10, reg_offs;
  825. offset0 = ctrl->reg_offsets[BRCMNAND_OOB_WRITE_BASE];
  826. offset10 = ctrl->reg_offsets[BRCMNAND_OOB_WRITE_10_BASE];
  827. if (offs >= ctrl->max_oob)
  828. return;
  829. if (offs >= 16 && offset10)
  830. reg_offs = offset10 + ((offs - 0x10) & ~0x03);
  831. else
  832. reg_offs = offset0 + (offs & ~0x03);
  833. nand_writereg(ctrl, reg_offs, data);
  834. }
  835. /*
  836. * read_oob_from_regs - read data from OOB registers
  837. * @ctrl: NAND controller
  838. * @i: sub-page sector index
  839. * @oob: buffer to read to
  840. * @sas: spare area sector size (i.e., OOB size per FLASH_CACHE)
  841. * @sector_1k: 1 for 1KiB sectors, 0 for 512B, other values are illegal
  842. */
  843. static int read_oob_from_regs(struct brcmnand_controller *ctrl, int i, u8 *oob,
  844. int sas, int sector_1k)
  845. {
  846. int tbytes = sas << sector_1k;
  847. int j;
  848. /* Adjust OOB values for 1K sector size */
  849. if (sector_1k && (i & 0x01))
  850. tbytes = max(0, tbytes - (int)ctrl->max_oob);
  851. tbytes = min_t(int, tbytes, ctrl->max_oob);
  852. for (j = 0; j < tbytes; j++)
  853. oob[j] = oob_reg_read(ctrl, j);
  854. return tbytes;
  855. }
  856. /*
  857. * write_oob_to_regs - write data to OOB registers
  858. * @i: sub-page sector index
  859. * @oob: buffer to write from
  860. * @sas: spare area sector size (i.e., OOB size per FLASH_CACHE)
  861. * @sector_1k: 1 for 1KiB sectors, 0 for 512B, other values are illegal
  862. */
  863. static int write_oob_to_regs(struct brcmnand_controller *ctrl, int i,
  864. const u8 *oob, int sas, int sector_1k)
  865. {
  866. int tbytes = sas << sector_1k;
  867. int j;
  868. /* Adjust OOB values for 1K sector size */
  869. if (sector_1k && (i & 0x01))
  870. tbytes = max(0, tbytes - (int)ctrl->max_oob);
  871. tbytes = min_t(int, tbytes, ctrl->max_oob);
  872. for (j = 0; j < tbytes; j += 4)
  873. oob_reg_write(ctrl, j,
  874. (oob[j + 0] << 24) |
  875. (oob[j + 1] << 16) |
  876. (oob[j + 2] << 8) |
  877. (oob[j + 3] << 0));
  878. return tbytes;
  879. }
  880. static irqreturn_t brcmnand_ctlrdy_irq(int irq, void *data)
  881. {
  882. struct brcmnand_controller *ctrl = data;
  883. /* Discard all NAND_CTLRDY interrupts during DMA */
  884. if (ctrl->dma_pending)
  885. return IRQ_HANDLED;
  886. complete(&ctrl->done);
  887. return IRQ_HANDLED;
  888. }
  889. /* Handle SoC-specific interrupt hardware */
  890. static irqreturn_t brcmnand_irq(int irq, void *data)
  891. {
  892. struct brcmnand_controller *ctrl = data;
  893. if (ctrl->soc->ctlrdy_ack(ctrl->soc))
  894. return brcmnand_ctlrdy_irq(irq, data);
  895. return IRQ_NONE;
  896. }
  897. static irqreturn_t brcmnand_dma_irq(int irq, void *data)
  898. {
  899. struct brcmnand_controller *ctrl = data;
  900. complete(&ctrl->dma_done);
  901. return IRQ_HANDLED;
  902. }
  903. static void brcmnand_send_cmd(struct brcmnand_host *host, int cmd)
  904. {
  905. struct brcmnand_controller *ctrl = host->ctrl;
  906. u32 intfc;
  907. dev_dbg(ctrl->dev, "send native cmd %d addr_lo 0x%x\n", cmd,
  908. brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS));
  909. BUG_ON(ctrl->cmd_pending != 0);
  910. ctrl->cmd_pending = cmd;
  911. intfc = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS);
  912. BUG_ON(!(intfc & INTFC_CTLR_READY));
  913. mb(); /* flush previous writes */
  914. brcmnand_write_reg(ctrl, BRCMNAND_CMD_START,
  915. cmd << brcmnand_cmd_shift(ctrl));
  916. }
  917. /***********************************************************************
  918. * NAND MTD API: read/program/erase
  919. ***********************************************************************/
  920. static void brcmnand_cmd_ctrl(struct mtd_info *mtd, int dat,
  921. unsigned int ctrl)
  922. {
  923. /* intentionally left blank */
  924. }
  925. static int brcmnand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
  926. {
  927. struct nand_chip *chip = mtd->priv;
  928. struct brcmnand_host *host = chip->priv;
  929. struct brcmnand_controller *ctrl = host->ctrl;
  930. unsigned long timeo = msecs_to_jiffies(100);
  931. dev_dbg(ctrl->dev, "wait on native cmd %d\n", ctrl->cmd_pending);
  932. if (ctrl->cmd_pending &&
  933. wait_for_completion_timeout(&ctrl->done, timeo) <= 0) {
  934. u32 cmd = brcmnand_read_reg(ctrl, BRCMNAND_CMD_START)
  935. >> brcmnand_cmd_shift(ctrl);
  936. dev_err_ratelimited(ctrl->dev,
  937. "timeout waiting for command %#02x\n", cmd);
  938. dev_err_ratelimited(ctrl->dev, "intfc status %08x\n",
  939. brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS));
  940. }
  941. ctrl->cmd_pending = 0;
  942. return brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS) &
  943. INTFC_FLASH_STATUS;
  944. }
  945. enum {
  946. LLOP_RE = BIT(16),
  947. LLOP_WE = BIT(17),
  948. LLOP_ALE = BIT(18),
  949. LLOP_CLE = BIT(19),
  950. LLOP_RETURN_IDLE = BIT(31),
  951. LLOP_DATA_MASK = GENMASK(15, 0),
  952. };
  953. static int brcmnand_low_level_op(struct brcmnand_host *host,
  954. enum brcmnand_llop_type type, u32 data,
  955. bool last_op)
  956. {
  957. struct mtd_info *mtd = &host->mtd;
  958. struct nand_chip *chip = &host->chip;
  959. struct brcmnand_controller *ctrl = host->ctrl;
  960. u32 tmp;
  961. tmp = data & LLOP_DATA_MASK;
  962. switch (type) {
  963. case LL_OP_CMD:
  964. tmp |= LLOP_WE | LLOP_CLE;
  965. break;
  966. case LL_OP_ADDR:
  967. /* WE | ALE */
  968. tmp |= LLOP_WE | LLOP_ALE;
  969. break;
  970. case LL_OP_WR:
  971. /* WE */
  972. tmp |= LLOP_WE;
  973. break;
  974. case LL_OP_RD:
  975. /* RE */
  976. tmp |= LLOP_RE;
  977. break;
  978. }
  979. if (last_op)
  980. /* RETURN_IDLE */
  981. tmp |= LLOP_RETURN_IDLE;
  982. dev_dbg(ctrl->dev, "ll_op cmd %#x\n", tmp);
  983. brcmnand_write_reg(ctrl, BRCMNAND_LL_OP, tmp);
  984. (void)brcmnand_read_reg(ctrl, BRCMNAND_LL_OP);
  985. brcmnand_send_cmd(host, CMD_LOW_LEVEL_OP);
  986. return brcmnand_waitfunc(mtd, chip);
  987. }
  988. static void brcmnand_cmdfunc(struct mtd_info *mtd, unsigned command,
  989. int column, int page_addr)
  990. {
  991. struct nand_chip *chip = mtd->priv;
  992. struct brcmnand_host *host = chip->priv;
  993. struct brcmnand_controller *ctrl = host->ctrl;
  994. u64 addr = (u64)page_addr << chip->page_shift;
  995. int native_cmd = 0;
  996. if (command == NAND_CMD_READID || command == NAND_CMD_PARAM ||
  997. command == NAND_CMD_RNDOUT)
  998. addr = (u64)column;
  999. /* Avoid propagating a negative, don't-care address */
  1000. else if (page_addr < 0)
  1001. addr = 0;
  1002. dev_dbg(ctrl->dev, "cmd 0x%x addr 0x%llx\n", command,
  1003. (unsigned long long)addr);
  1004. host->last_cmd = command;
  1005. host->last_byte = 0;
  1006. host->last_addr = addr;
  1007. switch (command) {
  1008. case NAND_CMD_RESET:
  1009. native_cmd = CMD_FLASH_RESET;
  1010. break;
  1011. case NAND_CMD_STATUS:
  1012. native_cmd = CMD_STATUS_READ;
  1013. break;
  1014. case NAND_CMD_READID:
  1015. native_cmd = CMD_DEVICE_ID_READ;
  1016. break;
  1017. case NAND_CMD_READOOB:
  1018. native_cmd = CMD_SPARE_AREA_READ;
  1019. break;
  1020. case NAND_CMD_ERASE1:
  1021. native_cmd = CMD_BLOCK_ERASE;
  1022. brcmnand_wp(mtd, 0);
  1023. break;
  1024. case NAND_CMD_PARAM:
  1025. native_cmd = CMD_PARAMETER_READ;
  1026. break;
  1027. case NAND_CMD_SET_FEATURES:
  1028. case NAND_CMD_GET_FEATURES:
  1029. brcmnand_low_level_op(host, LL_OP_CMD, command, false);
  1030. brcmnand_low_level_op(host, LL_OP_ADDR, column, false);
  1031. break;
  1032. case NAND_CMD_RNDOUT:
  1033. native_cmd = CMD_PARAMETER_CHANGE_COL;
  1034. addr &= ~((u64)(FC_BYTES - 1));
  1035. /*
  1036. * HW quirk: PARAMETER_CHANGE_COL requires SECTOR_SIZE_1K=0
  1037. * NB: hwcfg.sector_size_1k may not be initialized yet
  1038. */
  1039. if (brcmnand_get_sector_size_1k(host)) {
  1040. host->hwcfg.sector_size_1k =
  1041. brcmnand_get_sector_size_1k(host);
  1042. brcmnand_set_sector_size_1k(host, 0);
  1043. }
  1044. break;
  1045. }
  1046. if (!native_cmd)
  1047. return;
  1048. brcmnand_write_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS,
  1049. (host->cs << 16) | ((addr >> 32) & 0xffff));
  1050. (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS);
  1051. brcmnand_write_reg(ctrl, BRCMNAND_CMD_ADDRESS, lower_32_bits(addr));
  1052. (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
  1053. brcmnand_send_cmd(host, native_cmd);
  1054. brcmnand_waitfunc(mtd, chip);
  1055. if (native_cmd == CMD_PARAMETER_READ ||
  1056. native_cmd == CMD_PARAMETER_CHANGE_COL) {
  1057. int i;
  1058. brcmnand_soc_data_bus_prepare(ctrl->soc);
  1059. /*
  1060. * Must cache the FLASH_CACHE now, since changes in
  1061. * SECTOR_SIZE_1K may invalidate it
  1062. */
  1063. for (i = 0; i < FC_WORDS; i++)
  1064. ctrl->flash_cache[i] = brcmnand_read_fc(ctrl, i);
  1065. brcmnand_soc_data_bus_unprepare(ctrl->soc);
  1066. /* Cleanup from HW quirk: restore SECTOR_SIZE_1K */
  1067. if (host->hwcfg.sector_size_1k)
  1068. brcmnand_set_sector_size_1k(host,
  1069. host->hwcfg.sector_size_1k);
  1070. }
  1071. /* Re-enable protection is necessary only after erase */
  1072. if (command == NAND_CMD_ERASE1)
  1073. brcmnand_wp(mtd, 1);
  1074. }
  1075. static uint8_t brcmnand_read_byte(struct mtd_info *mtd)
  1076. {
  1077. struct nand_chip *chip = mtd->priv;
  1078. struct brcmnand_host *host = chip->priv;
  1079. struct brcmnand_controller *ctrl = host->ctrl;
  1080. uint8_t ret = 0;
  1081. int addr, offs;
  1082. switch (host->last_cmd) {
  1083. case NAND_CMD_READID:
  1084. if (host->last_byte < 4)
  1085. ret = brcmnand_read_reg(ctrl, BRCMNAND_ID) >>
  1086. (24 - (host->last_byte << 3));
  1087. else if (host->last_byte < 8)
  1088. ret = brcmnand_read_reg(ctrl, BRCMNAND_ID_EXT) >>
  1089. (56 - (host->last_byte << 3));
  1090. break;
  1091. case NAND_CMD_READOOB:
  1092. ret = oob_reg_read(ctrl, host->last_byte);
  1093. break;
  1094. case NAND_CMD_STATUS:
  1095. ret = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS) &
  1096. INTFC_FLASH_STATUS;
  1097. if (wp_on) /* hide WP status */
  1098. ret |= NAND_STATUS_WP;
  1099. break;
  1100. case NAND_CMD_PARAM:
  1101. case NAND_CMD_RNDOUT:
  1102. addr = host->last_addr + host->last_byte;
  1103. offs = addr & (FC_BYTES - 1);
  1104. /* At FC_BYTES boundary, switch to next column */
  1105. if (host->last_byte > 0 && offs == 0)
  1106. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, addr, -1);
  1107. ret = ctrl->flash_cache[offs >> 2] >>
  1108. (24 - ((offs & 0x03) << 3));
  1109. break;
  1110. case NAND_CMD_GET_FEATURES:
  1111. if (host->last_byte >= ONFI_SUBFEATURE_PARAM_LEN) {
  1112. ret = 0;
  1113. } else {
  1114. bool last = host->last_byte ==
  1115. ONFI_SUBFEATURE_PARAM_LEN - 1;
  1116. brcmnand_low_level_op(host, LL_OP_RD, 0, last);
  1117. ret = brcmnand_read_reg(ctrl, BRCMNAND_LL_RDATA) & 0xff;
  1118. }
  1119. }
  1120. dev_dbg(ctrl->dev, "read byte = 0x%02x\n", ret);
  1121. host->last_byte++;
  1122. return ret;
  1123. }
  1124. static void brcmnand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  1125. {
  1126. int i;
  1127. for (i = 0; i < len; i++, buf++)
  1128. *buf = brcmnand_read_byte(mtd);
  1129. }
  1130. static void brcmnand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
  1131. int len)
  1132. {
  1133. int i;
  1134. struct nand_chip *chip = mtd->priv;
  1135. struct brcmnand_host *host = chip->priv;
  1136. switch (host->last_cmd) {
  1137. case NAND_CMD_SET_FEATURES:
  1138. for (i = 0; i < len; i++)
  1139. brcmnand_low_level_op(host, LL_OP_WR, buf[i],
  1140. (i + 1) == len);
  1141. break;
  1142. default:
  1143. BUG();
  1144. break;
  1145. }
  1146. }
  1147. /**
  1148. * Construct a FLASH_DMA descriptor as part of a linked list. You must know the
  1149. * following ahead of time:
  1150. * - Is this descriptor the beginning or end of a linked list?
  1151. * - What is the (DMA) address of the next descriptor in the linked list?
  1152. */
  1153. static int brcmnand_fill_dma_desc(struct brcmnand_host *host,
  1154. struct brcm_nand_dma_desc *desc, u64 addr,
  1155. dma_addr_t buf, u32 len, u8 dma_cmd,
  1156. bool begin, bool end,
  1157. dma_addr_t next_desc)
  1158. {
  1159. memset(desc, 0, sizeof(*desc));
  1160. /* Descriptors are written in native byte order (wordwise) */
  1161. desc->next_desc = lower_32_bits(next_desc);
  1162. desc->next_desc_ext = upper_32_bits(next_desc);
  1163. desc->cmd_irq = (dma_cmd << 24) |
  1164. (end ? (0x03 << 8) : 0) | /* IRQ | STOP */
  1165. (!!begin) | ((!!end) << 1); /* head, tail */
  1166. #ifdef CONFIG_CPU_BIG_ENDIAN
  1167. desc->cmd_irq |= 0x01 << 12;
  1168. #endif
  1169. desc->dram_addr = lower_32_bits(buf);
  1170. desc->dram_addr_ext = upper_32_bits(buf);
  1171. desc->tfr_len = len;
  1172. desc->total_len = len;
  1173. desc->flash_addr = lower_32_bits(addr);
  1174. desc->flash_addr_ext = upper_32_bits(addr);
  1175. desc->cs = host->cs;
  1176. desc->status_valid = 0x01;
  1177. return 0;
  1178. }
  1179. /**
  1180. * Kick the FLASH_DMA engine, with a given DMA descriptor
  1181. */
  1182. static void brcmnand_dma_run(struct brcmnand_host *host, dma_addr_t desc)
  1183. {
  1184. struct brcmnand_controller *ctrl = host->ctrl;
  1185. unsigned long timeo = msecs_to_jiffies(100);
  1186. flash_dma_writel(ctrl, FLASH_DMA_FIRST_DESC, lower_32_bits(desc));
  1187. (void)flash_dma_readl(ctrl, FLASH_DMA_FIRST_DESC);
  1188. flash_dma_writel(ctrl, FLASH_DMA_FIRST_DESC_EXT, upper_32_bits(desc));
  1189. (void)flash_dma_readl(ctrl, FLASH_DMA_FIRST_DESC_EXT);
  1190. /* Start FLASH_DMA engine */
  1191. ctrl->dma_pending = true;
  1192. mb(); /* flush previous writes */
  1193. flash_dma_writel(ctrl, FLASH_DMA_CTRL, 0x03); /* wake | run */
  1194. if (wait_for_completion_timeout(&ctrl->dma_done, timeo) <= 0) {
  1195. dev_err(ctrl->dev,
  1196. "timeout waiting for DMA; status %#x, error status %#x\n",
  1197. flash_dma_readl(ctrl, FLASH_DMA_STATUS),
  1198. flash_dma_readl(ctrl, FLASH_DMA_ERROR_STATUS));
  1199. }
  1200. ctrl->dma_pending = false;
  1201. flash_dma_writel(ctrl, FLASH_DMA_CTRL, 0); /* force stop */
  1202. }
  1203. static int brcmnand_dma_trans(struct brcmnand_host *host, u64 addr, u32 *buf,
  1204. u32 len, u8 dma_cmd)
  1205. {
  1206. struct brcmnand_controller *ctrl = host->ctrl;
  1207. dma_addr_t buf_pa;
  1208. int dir = dma_cmd == CMD_PAGE_READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1209. buf_pa = dma_map_single(ctrl->dev, buf, len, dir);
  1210. if (dma_mapping_error(ctrl->dev, buf_pa)) {
  1211. dev_err(ctrl->dev, "unable to map buffer for DMA\n");
  1212. return -ENOMEM;
  1213. }
  1214. brcmnand_fill_dma_desc(host, ctrl->dma_desc, addr, buf_pa, len,
  1215. dma_cmd, true, true, 0);
  1216. brcmnand_dma_run(host, ctrl->dma_pa);
  1217. dma_unmap_single(ctrl->dev, buf_pa, len, dir);
  1218. if (ctrl->dma_desc->status_valid & FLASH_DMA_ECC_ERROR)
  1219. return -EBADMSG;
  1220. else if (ctrl->dma_desc->status_valid & FLASH_DMA_CORR_ERROR)
  1221. return -EUCLEAN;
  1222. return 0;
  1223. }
  1224. /*
  1225. * Assumes proper CS is already set
  1226. */
  1227. static int brcmnand_read_by_pio(struct mtd_info *mtd, struct nand_chip *chip,
  1228. u64 addr, unsigned int trans, u32 *buf,
  1229. u8 *oob, u64 *err_addr)
  1230. {
  1231. struct brcmnand_host *host = chip->priv;
  1232. struct brcmnand_controller *ctrl = host->ctrl;
  1233. int i, j, ret = 0;
  1234. /* Clear error addresses */
  1235. brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_ADDR, 0);
  1236. brcmnand_write_reg(ctrl, BRCMNAND_CORR_ADDR, 0);
  1237. brcmnand_write_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS,
  1238. (host->cs << 16) | ((addr >> 32) & 0xffff));
  1239. (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS);
  1240. for (i = 0; i < trans; i++, addr += FC_BYTES) {
  1241. brcmnand_write_reg(ctrl, BRCMNAND_CMD_ADDRESS,
  1242. lower_32_bits(addr));
  1243. (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
  1244. /* SPARE_AREA_READ does not use ECC, so just use PAGE_READ */
  1245. brcmnand_send_cmd(host, CMD_PAGE_READ);
  1246. brcmnand_waitfunc(mtd, chip);
  1247. if (likely(buf)) {
  1248. brcmnand_soc_data_bus_prepare(ctrl->soc);
  1249. for (j = 0; j < FC_WORDS; j++, buf++)
  1250. *buf = brcmnand_read_fc(ctrl, j);
  1251. brcmnand_soc_data_bus_unprepare(ctrl->soc);
  1252. }
  1253. if (oob)
  1254. oob += read_oob_from_regs(ctrl, i, oob,
  1255. mtd->oobsize / trans,
  1256. host->hwcfg.sector_size_1k);
  1257. if (!ret) {
  1258. *err_addr = brcmnand_read_reg(ctrl,
  1259. BRCMNAND_UNCORR_ADDR) |
  1260. ((u64)(brcmnand_read_reg(ctrl,
  1261. BRCMNAND_UNCORR_EXT_ADDR)
  1262. & 0xffff) << 32);
  1263. if (*err_addr)
  1264. ret = -EBADMSG;
  1265. }
  1266. if (!ret) {
  1267. *err_addr = brcmnand_read_reg(ctrl,
  1268. BRCMNAND_CORR_ADDR) |
  1269. ((u64)(brcmnand_read_reg(ctrl,
  1270. BRCMNAND_CORR_EXT_ADDR)
  1271. & 0xffff) << 32);
  1272. if (*err_addr)
  1273. ret = -EUCLEAN;
  1274. }
  1275. }
  1276. return ret;
  1277. }
  1278. static int brcmnand_read(struct mtd_info *mtd, struct nand_chip *chip,
  1279. u64 addr, unsigned int trans, u32 *buf, u8 *oob)
  1280. {
  1281. struct brcmnand_host *host = chip->priv;
  1282. struct brcmnand_controller *ctrl = host->ctrl;
  1283. u64 err_addr = 0;
  1284. int err;
  1285. dev_dbg(ctrl->dev, "read %llx -> %p\n", (unsigned long long)addr, buf);
  1286. brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_COUNT, 0);
  1287. if (has_flash_dma(ctrl) && !oob && flash_dma_buf_ok(buf)) {
  1288. err = brcmnand_dma_trans(host, addr, buf, trans * FC_BYTES,
  1289. CMD_PAGE_READ);
  1290. if (err) {
  1291. if (mtd_is_bitflip_or_eccerr(err))
  1292. err_addr = addr;
  1293. else
  1294. return -EIO;
  1295. }
  1296. } else {
  1297. if (oob)
  1298. memset(oob, 0x99, mtd->oobsize);
  1299. err = brcmnand_read_by_pio(mtd, chip, addr, trans, buf,
  1300. oob, &err_addr);
  1301. }
  1302. if (mtd_is_eccerr(err)) {
  1303. dev_dbg(ctrl->dev, "uncorrectable error at 0x%llx\n",
  1304. (unsigned long long)err_addr);
  1305. mtd->ecc_stats.failed++;
  1306. /* NAND layer expects zero on ECC errors */
  1307. return 0;
  1308. }
  1309. if (mtd_is_bitflip(err)) {
  1310. unsigned int corrected = brcmnand_count_corrected(ctrl);
  1311. dev_dbg(ctrl->dev, "corrected error at 0x%llx\n",
  1312. (unsigned long long)err_addr);
  1313. mtd->ecc_stats.corrected += corrected;
  1314. /* Always exceed the software-imposed threshold */
  1315. return max(mtd->bitflip_threshold, corrected);
  1316. }
  1317. return 0;
  1318. }
  1319. static int brcmnand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  1320. uint8_t *buf, int oob_required, int page)
  1321. {
  1322. struct brcmnand_host *host = chip->priv;
  1323. u8 *oob = oob_required ? (u8 *)chip->oob_poi : NULL;
  1324. return brcmnand_read(mtd, chip, host->last_addr,
  1325. mtd->writesize >> FC_SHIFT, (u32 *)buf, oob);
  1326. }
  1327. static int brcmnand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1328. uint8_t *buf, int oob_required, int page)
  1329. {
  1330. struct brcmnand_host *host = chip->priv;
  1331. u8 *oob = oob_required ? (u8 *)chip->oob_poi : NULL;
  1332. int ret;
  1333. brcmnand_set_ecc_enabled(host, 0);
  1334. ret = brcmnand_read(mtd, chip, host->last_addr,
  1335. mtd->writesize >> FC_SHIFT, (u32 *)buf, oob);
  1336. brcmnand_set_ecc_enabled(host, 1);
  1337. return ret;
  1338. }
  1339. static int brcmnand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  1340. int page)
  1341. {
  1342. return brcmnand_read(mtd, chip, (u64)page << chip->page_shift,
  1343. mtd->writesize >> FC_SHIFT,
  1344. NULL, (u8 *)chip->oob_poi);
  1345. }
  1346. static int brcmnand_read_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1347. int page)
  1348. {
  1349. struct brcmnand_host *host = chip->priv;
  1350. brcmnand_set_ecc_enabled(host, 0);
  1351. brcmnand_read(mtd, chip, (u64)page << chip->page_shift,
  1352. mtd->writesize >> FC_SHIFT,
  1353. NULL, (u8 *)chip->oob_poi);
  1354. brcmnand_set_ecc_enabled(host, 1);
  1355. return 0;
  1356. }
  1357. static int brcmnand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
  1358. uint32_t data_offs, uint32_t readlen,
  1359. uint8_t *bufpoi, int page)
  1360. {
  1361. struct brcmnand_host *host = chip->priv;
  1362. return brcmnand_read(mtd, chip, host->last_addr + data_offs,
  1363. readlen >> FC_SHIFT, (u32 *)bufpoi, NULL);
  1364. }
  1365. static int brcmnand_write(struct mtd_info *mtd, struct nand_chip *chip,
  1366. u64 addr, const u32 *buf, u8 *oob)
  1367. {
  1368. struct brcmnand_host *host = chip->priv;
  1369. struct brcmnand_controller *ctrl = host->ctrl;
  1370. unsigned int i, j, trans = mtd->writesize >> FC_SHIFT;
  1371. int status, ret = 0;
  1372. dev_dbg(ctrl->dev, "write %llx <- %p\n", (unsigned long long)addr, buf);
  1373. if (unlikely((unsigned long)buf & 0x03)) {
  1374. dev_warn(ctrl->dev, "unaligned buffer: %p\n", buf);
  1375. buf = (u32 *)((unsigned long)buf & ~0x03);
  1376. }
  1377. brcmnand_wp(mtd, 0);
  1378. for (i = 0; i < ctrl->max_oob; i += 4)
  1379. oob_reg_write(ctrl, i, 0xffffffff);
  1380. if (has_flash_dma(ctrl) && !oob && flash_dma_buf_ok(buf)) {
  1381. if (brcmnand_dma_trans(host, addr, (u32 *)buf,
  1382. mtd->writesize, CMD_PROGRAM_PAGE))
  1383. ret = -EIO;
  1384. goto out;
  1385. }
  1386. brcmnand_write_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS,
  1387. (host->cs << 16) | ((addr >> 32) & 0xffff));
  1388. (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS);
  1389. for (i = 0; i < trans; i++, addr += FC_BYTES) {
  1390. /* full address MUST be set before populating FC */
  1391. brcmnand_write_reg(ctrl, BRCMNAND_CMD_ADDRESS,
  1392. lower_32_bits(addr));
  1393. (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
  1394. if (buf) {
  1395. brcmnand_soc_data_bus_prepare(ctrl->soc);
  1396. for (j = 0; j < FC_WORDS; j++, buf++)
  1397. brcmnand_write_fc(ctrl, j, *buf);
  1398. brcmnand_soc_data_bus_unprepare(ctrl->soc);
  1399. } else if (oob) {
  1400. for (j = 0; j < FC_WORDS; j++)
  1401. brcmnand_write_fc(ctrl, j, 0xffffffff);
  1402. }
  1403. if (oob) {
  1404. oob += write_oob_to_regs(ctrl, i, oob,
  1405. mtd->oobsize / trans,
  1406. host->hwcfg.sector_size_1k);
  1407. }
  1408. /* we cannot use SPARE_AREA_PROGRAM when PARTIAL_PAGE_EN=0 */
  1409. brcmnand_send_cmd(host, CMD_PROGRAM_PAGE);
  1410. status = brcmnand_waitfunc(mtd, chip);
  1411. if (status & NAND_STATUS_FAIL) {
  1412. dev_info(ctrl->dev, "program failed at %llx\n",
  1413. (unsigned long long)addr);
  1414. ret = -EIO;
  1415. goto out;
  1416. }
  1417. }
  1418. out:
  1419. brcmnand_wp(mtd, 1);
  1420. return ret;
  1421. }
  1422. static int brcmnand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1423. const uint8_t *buf, int oob_required, int page)
  1424. {
  1425. struct brcmnand_host *host = chip->priv;
  1426. void *oob = oob_required ? chip->oob_poi : NULL;
  1427. brcmnand_write(mtd, chip, host->last_addr, (const u32 *)buf, oob);
  1428. return 0;
  1429. }
  1430. static int brcmnand_write_page_raw(struct mtd_info *mtd,
  1431. struct nand_chip *chip, const uint8_t *buf,
  1432. int oob_required, int page)
  1433. {
  1434. struct brcmnand_host *host = chip->priv;
  1435. void *oob = oob_required ? chip->oob_poi : NULL;
  1436. brcmnand_set_ecc_enabled(host, 0);
  1437. brcmnand_write(mtd, chip, host->last_addr, (const u32 *)buf, oob);
  1438. brcmnand_set_ecc_enabled(host, 1);
  1439. return 0;
  1440. }
  1441. static int brcmnand_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  1442. int page)
  1443. {
  1444. return brcmnand_write(mtd, chip, (u64)page << chip->page_shift,
  1445. NULL, chip->oob_poi);
  1446. }
  1447. static int brcmnand_write_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1448. int page)
  1449. {
  1450. struct brcmnand_host *host = chip->priv;
  1451. int ret;
  1452. brcmnand_set_ecc_enabled(host, 0);
  1453. ret = brcmnand_write(mtd, chip, (u64)page << chip->page_shift, NULL,
  1454. (u8 *)chip->oob_poi);
  1455. brcmnand_set_ecc_enabled(host, 1);
  1456. return ret;
  1457. }
  1458. /***********************************************************************
  1459. * Per-CS setup (1 NAND device)
  1460. ***********************************************************************/
  1461. static int brcmnand_set_cfg(struct brcmnand_host *host,
  1462. struct brcmnand_cfg *cfg)
  1463. {
  1464. struct brcmnand_controller *ctrl = host->ctrl;
  1465. struct nand_chip *chip = &host->chip;
  1466. u16 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
  1467. u16 cfg_ext_offs = brcmnand_cs_offset(ctrl, host->cs,
  1468. BRCMNAND_CS_CFG_EXT);
  1469. u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
  1470. BRCMNAND_CS_ACC_CONTROL);
  1471. u8 block_size = 0, page_size = 0, device_size = 0;
  1472. u32 tmp;
  1473. if (ctrl->block_sizes) {
  1474. int i, found;
  1475. for (i = 0, found = 0; ctrl->block_sizes[i]; i++)
  1476. if (ctrl->block_sizes[i] * 1024 == cfg->block_size) {
  1477. block_size = i;
  1478. found = 1;
  1479. }
  1480. if (!found) {
  1481. dev_warn(ctrl->dev, "invalid block size %u\n",
  1482. cfg->block_size);
  1483. return -EINVAL;
  1484. }
  1485. } else {
  1486. block_size = ffs(cfg->block_size) - ffs(BRCMNAND_MIN_BLOCKSIZE);
  1487. }
  1488. if (cfg->block_size < BRCMNAND_MIN_BLOCKSIZE || (ctrl->max_block_size &&
  1489. cfg->block_size > ctrl->max_block_size)) {
  1490. dev_warn(ctrl->dev, "invalid block size %u\n",
  1491. cfg->block_size);
  1492. block_size = 0;
  1493. }
  1494. if (ctrl->page_sizes) {
  1495. int i, found;
  1496. for (i = 0, found = 0; ctrl->page_sizes[i]; i++)
  1497. if (ctrl->page_sizes[i] == cfg->page_size) {
  1498. page_size = i;
  1499. found = 1;
  1500. }
  1501. if (!found) {
  1502. dev_warn(ctrl->dev, "invalid page size %u\n",
  1503. cfg->page_size);
  1504. return -EINVAL;
  1505. }
  1506. } else {
  1507. page_size = ffs(cfg->page_size) - ffs(BRCMNAND_MIN_PAGESIZE);
  1508. }
  1509. if (cfg->page_size < BRCMNAND_MIN_PAGESIZE || (ctrl->max_page_size &&
  1510. cfg->page_size > ctrl->max_page_size)) {
  1511. dev_warn(ctrl->dev, "invalid page size %u\n", cfg->page_size);
  1512. return -EINVAL;
  1513. }
  1514. if (fls64(cfg->device_size) < fls64(BRCMNAND_MIN_DEVSIZE)) {
  1515. dev_warn(ctrl->dev, "invalid device size 0x%llx\n",
  1516. (unsigned long long)cfg->device_size);
  1517. return -EINVAL;
  1518. }
  1519. device_size = fls64(cfg->device_size) - fls64(BRCMNAND_MIN_DEVSIZE);
  1520. tmp = (cfg->blk_adr_bytes << CFG_BLK_ADR_BYTES_SHIFT) |
  1521. (cfg->col_adr_bytes << CFG_COL_ADR_BYTES_SHIFT) |
  1522. (cfg->ful_adr_bytes << CFG_FUL_ADR_BYTES_SHIFT) |
  1523. (!!(cfg->device_width == 16) << CFG_BUS_WIDTH_SHIFT) |
  1524. (device_size << CFG_DEVICE_SIZE_SHIFT);
  1525. if (cfg_offs == cfg_ext_offs) {
  1526. tmp |= (page_size << CFG_PAGE_SIZE_SHIFT) |
  1527. (block_size << CFG_BLK_SIZE_SHIFT);
  1528. nand_writereg(ctrl, cfg_offs, tmp);
  1529. } else {
  1530. nand_writereg(ctrl, cfg_offs, tmp);
  1531. tmp = (page_size << CFG_EXT_PAGE_SIZE_SHIFT) |
  1532. (block_size << CFG_EXT_BLK_SIZE_SHIFT);
  1533. nand_writereg(ctrl, cfg_ext_offs, tmp);
  1534. }
  1535. tmp = nand_readreg(ctrl, acc_control_offs);
  1536. tmp &= ~brcmnand_ecc_level_mask(ctrl);
  1537. tmp |= cfg->ecc_level << NAND_ACC_CONTROL_ECC_SHIFT;
  1538. tmp &= ~brcmnand_spare_area_mask(ctrl);
  1539. tmp |= cfg->spare_area_size;
  1540. nand_writereg(ctrl, acc_control_offs, tmp);
  1541. brcmnand_set_sector_size_1k(host, cfg->sector_size_1k);
  1542. /* threshold = ceil(BCH-level * 0.75) */
  1543. brcmnand_wr_corr_thresh(host, DIV_ROUND_UP(chip->ecc.strength * 3, 4));
  1544. return 0;
  1545. }
  1546. static void brcmnand_print_cfg(char *buf, struct brcmnand_cfg *cfg)
  1547. {
  1548. buf += sprintf(buf,
  1549. "%lluMiB total, %uKiB blocks, %u%s pages, %uB OOB, %u-bit",
  1550. (unsigned long long)cfg->device_size >> 20,
  1551. cfg->block_size >> 10,
  1552. cfg->page_size >= 1024 ? cfg->page_size >> 10 : cfg->page_size,
  1553. cfg->page_size >= 1024 ? "KiB" : "B",
  1554. cfg->spare_area_size, cfg->device_width);
  1555. /* Account for Hamming ECC and for BCH 512B vs 1KiB sectors */
  1556. if (is_hamming_ecc(cfg))
  1557. sprintf(buf, ", Hamming ECC");
  1558. else if (cfg->sector_size_1k)
  1559. sprintf(buf, ", BCH-%u (1KiB sector)", cfg->ecc_level << 1);
  1560. else
  1561. sprintf(buf, ", BCH-%u", cfg->ecc_level);
  1562. }
  1563. /*
  1564. * Minimum number of bytes to address a page. Calculated as:
  1565. * roundup(log2(size / page-size) / 8)
  1566. *
  1567. * NB: the following does not "round up" for non-power-of-2 'size'; but this is
  1568. * OK because many other things will break if 'size' is irregular...
  1569. */
  1570. static inline int get_blk_adr_bytes(u64 size, u32 writesize)
  1571. {
  1572. return ALIGN(ilog2(size) - ilog2(writesize), 8) >> 3;
  1573. }
  1574. static int brcmnand_setup_dev(struct brcmnand_host *host)
  1575. {
  1576. struct mtd_info *mtd = &host->mtd;
  1577. struct nand_chip *chip = &host->chip;
  1578. struct brcmnand_controller *ctrl = host->ctrl;
  1579. struct brcmnand_cfg *cfg = &host->hwcfg;
  1580. char msg[128];
  1581. u32 offs, tmp, oob_sector;
  1582. int ret;
  1583. memset(cfg, 0, sizeof(*cfg));
  1584. ret = of_property_read_u32(chip->flash_node,
  1585. "brcm,nand-oob-sector-size",
  1586. &oob_sector);
  1587. if (ret) {
  1588. /* Use detected size */
  1589. cfg->spare_area_size = mtd->oobsize /
  1590. (mtd->writesize >> FC_SHIFT);
  1591. } else {
  1592. cfg->spare_area_size = oob_sector;
  1593. }
  1594. if (cfg->spare_area_size > ctrl->max_oob)
  1595. cfg->spare_area_size = ctrl->max_oob;
  1596. /*
  1597. * Set oobsize to be consistent with controller's spare_area_size, as
  1598. * the rest is inaccessible.
  1599. */
  1600. mtd->oobsize = cfg->spare_area_size * (mtd->writesize >> FC_SHIFT);
  1601. cfg->device_size = mtd->size;
  1602. cfg->block_size = mtd->erasesize;
  1603. cfg->page_size = mtd->writesize;
  1604. cfg->device_width = (chip->options & NAND_BUSWIDTH_16) ? 16 : 8;
  1605. cfg->col_adr_bytes = 2;
  1606. cfg->blk_adr_bytes = get_blk_adr_bytes(mtd->size, mtd->writesize);
  1607. switch (chip->ecc.size) {
  1608. case 512:
  1609. if (chip->ecc.strength == 1) /* Hamming */
  1610. cfg->ecc_level = 15;
  1611. else
  1612. cfg->ecc_level = chip->ecc.strength;
  1613. cfg->sector_size_1k = 0;
  1614. break;
  1615. case 1024:
  1616. if (!(ctrl->features & BRCMNAND_HAS_1K_SECTORS)) {
  1617. dev_err(ctrl->dev, "1KB sectors not supported\n");
  1618. return -EINVAL;
  1619. }
  1620. if (chip->ecc.strength & 0x1) {
  1621. dev_err(ctrl->dev,
  1622. "odd ECC not supported with 1KB sectors\n");
  1623. return -EINVAL;
  1624. }
  1625. cfg->ecc_level = chip->ecc.strength >> 1;
  1626. cfg->sector_size_1k = 1;
  1627. break;
  1628. default:
  1629. dev_err(ctrl->dev, "unsupported ECC size: %d\n",
  1630. chip->ecc.size);
  1631. return -EINVAL;
  1632. }
  1633. cfg->ful_adr_bytes = cfg->blk_adr_bytes;
  1634. if (mtd->writesize > 512)
  1635. cfg->ful_adr_bytes += cfg->col_adr_bytes;
  1636. else
  1637. cfg->ful_adr_bytes += 1;
  1638. ret = brcmnand_set_cfg(host, cfg);
  1639. if (ret)
  1640. return ret;
  1641. brcmnand_set_ecc_enabled(host, 1);
  1642. brcmnand_print_cfg(msg, cfg);
  1643. dev_info(ctrl->dev, "detected %s\n", msg);
  1644. /* Configure ACC_CONTROL */
  1645. offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_ACC_CONTROL);
  1646. tmp = nand_readreg(ctrl, offs);
  1647. tmp &= ~ACC_CONTROL_PARTIAL_PAGE;
  1648. tmp &= ~ACC_CONTROL_RD_ERASED;
  1649. tmp &= ~ACC_CONTROL_FAST_PGM_RDIN;
  1650. if (ctrl->features & BRCMNAND_HAS_PREFETCH)
  1651. tmp &= ~ACC_CONTROL_PREFETCH;
  1652. nand_writereg(ctrl, offs, tmp);
  1653. return 0;
  1654. }
  1655. static int brcmnand_init_cs(struct brcmnand_host *host)
  1656. {
  1657. struct brcmnand_controller *ctrl = host->ctrl;
  1658. struct device_node *dn = host->of_node;
  1659. struct platform_device *pdev = host->pdev;
  1660. struct mtd_info *mtd;
  1661. struct nand_chip *chip;
  1662. int ret;
  1663. u16 cfg_offs;
  1664. struct mtd_part_parser_data ppdata = { .of_node = dn };
  1665. ret = of_property_read_u32(dn, "reg", &host->cs);
  1666. if (ret) {
  1667. dev_err(&pdev->dev, "can't get chip-select\n");
  1668. return -ENXIO;
  1669. }
  1670. mtd = &host->mtd;
  1671. chip = &host->chip;
  1672. chip->flash_node = dn;
  1673. chip->priv = host;
  1674. mtd->priv = chip;
  1675. mtd->name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "brcmnand.%d",
  1676. host->cs);
  1677. mtd->owner = THIS_MODULE;
  1678. mtd->dev.parent = &pdev->dev;
  1679. chip->IO_ADDR_R = (void __iomem *)0xdeadbeef;
  1680. chip->IO_ADDR_W = (void __iomem *)0xdeadbeef;
  1681. chip->cmd_ctrl = brcmnand_cmd_ctrl;
  1682. chip->cmdfunc = brcmnand_cmdfunc;
  1683. chip->waitfunc = brcmnand_waitfunc;
  1684. chip->read_byte = brcmnand_read_byte;
  1685. chip->read_buf = brcmnand_read_buf;
  1686. chip->write_buf = brcmnand_write_buf;
  1687. chip->ecc.mode = NAND_ECC_HW;
  1688. chip->ecc.read_page = brcmnand_read_page;
  1689. chip->ecc.read_subpage = brcmnand_read_subpage;
  1690. chip->ecc.write_page = brcmnand_write_page;
  1691. chip->ecc.read_page_raw = brcmnand_read_page_raw;
  1692. chip->ecc.write_page_raw = brcmnand_write_page_raw;
  1693. chip->ecc.write_oob_raw = brcmnand_write_oob_raw;
  1694. chip->ecc.read_oob_raw = brcmnand_read_oob_raw;
  1695. chip->ecc.read_oob = brcmnand_read_oob;
  1696. chip->ecc.write_oob = brcmnand_write_oob;
  1697. chip->controller = &ctrl->controller;
  1698. /*
  1699. * The bootloader might have configured 16bit mode but
  1700. * NAND READID command only works in 8bit mode. We force
  1701. * 8bit mode here to ensure that NAND READID commands works.
  1702. */
  1703. cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
  1704. nand_writereg(ctrl, cfg_offs,
  1705. nand_readreg(ctrl, cfg_offs) & ~CFG_BUS_WIDTH);
  1706. if (nand_scan_ident(mtd, 1, NULL))
  1707. return -ENXIO;
  1708. chip->options |= NAND_NO_SUBPAGE_WRITE;
  1709. /*
  1710. * Avoid (for instance) kmap()'d buffers from JFFS2, which we can't DMA
  1711. * to/from, and have nand_base pass us a bounce buffer instead, as
  1712. * needed.
  1713. */
  1714. chip->options |= NAND_USE_BOUNCE_BUFFER;
  1715. if (of_get_nand_on_flash_bbt(dn))
  1716. chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
  1717. if (brcmnand_setup_dev(host))
  1718. return -ENXIO;
  1719. chip->ecc.size = host->hwcfg.sector_size_1k ? 1024 : 512;
  1720. /* only use our internal HW threshold */
  1721. mtd->bitflip_threshold = 1;
  1722. chip->ecc.layout = brcmstb_choose_ecc_layout(host);
  1723. if (!chip->ecc.layout)
  1724. return -ENXIO;
  1725. if (nand_scan_tail(mtd))
  1726. return -ENXIO;
  1727. return mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
  1728. }
  1729. static void brcmnand_save_restore_cs_config(struct brcmnand_host *host,
  1730. int restore)
  1731. {
  1732. struct brcmnand_controller *ctrl = host->ctrl;
  1733. u16 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
  1734. u16 cfg_ext_offs = brcmnand_cs_offset(ctrl, host->cs,
  1735. BRCMNAND_CS_CFG_EXT);
  1736. u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
  1737. BRCMNAND_CS_ACC_CONTROL);
  1738. u16 t1_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_TIMING1);
  1739. u16 t2_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_TIMING2);
  1740. if (restore) {
  1741. nand_writereg(ctrl, cfg_offs, host->hwcfg.config);
  1742. if (cfg_offs != cfg_ext_offs)
  1743. nand_writereg(ctrl, cfg_ext_offs,
  1744. host->hwcfg.config_ext);
  1745. nand_writereg(ctrl, acc_control_offs, host->hwcfg.acc_control);
  1746. nand_writereg(ctrl, t1_offs, host->hwcfg.timing_1);
  1747. nand_writereg(ctrl, t2_offs, host->hwcfg.timing_2);
  1748. } else {
  1749. host->hwcfg.config = nand_readreg(ctrl, cfg_offs);
  1750. if (cfg_offs != cfg_ext_offs)
  1751. host->hwcfg.config_ext =
  1752. nand_readreg(ctrl, cfg_ext_offs);
  1753. host->hwcfg.acc_control = nand_readreg(ctrl, acc_control_offs);
  1754. host->hwcfg.timing_1 = nand_readreg(ctrl, t1_offs);
  1755. host->hwcfg.timing_2 = nand_readreg(ctrl, t2_offs);
  1756. }
  1757. }
  1758. static int brcmnand_suspend(struct device *dev)
  1759. {
  1760. struct brcmnand_controller *ctrl = dev_get_drvdata(dev);
  1761. struct brcmnand_host *host;
  1762. list_for_each_entry(host, &ctrl->host_list, node)
  1763. brcmnand_save_restore_cs_config(host, 0);
  1764. ctrl->nand_cs_nand_select = brcmnand_read_reg(ctrl, BRCMNAND_CS_SELECT);
  1765. ctrl->nand_cs_nand_xor = brcmnand_read_reg(ctrl, BRCMNAND_CS_XOR);
  1766. ctrl->corr_stat_threshold =
  1767. brcmnand_read_reg(ctrl, BRCMNAND_CORR_THRESHOLD);
  1768. if (has_flash_dma(ctrl))
  1769. ctrl->flash_dma_mode = flash_dma_readl(ctrl, FLASH_DMA_MODE);
  1770. return 0;
  1771. }
  1772. static int brcmnand_resume(struct device *dev)
  1773. {
  1774. struct brcmnand_controller *ctrl = dev_get_drvdata(dev);
  1775. struct brcmnand_host *host;
  1776. if (has_flash_dma(ctrl)) {
  1777. flash_dma_writel(ctrl, FLASH_DMA_MODE, ctrl->flash_dma_mode);
  1778. flash_dma_writel(ctrl, FLASH_DMA_ERROR_STATUS, 0);
  1779. }
  1780. brcmnand_write_reg(ctrl, BRCMNAND_CS_SELECT, ctrl->nand_cs_nand_select);
  1781. brcmnand_write_reg(ctrl, BRCMNAND_CS_XOR, ctrl->nand_cs_nand_xor);
  1782. brcmnand_write_reg(ctrl, BRCMNAND_CORR_THRESHOLD,
  1783. ctrl->corr_stat_threshold);
  1784. if (ctrl->soc) {
  1785. /* Clear/re-enable interrupt */
  1786. ctrl->soc->ctlrdy_ack(ctrl->soc);
  1787. ctrl->soc->ctlrdy_set_enabled(ctrl->soc, true);
  1788. }
  1789. list_for_each_entry(host, &ctrl->host_list, node) {
  1790. struct mtd_info *mtd = &host->mtd;
  1791. struct nand_chip *chip = mtd->priv;
  1792. brcmnand_save_restore_cs_config(host, 1);
  1793. /* Reset the chip, required by some chips after power-up */
  1794. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  1795. }
  1796. return 0;
  1797. }
  1798. const struct dev_pm_ops brcmnand_pm_ops = {
  1799. .suspend = brcmnand_suspend,
  1800. .resume = brcmnand_resume,
  1801. };
  1802. EXPORT_SYMBOL_GPL(brcmnand_pm_ops);
  1803. static const struct of_device_id brcmnand_of_match[] = {
  1804. { .compatible = "brcm,brcmnand-v4.0" },
  1805. { .compatible = "brcm,brcmnand-v5.0" },
  1806. { .compatible = "brcm,brcmnand-v6.0" },
  1807. { .compatible = "brcm,brcmnand-v6.1" },
  1808. { .compatible = "brcm,brcmnand-v7.0" },
  1809. { .compatible = "brcm,brcmnand-v7.1" },
  1810. {},
  1811. };
  1812. MODULE_DEVICE_TABLE(of, brcmnand_of_match);
  1813. /***********************************************************************
  1814. * Platform driver setup (per controller)
  1815. ***********************************************************************/
  1816. int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc)
  1817. {
  1818. struct device *dev = &pdev->dev;
  1819. struct device_node *dn = dev->of_node, *child;
  1820. struct brcmnand_controller *ctrl;
  1821. struct resource *res;
  1822. int ret;
  1823. /* We only support device-tree instantiation */
  1824. if (!dn)
  1825. return -ENODEV;
  1826. if (!of_match_node(brcmnand_of_match, dn))
  1827. return -ENODEV;
  1828. ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
  1829. if (!ctrl)
  1830. return -ENOMEM;
  1831. dev_set_drvdata(dev, ctrl);
  1832. ctrl->dev = dev;
  1833. init_completion(&ctrl->done);
  1834. init_completion(&ctrl->dma_done);
  1835. spin_lock_init(&ctrl->controller.lock);
  1836. init_waitqueue_head(&ctrl->controller.wq);
  1837. INIT_LIST_HEAD(&ctrl->host_list);
  1838. /* NAND register range */
  1839. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1840. ctrl->nand_base = devm_ioremap_resource(dev, res);
  1841. if (IS_ERR(ctrl->nand_base))
  1842. return PTR_ERR(ctrl->nand_base);
  1843. /* Initialize NAND revision */
  1844. ret = brcmnand_revision_init(ctrl);
  1845. if (ret)
  1846. return ret;
  1847. /*
  1848. * Most chips have this cache at a fixed offset within 'nand' block.
  1849. * Some must specify this region separately.
  1850. */
  1851. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand-cache");
  1852. if (res) {
  1853. ctrl->nand_fc = devm_ioremap_resource(dev, res);
  1854. if (IS_ERR(ctrl->nand_fc))
  1855. return PTR_ERR(ctrl->nand_fc);
  1856. } else {
  1857. ctrl->nand_fc = ctrl->nand_base +
  1858. ctrl->reg_offsets[BRCMNAND_FC_BASE];
  1859. }
  1860. /* FLASH_DMA */
  1861. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "flash-dma");
  1862. if (res) {
  1863. ctrl->flash_dma_base = devm_ioremap_resource(dev, res);
  1864. if (IS_ERR(ctrl->flash_dma_base))
  1865. return PTR_ERR(ctrl->flash_dma_base);
  1866. flash_dma_writel(ctrl, FLASH_DMA_MODE, 1); /* linked-list */
  1867. flash_dma_writel(ctrl, FLASH_DMA_ERROR_STATUS, 0);
  1868. /* Allocate descriptor(s) */
  1869. ctrl->dma_desc = dmam_alloc_coherent(dev,
  1870. sizeof(*ctrl->dma_desc),
  1871. &ctrl->dma_pa, GFP_KERNEL);
  1872. if (!ctrl->dma_desc)
  1873. return -ENOMEM;
  1874. ctrl->dma_irq = platform_get_irq(pdev, 1);
  1875. if ((int)ctrl->dma_irq < 0) {
  1876. dev_err(dev, "missing FLASH_DMA IRQ\n");
  1877. return -ENODEV;
  1878. }
  1879. ret = devm_request_irq(dev, ctrl->dma_irq,
  1880. brcmnand_dma_irq, 0, DRV_NAME,
  1881. ctrl);
  1882. if (ret < 0) {
  1883. dev_err(dev, "can't allocate IRQ %d: error %d\n",
  1884. ctrl->dma_irq, ret);
  1885. return ret;
  1886. }
  1887. dev_info(dev, "enabling FLASH_DMA\n");
  1888. }
  1889. /* Disable automatic device ID config, direct addressing */
  1890. brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT,
  1891. CS_SELECT_AUTO_DEVICE_ID_CFG | 0xff, 0, 0);
  1892. /* Disable XOR addressing */
  1893. brcmnand_rmw_reg(ctrl, BRCMNAND_CS_XOR, 0xff, 0, 0);
  1894. if (ctrl->features & BRCMNAND_HAS_WP) {
  1895. /* Permanently disable write protection */
  1896. if (wp_on == 2)
  1897. brcmnand_set_wp(ctrl, false);
  1898. } else {
  1899. wp_on = 0;
  1900. }
  1901. /* IRQ */
  1902. ctrl->irq = platform_get_irq(pdev, 0);
  1903. if ((int)ctrl->irq < 0) {
  1904. dev_err(dev, "no IRQ defined\n");
  1905. return -ENODEV;
  1906. }
  1907. /*
  1908. * Some SoCs integrate this controller (e.g., its interrupt bits) in
  1909. * interesting ways
  1910. */
  1911. if (soc) {
  1912. ctrl->soc = soc;
  1913. ret = devm_request_irq(dev, ctrl->irq, brcmnand_irq, 0,
  1914. DRV_NAME, ctrl);
  1915. /* Enable interrupt */
  1916. ctrl->soc->ctlrdy_ack(ctrl->soc);
  1917. ctrl->soc->ctlrdy_set_enabled(ctrl->soc, true);
  1918. } else {
  1919. /* Use standard interrupt infrastructure */
  1920. ret = devm_request_irq(dev, ctrl->irq, brcmnand_ctlrdy_irq, 0,
  1921. DRV_NAME, ctrl);
  1922. }
  1923. if (ret < 0) {
  1924. dev_err(dev, "can't allocate IRQ %d: error %d\n",
  1925. ctrl->irq, ret);
  1926. return ret;
  1927. }
  1928. for_each_available_child_of_node(dn, child) {
  1929. if (of_device_is_compatible(child, "brcm,nandcs")) {
  1930. struct brcmnand_host *host;
  1931. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  1932. if (!host)
  1933. return -ENOMEM;
  1934. host->pdev = pdev;
  1935. host->ctrl = ctrl;
  1936. host->of_node = child;
  1937. ret = brcmnand_init_cs(host);
  1938. if (ret)
  1939. continue; /* Try all chip-selects */
  1940. list_add_tail(&host->node, &ctrl->host_list);
  1941. }
  1942. }
  1943. /* No chip-selects could initialize properly */
  1944. if (list_empty(&ctrl->host_list))
  1945. return -ENODEV;
  1946. return 0;
  1947. }
  1948. EXPORT_SYMBOL_GPL(brcmnand_probe);
  1949. int brcmnand_remove(struct platform_device *pdev)
  1950. {
  1951. struct brcmnand_controller *ctrl = dev_get_drvdata(&pdev->dev);
  1952. struct brcmnand_host *host;
  1953. list_for_each_entry(host, &ctrl->host_list, node)
  1954. nand_release(&host->mtd);
  1955. dev_set_drvdata(&pdev->dev, NULL);
  1956. return 0;
  1957. }
  1958. EXPORT_SYMBOL_GPL(brcmnand_remove);
  1959. MODULE_LICENSE("GPL v2");
  1960. MODULE_AUTHOR("Kevin Cernekee");
  1961. MODULE_AUTHOR("Brian Norris");
  1962. MODULE_DESCRIPTION("NAND driver for Broadcom chips");
  1963. MODULE_ALIAS("platform:brcmnand");