omap-gpmc.c 69 KB

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