gpmc.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881
  1. /*
  2. * GPMC support functions
  3. *
  4. * Copyright (C) 2005-2006 Nokia Corporation
  5. *
  6. * Author: Juha Yrjola
  7. *
  8. * Copyright (C) 2009 Texas Instruments
  9. * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #undef DEBUG
  16. #include <linux/irq.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/err.h>
  20. #include <linux/clk.h>
  21. #include <linux/ioport.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/io.h>
  24. #include <linux/module.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/of.h>
  28. #include <linux/of_address.h>
  29. #include <linux/of_mtd.h>
  30. #include <linux/of_device.h>
  31. #include <linux/mtd/nand.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/platform_data/mtd-nand-omap2.h>
  34. #include <asm/mach-types.h>
  35. #include "soc.h"
  36. #include "common.h"
  37. #include "omap_device.h"
  38. #include "gpmc.h"
  39. #include "gpmc-nand.h"
  40. #include "gpmc-onenand.h"
  41. #define DEVICE_NAME "omap-gpmc"
  42. /* GPMC register offsets */
  43. #define GPMC_REVISION 0x00
  44. #define GPMC_SYSCONFIG 0x10
  45. #define GPMC_SYSSTATUS 0x14
  46. #define GPMC_IRQSTATUS 0x18
  47. #define GPMC_IRQENABLE 0x1c
  48. #define GPMC_TIMEOUT_CONTROL 0x40
  49. #define GPMC_ERR_ADDRESS 0x44
  50. #define GPMC_ERR_TYPE 0x48
  51. #define GPMC_CONFIG 0x50
  52. #define GPMC_STATUS 0x54
  53. #define GPMC_PREFETCH_CONFIG1 0x1e0
  54. #define GPMC_PREFETCH_CONFIG2 0x1e4
  55. #define GPMC_PREFETCH_CONTROL 0x1ec
  56. #define GPMC_PREFETCH_STATUS 0x1f0
  57. #define GPMC_ECC_CONFIG 0x1f4
  58. #define GPMC_ECC_CONTROL 0x1f8
  59. #define GPMC_ECC_SIZE_CONFIG 0x1fc
  60. #define GPMC_ECC1_RESULT 0x200
  61. #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
  62. #define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */
  63. #define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */
  64. #define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */
  65. /* GPMC ECC control settings */
  66. #define GPMC_ECC_CTRL_ECCCLEAR 0x100
  67. #define GPMC_ECC_CTRL_ECCDISABLE 0x000
  68. #define GPMC_ECC_CTRL_ECCREG1 0x001
  69. #define GPMC_ECC_CTRL_ECCREG2 0x002
  70. #define GPMC_ECC_CTRL_ECCREG3 0x003
  71. #define GPMC_ECC_CTRL_ECCREG4 0x004
  72. #define GPMC_ECC_CTRL_ECCREG5 0x005
  73. #define GPMC_ECC_CTRL_ECCREG6 0x006
  74. #define GPMC_ECC_CTRL_ECCREG7 0x007
  75. #define GPMC_ECC_CTRL_ECCREG8 0x008
  76. #define GPMC_ECC_CTRL_ECCREG9 0x009
  77. #define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
  78. #define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
  79. #define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
  80. #define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
  81. #define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
  82. #define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
  83. #define GPMC_CS0_OFFSET 0x60
  84. #define GPMC_CS_SIZE 0x30
  85. #define GPMC_BCH_SIZE 0x10
  86. #define GPMC_MEM_END 0x3FFFFFFF
  87. #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
  88. #define GPMC_SECTION_SHIFT 28 /* 128 MB */
  89. #define CS_NUM_SHIFT 24
  90. #define ENABLE_PREFETCH (0x1 << 7)
  91. #define DMA_MPU_MODE 2
  92. #define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
  93. #define GPMC_REVISION_MINOR(l) (l & 0xf)
  94. #define GPMC_HAS_WR_ACCESS 0x1
  95. #define GPMC_HAS_WR_DATA_MUX_BUS 0x2
  96. #define GPMC_HAS_MUX_AAD 0x4
  97. #define GPMC_NR_WAITPINS 4
  98. /* XXX: Only NAND irq has been considered,currently these are the only ones used
  99. */
  100. #define GPMC_NR_IRQ 2
  101. struct gpmc_client_irq {
  102. unsigned irq;
  103. u32 bitmask;
  104. };
  105. /* Structure to save gpmc cs context */
  106. struct gpmc_cs_config {
  107. u32 config1;
  108. u32 config2;
  109. u32 config3;
  110. u32 config4;
  111. u32 config5;
  112. u32 config6;
  113. u32 config7;
  114. int is_valid;
  115. };
  116. /*
  117. * Structure to save/restore gpmc context
  118. * to support core off on OMAP3
  119. */
  120. struct omap3_gpmc_regs {
  121. u32 sysconfig;
  122. u32 irqenable;
  123. u32 timeout_ctrl;
  124. u32 config;
  125. u32 prefetch_config1;
  126. u32 prefetch_config2;
  127. u32 prefetch_control;
  128. struct gpmc_cs_config cs_context[GPMC_CS_NUM];
  129. };
  130. static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
  131. static struct irq_chip gpmc_irq_chip;
  132. static int gpmc_irq_start;
  133. static struct resource gpmc_mem_root;
  134. static struct resource gpmc_cs_mem[GPMC_CS_NUM];
  135. static DEFINE_SPINLOCK(gpmc_mem_lock);
  136. /* Define chip-selects as reserved by default until probe completes */
  137. static unsigned int gpmc_cs_map = ((1 << GPMC_CS_NUM) - 1);
  138. static unsigned int gpmc_cs_num = GPMC_CS_NUM;
  139. static unsigned int gpmc_nr_waitpins;
  140. static struct device *gpmc_dev;
  141. static int gpmc_irq;
  142. static resource_size_t phys_base, mem_size;
  143. static unsigned gpmc_capability;
  144. static void __iomem *gpmc_base;
  145. static struct clk *gpmc_l3_clk;
  146. static irqreturn_t gpmc_handle_irq(int irq, void *dev);
  147. static void gpmc_write_reg(int idx, u32 val)
  148. {
  149. __raw_writel(val, gpmc_base + idx);
  150. }
  151. static u32 gpmc_read_reg(int idx)
  152. {
  153. return __raw_readl(gpmc_base + idx);
  154. }
  155. void gpmc_cs_write_reg(int cs, int idx, u32 val)
  156. {
  157. void __iomem *reg_addr;
  158. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  159. __raw_writel(val, reg_addr);
  160. }
  161. static u32 gpmc_cs_read_reg(int cs, int idx)
  162. {
  163. void __iomem *reg_addr;
  164. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  165. return __raw_readl(reg_addr);
  166. }
  167. /* TODO: Add support for gpmc_fck to clock framework and use it */
  168. static unsigned long gpmc_get_fclk_period(void)
  169. {
  170. unsigned long rate = clk_get_rate(gpmc_l3_clk);
  171. if (rate == 0) {
  172. printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
  173. return 0;
  174. }
  175. rate /= 1000;
  176. rate = 1000000000 / rate; /* In picoseconds */
  177. return rate;
  178. }
  179. static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
  180. {
  181. unsigned long tick_ps;
  182. /* Calculate in picosecs to yield more exact results */
  183. tick_ps = gpmc_get_fclk_period();
  184. return (time_ns * 1000 + tick_ps - 1) / tick_ps;
  185. }
  186. static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
  187. {
  188. unsigned long tick_ps;
  189. /* Calculate in picosecs to yield more exact results */
  190. tick_ps = gpmc_get_fclk_period();
  191. return (time_ps + tick_ps - 1) / tick_ps;
  192. }
  193. unsigned int gpmc_ticks_to_ns(unsigned int ticks)
  194. {
  195. return ticks * gpmc_get_fclk_period() / 1000;
  196. }
  197. static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
  198. {
  199. return ticks * gpmc_get_fclk_period();
  200. }
  201. static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
  202. {
  203. unsigned long ticks = gpmc_ps_to_ticks(time_ps);
  204. return ticks * gpmc_get_fclk_period();
  205. }
  206. static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
  207. {
  208. u32 l;
  209. l = gpmc_cs_read_reg(cs, reg);
  210. if (value)
  211. l |= mask;
  212. else
  213. l &= ~mask;
  214. gpmc_cs_write_reg(cs, reg, l);
  215. }
  216. static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
  217. {
  218. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
  219. GPMC_CONFIG1_TIME_PARA_GRAN,
  220. p->time_para_granularity);
  221. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
  222. GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
  223. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
  224. GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
  225. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
  226. GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
  227. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
  228. GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay);
  229. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
  230. GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
  231. p->cycle2cyclesamecsen);
  232. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
  233. GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
  234. p->cycle2cyclediffcsen);
  235. }
  236. #ifdef DEBUG
  237. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  238. int time, const char *name)
  239. #else
  240. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  241. int time)
  242. #endif
  243. {
  244. u32 l;
  245. int ticks, mask, nr_bits;
  246. if (time == 0)
  247. ticks = 0;
  248. else
  249. ticks = gpmc_ns_to_ticks(time);
  250. nr_bits = end_bit - st_bit + 1;
  251. if (ticks >= 1 << nr_bits) {
  252. #ifdef DEBUG
  253. printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
  254. cs, name, time, ticks, 1 << nr_bits);
  255. #endif
  256. return -1;
  257. }
  258. mask = (1 << nr_bits) - 1;
  259. l = gpmc_cs_read_reg(cs, reg);
  260. #ifdef DEBUG
  261. printk(KERN_INFO
  262. "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
  263. cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
  264. (l >> st_bit) & mask, time);
  265. #endif
  266. l &= ~(mask << st_bit);
  267. l |= ticks << st_bit;
  268. gpmc_cs_write_reg(cs, reg, l);
  269. return 0;
  270. }
  271. #ifdef DEBUG
  272. #define GPMC_SET_ONE(reg, st, end, field) \
  273. if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
  274. t->field, #field) < 0) \
  275. return -1
  276. #else
  277. #define GPMC_SET_ONE(reg, st, end, field) \
  278. if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
  279. return -1
  280. #endif
  281. int gpmc_calc_divider(unsigned int sync_clk)
  282. {
  283. int div;
  284. u32 l;
  285. l = sync_clk + (gpmc_get_fclk_period() - 1);
  286. div = l / gpmc_get_fclk_period();
  287. if (div > 4)
  288. return -1;
  289. if (div <= 0)
  290. div = 1;
  291. return div;
  292. }
  293. int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
  294. {
  295. int div;
  296. u32 l;
  297. div = gpmc_calc_divider(t->sync_clk);
  298. if (div < 0)
  299. return div;
  300. GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
  301. GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
  302. GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
  303. GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
  304. GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
  305. GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
  306. GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
  307. GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
  308. GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
  309. GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
  310. GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
  311. GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
  312. GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
  313. GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
  314. GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
  315. GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
  316. GPMC_SET_ONE(GPMC_CS_CONFIG1, 18, 19, wait_monitoring);
  317. GPMC_SET_ONE(GPMC_CS_CONFIG1, 25, 26, clk_activation);
  318. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  319. GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
  320. if (gpmc_capability & GPMC_HAS_WR_ACCESS)
  321. GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
  322. /* caller is expected to have initialized CONFIG1 to cover
  323. * at least sync vs async
  324. */
  325. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  326. if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
  327. #ifdef DEBUG
  328. printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
  329. cs, (div * gpmc_get_fclk_period()) / 1000, div);
  330. #endif
  331. l &= ~0x03;
  332. l |= (div - 1);
  333. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
  334. }
  335. gpmc_cs_bool_timings(cs, &t->bool_timings);
  336. return 0;
  337. }
  338. static int gpmc_cs_enable_mem(int cs, u32 base, u32 size)
  339. {
  340. u32 l;
  341. u32 mask;
  342. /*
  343. * Ensure that base address is aligned on a
  344. * boundary equal to or greater than size.
  345. */
  346. if (base & (size - 1))
  347. return -EINVAL;
  348. mask = (1 << GPMC_SECTION_SHIFT) - size;
  349. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  350. l &= ~0x3f;
  351. l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
  352. l &= ~(0x0f << 8);
  353. l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
  354. l |= GPMC_CONFIG7_CSVALID;
  355. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  356. return 0;
  357. }
  358. static void gpmc_cs_disable_mem(int cs)
  359. {
  360. u32 l;
  361. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  362. l &= ~GPMC_CONFIG7_CSVALID;
  363. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  364. }
  365. static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
  366. {
  367. u32 l;
  368. u32 mask;
  369. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  370. *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
  371. mask = (l >> 8) & 0x0f;
  372. *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
  373. }
  374. static int gpmc_cs_mem_enabled(int cs)
  375. {
  376. u32 l;
  377. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  378. return l & GPMC_CONFIG7_CSVALID;
  379. }
  380. static void gpmc_cs_set_reserved(int cs, int reserved)
  381. {
  382. gpmc_cs_map &= ~(1 << cs);
  383. gpmc_cs_map |= (reserved ? 1 : 0) << cs;
  384. }
  385. static bool gpmc_cs_reserved(int cs)
  386. {
  387. return gpmc_cs_map & (1 << cs);
  388. }
  389. static unsigned long gpmc_mem_align(unsigned long size)
  390. {
  391. int order;
  392. size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
  393. order = GPMC_CHUNK_SHIFT - 1;
  394. do {
  395. size >>= 1;
  396. order++;
  397. } while (size);
  398. size = 1 << order;
  399. return size;
  400. }
  401. static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
  402. {
  403. struct resource *res = &gpmc_cs_mem[cs];
  404. int r;
  405. size = gpmc_mem_align(size);
  406. spin_lock(&gpmc_mem_lock);
  407. res->start = base;
  408. res->end = base + size - 1;
  409. r = request_resource(&gpmc_mem_root, res);
  410. spin_unlock(&gpmc_mem_lock);
  411. return r;
  412. }
  413. static int gpmc_cs_delete_mem(int cs)
  414. {
  415. struct resource *res = &gpmc_cs_mem[cs];
  416. int r;
  417. spin_lock(&gpmc_mem_lock);
  418. r = release_resource(&gpmc_cs_mem[cs]);
  419. res->start = 0;
  420. res->end = 0;
  421. spin_unlock(&gpmc_mem_lock);
  422. return r;
  423. }
  424. /**
  425. * gpmc_cs_remap - remaps a chip-select physical base address
  426. * @cs: chip-select to remap
  427. * @base: physical base address to re-map chip-select to
  428. *
  429. * Re-maps a chip-select to a new physical base address specified by
  430. * "base". Returns 0 on success and appropriate negative error code
  431. * on failure.
  432. */
  433. static int gpmc_cs_remap(int cs, u32 base)
  434. {
  435. int ret;
  436. u32 old_base, size;
  437. if (cs > gpmc_cs_num) {
  438. pr_err("%s: requested chip-select is disabled\n", __func__);
  439. return -ENODEV;
  440. }
  441. gpmc_cs_get_memconf(cs, &old_base, &size);
  442. if (base == old_base)
  443. return 0;
  444. gpmc_cs_disable_mem(cs);
  445. ret = gpmc_cs_delete_mem(cs);
  446. if (ret < 0)
  447. return ret;
  448. ret = gpmc_cs_insert_mem(cs, base, size);
  449. if (ret < 0)
  450. return ret;
  451. ret = gpmc_cs_enable_mem(cs, base, size);
  452. if (ret < 0)
  453. return ret;
  454. return 0;
  455. }
  456. int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
  457. {
  458. struct resource *res = &gpmc_cs_mem[cs];
  459. int r = -1;
  460. if (cs > gpmc_cs_num) {
  461. pr_err("%s: requested chip-select is disabled\n", __func__);
  462. return -ENODEV;
  463. }
  464. size = gpmc_mem_align(size);
  465. if (size > (1 << GPMC_SECTION_SHIFT))
  466. return -ENOMEM;
  467. spin_lock(&gpmc_mem_lock);
  468. if (gpmc_cs_reserved(cs)) {
  469. r = -EBUSY;
  470. goto out;
  471. }
  472. if (gpmc_cs_mem_enabled(cs))
  473. r = adjust_resource(res, res->start & ~(size - 1), size);
  474. if (r < 0)
  475. r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
  476. size, NULL, NULL);
  477. if (r < 0)
  478. goto out;
  479. r = gpmc_cs_enable_mem(cs, res->start, resource_size(res));
  480. if (r < 0) {
  481. release_resource(res);
  482. goto out;
  483. }
  484. *base = res->start;
  485. gpmc_cs_set_reserved(cs, 1);
  486. out:
  487. spin_unlock(&gpmc_mem_lock);
  488. return r;
  489. }
  490. EXPORT_SYMBOL(gpmc_cs_request);
  491. void gpmc_cs_free(int cs)
  492. {
  493. spin_lock(&gpmc_mem_lock);
  494. if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) {
  495. printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
  496. BUG();
  497. spin_unlock(&gpmc_mem_lock);
  498. return;
  499. }
  500. gpmc_cs_disable_mem(cs);
  501. release_resource(&gpmc_cs_mem[cs]);
  502. gpmc_cs_set_reserved(cs, 0);
  503. spin_unlock(&gpmc_mem_lock);
  504. }
  505. EXPORT_SYMBOL(gpmc_cs_free);
  506. /**
  507. * gpmc_configure - write request to configure gpmc
  508. * @cmd: command type
  509. * @wval: value to write
  510. * @return status of the operation
  511. */
  512. int gpmc_configure(int cmd, int wval)
  513. {
  514. u32 regval;
  515. switch (cmd) {
  516. case GPMC_ENABLE_IRQ:
  517. gpmc_write_reg(GPMC_IRQENABLE, wval);
  518. break;
  519. case GPMC_SET_IRQ_STATUS:
  520. gpmc_write_reg(GPMC_IRQSTATUS, wval);
  521. break;
  522. case GPMC_CONFIG_WP:
  523. regval = gpmc_read_reg(GPMC_CONFIG);
  524. if (wval)
  525. regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
  526. else
  527. regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
  528. gpmc_write_reg(GPMC_CONFIG, regval);
  529. break;
  530. default:
  531. pr_err("%s: command not supported\n", __func__);
  532. return -EINVAL;
  533. }
  534. return 0;
  535. }
  536. EXPORT_SYMBOL(gpmc_configure);
  537. void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
  538. {
  539. int i;
  540. reg->gpmc_status = gpmc_base + GPMC_STATUS;
  541. reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
  542. GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
  543. reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
  544. GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
  545. reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
  546. GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
  547. reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
  548. reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
  549. reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
  550. reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
  551. reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
  552. reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
  553. reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
  554. reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
  555. for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
  556. reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
  557. GPMC_BCH_SIZE * i;
  558. reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
  559. GPMC_BCH_SIZE * i;
  560. reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
  561. GPMC_BCH_SIZE * i;
  562. reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
  563. GPMC_BCH_SIZE * i;
  564. }
  565. }
  566. int gpmc_get_client_irq(unsigned irq_config)
  567. {
  568. int i;
  569. if (hweight32(irq_config) > 1)
  570. return 0;
  571. for (i = 0; i < GPMC_NR_IRQ; i++)
  572. if (gpmc_client_irq[i].bitmask & irq_config)
  573. return gpmc_client_irq[i].irq;
  574. return 0;
  575. }
  576. static int gpmc_irq_endis(unsigned irq, bool endis)
  577. {
  578. int i;
  579. u32 regval;
  580. for (i = 0; i < GPMC_NR_IRQ; i++)
  581. if (irq == gpmc_client_irq[i].irq) {
  582. regval = gpmc_read_reg(GPMC_IRQENABLE);
  583. if (endis)
  584. regval |= gpmc_client_irq[i].bitmask;
  585. else
  586. regval &= ~gpmc_client_irq[i].bitmask;
  587. gpmc_write_reg(GPMC_IRQENABLE, regval);
  588. break;
  589. }
  590. return 0;
  591. }
  592. static void gpmc_irq_disable(struct irq_data *p)
  593. {
  594. gpmc_irq_endis(p->irq, false);
  595. }
  596. static void gpmc_irq_enable(struct irq_data *p)
  597. {
  598. gpmc_irq_endis(p->irq, true);
  599. }
  600. static void gpmc_irq_noop(struct irq_data *data) { }
  601. static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
  602. static int gpmc_setup_irq(void)
  603. {
  604. int i;
  605. u32 regval;
  606. if (!gpmc_irq)
  607. return -EINVAL;
  608. gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
  609. if (gpmc_irq_start < 0) {
  610. pr_err("irq_alloc_descs failed\n");
  611. return gpmc_irq_start;
  612. }
  613. gpmc_irq_chip.name = "gpmc";
  614. gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
  615. gpmc_irq_chip.irq_enable = gpmc_irq_enable;
  616. gpmc_irq_chip.irq_disable = gpmc_irq_disable;
  617. gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
  618. gpmc_irq_chip.irq_ack = gpmc_irq_noop;
  619. gpmc_irq_chip.irq_mask = gpmc_irq_noop;
  620. gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
  621. gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
  622. gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
  623. for (i = 0; i < GPMC_NR_IRQ; i++) {
  624. gpmc_client_irq[i].irq = gpmc_irq_start + i;
  625. irq_set_chip_and_handler(gpmc_client_irq[i].irq,
  626. &gpmc_irq_chip, handle_simple_irq);
  627. set_irq_flags(gpmc_client_irq[i].irq,
  628. IRQF_VALID | IRQF_NOAUTOEN);
  629. }
  630. /* Disable interrupts */
  631. gpmc_write_reg(GPMC_IRQENABLE, 0);
  632. /* clear interrupts */
  633. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  634. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  635. return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
  636. }
  637. static int gpmc_free_irq(void)
  638. {
  639. int i;
  640. if (gpmc_irq)
  641. free_irq(gpmc_irq, NULL);
  642. for (i = 0; i < GPMC_NR_IRQ; i++) {
  643. irq_set_handler(gpmc_client_irq[i].irq, NULL);
  644. irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip);
  645. irq_modify_status(gpmc_client_irq[i].irq, 0, 0);
  646. }
  647. irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ);
  648. return 0;
  649. }
  650. static void gpmc_mem_exit(void)
  651. {
  652. int cs;
  653. for (cs = 0; cs < gpmc_cs_num; cs++) {
  654. if (!gpmc_cs_mem_enabled(cs))
  655. continue;
  656. gpmc_cs_delete_mem(cs);
  657. }
  658. }
  659. static void gpmc_mem_init(void)
  660. {
  661. int cs;
  662. /*
  663. * The first 1MB of GPMC address space is typically mapped to
  664. * the internal ROM. Never allocate the first page, to
  665. * facilitate bug detection; even if we didn't boot from ROM.
  666. */
  667. gpmc_mem_root.start = SZ_1M;
  668. gpmc_mem_root.end = GPMC_MEM_END;
  669. /* Reserve all regions that has been set up by bootloader */
  670. for (cs = 0; cs < gpmc_cs_num; cs++) {
  671. u32 base, size;
  672. if (!gpmc_cs_mem_enabled(cs))
  673. continue;
  674. gpmc_cs_get_memconf(cs, &base, &size);
  675. if (gpmc_cs_insert_mem(cs, base, size)) {
  676. pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n",
  677. __func__, cs, base, base + size);
  678. gpmc_cs_disable_mem(cs);
  679. }
  680. }
  681. }
  682. static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
  683. {
  684. u32 temp;
  685. int div;
  686. div = gpmc_calc_divider(sync_clk);
  687. temp = gpmc_ps_to_ticks(time_ps);
  688. temp = (temp + div - 1) / div;
  689. return gpmc_ticks_to_ps(temp * div);
  690. }
  691. /* XXX: can the cycles be avoided ? */
  692. static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
  693. struct gpmc_device_timings *dev_t,
  694. bool mux)
  695. {
  696. u32 temp;
  697. /* adv_rd_off */
  698. temp = dev_t->t_avdp_r;
  699. /* XXX: mux check required ? */
  700. if (mux) {
  701. /* XXX: t_avdp not to be required for sync, only added for tusb
  702. * this indirectly necessitates requirement of t_avdp_r and
  703. * t_avdp_w instead of having a single t_avdp
  704. */
  705. temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
  706. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  707. }
  708. gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
  709. /* oe_on */
  710. temp = dev_t->t_oeasu; /* XXX: remove this ? */
  711. if (mux) {
  712. temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
  713. temp = max_t(u32, temp, gpmc_t->adv_rd_off +
  714. gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
  715. }
  716. gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
  717. /* access */
  718. /* XXX: any scope for improvement ?, by combining oe_on
  719. * and clk_activation, need to check whether
  720. * access = clk_activation + round to sync clk ?
  721. */
  722. temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
  723. temp += gpmc_t->clk_activation;
  724. if (dev_t->cyc_oe)
  725. temp = max_t(u32, temp, gpmc_t->oe_on +
  726. gpmc_ticks_to_ps(dev_t->cyc_oe));
  727. gpmc_t->access = gpmc_round_ps_to_ticks(temp);
  728. gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
  729. gpmc_t->cs_rd_off = gpmc_t->oe_off;
  730. /* rd_cycle */
  731. temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
  732. temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
  733. gpmc_t->access;
  734. /* XXX: barter t_ce_rdyz with t_cez_r ? */
  735. if (dev_t->t_ce_rdyz)
  736. temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
  737. gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
  738. return 0;
  739. }
  740. static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
  741. struct gpmc_device_timings *dev_t,
  742. bool mux)
  743. {
  744. u32 temp;
  745. /* adv_wr_off */
  746. temp = dev_t->t_avdp_w;
  747. if (mux) {
  748. temp = max_t(u32, temp,
  749. gpmc_t->clk_activation + dev_t->t_avdh);
  750. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  751. }
  752. gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
  753. /* wr_data_mux_bus */
  754. temp = max_t(u32, dev_t->t_weasu,
  755. gpmc_t->clk_activation + dev_t->t_rdyo);
  756. /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
  757. * and in that case remember to handle we_on properly
  758. */
  759. if (mux) {
  760. temp = max_t(u32, temp,
  761. gpmc_t->adv_wr_off + dev_t->t_aavdh);
  762. temp = max_t(u32, temp, gpmc_t->adv_wr_off +
  763. gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
  764. }
  765. gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
  766. /* we_on */
  767. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  768. gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
  769. else
  770. gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
  771. /* wr_access */
  772. /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
  773. gpmc_t->wr_access = gpmc_t->access;
  774. /* we_off */
  775. temp = gpmc_t->we_on + dev_t->t_wpl;
  776. temp = max_t(u32, temp,
  777. gpmc_t->wr_access + gpmc_ticks_to_ps(1));
  778. temp = max_t(u32, temp,
  779. gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
  780. gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
  781. gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
  782. dev_t->t_wph);
  783. /* wr_cycle */
  784. temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
  785. temp += gpmc_t->wr_access;
  786. /* XXX: barter t_ce_rdyz with t_cez_w ? */
  787. if (dev_t->t_ce_rdyz)
  788. temp = max_t(u32, temp,
  789. gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
  790. gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
  791. return 0;
  792. }
  793. static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
  794. struct gpmc_device_timings *dev_t,
  795. bool mux)
  796. {
  797. u32 temp;
  798. /* adv_rd_off */
  799. temp = dev_t->t_avdp_r;
  800. if (mux)
  801. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  802. gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
  803. /* oe_on */
  804. temp = dev_t->t_oeasu;
  805. if (mux)
  806. temp = max_t(u32, temp,
  807. gpmc_t->adv_rd_off + dev_t->t_aavdh);
  808. gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
  809. /* access */
  810. temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
  811. gpmc_t->oe_on + dev_t->t_oe);
  812. temp = max_t(u32, temp,
  813. gpmc_t->cs_on + dev_t->t_ce);
  814. temp = max_t(u32, temp,
  815. gpmc_t->adv_on + dev_t->t_aa);
  816. gpmc_t->access = gpmc_round_ps_to_ticks(temp);
  817. gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
  818. gpmc_t->cs_rd_off = gpmc_t->oe_off;
  819. /* rd_cycle */
  820. temp = max_t(u32, dev_t->t_rd_cycle,
  821. gpmc_t->cs_rd_off + dev_t->t_cez_r);
  822. temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
  823. gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
  824. return 0;
  825. }
  826. static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
  827. struct gpmc_device_timings *dev_t,
  828. bool mux)
  829. {
  830. u32 temp;
  831. /* adv_wr_off */
  832. temp = dev_t->t_avdp_w;
  833. if (mux)
  834. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  835. gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
  836. /* wr_data_mux_bus */
  837. temp = dev_t->t_weasu;
  838. if (mux) {
  839. temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
  840. temp = max_t(u32, temp, gpmc_t->adv_wr_off +
  841. gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
  842. }
  843. gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
  844. /* we_on */
  845. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  846. gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
  847. else
  848. gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
  849. /* we_off */
  850. temp = gpmc_t->we_on + dev_t->t_wpl;
  851. gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
  852. gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
  853. dev_t->t_wph);
  854. /* wr_cycle */
  855. temp = max_t(u32, dev_t->t_wr_cycle,
  856. gpmc_t->cs_wr_off + dev_t->t_cez_w);
  857. gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
  858. return 0;
  859. }
  860. static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
  861. struct gpmc_device_timings *dev_t)
  862. {
  863. u32 temp;
  864. gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
  865. gpmc_get_fclk_period();
  866. gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
  867. dev_t->t_bacc,
  868. gpmc_t->sync_clk);
  869. temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
  870. gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
  871. if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
  872. return 0;
  873. if (dev_t->ce_xdelay)
  874. gpmc_t->bool_timings.cs_extra_delay = true;
  875. if (dev_t->avd_xdelay)
  876. gpmc_t->bool_timings.adv_extra_delay = true;
  877. if (dev_t->oe_xdelay)
  878. gpmc_t->bool_timings.oe_extra_delay = true;
  879. if (dev_t->we_xdelay)
  880. gpmc_t->bool_timings.we_extra_delay = true;
  881. return 0;
  882. }
  883. static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
  884. struct gpmc_device_timings *dev_t,
  885. bool sync)
  886. {
  887. u32 temp;
  888. /* cs_on */
  889. gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
  890. /* adv_on */
  891. temp = dev_t->t_avdasu;
  892. if (dev_t->t_ce_avd)
  893. temp = max_t(u32, temp,
  894. gpmc_t->cs_on + dev_t->t_ce_avd);
  895. gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
  896. if (sync)
  897. gpmc_calc_sync_common_timings(gpmc_t, dev_t);
  898. return 0;
  899. }
  900. /* TODO: remove this function once all peripherals are confirmed to
  901. * work with generic timing. Simultaneously gpmc_cs_set_timings()
  902. * has to be modified to handle timings in ps instead of ns
  903. */
  904. static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
  905. {
  906. t->cs_on /= 1000;
  907. t->cs_rd_off /= 1000;
  908. t->cs_wr_off /= 1000;
  909. t->adv_on /= 1000;
  910. t->adv_rd_off /= 1000;
  911. t->adv_wr_off /= 1000;
  912. t->we_on /= 1000;
  913. t->we_off /= 1000;
  914. t->oe_on /= 1000;
  915. t->oe_off /= 1000;
  916. t->page_burst_access /= 1000;
  917. t->access /= 1000;
  918. t->rd_cycle /= 1000;
  919. t->wr_cycle /= 1000;
  920. t->bus_turnaround /= 1000;
  921. t->cycle2cycle_delay /= 1000;
  922. t->wait_monitoring /= 1000;
  923. t->clk_activation /= 1000;
  924. t->wr_access /= 1000;
  925. t->wr_data_mux_bus /= 1000;
  926. }
  927. int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
  928. struct gpmc_settings *gpmc_s,
  929. struct gpmc_device_timings *dev_t)
  930. {
  931. bool mux = false, sync = false;
  932. if (gpmc_s) {
  933. mux = gpmc_s->mux_add_data ? true : false;
  934. sync = (gpmc_s->sync_read || gpmc_s->sync_write);
  935. }
  936. memset(gpmc_t, 0, sizeof(*gpmc_t));
  937. gpmc_calc_common_timings(gpmc_t, dev_t, sync);
  938. if (gpmc_s && gpmc_s->sync_read)
  939. gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
  940. else
  941. gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
  942. if (gpmc_s && gpmc_s->sync_write)
  943. gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
  944. else
  945. gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
  946. /* TODO: remove, see function definition */
  947. gpmc_convert_ps_to_ns(gpmc_t);
  948. return 0;
  949. }
  950. /**
  951. * gpmc_cs_program_settings - programs non-timing related settings
  952. * @cs: GPMC chip-select to program
  953. * @p: pointer to GPMC settings structure
  954. *
  955. * Programs non-timing related settings for a GPMC chip-select, such as
  956. * bus-width, burst configuration, etc. Function should be called once
  957. * for each chip-select that is being used and must be called before
  958. * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
  959. * register will be initialised to zero by this function. Returns 0 on
  960. * success and appropriate negative error code on failure.
  961. */
  962. int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
  963. {
  964. u32 config1;
  965. if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
  966. pr_err("%s: invalid width %d!", __func__, p->device_width);
  967. return -EINVAL;
  968. }
  969. /* Address-data multiplexing not supported for NAND devices */
  970. if (p->device_nand && p->mux_add_data) {
  971. pr_err("%s: invalid configuration!\n", __func__);
  972. return -EINVAL;
  973. }
  974. if ((p->mux_add_data > GPMC_MUX_AD) ||
  975. ((p->mux_add_data == GPMC_MUX_AAD) &&
  976. !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
  977. pr_err("%s: invalid multiplex configuration!\n", __func__);
  978. return -EINVAL;
  979. }
  980. /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
  981. if (p->burst_read || p->burst_write) {
  982. switch (p->burst_len) {
  983. case GPMC_BURST_4:
  984. case GPMC_BURST_8:
  985. case GPMC_BURST_16:
  986. break;
  987. default:
  988. pr_err("%s: invalid page/burst-length (%d)\n",
  989. __func__, p->burst_len);
  990. return -EINVAL;
  991. }
  992. }
  993. if ((p->wait_on_read || p->wait_on_write) &&
  994. (p->wait_pin > gpmc_nr_waitpins)) {
  995. pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
  996. return -EINVAL;
  997. }
  998. config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
  999. if (p->sync_read)
  1000. config1 |= GPMC_CONFIG1_READTYPE_SYNC;
  1001. if (p->sync_write)
  1002. config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
  1003. if (p->wait_on_read)
  1004. config1 |= GPMC_CONFIG1_WAIT_READ_MON;
  1005. if (p->wait_on_write)
  1006. config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
  1007. if (p->wait_on_read || p->wait_on_write)
  1008. config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
  1009. if (p->device_nand)
  1010. config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
  1011. if (p->mux_add_data)
  1012. config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
  1013. if (p->burst_read)
  1014. config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
  1015. if (p->burst_write)
  1016. config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
  1017. if (p->burst_read || p->burst_write) {
  1018. config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
  1019. config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
  1020. }
  1021. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
  1022. return 0;
  1023. }
  1024. #ifdef CONFIG_OF
  1025. static struct of_device_id gpmc_dt_ids[] = {
  1026. { .compatible = "ti,omap2420-gpmc" },
  1027. { .compatible = "ti,omap2430-gpmc" },
  1028. { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
  1029. { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
  1030. { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
  1031. { }
  1032. };
  1033. MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
  1034. /**
  1035. * gpmc_read_settings_dt - read gpmc settings from device-tree
  1036. * @np: pointer to device-tree node for a gpmc child device
  1037. * @p: pointer to gpmc settings structure
  1038. *
  1039. * Reads the GPMC settings for a GPMC child device from device-tree and
  1040. * stores them in the GPMC settings structure passed. The GPMC settings
  1041. * structure is initialised to zero by this function and so any
  1042. * previously stored settings will be cleared.
  1043. */
  1044. void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
  1045. {
  1046. memset(p, 0, sizeof(struct gpmc_settings));
  1047. p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
  1048. p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
  1049. of_property_read_u32(np, "gpmc,device-width", &p->device_width);
  1050. of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
  1051. if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
  1052. p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
  1053. p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
  1054. p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
  1055. if (!p->burst_read && !p->burst_write)
  1056. pr_warn("%s: page/burst-length set but not used!\n",
  1057. __func__);
  1058. }
  1059. if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
  1060. p->wait_on_read = of_property_read_bool(np,
  1061. "gpmc,wait-on-read");
  1062. p->wait_on_write = of_property_read_bool(np,
  1063. "gpmc,wait-on-write");
  1064. if (!p->wait_on_read && !p->wait_on_write)
  1065. pr_warn("%s: read/write wait monitoring not enabled!\n",
  1066. __func__);
  1067. }
  1068. }
  1069. static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
  1070. struct gpmc_timings *gpmc_t)
  1071. {
  1072. struct gpmc_bool_timings *p;
  1073. if (!np || !gpmc_t)
  1074. return;
  1075. memset(gpmc_t, 0, sizeof(*gpmc_t));
  1076. /* minimum clock period for syncronous mode */
  1077. of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
  1078. /* chip select timtings */
  1079. of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
  1080. of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
  1081. of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
  1082. /* ADV signal timings */
  1083. of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
  1084. of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
  1085. of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
  1086. /* WE signal timings */
  1087. of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
  1088. of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
  1089. /* OE signal timings */
  1090. of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
  1091. of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
  1092. /* access and cycle timings */
  1093. of_property_read_u32(np, "gpmc,page-burst-access-ns",
  1094. &gpmc_t->page_burst_access);
  1095. of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
  1096. of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
  1097. of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
  1098. of_property_read_u32(np, "gpmc,bus-turnaround-ns",
  1099. &gpmc_t->bus_turnaround);
  1100. of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns",
  1101. &gpmc_t->cycle2cycle_delay);
  1102. of_property_read_u32(np, "gpmc,wait-monitoring-ns",
  1103. &gpmc_t->wait_monitoring);
  1104. of_property_read_u32(np, "gpmc,clk-activation-ns",
  1105. &gpmc_t->clk_activation);
  1106. /* only applicable to OMAP3+ */
  1107. of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
  1108. of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns",
  1109. &gpmc_t->wr_data_mux_bus);
  1110. /* bool timing parameters */
  1111. p = &gpmc_t->bool_timings;
  1112. p->cycle2cyclediffcsen =
  1113. of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen");
  1114. p->cycle2cyclesamecsen =
  1115. of_property_read_bool(np, "gpmc,cycle2cycle-samecsen");
  1116. p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay");
  1117. p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay");
  1118. p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay");
  1119. p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay");
  1120. p->time_para_granularity =
  1121. of_property_read_bool(np, "gpmc,time-para-granularity");
  1122. }
  1123. #ifdef CONFIG_MTD_NAND
  1124. static const char * const nand_xfer_types[] = {
  1125. [NAND_OMAP_PREFETCH_POLLED] = "prefetch-polled",
  1126. [NAND_OMAP_POLLED] = "polled",
  1127. [NAND_OMAP_PREFETCH_DMA] = "prefetch-dma",
  1128. [NAND_OMAP_PREFETCH_IRQ] = "prefetch-irq",
  1129. };
  1130. static int gpmc_probe_nand_child(struct platform_device *pdev,
  1131. struct device_node *child)
  1132. {
  1133. u32 val;
  1134. const char *s;
  1135. struct gpmc_timings gpmc_t;
  1136. struct omap_nand_platform_data *gpmc_nand_data;
  1137. if (of_property_read_u32(child, "reg", &val) < 0) {
  1138. dev_err(&pdev->dev, "%s has no 'reg' property\n",
  1139. child->full_name);
  1140. return -ENODEV;
  1141. }
  1142. gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data),
  1143. GFP_KERNEL);
  1144. if (!gpmc_nand_data)
  1145. return -ENOMEM;
  1146. gpmc_nand_data->cs = val;
  1147. gpmc_nand_data->of_node = child;
  1148. /* Detect availability of ELM module */
  1149. gpmc_nand_data->elm_of_node = of_parse_phandle(child, "ti,elm-id", 0);
  1150. if (gpmc_nand_data->elm_of_node == NULL)
  1151. gpmc_nand_data->elm_of_node =
  1152. of_parse_phandle(child, "elm_id", 0);
  1153. if (gpmc_nand_data->elm_of_node == NULL)
  1154. pr_warn("%s: ti,elm-id property not found\n", __func__);
  1155. /* select ecc-scheme for NAND */
  1156. if (of_property_read_string(child, "ti,nand-ecc-opt", &s)) {
  1157. pr_err("%s: ti,nand-ecc-opt not found\n", __func__);
  1158. return -ENODEV;
  1159. }
  1160. if (!strcmp(s, "ham1") || !strcmp(s, "sw") ||
  1161. !strcmp(s, "hw") || !strcmp(s, "hw-romcode"))
  1162. gpmc_nand_data->ecc_opt =
  1163. OMAP_ECC_HAM1_CODE_HW;
  1164. else if (!strcmp(s, "bch4"))
  1165. if (gpmc_nand_data->elm_of_node)
  1166. gpmc_nand_data->ecc_opt =
  1167. OMAP_ECC_BCH4_CODE_HW;
  1168. else
  1169. gpmc_nand_data->ecc_opt =
  1170. OMAP_ECC_BCH4_CODE_HW_DETECTION_SW;
  1171. else if (!strcmp(s, "bch8"))
  1172. if (gpmc_nand_data->elm_of_node)
  1173. gpmc_nand_data->ecc_opt =
  1174. OMAP_ECC_BCH8_CODE_HW;
  1175. else
  1176. gpmc_nand_data->ecc_opt =
  1177. OMAP_ECC_BCH8_CODE_HW_DETECTION_SW;
  1178. else
  1179. pr_err("%s: ti,nand-ecc-opt invalid value\n", __func__);
  1180. /* select data transfer mode for NAND controller */
  1181. if (!of_property_read_string(child, "ti,nand-xfer-type", &s))
  1182. for (val = 0; val < ARRAY_SIZE(nand_xfer_types); val++)
  1183. if (!strcasecmp(s, nand_xfer_types[val])) {
  1184. gpmc_nand_data->xfer_type = val;
  1185. break;
  1186. }
  1187. val = of_get_nand_bus_width(child);
  1188. if (val == 16)
  1189. gpmc_nand_data->devsize = NAND_BUSWIDTH_16;
  1190. gpmc_read_timings_dt(child, &gpmc_t);
  1191. gpmc_nand_init(gpmc_nand_data, &gpmc_t);
  1192. return 0;
  1193. }
  1194. #else
  1195. static int gpmc_probe_nand_child(struct platform_device *pdev,
  1196. struct device_node *child)
  1197. {
  1198. return 0;
  1199. }
  1200. #endif
  1201. #ifdef CONFIG_MTD_ONENAND
  1202. static int gpmc_probe_onenand_child(struct platform_device *pdev,
  1203. struct device_node *child)
  1204. {
  1205. u32 val;
  1206. struct omap_onenand_platform_data *gpmc_onenand_data;
  1207. if (of_property_read_u32(child, "reg", &val) < 0) {
  1208. dev_err(&pdev->dev, "%s has no 'reg' property\n",
  1209. child->full_name);
  1210. return -ENODEV;
  1211. }
  1212. gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
  1213. GFP_KERNEL);
  1214. if (!gpmc_onenand_data)
  1215. return -ENOMEM;
  1216. gpmc_onenand_data->cs = val;
  1217. gpmc_onenand_data->of_node = child;
  1218. gpmc_onenand_data->dma_channel = -1;
  1219. if (!of_property_read_u32(child, "dma-channel", &val))
  1220. gpmc_onenand_data->dma_channel = val;
  1221. gpmc_onenand_init(gpmc_onenand_data);
  1222. return 0;
  1223. }
  1224. #else
  1225. static int gpmc_probe_onenand_child(struct platform_device *pdev,
  1226. struct device_node *child)
  1227. {
  1228. return 0;
  1229. }
  1230. #endif
  1231. /**
  1232. * gpmc_probe_generic_child - configures the gpmc for a child device
  1233. * @pdev: pointer to gpmc platform device
  1234. * @child: pointer to device-tree node for child device
  1235. *
  1236. * Allocates and configures a GPMC chip-select for a child device.
  1237. * Returns 0 on success and appropriate negative error code on failure.
  1238. */
  1239. static int gpmc_probe_generic_child(struct platform_device *pdev,
  1240. struct device_node *child)
  1241. {
  1242. struct gpmc_settings gpmc_s;
  1243. struct gpmc_timings gpmc_t;
  1244. struct resource res;
  1245. unsigned long base;
  1246. int ret, cs;
  1247. if (of_property_read_u32(child, "reg", &cs) < 0) {
  1248. dev_err(&pdev->dev, "%s has no 'reg' property\n",
  1249. child->full_name);
  1250. return -ENODEV;
  1251. }
  1252. if (of_address_to_resource(child, 0, &res) < 0) {
  1253. dev_err(&pdev->dev, "%s has malformed 'reg' property\n",
  1254. child->full_name);
  1255. return -ENODEV;
  1256. }
  1257. ret = gpmc_cs_request(cs, resource_size(&res), &base);
  1258. if (ret < 0) {
  1259. dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
  1260. return ret;
  1261. }
  1262. /*
  1263. * FIXME: gpmc_cs_request() will map the CS to an arbitary
  1264. * location in the gpmc address space. When booting with
  1265. * device-tree we want the NOR flash to be mapped to the
  1266. * location specified in the device-tree blob. So remap the
  1267. * CS to this location. Once DT migration is complete should
  1268. * just make gpmc_cs_request() map a specific address.
  1269. */
  1270. ret = gpmc_cs_remap(cs, res.start);
  1271. if (ret < 0) {
  1272. dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n",
  1273. cs, &res.start);
  1274. goto err;
  1275. }
  1276. gpmc_read_settings_dt(child, &gpmc_s);
  1277. ret = of_property_read_u32(child, "bank-width", &gpmc_s.device_width);
  1278. if (ret < 0)
  1279. goto err;
  1280. ret = gpmc_cs_program_settings(cs, &gpmc_s);
  1281. if (ret < 0)
  1282. goto err;
  1283. gpmc_read_timings_dt(child, &gpmc_t);
  1284. gpmc_cs_set_timings(cs, &gpmc_t);
  1285. if (of_platform_device_create(child, NULL, &pdev->dev))
  1286. return 0;
  1287. dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
  1288. ret = -ENODEV;
  1289. err:
  1290. gpmc_cs_free(cs);
  1291. return ret;
  1292. }
  1293. /*
  1294. * REVISIT: Add timing support from slls644g.pdf
  1295. */
  1296. static int gpmc_probe_8250(struct platform_device *pdev,
  1297. struct device_node *child)
  1298. {
  1299. struct resource res;
  1300. unsigned long base;
  1301. int ret, cs;
  1302. if (of_property_read_u32(child, "reg", &cs) < 0) {
  1303. dev_err(&pdev->dev, "%s has no 'reg' property\n",
  1304. child->full_name);
  1305. return -ENODEV;
  1306. }
  1307. if (of_address_to_resource(child, 0, &res) < 0) {
  1308. dev_err(&pdev->dev, "%s has malformed 'reg' property\n",
  1309. child->full_name);
  1310. return -ENODEV;
  1311. }
  1312. ret = gpmc_cs_request(cs, resource_size(&res), &base);
  1313. if (ret < 0) {
  1314. dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
  1315. return ret;
  1316. }
  1317. if (of_platform_device_create(child, NULL, &pdev->dev))
  1318. return 0;
  1319. dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
  1320. return -ENODEV;
  1321. }
  1322. static int gpmc_probe_dt(struct platform_device *pdev)
  1323. {
  1324. int ret;
  1325. struct device_node *child;
  1326. const struct of_device_id *of_id =
  1327. of_match_device(gpmc_dt_ids, &pdev->dev);
  1328. if (!of_id)
  1329. return 0;
  1330. ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs",
  1331. &gpmc_cs_num);
  1332. if (ret < 0) {
  1333. pr_err("%s: number of chip-selects not defined\n", __func__);
  1334. return ret;
  1335. } else if (gpmc_cs_num < 1) {
  1336. pr_err("%s: all chip-selects are disabled\n", __func__);
  1337. return -EINVAL;
  1338. } else if (gpmc_cs_num > GPMC_CS_NUM) {
  1339. pr_err("%s: number of supported chip-selects cannot be > %d\n",
  1340. __func__, GPMC_CS_NUM);
  1341. return -EINVAL;
  1342. }
  1343. ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
  1344. &gpmc_nr_waitpins);
  1345. if (ret < 0) {
  1346. pr_err("%s: number of wait pins not found!\n", __func__);
  1347. return ret;
  1348. }
  1349. for_each_child_of_node(pdev->dev.of_node, child) {
  1350. if (!child->name)
  1351. continue;
  1352. if (of_node_cmp(child->name, "nand") == 0)
  1353. ret = gpmc_probe_nand_child(pdev, child);
  1354. else if (of_node_cmp(child->name, "onenand") == 0)
  1355. ret = gpmc_probe_onenand_child(pdev, child);
  1356. else if (of_node_cmp(child->name, "ethernet") == 0 ||
  1357. of_node_cmp(child->name, "nor") == 0)
  1358. ret = gpmc_probe_generic_child(pdev, child);
  1359. else if (of_node_cmp(child->name, "8250") == 0)
  1360. ret = gpmc_probe_8250(pdev, child);
  1361. if (WARN(ret < 0, "%s: probing gpmc child %s failed\n",
  1362. __func__, child->full_name))
  1363. of_node_put(child);
  1364. }
  1365. return 0;
  1366. }
  1367. #else
  1368. static int gpmc_probe_dt(struct platform_device *pdev)
  1369. {
  1370. return 0;
  1371. }
  1372. #endif
  1373. static int gpmc_probe(struct platform_device *pdev)
  1374. {
  1375. int rc;
  1376. u32 l;
  1377. struct resource *res;
  1378. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1379. if (res == NULL)
  1380. return -ENOENT;
  1381. phys_base = res->start;
  1382. mem_size = resource_size(res);
  1383. gpmc_base = devm_ioremap_resource(&pdev->dev, res);
  1384. if (IS_ERR(gpmc_base))
  1385. return PTR_ERR(gpmc_base);
  1386. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1387. if (res == NULL)
  1388. dev_warn(&pdev->dev, "Failed to get resource: irq\n");
  1389. else
  1390. gpmc_irq = res->start;
  1391. gpmc_l3_clk = clk_get(&pdev->dev, "fck");
  1392. if (IS_ERR(gpmc_l3_clk)) {
  1393. dev_err(&pdev->dev, "error: clk_get\n");
  1394. gpmc_irq = 0;
  1395. return PTR_ERR(gpmc_l3_clk);
  1396. }
  1397. pm_runtime_enable(&pdev->dev);
  1398. pm_runtime_get_sync(&pdev->dev);
  1399. gpmc_dev = &pdev->dev;
  1400. l = gpmc_read_reg(GPMC_REVISION);
  1401. /*
  1402. * FIXME: Once device-tree migration is complete the below flags
  1403. * should be populated based upon the device-tree compatible
  1404. * string. For now just use the IP revision. OMAP3+ devices have
  1405. * the wr_access and wr_data_mux_bus register fields. OMAP4+
  1406. * devices support the addr-addr-data multiplex protocol.
  1407. *
  1408. * GPMC IP revisions:
  1409. * - OMAP24xx = 2.0
  1410. * - OMAP3xxx = 5.0
  1411. * - OMAP44xx/54xx/AM335x = 6.0
  1412. */
  1413. if (GPMC_REVISION_MAJOR(l) > 0x4)
  1414. gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
  1415. if (GPMC_REVISION_MAJOR(l) > 0x5)
  1416. gpmc_capability |= GPMC_HAS_MUX_AAD;
  1417. dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
  1418. GPMC_REVISION_MINOR(l));
  1419. gpmc_mem_init();
  1420. if (gpmc_setup_irq() < 0)
  1421. dev_warn(gpmc_dev, "gpmc_setup_irq failed\n");
  1422. /* Now the GPMC is initialised, unreserve the chip-selects */
  1423. gpmc_cs_map = 0;
  1424. if (!pdev->dev.of_node) {
  1425. gpmc_cs_num = GPMC_CS_NUM;
  1426. gpmc_nr_waitpins = GPMC_NR_WAITPINS;
  1427. }
  1428. rc = gpmc_probe_dt(pdev);
  1429. if (rc < 0) {
  1430. pm_runtime_put_sync(&pdev->dev);
  1431. clk_put(gpmc_l3_clk);
  1432. dev_err(gpmc_dev, "failed to probe DT parameters\n");
  1433. return rc;
  1434. }
  1435. return 0;
  1436. }
  1437. static int gpmc_remove(struct platform_device *pdev)
  1438. {
  1439. gpmc_free_irq();
  1440. gpmc_mem_exit();
  1441. pm_runtime_put_sync(&pdev->dev);
  1442. pm_runtime_disable(&pdev->dev);
  1443. gpmc_dev = NULL;
  1444. return 0;
  1445. }
  1446. #ifdef CONFIG_PM_SLEEP
  1447. static int gpmc_suspend(struct device *dev)
  1448. {
  1449. omap3_gpmc_save_context();
  1450. pm_runtime_put_sync(dev);
  1451. return 0;
  1452. }
  1453. static int gpmc_resume(struct device *dev)
  1454. {
  1455. pm_runtime_get_sync(dev);
  1456. omap3_gpmc_restore_context();
  1457. return 0;
  1458. }
  1459. #endif
  1460. static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume);
  1461. static struct platform_driver gpmc_driver = {
  1462. .probe = gpmc_probe,
  1463. .remove = gpmc_remove,
  1464. .driver = {
  1465. .name = DEVICE_NAME,
  1466. .owner = THIS_MODULE,
  1467. .of_match_table = of_match_ptr(gpmc_dt_ids),
  1468. .pm = &gpmc_pm_ops,
  1469. },
  1470. };
  1471. static __init int gpmc_init(void)
  1472. {
  1473. return platform_driver_register(&gpmc_driver);
  1474. }
  1475. static __exit void gpmc_exit(void)
  1476. {
  1477. platform_driver_unregister(&gpmc_driver);
  1478. }
  1479. omap_postcore_initcall(gpmc_init);
  1480. module_exit(gpmc_exit);
  1481. static int __init omap_gpmc_init(void)
  1482. {
  1483. struct omap_hwmod *oh;
  1484. struct platform_device *pdev;
  1485. char *oh_name = "gpmc";
  1486. /*
  1487. * if the board boots up with a populated DT, do not
  1488. * manually add the device from this initcall
  1489. */
  1490. if (of_have_populated_dt())
  1491. return -ENODEV;
  1492. oh = omap_hwmod_lookup(oh_name);
  1493. if (!oh) {
  1494. pr_err("Could not look up %s\n", oh_name);
  1495. return -ENODEV;
  1496. }
  1497. pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0);
  1498. WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name);
  1499. return PTR_RET(pdev);
  1500. }
  1501. omap_postcore_initcall(omap_gpmc_init);
  1502. static irqreturn_t gpmc_handle_irq(int irq, void *dev)
  1503. {
  1504. int i;
  1505. u32 regval;
  1506. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  1507. if (!regval)
  1508. return IRQ_NONE;
  1509. for (i = 0; i < GPMC_NR_IRQ; i++)
  1510. if (regval & gpmc_client_irq[i].bitmask)
  1511. generic_handle_irq(gpmc_client_irq[i].irq);
  1512. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  1513. return IRQ_HANDLED;
  1514. }
  1515. static struct omap3_gpmc_regs gpmc_context;
  1516. void omap3_gpmc_save_context(void)
  1517. {
  1518. int i;
  1519. gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
  1520. gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
  1521. gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
  1522. gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
  1523. gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
  1524. gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
  1525. gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
  1526. for (i = 0; i < gpmc_cs_num; i++) {
  1527. gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
  1528. if (gpmc_context.cs_context[i].is_valid) {
  1529. gpmc_context.cs_context[i].config1 =
  1530. gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
  1531. gpmc_context.cs_context[i].config2 =
  1532. gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
  1533. gpmc_context.cs_context[i].config3 =
  1534. gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
  1535. gpmc_context.cs_context[i].config4 =
  1536. gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
  1537. gpmc_context.cs_context[i].config5 =
  1538. gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
  1539. gpmc_context.cs_context[i].config6 =
  1540. gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
  1541. gpmc_context.cs_context[i].config7 =
  1542. gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
  1543. }
  1544. }
  1545. }
  1546. void omap3_gpmc_restore_context(void)
  1547. {
  1548. int i;
  1549. gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
  1550. gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
  1551. gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
  1552. gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
  1553. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
  1554. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
  1555. gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
  1556. for (i = 0; i < gpmc_cs_num; i++) {
  1557. if (gpmc_context.cs_context[i].is_valid) {
  1558. gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
  1559. gpmc_context.cs_context[i].config1);
  1560. gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
  1561. gpmc_context.cs_context[i].config2);
  1562. gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
  1563. gpmc_context.cs_context[i].config3);
  1564. gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
  1565. gpmc_context.cs_context[i].config4);
  1566. gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
  1567. gpmc_context.cs_context[i].config5);
  1568. gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
  1569. gpmc_context.cs_context[i].config6);
  1570. gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
  1571. gpmc_context.cs_context[i].config7);
  1572. }
  1573. }
  1574. }