omap-gpmc.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324
  1. /*
  2. * GPMC support functions
  3. *
  4. * Copyright (C) 2005-2006 Nokia Corporation
  5. *
  6. * Author: Juha Yrjola
  7. *
  8. * Copyright (C) 2009 Texas Instruments
  9. * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/irq.h>
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/err.h>
  19. #include <linux/clk.h>
  20. #include <linux/ioport.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/io.h>
  23. #include <linux/module.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/irqdomain.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/of.h>
  28. #include <linux/of_address.h>
  29. #include <linux/of_mtd.h>
  30. #include <linux/of_device.h>
  31. #include <linux/of_platform.h>
  32. #include <linux/omap-gpmc.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/platform_data/mtd-nand-omap2.h>
  35. #include <linux/platform_data/mtd-onenand-omap2.h>
  36. #include <asm/mach-types.h>
  37. #define DEVICE_NAME "omap-gpmc"
  38. /* GPMC register offsets */
  39. #define GPMC_REVISION 0x00
  40. #define GPMC_SYSCONFIG 0x10
  41. #define GPMC_SYSSTATUS 0x14
  42. #define GPMC_IRQSTATUS 0x18
  43. #define GPMC_IRQENABLE 0x1c
  44. #define GPMC_TIMEOUT_CONTROL 0x40
  45. #define GPMC_ERR_ADDRESS 0x44
  46. #define GPMC_ERR_TYPE 0x48
  47. #define GPMC_CONFIG 0x50
  48. #define GPMC_STATUS 0x54
  49. #define GPMC_PREFETCH_CONFIG1 0x1e0
  50. #define GPMC_PREFETCH_CONFIG2 0x1e4
  51. #define GPMC_PREFETCH_CONTROL 0x1ec
  52. #define GPMC_PREFETCH_STATUS 0x1f0
  53. #define GPMC_ECC_CONFIG 0x1f4
  54. #define GPMC_ECC_CONTROL 0x1f8
  55. #define GPMC_ECC_SIZE_CONFIG 0x1fc
  56. #define GPMC_ECC1_RESULT 0x200
  57. #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
  58. #define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */
  59. #define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */
  60. #define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */
  61. #define GPMC_ECC_BCH_RESULT_4 0x300 /* not available on OMAP2 */
  62. #define GPMC_ECC_BCH_RESULT_5 0x304 /* not available on OMAP2 */
  63. #define GPMC_ECC_BCH_RESULT_6 0x308 /* not available on OMAP2 */
  64. /* GPMC ECC control settings */
  65. #define GPMC_ECC_CTRL_ECCCLEAR 0x100
  66. #define GPMC_ECC_CTRL_ECCDISABLE 0x000
  67. #define GPMC_ECC_CTRL_ECCREG1 0x001
  68. #define GPMC_ECC_CTRL_ECCREG2 0x002
  69. #define GPMC_ECC_CTRL_ECCREG3 0x003
  70. #define GPMC_ECC_CTRL_ECCREG4 0x004
  71. #define GPMC_ECC_CTRL_ECCREG5 0x005
  72. #define GPMC_ECC_CTRL_ECCREG6 0x006
  73. #define GPMC_ECC_CTRL_ECCREG7 0x007
  74. #define GPMC_ECC_CTRL_ECCREG8 0x008
  75. #define GPMC_ECC_CTRL_ECCREG9 0x009
  76. #define GPMC_CONFIG_LIMITEDADDRESS BIT(1)
  77. #define GPMC_STATUS_EMPTYWRITEBUFFERSTATUS BIT(0)
  78. #define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
  79. #define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
  80. #define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
  81. #define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
  82. #define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
  83. #define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
  84. #define GPMC_CS0_OFFSET 0x60
  85. #define GPMC_CS_SIZE 0x30
  86. #define GPMC_BCH_SIZE 0x10
  87. #define GPMC_MEM_END 0x3FFFFFFF
  88. #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
  89. #define GPMC_SECTION_SHIFT 28 /* 128 MB */
  90. #define CS_NUM_SHIFT 24
  91. #define ENABLE_PREFETCH (0x1 << 7)
  92. #define DMA_MPU_MODE 2
  93. #define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
  94. #define GPMC_REVISION_MINOR(l) (l & 0xf)
  95. #define GPMC_HAS_WR_ACCESS 0x1
  96. #define GPMC_HAS_WR_DATA_MUX_BUS 0x2
  97. #define GPMC_HAS_MUX_AAD 0x4
  98. #define GPMC_NR_WAITPINS 4
  99. #define GPMC_CS_CONFIG1 0x00
  100. #define GPMC_CS_CONFIG2 0x04
  101. #define GPMC_CS_CONFIG3 0x08
  102. #define GPMC_CS_CONFIG4 0x0c
  103. #define GPMC_CS_CONFIG5 0x10
  104. #define GPMC_CS_CONFIG6 0x14
  105. #define GPMC_CS_CONFIG7 0x18
  106. #define GPMC_CS_NAND_COMMAND 0x1c
  107. #define GPMC_CS_NAND_ADDRESS 0x20
  108. #define GPMC_CS_NAND_DATA 0x24
  109. /* Control Commands */
  110. #define GPMC_CONFIG_RDY_BSY 0x00000001
  111. #define GPMC_CONFIG_DEV_SIZE 0x00000002
  112. #define GPMC_CONFIG_DEV_TYPE 0x00000003
  113. #define GPMC_CONFIG1_WRAPBURST_SUPP (1 << 31)
  114. #define GPMC_CONFIG1_READMULTIPLE_SUPP (1 << 30)
  115. #define GPMC_CONFIG1_READTYPE_ASYNC (0 << 29)
  116. #define GPMC_CONFIG1_READTYPE_SYNC (1 << 29)
  117. #define GPMC_CONFIG1_WRITEMULTIPLE_SUPP (1 << 28)
  118. #define GPMC_CONFIG1_WRITETYPE_ASYNC (0 << 27)
  119. #define GPMC_CONFIG1_WRITETYPE_SYNC (1 << 27)
  120. #define GPMC_CONFIG1_CLKACTIVATIONTIME(val) ((val & 3) << 25)
  121. /** CLKACTIVATIONTIME Max Ticks */
  122. #define GPMC_CONFIG1_CLKACTIVATIONTIME_MAX 2
  123. #define GPMC_CONFIG1_PAGE_LEN(val) ((val & 3) << 23)
  124. /** ATTACHEDDEVICEPAGELENGTH Max Value */
  125. #define GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX 2
  126. #define GPMC_CONFIG1_WAIT_READ_MON (1 << 22)
  127. #define GPMC_CONFIG1_WAIT_WRITE_MON (1 << 21)
  128. #define GPMC_CONFIG1_WAIT_MON_TIME(val) ((val & 3) << 18)
  129. /** WAITMONITORINGTIME Max Ticks */
  130. #define GPMC_CONFIG1_WAITMONITORINGTIME_MAX 2
  131. #define GPMC_CONFIG1_WAIT_PIN_SEL(val) ((val & 3) << 16)
  132. #define GPMC_CONFIG1_DEVICESIZE(val) ((val & 3) << 12)
  133. #define GPMC_CONFIG1_DEVICESIZE_16 GPMC_CONFIG1_DEVICESIZE(1)
  134. /** DEVICESIZE Max Value */
  135. #define GPMC_CONFIG1_DEVICESIZE_MAX 1
  136. #define GPMC_CONFIG1_DEVICETYPE(val) ((val & 3) << 10)
  137. #define GPMC_CONFIG1_DEVICETYPE_NOR GPMC_CONFIG1_DEVICETYPE(0)
  138. #define GPMC_CONFIG1_MUXTYPE(val) ((val & 3) << 8)
  139. #define GPMC_CONFIG1_TIME_PARA_GRAN (1 << 4)
  140. #define GPMC_CONFIG1_FCLK_DIV(val) (val & 3)
  141. #define GPMC_CONFIG1_FCLK_DIV2 (GPMC_CONFIG1_FCLK_DIV(1))
  142. #define GPMC_CONFIG1_FCLK_DIV3 (GPMC_CONFIG1_FCLK_DIV(2))
  143. #define GPMC_CONFIG1_FCLK_DIV4 (GPMC_CONFIG1_FCLK_DIV(3))
  144. #define GPMC_CONFIG7_CSVALID (1 << 6)
  145. #define GPMC_CONFIG7_BASEADDRESS_MASK 0x3f
  146. #define GPMC_CONFIG7_CSVALID_MASK BIT(6)
  147. #define GPMC_CONFIG7_MASKADDRESS_OFFSET 8
  148. #define GPMC_CONFIG7_MASKADDRESS_MASK (0xf << GPMC_CONFIG7_MASKADDRESS_OFFSET)
  149. /* All CONFIG7 bits except reserved bits */
  150. #define GPMC_CONFIG7_MASK (GPMC_CONFIG7_BASEADDRESS_MASK | \
  151. GPMC_CONFIG7_CSVALID_MASK | \
  152. GPMC_CONFIG7_MASKADDRESS_MASK)
  153. #define GPMC_DEVICETYPE_NOR 0
  154. #define GPMC_DEVICETYPE_NAND 2
  155. #define GPMC_CONFIG_WRITEPROTECT 0x00000010
  156. #define WR_RD_PIN_MONITORING 0x00600000
  157. /* ECC commands */
  158. #define GPMC_ECC_READ 0 /* Reset Hardware ECC for read */
  159. #define GPMC_ECC_WRITE 1 /* Reset Hardware ECC for write */
  160. #define GPMC_ECC_READSYN 2 /* Reset before syndrom is read back */
  161. /* XXX: Only NAND irq has been considered,currently these are the only ones used
  162. */
  163. #define GPMC_NR_IRQ 2
  164. enum gpmc_clk_domain {
  165. GPMC_CD_FCLK,
  166. GPMC_CD_CLK
  167. };
  168. struct gpmc_cs_data {
  169. const char *name;
  170. #define GPMC_CS_RESERVED (1 << 0)
  171. u32 flags;
  172. struct resource mem;
  173. };
  174. /* Structure to save gpmc cs context */
  175. struct gpmc_cs_config {
  176. u32 config1;
  177. u32 config2;
  178. u32 config3;
  179. u32 config4;
  180. u32 config5;
  181. u32 config6;
  182. u32 config7;
  183. int is_valid;
  184. };
  185. /*
  186. * Structure to save/restore gpmc context
  187. * to support core off on OMAP3
  188. */
  189. struct omap3_gpmc_regs {
  190. u32 sysconfig;
  191. u32 irqenable;
  192. u32 timeout_ctrl;
  193. u32 config;
  194. u32 prefetch_config1;
  195. u32 prefetch_config2;
  196. u32 prefetch_control;
  197. struct gpmc_cs_config cs_context[GPMC_CS_NUM];
  198. };
  199. struct gpmc_device {
  200. struct device *dev;
  201. int irq;
  202. struct irq_chip irq_chip;
  203. };
  204. static struct irq_domain *gpmc_irq_domain;
  205. static struct resource gpmc_mem_root;
  206. static struct gpmc_cs_data gpmc_cs[GPMC_CS_NUM];
  207. static DEFINE_SPINLOCK(gpmc_mem_lock);
  208. /* Define chip-selects as reserved by default until probe completes */
  209. static unsigned int gpmc_cs_num = GPMC_CS_NUM;
  210. static unsigned int gpmc_nr_waitpins;
  211. static resource_size_t phys_base, mem_size;
  212. static unsigned gpmc_capability;
  213. static void __iomem *gpmc_base;
  214. static struct clk *gpmc_l3_clk;
  215. static irqreturn_t gpmc_handle_irq(int irq, void *dev);
  216. static void gpmc_write_reg(int idx, u32 val)
  217. {
  218. writel_relaxed(val, gpmc_base + idx);
  219. }
  220. static u32 gpmc_read_reg(int idx)
  221. {
  222. return readl_relaxed(gpmc_base + idx);
  223. }
  224. void gpmc_cs_write_reg(int cs, int idx, u32 val)
  225. {
  226. void __iomem *reg_addr;
  227. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  228. writel_relaxed(val, reg_addr);
  229. }
  230. static u32 gpmc_cs_read_reg(int cs, int idx)
  231. {
  232. void __iomem *reg_addr;
  233. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  234. return readl_relaxed(reg_addr);
  235. }
  236. /* TODO: Add support for gpmc_fck to clock framework and use it */
  237. static unsigned long gpmc_get_fclk_period(void)
  238. {
  239. unsigned long rate = clk_get_rate(gpmc_l3_clk);
  240. rate /= 1000;
  241. rate = 1000000000 / rate; /* In picoseconds */
  242. return rate;
  243. }
  244. /**
  245. * gpmc_get_clk_period - get period of selected clock domain in ps
  246. * @cs Chip Select Region.
  247. * @cd Clock Domain.
  248. *
  249. * GPMC_CS_CONFIG1 GPMCFCLKDIVIDER for cs has to be setup
  250. * prior to calling this function with GPMC_CD_CLK.
  251. */
  252. static unsigned long gpmc_get_clk_period(int cs, enum gpmc_clk_domain cd)
  253. {
  254. unsigned long tick_ps = gpmc_get_fclk_period();
  255. u32 l;
  256. int div;
  257. switch (cd) {
  258. case GPMC_CD_CLK:
  259. /* get current clk divider */
  260. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  261. div = (l & 0x03) + 1;
  262. /* get GPMC_CLK period */
  263. tick_ps *= div;
  264. break;
  265. case GPMC_CD_FCLK:
  266. /* FALL-THROUGH */
  267. default:
  268. break;
  269. }
  270. return tick_ps;
  271. }
  272. static unsigned int gpmc_ns_to_clk_ticks(unsigned int time_ns, int cs,
  273. enum gpmc_clk_domain cd)
  274. {
  275. unsigned long tick_ps;
  276. /* Calculate in picosecs to yield more exact results */
  277. tick_ps = gpmc_get_clk_period(cs, cd);
  278. return (time_ns * 1000 + tick_ps - 1) / tick_ps;
  279. }
  280. static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
  281. {
  282. return gpmc_ns_to_clk_ticks(time_ns, /* any CS */ 0, GPMC_CD_FCLK);
  283. }
  284. static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
  285. {
  286. unsigned long tick_ps;
  287. /* Calculate in picosecs to yield more exact results */
  288. tick_ps = gpmc_get_fclk_period();
  289. return (time_ps + tick_ps - 1) / tick_ps;
  290. }
  291. unsigned int gpmc_clk_ticks_to_ns(unsigned ticks, int cs,
  292. enum gpmc_clk_domain cd)
  293. {
  294. return ticks * gpmc_get_clk_period(cs, cd) / 1000;
  295. }
  296. unsigned int gpmc_ticks_to_ns(unsigned int ticks)
  297. {
  298. return gpmc_clk_ticks_to_ns(ticks, /* any CS */ 0, GPMC_CD_FCLK);
  299. }
  300. static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
  301. {
  302. return ticks * gpmc_get_fclk_period();
  303. }
  304. static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
  305. {
  306. unsigned long ticks = gpmc_ps_to_ticks(time_ps);
  307. return ticks * gpmc_get_fclk_period();
  308. }
  309. static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
  310. {
  311. u32 l;
  312. l = gpmc_cs_read_reg(cs, reg);
  313. if (value)
  314. l |= mask;
  315. else
  316. l &= ~mask;
  317. gpmc_cs_write_reg(cs, reg, l);
  318. }
  319. static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
  320. {
  321. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
  322. GPMC_CONFIG1_TIME_PARA_GRAN,
  323. p->time_para_granularity);
  324. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
  325. GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
  326. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
  327. GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
  328. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
  329. GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
  330. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
  331. GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay);
  332. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
  333. GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
  334. p->cycle2cyclesamecsen);
  335. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
  336. GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
  337. p->cycle2cyclediffcsen);
  338. }
  339. #ifdef CONFIG_OMAP_GPMC_DEBUG
  340. /**
  341. * get_gpmc_timing_reg - read a timing parameter and print DTS settings for it.
  342. * @cs: Chip Select Region
  343. * @reg: GPMC_CS_CONFIGn register offset.
  344. * @st_bit: Start Bit
  345. * @end_bit: End Bit. Must be >= @st_bit.
  346. * @ma:x Maximum parameter value (before optional @shift).
  347. * If 0, maximum is as high as @st_bit and @end_bit allow.
  348. * @name: DTS node name, w/o "gpmc,"
  349. * @cd: Clock Domain of timing parameter.
  350. * @shift: Parameter value left shifts @shift, which is then printed instead of value.
  351. * @raw: Raw Format Option.
  352. * raw format: gpmc,name = <value>
  353. * tick format: gpmc,name = <value> /&zwj;* x ns -- y ns; x ticks *&zwj;/
  354. * Where x ns -- y ns result in the same tick value.
  355. * When @max is exceeded, "invalid" is printed inside comment.
  356. * @noval: Parameter values equal to 0 are not printed.
  357. * @return: Specified timing parameter (after optional @shift).
  358. *
  359. */
  360. static int get_gpmc_timing_reg(
  361. /* timing specifiers */
  362. int cs, int reg, int st_bit, int end_bit, int max,
  363. const char *name, const enum gpmc_clk_domain cd,
  364. /* value transform */
  365. int shift,
  366. /* format specifiers */
  367. bool raw, bool noval)
  368. {
  369. u32 l;
  370. int nr_bits;
  371. int mask;
  372. bool invalid;
  373. l = gpmc_cs_read_reg(cs, reg);
  374. nr_bits = end_bit - st_bit + 1;
  375. mask = (1 << nr_bits) - 1;
  376. l = (l >> st_bit) & mask;
  377. if (!max)
  378. max = mask;
  379. invalid = l > max;
  380. if (shift)
  381. l = (shift << l);
  382. if (noval && (l == 0))
  383. return 0;
  384. if (!raw) {
  385. /* DTS tick format for timings in ns */
  386. unsigned int time_ns;
  387. unsigned int time_ns_min = 0;
  388. if (l)
  389. time_ns_min = gpmc_clk_ticks_to_ns(l - 1, cs, cd) + 1;
  390. time_ns = gpmc_clk_ticks_to_ns(l, cs, cd);
  391. pr_info("gpmc,%s = <%u> /* %u ns - %u ns; %i ticks%s*/\n",
  392. name, time_ns, time_ns_min, time_ns, l,
  393. invalid ? "; invalid " : " ");
  394. } else {
  395. /* raw format */
  396. pr_info("gpmc,%s = <%u>%s\n", name, l,
  397. invalid ? " /* invalid */" : "");
  398. }
  399. return l;
  400. }
  401. #define GPMC_PRINT_CONFIG(cs, config) \
  402. pr_info("cs%i %s: 0x%08x\n", cs, #config, \
  403. gpmc_cs_read_reg(cs, config))
  404. #define GPMC_GET_RAW(reg, st, end, field) \
  405. get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 0)
  406. #define GPMC_GET_RAW_MAX(reg, st, end, max, field) \
  407. get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, 0, 1, 0)
  408. #define GPMC_GET_RAW_BOOL(reg, st, end, field) \
  409. get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 1)
  410. #define GPMC_GET_RAW_SHIFT_MAX(reg, st, end, shift, max, field) \
  411. get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, (shift), 1, 1)
  412. #define GPMC_GET_TICKS(reg, st, end, field) \
  413. get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 0, 0)
  414. #define GPMC_GET_TICKS_CD(reg, st, end, field, cd) \
  415. get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, (cd), 0, 0, 0)
  416. #define GPMC_GET_TICKS_CD_MAX(reg, st, end, max, field, cd) \
  417. get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, (cd), 0, 0, 0)
  418. static void gpmc_show_regs(int cs, const char *desc)
  419. {
  420. pr_info("gpmc cs%i %s:\n", cs, desc);
  421. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG1);
  422. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG2);
  423. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG3);
  424. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG4);
  425. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG5);
  426. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG6);
  427. }
  428. /*
  429. * Note that gpmc,wait-pin handing wrongly assumes bit 8 is available,
  430. * see commit c9fb809.
  431. */
  432. static void gpmc_cs_show_timings(int cs, const char *desc)
  433. {
  434. gpmc_show_regs(cs, desc);
  435. pr_info("gpmc cs%i access configuration:\n", cs);
  436. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 4, 4, "time-para-granularity");
  437. GPMC_GET_RAW(GPMC_CS_CONFIG1, 8, 9, "mux-add-data");
  438. GPMC_GET_RAW_MAX(GPMC_CS_CONFIG1, 12, 13,
  439. GPMC_CONFIG1_DEVICESIZE_MAX, "device-width");
  440. GPMC_GET_RAW(GPMC_CS_CONFIG1, 16, 17, "wait-pin");
  441. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 21, 21, "wait-on-write");
  442. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 22, 22, "wait-on-read");
  443. GPMC_GET_RAW_SHIFT_MAX(GPMC_CS_CONFIG1, 23, 24, 4,
  444. GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX,
  445. "burst-length");
  446. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 27, 27, "sync-write");
  447. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 28, 28, "burst-write");
  448. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 29, 29, "gpmc,sync-read");
  449. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 30, 30, "burst-read");
  450. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 31, 31, "burst-wrap");
  451. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG2, 7, 7, "cs-extra-delay");
  452. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG3, 7, 7, "adv-extra-delay");
  453. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 23, 23, "we-extra-delay");
  454. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 7, 7, "oe-extra-delay");
  455. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 7, 7, "cycle2cycle-samecsen");
  456. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 6, 6, "cycle2cycle-diffcsen");
  457. pr_info("gpmc cs%i timings configuration:\n", cs);
  458. GPMC_GET_TICKS(GPMC_CS_CONFIG2, 0, 3, "cs-on-ns");
  459. GPMC_GET_TICKS(GPMC_CS_CONFIG2, 8, 12, "cs-rd-off-ns");
  460. GPMC_GET_TICKS(GPMC_CS_CONFIG2, 16, 20, "cs-wr-off-ns");
  461. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 0, 3, "adv-on-ns");
  462. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 8, 12, "adv-rd-off-ns");
  463. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 16, 20, "adv-wr-off-ns");
  464. if (gpmc_capability & GPMC_HAS_MUX_AAD) {
  465. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 4, 6, "adv-aad-mux-on-ns");
  466. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 24, 26,
  467. "adv-aad-mux-rd-off-ns");
  468. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 28, 30,
  469. "adv-aad-mux-wr-off-ns");
  470. }
  471. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 0, 3, "oe-on-ns");
  472. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 8, 12, "oe-off-ns");
  473. if (gpmc_capability & GPMC_HAS_MUX_AAD) {
  474. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 4, 6, "oe-aad-mux-on-ns");
  475. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 13, 15, "oe-aad-mux-off-ns");
  476. }
  477. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 16, 19, "we-on-ns");
  478. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 24, 28, "we-off-ns");
  479. GPMC_GET_TICKS(GPMC_CS_CONFIG5, 0, 4, "rd-cycle-ns");
  480. GPMC_GET_TICKS(GPMC_CS_CONFIG5, 8, 12, "wr-cycle-ns");
  481. GPMC_GET_TICKS(GPMC_CS_CONFIG5, 16, 20, "access-ns");
  482. GPMC_GET_TICKS(GPMC_CS_CONFIG5, 24, 27, "page-burst-access-ns");
  483. GPMC_GET_TICKS(GPMC_CS_CONFIG6, 0, 3, "bus-turnaround-ns");
  484. GPMC_GET_TICKS(GPMC_CS_CONFIG6, 8, 11, "cycle2cycle-delay-ns");
  485. GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 18, 19,
  486. GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
  487. "wait-monitoring-ns", GPMC_CD_CLK);
  488. GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 25, 26,
  489. GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
  490. "clk-activation-ns", GPMC_CD_FCLK);
  491. GPMC_GET_TICKS(GPMC_CS_CONFIG6, 16, 19, "wr-data-mux-bus-ns");
  492. GPMC_GET_TICKS(GPMC_CS_CONFIG6, 24, 28, "wr-access-ns");
  493. }
  494. #else
  495. static inline void gpmc_cs_show_timings(int cs, const char *desc)
  496. {
  497. }
  498. #endif
  499. /**
  500. * set_gpmc_timing_reg - set a single timing parameter for Chip Select Region.
  501. * Caller is expected to have initialized CONFIG1 GPMCFCLKDIVIDER
  502. * prior to calling this function with @cd equal to GPMC_CD_CLK.
  503. *
  504. * @cs: Chip Select Region.
  505. * @reg: GPMC_CS_CONFIGn register offset.
  506. * @st_bit: Start Bit
  507. * @end_bit: End Bit. Must be >= @st_bit.
  508. * @max: Maximum parameter value.
  509. * If 0, maximum is as high as @st_bit and @end_bit allow.
  510. * @time: Timing parameter in ns.
  511. * @cd: Timing parameter clock domain.
  512. * @name: Timing parameter name.
  513. * @return: 0 on success, -1 on error.
  514. */
  515. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit, int max,
  516. int time, enum gpmc_clk_domain cd, const char *name)
  517. {
  518. u32 l;
  519. int ticks, mask, nr_bits;
  520. if (time == 0)
  521. ticks = 0;
  522. else
  523. ticks = gpmc_ns_to_clk_ticks(time, cs, cd);
  524. nr_bits = end_bit - st_bit + 1;
  525. mask = (1 << nr_bits) - 1;
  526. if (!max)
  527. max = mask;
  528. if (ticks > max) {
  529. pr_err("%s: GPMC CS%d: %s %d ns, %d ticks > %d ticks\n",
  530. __func__, cs, name, time, ticks, max);
  531. return -1;
  532. }
  533. l = gpmc_cs_read_reg(cs, reg);
  534. #ifdef CONFIG_OMAP_GPMC_DEBUG
  535. pr_info(
  536. "GPMC CS%d: %-17s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
  537. cs, name, ticks, gpmc_get_clk_period(cs, cd) * ticks / 1000,
  538. (l >> st_bit) & mask, time);
  539. #endif
  540. l &= ~(mask << st_bit);
  541. l |= ticks << st_bit;
  542. gpmc_cs_write_reg(cs, reg, l);
  543. return 0;
  544. }
  545. #define GPMC_SET_ONE_CD_MAX(reg, st, end, max, field, cd) \
  546. if (set_gpmc_timing_reg(cs, (reg), (st), (end), (max), \
  547. t->field, (cd), #field) < 0) \
  548. return -1
  549. #define GPMC_SET_ONE(reg, st, end, field) \
  550. GPMC_SET_ONE_CD_MAX(reg, st, end, 0, field, GPMC_CD_FCLK)
  551. /**
  552. * gpmc_calc_waitmonitoring_divider - calculate proper GPMCFCLKDIVIDER based on WAITMONITORINGTIME
  553. * WAITMONITORINGTIME will be _at least_ as long as desired, i.e.
  554. * read --> don't sample bus too early
  555. * write --> data is longer on bus
  556. *
  557. * Formula:
  558. * gpmc_clk_div + 1 = ceil(ceil(waitmonitoringtime_ns / gpmc_fclk_ns)
  559. * / waitmonitoring_ticks)
  560. * WAITMONITORINGTIME resulting in 0 or 1 tick with div = 1 are caught by
  561. * div <= 0 check.
  562. *
  563. * @wait_monitoring: WAITMONITORINGTIME in ns.
  564. * @return: -1 on failure to scale, else proper divider > 0.
  565. */
  566. static int gpmc_calc_waitmonitoring_divider(unsigned int wait_monitoring)
  567. {
  568. int div = gpmc_ns_to_ticks(wait_monitoring);
  569. div += GPMC_CONFIG1_WAITMONITORINGTIME_MAX - 1;
  570. div /= GPMC_CONFIG1_WAITMONITORINGTIME_MAX;
  571. if (div > 4)
  572. return -1;
  573. if (div <= 0)
  574. div = 1;
  575. return div;
  576. }
  577. /**
  578. * gpmc_calc_divider - calculate GPMC_FCLK divider for sync_clk GPMC_CLK period.
  579. * @sync_clk: GPMC_CLK period in ps.
  580. * @return: Returns at least 1 if GPMC_FCLK can be divided to GPMC_CLK.
  581. * Else, returns -1.
  582. */
  583. int gpmc_calc_divider(unsigned int sync_clk)
  584. {
  585. int div = gpmc_ps_to_ticks(sync_clk);
  586. if (div > 4)
  587. return -1;
  588. if (div <= 0)
  589. div = 1;
  590. return div;
  591. }
  592. /**
  593. * gpmc_cs_set_timings - program timing parameters for Chip Select Region.
  594. * @cs: Chip Select Region.
  595. * @t: GPMC timing parameters.
  596. * @s: GPMC timing settings.
  597. * @return: 0 on success, -1 on error.
  598. */
  599. int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t,
  600. const struct gpmc_settings *s)
  601. {
  602. int div;
  603. u32 l;
  604. div = gpmc_calc_divider(t->sync_clk);
  605. if (div < 0)
  606. return div;
  607. /*
  608. * See if we need to change the divider for waitmonitoringtime.
  609. *
  610. * Calculate GPMCFCLKDIVIDER independent of gpmc,sync-clk-ps in DT for
  611. * pure asynchronous accesses, i.e. both read and write asynchronous.
  612. * However, only do so if WAITMONITORINGTIME is actually used, i.e.
  613. * either WAITREADMONITORING or WAITWRITEMONITORING is set.
  614. *
  615. * This statement must not change div to scale async WAITMONITORINGTIME
  616. * to protect mixed synchronous and asynchronous accesses.
  617. *
  618. * We raise an error later if WAITMONITORINGTIME does not fit.
  619. */
  620. if (!s->sync_read && !s->sync_write &&
  621. (s->wait_on_read || s->wait_on_write)
  622. ) {
  623. div = gpmc_calc_waitmonitoring_divider(t->wait_monitoring);
  624. if (div < 0) {
  625. pr_err("%s: waitmonitoringtime %3d ns too large for greatest gpmcfclkdivider.\n",
  626. __func__,
  627. t->wait_monitoring
  628. );
  629. return -1;
  630. }
  631. }
  632. GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
  633. GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
  634. GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
  635. GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
  636. GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
  637. GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
  638. if (gpmc_capability & GPMC_HAS_MUX_AAD) {
  639. GPMC_SET_ONE(GPMC_CS_CONFIG3, 4, 6, adv_aad_mux_on);
  640. GPMC_SET_ONE(GPMC_CS_CONFIG3, 24, 26, adv_aad_mux_rd_off);
  641. GPMC_SET_ONE(GPMC_CS_CONFIG3, 28, 30, adv_aad_mux_wr_off);
  642. }
  643. GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
  644. GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
  645. if (gpmc_capability & GPMC_HAS_MUX_AAD) {
  646. GPMC_SET_ONE(GPMC_CS_CONFIG4, 4, 6, oe_aad_mux_on);
  647. GPMC_SET_ONE(GPMC_CS_CONFIG4, 13, 15, oe_aad_mux_off);
  648. }
  649. GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
  650. GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
  651. GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
  652. GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
  653. GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
  654. GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
  655. GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
  656. GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
  657. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  658. GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
  659. if (gpmc_capability & GPMC_HAS_WR_ACCESS)
  660. GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
  661. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  662. l &= ~0x03;
  663. l |= (div - 1);
  664. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
  665. GPMC_SET_ONE_CD_MAX(GPMC_CS_CONFIG1, 18, 19,
  666. GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
  667. wait_monitoring, GPMC_CD_CLK);
  668. GPMC_SET_ONE_CD_MAX(GPMC_CS_CONFIG1, 25, 26,
  669. GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
  670. clk_activation, GPMC_CD_FCLK);
  671. #ifdef CONFIG_OMAP_GPMC_DEBUG
  672. pr_info("GPMC CS%d CLK period is %lu ns (div %d)\n",
  673. cs, (div * gpmc_get_fclk_period()) / 1000, div);
  674. #endif
  675. gpmc_cs_bool_timings(cs, &t->bool_timings);
  676. gpmc_cs_show_timings(cs, "after gpmc_cs_set_timings");
  677. return 0;
  678. }
  679. static int gpmc_cs_set_memconf(int cs, u32 base, u32 size)
  680. {
  681. u32 l;
  682. u32 mask;
  683. /*
  684. * Ensure that base address is aligned on a
  685. * boundary equal to or greater than size.
  686. */
  687. if (base & (size - 1))
  688. return -EINVAL;
  689. base >>= GPMC_CHUNK_SHIFT;
  690. mask = (1 << GPMC_SECTION_SHIFT) - size;
  691. mask >>= GPMC_CHUNK_SHIFT;
  692. mask <<= GPMC_CONFIG7_MASKADDRESS_OFFSET;
  693. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  694. l &= ~GPMC_CONFIG7_MASK;
  695. l |= base & GPMC_CONFIG7_BASEADDRESS_MASK;
  696. l |= mask & GPMC_CONFIG7_MASKADDRESS_MASK;
  697. l |= GPMC_CONFIG7_CSVALID;
  698. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  699. return 0;
  700. }
  701. static void gpmc_cs_enable_mem(int cs)
  702. {
  703. u32 l;
  704. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  705. l |= GPMC_CONFIG7_CSVALID;
  706. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  707. }
  708. static void gpmc_cs_disable_mem(int cs)
  709. {
  710. u32 l;
  711. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  712. l &= ~GPMC_CONFIG7_CSVALID;
  713. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  714. }
  715. static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
  716. {
  717. u32 l;
  718. u32 mask;
  719. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  720. *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
  721. mask = (l >> 8) & 0x0f;
  722. *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
  723. }
  724. static int gpmc_cs_mem_enabled(int cs)
  725. {
  726. u32 l;
  727. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  728. return l & GPMC_CONFIG7_CSVALID;
  729. }
  730. static void gpmc_cs_set_reserved(int cs, int reserved)
  731. {
  732. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  733. gpmc->flags |= GPMC_CS_RESERVED;
  734. }
  735. static bool gpmc_cs_reserved(int cs)
  736. {
  737. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  738. return gpmc->flags & GPMC_CS_RESERVED;
  739. }
  740. static void gpmc_cs_set_name(int cs, const char *name)
  741. {
  742. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  743. gpmc->name = name;
  744. }
  745. static const char *gpmc_cs_get_name(int cs)
  746. {
  747. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  748. return gpmc->name;
  749. }
  750. static unsigned long gpmc_mem_align(unsigned long size)
  751. {
  752. int order;
  753. size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
  754. order = GPMC_CHUNK_SHIFT - 1;
  755. do {
  756. size >>= 1;
  757. order++;
  758. } while (size);
  759. size = 1 << order;
  760. return size;
  761. }
  762. static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
  763. {
  764. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  765. struct resource *res = &gpmc->mem;
  766. int r;
  767. size = gpmc_mem_align(size);
  768. spin_lock(&gpmc_mem_lock);
  769. res->start = base;
  770. res->end = base + size - 1;
  771. r = request_resource(&gpmc_mem_root, res);
  772. spin_unlock(&gpmc_mem_lock);
  773. return r;
  774. }
  775. static int gpmc_cs_delete_mem(int cs)
  776. {
  777. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  778. struct resource *res = &gpmc->mem;
  779. int r;
  780. spin_lock(&gpmc_mem_lock);
  781. r = release_resource(res);
  782. res->start = 0;
  783. res->end = 0;
  784. spin_unlock(&gpmc_mem_lock);
  785. return r;
  786. }
  787. /**
  788. * gpmc_cs_remap - remaps a chip-select physical base address
  789. * @cs: chip-select to remap
  790. * @base: physical base address to re-map chip-select to
  791. *
  792. * Re-maps a chip-select to a new physical base address specified by
  793. * "base". Returns 0 on success and appropriate negative error code
  794. * on failure.
  795. */
  796. static int gpmc_cs_remap(int cs, u32 base)
  797. {
  798. int ret;
  799. u32 old_base, size;
  800. if (cs > gpmc_cs_num) {
  801. pr_err("%s: requested chip-select is disabled\n", __func__);
  802. return -ENODEV;
  803. }
  804. /*
  805. * Make sure we ignore any device offsets from the GPMC partition
  806. * allocated for the chip select and that the new base confirms
  807. * to the GPMC 16MB minimum granularity.
  808. */
  809. base &= ~(SZ_16M - 1);
  810. gpmc_cs_get_memconf(cs, &old_base, &size);
  811. if (base == old_base)
  812. return 0;
  813. ret = gpmc_cs_delete_mem(cs);
  814. if (ret < 0)
  815. return ret;
  816. ret = gpmc_cs_insert_mem(cs, base, size);
  817. if (ret < 0)
  818. return ret;
  819. ret = gpmc_cs_set_memconf(cs, base, size);
  820. return ret;
  821. }
  822. int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
  823. {
  824. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  825. struct resource *res = &gpmc->mem;
  826. int r = -1;
  827. if (cs > gpmc_cs_num) {
  828. pr_err("%s: requested chip-select is disabled\n", __func__);
  829. return -ENODEV;
  830. }
  831. size = gpmc_mem_align(size);
  832. if (size > (1 << GPMC_SECTION_SHIFT))
  833. return -ENOMEM;
  834. spin_lock(&gpmc_mem_lock);
  835. if (gpmc_cs_reserved(cs)) {
  836. r = -EBUSY;
  837. goto out;
  838. }
  839. if (gpmc_cs_mem_enabled(cs))
  840. r = adjust_resource(res, res->start & ~(size - 1), size);
  841. if (r < 0)
  842. r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
  843. size, NULL, NULL);
  844. if (r < 0)
  845. goto out;
  846. /* Disable CS while changing base address and size mask */
  847. gpmc_cs_disable_mem(cs);
  848. r = gpmc_cs_set_memconf(cs, res->start, resource_size(res));
  849. if (r < 0) {
  850. release_resource(res);
  851. goto out;
  852. }
  853. /* Enable CS */
  854. gpmc_cs_enable_mem(cs);
  855. *base = res->start;
  856. gpmc_cs_set_reserved(cs, 1);
  857. out:
  858. spin_unlock(&gpmc_mem_lock);
  859. return r;
  860. }
  861. EXPORT_SYMBOL(gpmc_cs_request);
  862. void gpmc_cs_free(int cs)
  863. {
  864. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  865. struct resource *res = &gpmc->mem;
  866. spin_lock(&gpmc_mem_lock);
  867. if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) {
  868. printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
  869. BUG();
  870. spin_unlock(&gpmc_mem_lock);
  871. return;
  872. }
  873. gpmc_cs_disable_mem(cs);
  874. if (res->flags)
  875. release_resource(res);
  876. gpmc_cs_set_reserved(cs, 0);
  877. spin_unlock(&gpmc_mem_lock);
  878. }
  879. EXPORT_SYMBOL(gpmc_cs_free);
  880. /**
  881. * gpmc_configure - write request to configure gpmc
  882. * @cmd: command type
  883. * @wval: value to write
  884. * @return status of the operation
  885. */
  886. int gpmc_configure(int cmd, int wval)
  887. {
  888. u32 regval;
  889. switch (cmd) {
  890. case GPMC_CONFIG_WP:
  891. regval = gpmc_read_reg(GPMC_CONFIG);
  892. if (wval)
  893. regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
  894. else
  895. regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
  896. gpmc_write_reg(GPMC_CONFIG, regval);
  897. break;
  898. default:
  899. pr_err("%s: command not supported\n", __func__);
  900. return -EINVAL;
  901. }
  902. return 0;
  903. }
  904. EXPORT_SYMBOL(gpmc_configure);
  905. void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
  906. {
  907. int i;
  908. reg->gpmc_status = gpmc_base + GPMC_STATUS;
  909. reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
  910. GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
  911. reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
  912. GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
  913. reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
  914. GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
  915. reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
  916. reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
  917. reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
  918. reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
  919. reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
  920. reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
  921. reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
  922. reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
  923. for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
  924. reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
  925. GPMC_BCH_SIZE * i;
  926. reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
  927. GPMC_BCH_SIZE * i;
  928. reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
  929. GPMC_BCH_SIZE * i;
  930. reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
  931. GPMC_BCH_SIZE * i;
  932. reg->gpmc_bch_result4[i] = gpmc_base + GPMC_ECC_BCH_RESULT_4 +
  933. i * GPMC_BCH_SIZE;
  934. reg->gpmc_bch_result5[i] = gpmc_base + GPMC_ECC_BCH_RESULT_5 +
  935. i * GPMC_BCH_SIZE;
  936. reg->gpmc_bch_result6[i] = gpmc_base + GPMC_ECC_BCH_RESULT_6 +
  937. i * GPMC_BCH_SIZE;
  938. }
  939. }
  940. static bool gpmc_nand_writebuffer_empty(void)
  941. {
  942. if (gpmc_read_reg(GPMC_STATUS) & GPMC_STATUS_EMPTYWRITEBUFFERSTATUS)
  943. return true;
  944. return false;
  945. }
  946. static struct gpmc_nand_ops nand_ops = {
  947. .nand_writebuffer_empty = gpmc_nand_writebuffer_empty,
  948. };
  949. /**
  950. * gpmc_omap_get_nand_ops - Get the GPMC NAND interface
  951. * @regs: the GPMC NAND register map exclusive for NAND use.
  952. * @cs: GPMC chip select number on which the NAND sits. The
  953. * register map returned will be specific to this chip select.
  954. *
  955. * Returns NULL on error e.g. invalid cs.
  956. */
  957. struct gpmc_nand_ops *gpmc_omap_get_nand_ops(struct gpmc_nand_regs *reg, int cs)
  958. {
  959. if (cs >= gpmc_cs_num)
  960. return NULL;
  961. gpmc_update_nand_reg(reg, cs);
  962. return &nand_ops;
  963. }
  964. EXPORT_SYMBOL_GPL(gpmc_omap_get_nand_ops);
  965. int gpmc_get_client_irq(unsigned irq_config)
  966. {
  967. if (!gpmc_irq_domain) {
  968. pr_warn("%s called before GPMC IRQ domain available\n",
  969. __func__);
  970. return 0;
  971. }
  972. if (irq_config >= GPMC_NR_IRQ)
  973. return 0;
  974. return irq_create_mapping(gpmc_irq_domain, irq_config);
  975. }
  976. static int gpmc_irq_endis(unsigned long hwirq, bool endis)
  977. {
  978. u32 regval;
  979. regval = gpmc_read_reg(GPMC_IRQENABLE);
  980. if (endis)
  981. regval |= BIT(hwirq);
  982. else
  983. regval &= ~BIT(hwirq);
  984. gpmc_write_reg(GPMC_IRQENABLE, regval);
  985. return 0;
  986. }
  987. static void gpmc_irq_disable(struct irq_data *p)
  988. {
  989. gpmc_irq_endis(p->hwirq, false);
  990. }
  991. static void gpmc_irq_enable(struct irq_data *p)
  992. {
  993. gpmc_irq_endis(p->hwirq, true);
  994. }
  995. static void gpmc_irq_noop(struct irq_data *data) { }
  996. static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
  997. static int gpmc_irq_map(struct irq_domain *d, unsigned int virq,
  998. irq_hw_number_t hw)
  999. {
  1000. struct gpmc_device *gpmc = d->host_data;
  1001. irq_set_chip_data(virq, gpmc);
  1002. irq_set_chip_and_handler(virq, &gpmc->irq_chip, handle_simple_irq);
  1003. irq_modify_status(virq, IRQ_NOREQUEST, IRQ_NOAUTOEN);
  1004. return 0;
  1005. }
  1006. static const struct irq_domain_ops gpmc_irq_domain_ops = {
  1007. .map = gpmc_irq_map,
  1008. .xlate = irq_domain_xlate_twocell,
  1009. };
  1010. static irqreturn_t gpmc_handle_irq(int irq, void *data)
  1011. {
  1012. int hwirq, virq;
  1013. u32 regval;
  1014. struct gpmc_device *gpmc = data;
  1015. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  1016. if (!regval)
  1017. return IRQ_NONE;
  1018. for (hwirq = 0; hwirq < GPMC_NR_IRQ; hwirq++) {
  1019. if (regval & BIT(hwirq)) {
  1020. virq = irq_find_mapping(gpmc_irq_domain, hwirq);
  1021. if (!virq) {
  1022. dev_warn(gpmc->dev,
  1023. "spurious irq detected hwirq %d, virq %d\n",
  1024. hwirq, virq);
  1025. }
  1026. generic_handle_irq(virq);
  1027. }
  1028. }
  1029. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  1030. return IRQ_HANDLED;
  1031. }
  1032. static int gpmc_setup_irq(struct gpmc_device *gpmc)
  1033. {
  1034. u32 regval;
  1035. int rc;
  1036. /* Disable interrupts */
  1037. gpmc_write_reg(GPMC_IRQENABLE, 0);
  1038. /* clear interrupts */
  1039. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  1040. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  1041. gpmc->irq_chip.name = "gpmc";
  1042. gpmc->irq_chip.irq_startup = gpmc_irq_noop_ret;
  1043. gpmc->irq_chip.irq_enable = gpmc_irq_enable;
  1044. gpmc->irq_chip.irq_disable = gpmc_irq_disable;
  1045. gpmc->irq_chip.irq_shutdown = gpmc_irq_noop;
  1046. gpmc->irq_chip.irq_ack = gpmc_irq_noop;
  1047. gpmc->irq_chip.irq_mask = gpmc_irq_noop;
  1048. gpmc->irq_chip.irq_unmask = gpmc_irq_noop;
  1049. gpmc_irq_domain = irq_domain_add_linear(gpmc->dev->of_node,
  1050. GPMC_NR_IRQ,
  1051. &gpmc_irq_domain_ops,
  1052. gpmc);
  1053. if (!gpmc_irq_domain) {
  1054. dev_err(gpmc->dev, "IRQ domain add failed\n");
  1055. return -ENODEV;
  1056. }
  1057. rc = request_irq(gpmc->irq, gpmc_handle_irq, 0, "gpmc", gpmc);
  1058. if (rc) {
  1059. dev_err(gpmc->dev, "failed to request irq %d: %d\n",
  1060. gpmc->irq, rc);
  1061. irq_domain_remove(gpmc_irq_domain);
  1062. gpmc_irq_domain = NULL;
  1063. }
  1064. return rc;
  1065. }
  1066. static int gpmc_free_irq(struct gpmc_device *gpmc)
  1067. {
  1068. int hwirq;
  1069. free_irq(gpmc->irq, gpmc);
  1070. for (hwirq = 0; hwirq < GPMC_NR_IRQ; hwirq++)
  1071. irq_dispose_mapping(irq_find_mapping(gpmc_irq_domain, hwirq));
  1072. irq_domain_remove(gpmc_irq_domain);
  1073. gpmc_irq_domain = NULL;
  1074. return 0;
  1075. }
  1076. static void gpmc_mem_exit(void)
  1077. {
  1078. int cs;
  1079. for (cs = 0; cs < gpmc_cs_num; cs++) {
  1080. if (!gpmc_cs_mem_enabled(cs))
  1081. continue;
  1082. gpmc_cs_delete_mem(cs);
  1083. }
  1084. }
  1085. static void gpmc_mem_init(void)
  1086. {
  1087. int cs;
  1088. /*
  1089. * The first 1MB of GPMC address space is typically mapped to
  1090. * the internal ROM. Never allocate the first page, to
  1091. * facilitate bug detection; even if we didn't boot from ROM.
  1092. */
  1093. gpmc_mem_root.start = SZ_1M;
  1094. gpmc_mem_root.end = GPMC_MEM_END;
  1095. /* Reserve all regions that has been set up by bootloader */
  1096. for (cs = 0; cs < gpmc_cs_num; cs++) {
  1097. u32 base, size;
  1098. if (!gpmc_cs_mem_enabled(cs))
  1099. continue;
  1100. gpmc_cs_get_memconf(cs, &base, &size);
  1101. if (gpmc_cs_insert_mem(cs, base, size)) {
  1102. pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n",
  1103. __func__, cs, base, base + size);
  1104. gpmc_cs_disable_mem(cs);
  1105. }
  1106. }
  1107. }
  1108. static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
  1109. {
  1110. u32 temp;
  1111. int div;
  1112. div = gpmc_calc_divider(sync_clk);
  1113. temp = gpmc_ps_to_ticks(time_ps);
  1114. temp = (temp + div - 1) / div;
  1115. return gpmc_ticks_to_ps(temp * div);
  1116. }
  1117. /* XXX: can the cycles be avoided ? */
  1118. static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
  1119. struct gpmc_device_timings *dev_t,
  1120. bool mux)
  1121. {
  1122. u32 temp;
  1123. /* adv_rd_off */
  1124. temp = dev_t->t_avdp_r;
  1125. /* XXX: mux check required ? */
  1126. if (mux) {
  1127. /* XXX: t_avdp not to be required for sync, only added for tusb
  1128. * this indirectly necessitates requirement of t_avdp_r and
  1129. * t_avdp_w instead of having a single t_avdp
  1130. */
  1131. temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
  1132. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  1133. }
  1134. gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
  1135. /* oe_on */
  1136. temp = dev_t->t_oeasu; /* XXX: remove this ? */
  1137. if (mux) {
  1138. temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
  1139. temp = max_t(u32, temp, gpmc_t->adv_rd_off +
  1140. gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
  1141. }
  1142. gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
  1143. /* access */
  1144. /* XXX: any scope for improvement ?, by combining oe_on
  1145. * and clk_activation, need to check whether
  1146. * access = clk_activation + round to sync clk ?
  1147. */
  1148. temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
  1149. temp += gpmc_t->clk_activation;
  1150. if (dev_t->cyc_oe)
  1151. temp = max_t(u32, temp, gpmc_t->oe_on +
  1152. gpmc_ticks_to_ps(dev_t->cyc_oe));
  1153. gpmc_t->access = gpmc_round_ps_to_ticks(temp);
  1154. gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
  1155. gpmc_t->cs_rd_off = gpmc_t->oe_off;
  1156. /* rd_cycle */
  1157. temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
  1158. temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
  1159. gpmc_t->access;
  1160. /* XXX: barter t_ce_rdyz with t_cez_r ? */
  1161. if (dev_t->t_ce_rdyz)
  1162. temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
  1163. gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
  1164. return 0;
  1165. }
  1166. static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
  1167. struct gpmc_device_timings *dev_t,
  1168. bool mux)
  1169. {
  1170. u32 temp;
  1171. /* adv_wr_off */
  1172. temp = dev_t->t_avdp_w;
  1173. if (mux) {
  1174. temp = max_t(u32, temp,
  1175. gpmc_t->clk_activation + dev_t->t_avdh);
  1176. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  1177. }
  1178. gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
  1179. /* wr_data_mux_bus */
  1180. temp = max_t(u32, dev_t->t_weasu,
  1181. gpmc_t->clk_activation + dev_t->t_rdyo);
  1182. /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
  1183. * and in that case remember to handle we_on properly
  1184. */
  1185. if (mux) {
  1186. temp = max_t(u32, temp,
  1187. gpmc_t->adv_wr_off + dev_t->t_aavdh);
  1188. temp = max_t(u32, temp, gpmc_t->adv_wr_off +
  1189. gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
  1190. }
  1191. gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
  1192. /* we_on */
  1193. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  1194. gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
  1195. else
  1196. gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
  1197. /* wr_access */
  1198. /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
  1199. gpmc_t->wr_access = gpmc_t->access;
  1200. /* we_off */
  1201. temp = gpmc_t->we_on + dev_t->t_wpl;
  1202. temp = max_t(u32, temp,
  1203. gpmc_t->wr_access + gpmc_ticks_to_ps(1));
  1204. temp = max_t(u32, temp,
  1205. gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
  1206. gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
  1207. gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
  1208. dev_t->t_wph);
  1209. /* wr_cycle */
  1210. temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
  1211. temp += gpmc_t->wr_access;
  1212. /* XXX: barter t_ce_rdyz with t_cez_w ? */
  1213. if (dev_t->t_ce_rdyz)
  1214. temp = max_t(u32, temp,
  1215. gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
  1216. gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
  1217. return 0;
  1218. }
  1219. static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
  1220. struct gpmc_device_timings *dev_t,
  1221. bool mux)
  1222. {
  1223. u32 temp;
  1224. /* adv_rd_off */
  1225. temp = dev_t->t_avdp_r;
  1226. if (mux)
  1227. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  1228. gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
  1229. /* oe_on */
  1230. temp = dev_t->t_oeasu;
  1231. if (mux)
  1232. temp = max_t(u32, temp,
  1233. gpmc_t->adv_rd_off + dev_t->t_aavdh);
  1234. gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
  1235. /* access */
  1236. temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
  1237. gpmc_t->oe_on + dev_t->t_oe);
  1238. temp = max_t(u32, temp,
  1239. gpmc_t->cs_on + dev_t->t_ce);
  1240. temp = max_t(u32, temp,
  1241. gpmc_t->adv_on + dev_t->t_aa);
  1242. gpmc_t->access = gpmc_round_ps_to_ticks(temp);
  1243. gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
  1244. gpmc_t->cs_rd_off = gpmc_t->oe_off;
  1245. /* rd_cycle */
  1246. temp = max_t(u32, dev_t->t_rd_cycle,
  1247. gpmc_t->cs_rd_off + dev_t->t_cez_r);
  1248. temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
  1249. gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
  1250. return 0;
  1251. }
  1252. static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
  1253. struct gpmc_device_timings *dev_t,
  1254. bool mux)
  1255. {
  1256. u32 temp;
  1257. /* adv_wr_off */
  1258. temp = dev_t->t_avdp_w;
  1259. if (mux)
  1260. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  1261. gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
  1262. /* wr_data_mux_bus */
  1263. temp = dev_t->t_weasu;
  1264. if (mux) {
  1265. temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
  1266. temp = max_t(u32, temp, gpmc_t->adv_wr_off +
  1267. gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
  1268. }
  1269. gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
  1270. /* we_on */
  1271. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  1272. gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
  1273. else
  1274. gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
  1275. /* we_off */
  1276. temp = gpmc_t->we_on + dev_t->t_wpl;
  1277. gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
  1278. gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
  1279. dev_t->t_wph);
  1280. /* wr_cycle */
  1281. temp = max_t(u32, dev_t->t_wr_cycle,
  1282. gpmc_t->cs_wr_off + dev_t->t_cez_w);
  1283. gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
  1284. return 0;
  1285. }
  1286. static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
  1287. struct gpmc_device_timings *dev_t)
  1288. {
  1289. u32 temp;
  1290. gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
  1291. gpmc_get_fclk_period();
  1292. gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
  1293. dev_t->t_bacc,
  1294. gpmc_t->sync_clk);
  1295. temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
  1296. gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
  1297. if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
  1298. return 0;
  1299. if (dev_t->ce_xdelay)
  1300. gpmc_t->bool_timings.cs_extra_delay = true;
  1301. if (dev_t->avd_xdelay)
  1302. gpmc_t->bool_timings.adv_extra_delay = true;
  1303. if (dev_t->oe_xdelay)
  1304. gpmc_t->bool_timings.oe_extra_delay = true;
  1305. if (dev_t->we_xdelay)
  1306. gpmc_t->bool_timings.we_extra_delay = true;
  1307. return 0;
  1308. }
  1309. static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
  1310. struct gpmc_device_timings *dev_t,
  1311. bool sync)
  1312. {
  1313. u32 temp;
  1314. /* cs_on */
  1315. gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
  1316. /* adv_on */
  1317. temp = dev_t->t_avdasu;
  1318. if (dev_t->t_ce_avd)
  1319. temp = max_t(u32, temp,
  1320. gpmc_t->cs_on + dev_t->t_ce_avd);
  1321. gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
  1322. if (sync)
  1323. gpmc_calc_sync_common_timings(gpmc_t, dev_t);
  1324. return 0;
  1325. }
  1326. /* TODO: remove this function once all peripherals are confirmed to
  1327. * work with generic timing. Simultaneously gpmc_cs_set_timings()
  1328. * has to be modified to handle timings in ps instead of ns
  1329. */
  1330. static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
  1331. {
  1332. t->cs_on /= 1000;
  1333. t->cs_rd_off /= 1000;
  1334. t->cs_wr_off /= 1000;
  1335. t->adv_on /= 1000;
  1336. t->adv_rd_off /= 1000;
  1337. t->adv_wr_off /= 1000;
  1338. t->we_on /= 1000;
  1339. t->we_off /= 1000;
  1340. t->oe_on /= 1000;
  1341. t->oe_off /= 1000;
  1342. t->page_burst_access /= 1000;
  1343. t->access /= 1000;
  1344. t->rd_cycle /= 1000;
  1345. t->wr_cycle /= 1000;
  1346. t->bus_turnaround /= 1000;
  1347. t->cycle2cycle_delay /= 1000;
  1348. t->wait_monitoring /= 1000;
  1349. t->clk_activation /= 1000;
  1350. t->wr_access /= 1000;
  1351. t->wr_data_mux_bus /= 1000;
  1352. }
  1353. int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
  1354. struct gpmc_settings *gpmc_s,
  1355. struct gpmc_device_timings *dev_t)
  1356. {
  1357. bool mux = false, sync = false;
  1358. if (gpmc_s) {
  1359. mux = gpmc_s->mux_add_data ? true : false;
  1360. sync = (gpmc_s->sync_read || gpmc_s->sync_write);
  1361. }
  1362. memset(gpmc_t, 0, sizeof(*gpmc_t));
  1363. gpmc_calc_common_timings(gpmc_t, dev_t, sync);
  1364. if (gpmc_s && gpmc_s->sync_read)
  1365. gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
  1366. else
  1367. gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
  1368. if (gpmc_s && gpmc_s->sync_write)
  1369. gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
  1370. else
  1371. gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
  1372. /* TODO: remove, see function definition */
  1373. gpmc_convert_ps_to_ns(gpmc_t);
  1374. return 0;
  1375. }
  1376. /**
  1377. * gpmc_cs_program_settings - programs non-timing related settings
  1378. * @cs: GPMC chip-select to program
  1379. * @p: pointer to GPMC settings structure
  1380. *
  1381. * Programs non-timing related settings for a GPMC chip-select, such as
  1382. * bus-width, burst configuration, etc. Function should be called once
  1383. * for each chip-select that is being used and must be called before
  1384. * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
  1385. * register will be initialised to zero by this function. Returns 0 on
  1386. * success and appropriate negative error code on failure.
  1387. */
  1388. int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
  1389. {
  1390. u32 config1;
  1391. if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
  1392. pr_err("%s: invalid width %d!", __func__, p->device_width);
  1393. return -EINVAL;
  1394. }
  1395. /* Address-data multiplexing not supported for NAND devices */
  1396. if (p->device_nand && p->mux_add_data) {
  1397. pr_err("%s: invalid configuration!\n", __func__);
  1398. return -EINVAL;
  1399. }
  1400. if ((p->mux_add_data > GPMC_MUX_AD) ||
  1401. ((p->mux_add_data == GPMC_MUX_AAD) &&
  1402. !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
  1403. pr_err("%s: invalid multiplex configuration!\n", __func__);
  1404. return -EINVAL;
  1405. }
  1406. /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
  1407. if (p->burst_read || p->burst_write) {
  1408. switch (p->burst_len) {
  1409. case GPMC_BURST_4:
  1410. case GPMC_BURST_8:
  1411. case GPMC_BURST_16:
  1412. break;
  1413. default:
  1414. pr_err("%s: invalid page/burst-length (%d)\n",
  1415. __func__, p->burst_len);
  1416. return -EINVAL;
  1417. }
  1418. }
  1419. if (p->wait_pin > gpmc_nr_waitpins) {
  1420. pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
  1421. return -EINVAL;
  1422. }
  1423. config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
  1424. if (p->sync_read)
  1425. config1 |= GPMC_CONFIG1_READTYPE_SYNC;
  1426. if (p->sync_write)
  1427. config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
  1428. if (p->wait_on_read)
  1429. config1 |= GPMC_CONFIG1_WAIT_READ_MON;
  1430. if (p->wait_on_write)
  1431. config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
  1432. if (p->wait_on_read || p->wait_on_write)
  1433. config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
  1434. if (p->device_nand)
  1435. config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
  1436. if (p->mux_add_data)
  1437. config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
  1438. if (p->burst_read)
  1439. config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
  1440. if (p->burst_write)
  1441. config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
  1442. if (p->burst_read || p->burst_write) {
  1443. config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
  1444. config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
  1445. }
  1446. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
  1447. return 0;
  1448. }
  1449. #ifdef CONFIG_OF
  1450. static const struct of_device_id gpmc_dt_ids[] = {
  1451. { .compatible = "ti,omap2420-gpmc" },
  1452. { .compatible = "ti,omap2430-gpmc" },
  1453. { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
  1454. { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
  1455. { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
  1456. { }
  1457. };
  1458. MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
  1459. /**
  1460. * gpmc_read_settings_dt - read gpmc settings from device-tree
  1461. * @np: pointer to device-tree node for a gpmc child device
  1462. * @p: pointer to gpmc settings structure
  1463. *
  1464. * Reads the GPMC settings for a GPMC child device from device-tree and
  1465. * stores them in the GPMC settings structure passed. The GPMC settings
  1466. * structure is initialised to zero by this function and so any
  1467. * previously stored settings will be cleared.
  1468. */
  1469. void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
  1470. {
  1471. memset(p, 0, sizeof(struct gpmc_settings));
  1472. p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
  1473. p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
  1474. of_property_read_u32(np, "gpmc,device-width", &p->device_width);
  1475. of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
  1476. if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
  1477. p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
  1478. p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
  1479. p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
  1480. if (!p->burst_read && !p->burst_write)
  1481. pr_warn("%s: page/burst-length set but not used!\n",
  1482. __func__);
  1483. }
  1484. if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
  1485. p->wait_on_read = of_property_read_bool(np,
  1486. "gpmc,wait-on-read");
  1487. p->wait_on_write = of_property_read_bool(np,
  1488. "gpmc,wait-on-write");
  1489. if (!p->wait_on_read && !p->wait_on_write)
  1490. pr_debug("%s: rd/wr wait monitoring not enabled!\n",
  1491. __func__);
  1492. }
  1493. }
  1494. static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
  1495. struct gpmc_timings *gpmc_t)
  1496. {
  1497. struct gpmc_bool_timings *p;
  1498. if (!np || !gpmc_t)
  1499. return;
  1500. memset(gpmc_t, 0, sizeof(*gpmc_t));
  1501. /* minimum clock period for syncronous mode */
  1502. of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
  1503. /* chip select timtings */
  1504. of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
  1505. of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
  1506. of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
  1507. /* ADV signal timings */
  1508. of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
  1509. of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
  1510. of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
  1511. of_property_read_u32(np, "gpmc,adv-aad-mux-on-ns",
  1512. &gpmc_t->adv_aad_mux_on);
  1513. of_property_read_u32(np, "gpmc,adv-aad-mux-rd-off-ns",
  1514. &gpmc_t->adv_aad_mux_rd_off);
  1515. of_property_read_u32(np, "gpmc,adv-aad-mux-wr-off-ns",
  1516. &gpmc_t->adv_aad_mux_wr_off);
  1517. /* WE signal timings */
  1518. of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
  1519. of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
  1520. /* OE signal timings */
  1521. of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
  1522. of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
  1523. of_property_read_u32(np, "gpmc,oe-aad-mux-on-ns",
  1524. &gpmc_t->oe_aad_mux_on);
  1525. of_property_read_u32(np, "gpmc,oe-aad-mux-off-ns",
  1526. &gpmc_t->oe_aad_mux_off);
  1527. /* access and cycle timings */
  1528. of_property_read_u32(np, "gpmc,page-burst-access-ns",
  1529. &gpmc_t->page_burst_access);
  1530. of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
  1531. of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
  1532. of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
  1533. of_property_read_u32(np, "gpmc,bus-turnaround-ns",
  1534. &gpmc_t->bus_turnaround);
  1535. of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns",
  1536. &gpmc_t->cycle2cycle_delay);
  1537. of_property_read_u32(np, "gpmc,wait-monitoring-ns",
  1538. &gpmc_t->wait_monitoring);
  1539. of_property_read_u32(np, "gpmc,clk-activation-ns",
  1540. &gpmc_t->clk_activation);
  1541. /* only applicable to OMAP3+ */
  1542. of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
  1543. of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns",
  1544. &gpmc_t->wr_data_mux_bus);
  1545. /* bool timing parameters */
  1546. p = &gpmc_t->bool_timings;
  1547. p->cycle2cyclediffcsen =
  1548. of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen");
  1549. p->cycle2cyclesamecsen =
  1550. of_property_read_bool(np, "gpmc,cycle2cycle-samecsen");
  1551. p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay");
  1552. p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay");
  1553. p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay");
  1554. p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay");
  1555. p->time_para_granularity =
  1556. of_property_read_bool(np, "gpmc,time-para-granularity");
  1557. }
  1558. #if IS_ENABLED(CONFIG_MTD_ONENAND)
  1559. static int gpmc_probe_onenand_child(struct platform_device *pdev,
  1560. struct device_node *child)
  1561. {
  1562. u32 val;
  1563. struct omap_onenand_platform_data *gpmc_onenand_data;
  1564. if (of_property_read_u32(child, "reg", &val) < 0) {
  1565. dev_err(&pdev->dev, "%s has no 'reg' property\n",
  1566. child->full_name);
  1567. return -ENODEV;
  1568. }
  1569. gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
  1570. GFP_KERNEL);
  1571. if (!gpmc_onenand_data)
  1572. return -ENOMEM;
  1573. gpmc_onenand_data->cs = val;
  1574. gpmc_onenand_data->of_node = child;
  1575. gpmc_onenand_data->dma_channel = -1;
  1576. if (!of_property_read_u32(child, "dma-channel", &val))
  1577. gpmc_onenand_data->dma_channel = val;
  1578. gpmc_onenand_init(gpmc_onenand_data);
  1579. return 0;
  1580. }
  1581. #else
  1582. static int gpmc_probe_onenand_child(struct platform_device *pdev,
  1583. struct device_node *child)
  1584. {
  1585. return 0;
  1586. }
  1587. #endif
  1588. /**
  1589. * gpmc_probe_generic_child - configures the gpmc for a child device
  1590. * @pdev: pointer to gpmc platform device
  1591. * @child: pointer to device-tree node for child device
  1592. *
  1593. * Allocates and configures a GPMC chip-select for a child device.
  1594. * Returns 0 on success and appropriate negative error code on failure.
  1595. */
  1596. static int gpmc_probe_generic_child(struct platform_device *pdev,
  1597. struct device_node *child)
  1598. {
  1599. struct gpmc_settings gpmc_s;
  1600. struct gpmc_timings gpmc_t;
  1601. struct resource res;
  1602. unsigned long base;
  1603. const char *name;
  1604. int ret, cs;
  1605. u32 val;
  1606. if (of_property_read_u32(child, "reg", &cs) < 0) {
  1607. dev_err(&pdev->dev, "%s has no 'reg' property\n",
  1608. child->full_name);
  1609. return -ENODEV;
  1610. }
  1611. if (of_address_to_resource(child, 0, &res) < 0) {
  1612. dev_err(&pdev->dev, "%s has malformed 'reg' property\n",
  1613. child->full_name);
  1614. return -ENODEV;
  1615. }
  1616. /*
  1617. * Check if we have multiple instances of the same device
  1618. * on a single chip select. If so, use the already initialized
  1619. * timings.
  1620. */
  1621. name = gpmc_cs_get_name(cs);
  1622. if (name && child->name && of_node_cmp(child->name, name) == 0)
  1623. goto no_timings;
  1624. ret = gpmc_cs_request(cs, resource_size(&res), &base);
  1625. if (ret < 0) {
  1626. dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
  1627. return ret;
  1628. }
  1629. gpmc_cs_set_name(cs, child->name);
  1630. gpmc_read_settings_dt(child, &gpmc_s);
  1631. gpmc_read_timings_dt(child, &gpmc_t);
  1632. /*
  1633. * For some GPMC devices we still need to rely on the bootloader
  1634. * timings because the devices can be connected via FPGA.
  1635. * REVISIT: Add timing support from slls644g.pdf.
  1636. */
  1637. if (!gpmc_t.cs_rd_off) {
  1638. WARN(1, "enable GPMC debug to configure .dts timings for CS%i\n",
  1639. cs);
  1640. gpmc_cs_show_timings(cs,
  1641. "please add GPMC bootloader timings to .dts");
  1642. goto no_timings;
  1643. }
  1644. /* CS must be disabled while making changes to gpmc configuration */
  1645. gpmc_cs_disable_mem(cs);
  1646. /*
  1647. * FIXME: gpmc_cs_request() will map the CS to an arbitary
  1648. * location in the gpmc address space. When booting with
  1649. * device-tree we want the NOR flash to be mapped to the
  1650. * location specified in the device-tree blob. So remap the
  1651. * CS to this location. Once DT migration is complete should
  1652. * just make gpmc_cs_request() map a specific address.
  1653. */
  1654. ret = gpmc_cs_remap(cs, res.start);
  1655. if (ret < 0) {
  1656. dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n",
  1657. cs, &res.start);
  1658. goto err;
  1659. }
  1660. if (of_node_cmp(child->name, "nand") == 0) {
  1661. /* Warn about older DT blobs with no compatible property */
  1662. if (!of_property_read_bool(child, "compatible")) {
  1663. dev_warn(&pdev->dev,
  1664. "Incompatible NAND node: missing compatible");
  1665. ret = -EINVAL;
  1666. goto err;
  1667. }
  1668. }
  1669. if (of_device_is_compatible(child, "ti,omap2-nand")) {
  1670. /* NAND specific setup */
  1671. val = of_get_nand_bus_width(child);
  1672. switch (val) {
  1673. case 8:
  1674. gpmc_s.device_width = GPMC_DEVWIDTH_8BIT;
  1675. break;
  1676. case 16:
  1677. gpmc_s.device_width = GPMC_DEVWIDTH_16BIT;
  1678. break;
  1679. default:
  1680. dev_err(&pdev->dev, "%s: invalid 'nand-bus-width'\n",
  1681. child->name);
  1682. ret = -EINVAL;
  1683. goto err;
  1684. }
  1685. /* disable write protect */
  1686. gpmc_configure(GPMC_CONFIG_WP, 0);
  1687. gpmc_s.device_nand = true;
  1688. } else {
  1689. ret = of_property_read_u32(child, "bank-width",
  1690. &gpmc_s.device_width);
  1691. if (ret < 0)
  1692. goto err;
  1693. }
  1694. gpmc_cs_show_timings(cs, "before gpmc_cs_program_settings");
  1695. ret = gpmc_cs_program_settings(cs, &gpmc_s);
  1696. if (ret < 0)
  1697. goto err;
  1698. ret = gpmc_cs_set_timings(cs, &gpmc_t, &gpmc_s);
  1699. if (ret) {
  1700. dev_err(&pdev->dev, "failed to set gpmc timings for: %s\n",
  1701. child->name);
  1702. goto err;
  1703. }
  1704. /* Clear limited address i.e. enable A26-A11 */
  1705. val = gpmc_read_reg(GPMC_CONFIG);
  1706. val &= ~GPMC_CONFIG_LIMITEDADDRESS;
  1707. gpmc_write_reg(GPMC_CONFIG, val);
  1708. /* Enable CS region */
  1709. gpmc_cs_enable_mem(cs);
  1710. no_timings:
  1711. /* create platform device, NULL on error or when disabled */
  1712. if (!of_platform_device_create(child, NULL, &pdev->dev))
  1713. goto err_child_fail;
  1714. /* is child a common bus? */
  1715. if (of_match_node(of_default_bus_match_table, child))
  1716. /* create children and other common bus children */
  1717. if (of_platform_populate(child, of_default_bus_match_table,
  1718. NULL, &pdev->dev))
  1719. goto err_child_fail;
  1720. return 0;
  1721. err_child_fail:
  1722. dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
  1723. ret = -ENODEV;
  1724. err:
  1725. gpmc_cs_free(cs);
  1726. return ret;
  1727. }
  1728. static int gpmc_probe_dt(struct platform_device *pdev)
  1729. {
  1730. int ret;
  1731. struct device_node *child;
  1732. const struct of_device_id *of_id =
  1733. of_match_device(gpmc_dt_ids, &pdev->dev);
  1734. if (!of_id)
  1735. return 0;
  1736. ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs",
  1737. &gpmc_cs_num);
  1738. if (ret < 0) {
  1739. pr_err("%s: number of chip-selects not defined\n", __func__);
  1740. return ret;
  1741. } else if (gpmc_cs_num < 1) {
  1742. pr_err("%s: all chip-selects are disabled\n", __func__);
  1743. return -EINVAL;
  1744. } else if (gpmc_cs_num > GPMC_CS_NUM) {
  1745. pr_err("%s: number of supported chip-selects cannot be > %d\n",
  1746. __func__, GPMC_CS_NUM);
  1747. return -EINVAL;
  1748. }
  1749. ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
  1750. &gpmc_nr_waitpins);
  1751. if (ret < 0) {
  1752. pr_err("%s: number of wait pins not found!\n", __func__);
  1753. return ret;
  1754. }
  1755. for_each_available_child_of_node(pdev->dev.of_node, child) {
  1756. if (!child->name)
  1757. continue;
  1758. if (of_node_cmp(child->name, "onenand") == 0)
  1759. ret = gpmc_probe_onenand_child(pdev, child);
  1760. else
  1761. ret = gpmc_probe_generic_child(pdev, child);
  1762. }
  1763. return 0;
  1764. }
  1765. #else
  1766. static int gpmc_probe_dt(struct platform_device *pdev)
  1767. {
  1768. return 0;
  1769. }
  1770. #endif
  1771. static int gpmc_probe(struct platform_device *pdev)
  1772. {
  1773. int rc;
  1774. u32 l;
  1775. struct resource *res;
  1776. struct gpmc_device *gpmc;
  1777. gpmc = devm_kzalloc(&pdev->dev, sizeof(*gpmc), GFP_KERNEL);
  1778. if (!gpmc)
  1779. return -ENOMEM;
  1780. gpmc->dev = &pdev->dev;
  1781. platform_set_drvdata(pdev, gpmc);
  1782. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1783. if (res == NULL)
  1784. return -ENOENT;
  1785. phys_base = res->start;
  1786. mem_size = resource_size(res);
  1787. gpmc_base = devm_ioremap_resource(&pdev->dev, res);
  1788. if (IS_ERR(gpmc_base))
  1789. return PTR_ERR(gpmc_base);
  1790. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1791. if (!res) {
  1792. dev_err(&pdev->dev, "Failed to get resource: irq\n");
  1793. return -ENOENT;
  1794. }
  1795. gpmc->irq = res->start;
  1796. gpmc_l3_clk = devm_clk_get(&pdev->dev, "fck");
  1797. if (IS_ERR(gpmc_l3_clk)) {
  1798. dev_err(&pdev->dev, "Failed to get GPMC fck\n");
  1799. return PTR_ERR(gpmc_l3_clk);
  1800. }
  1801. if (!clk_get_rate(gpmc_l3_clk)) {
  1802. dev_err(&pdev->dev, "Invalid GPMC fck clock rate\n");
  1803. return -EINVAL;
  1804. }
  1805. pm_runtime_enable(&pdev->dev);
  1806. pm_runtime_get_sync(&pdev->dev);
  1807. l = gpmc_read_reg(GPMC_REVISION);
  1808. /*
  1809. * FIXME: Once device-tree migration is complete the below flags
  1810. * should be populated based upon the device-tree compatible
  1811. * string. For now just use the IP revision. OMAP3+ devices have
  1812. * the wr_access and wr_data_mux_bus register fields. OMAP4+
  1813. * devices support the addr-addr-data multiplex protocol.
  1814. *
  1815. * GPMC IP revisions:
  1816. * - OMAP24xx = 2.0
  1817. * - OMAP3xxx = 5.0
  1818. * - OMAP44xx/54xx/AM335x = 6.0
  1819. */
  1820. if (GPMC_REVISION_MAJOR(l) > 0x4)
  1821. gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
  1822. if (GPMC_REVISION_MAJOR(l) > 0x5)
  1823. gpmc_capability |= GPMC_HAS_MUX_AAD;
  1824. dev_info(gpmc->dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
  1825. GPMC_REVISION_MINOR(l));
  1826. gpmc_mem_init();
  1827. rc = gpmc_setup_irq(gpmc);
  1828. if (rc) {
  1829. dev_err(gpmc->dev, "gpmc_setup_irq failed\n");
  1830. goto fail;
  1831. }
  1832. if (!pdev->dev.of_node) {
  1833. gpmc_cs_num = GPMC_CS_NUM;
  1834. gpmc_nr_waitpins = GPMC_NR_WAITPINS;
  1835. }
  1836. rc = gpmc_probe_dt(pdev);
  1837. if (rc < 0) {
  1838. dev_err(gpmc->dev, "failed to probe DT parameters\n");
  1839. gpmc_free_irq(gpmc);
  1840. goto fail;
  1841. }
  1842. return 0;
  1843. fail:
  1844. pm_runtime_put_sync(&pdev->dev);
  1845. return rc;
  1846. }
  1847. static int gpmc_remove(struct platform_device *pdev)
  1848. {
  1849. struct gpmc_device *gpmc = platform_get_drvdata(pdev);
  1850. gpmc_free_irq(gpmc);
  1851. gpmc_mem_exit();
  1852. pm_runtime_put_sync(&pdev->dev);
  1853. pm_runtime_disable(&pdev->dev);
  1854. return 0;
  1855. }
  1856. #ifdef CONFIG_PM_SLEEP
  1857. static int gpmc_suspend(struct device *dev)
  1858. {
  1859. omap3_gpmc_save_context();
  1860. pm_runtime_put_sync(dev);
  1861. return 0;
  1862. }
  1863. static int gpmc_resume(struct device *dev)
  1864. {
  1865. pm_runtime_get_sync(dev);
  1866. omap3_gpmc_restore_context();
  1867. return 0;
  1868. }
  1869. #endif
  1870. static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume);
  1871. static struct platform_driver gpmc_driver = {
  1872. .probe = gpmc_probe,
  1873. .remove = gpmc_remove,
  1874. .driver = {
  1875. .name = DEVICE_NAME,
  1876. .of_match_table = of_match_ptr(gpmc_dt_ids),
  1877. .pm = &gpmc_pm_ops,
  1878. },
  1879. };
  1880. static __init int gpmc_init(void)
  1881. {
  1882. return platform_driver_register(&gpmc_driver);
  1883. }
  1884. static __exit void gpmc_exit(void)
  1885. {
  1886. platform_driver_unregister(&gpmc_driver);
  1887. }
  1888. postcore_initcall(gpmc_init);
  1889. module_exit(gpmc_exit);
  1890. static struct omap3_gpmc_regs gpmc_context;
  1891. void omap3_gpmc_save_context(void)
  1892. {
  1893. int i;
  1894. if (!gpmc_base)
  1895. return;
  1896. gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
  1897. gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
  1898. gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
  1899. gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
  1900. gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
  1901. gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
  1902. gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
  1903. for (i = 0; i < gpmc_cs_num; i++) {
  1904. gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
  1905. if (gpmc_context.cs_context[i].is_valid) {
  1906. gpmc_context.cs_context[i].config1 =
  1907. gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
  1908. gpmc_context.cs_context[i].config2 =
  1909. gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
  1910. gpmc_context.cs_context[i].config3 =
  1911. gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
  1912. gpmc_context.cs_context[i].config4 =
  1913. gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
  1914. gpmc_context.cs_context[i].config5 =
  1915. gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
  1916. gpmc_context.cs_context[i].config6 =
  1917. gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
  1918. gpmc_context.cs_context[i].config7 =
  1919. gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
  1920. }
  1921. }
  1922. }
  1923. void omap3_gpmc_restore_context(void)
  1924. {
  1925. int i;
  1926. if (!gpmc_base)
  1927. return;
  1928. gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
  1929. gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
  1930. gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
  1931. gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
  1932. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
  1933. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
  1934. gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
  1935. for (i = 0; i < gpmc_cs_num; i++) {
  1936. if (gpmc_context.cs_context[i].is_valid) {
  1937. gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
  1938. gpmc_context.cs_context[i].config1);
  1939. gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
  1940. gpmc_context.cs_context[i].config2);
  1941. gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
  1942. gpmc_context.cs_context[i].config3);
  1943. gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
  1944. gpmc_context.cs_context[i].config4);
  1945. gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
  1946. gpmc_context.cs_context[i].config5);
  1947. gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
  1948. gpmc_context.cs_context[i].config6);
  1949. gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
  1950. gpmc_context.cs_context[i].config7);
  1951. }
  1952. }
  1953. }