omap-gpmc.c 74 KB

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