gpmc.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  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/platform_data/mtd-nand-omap2.h>
  33. #include <asm/mach-types.h>
  34. #include "soc.h"
  35. #include "common.h"
  36. #include "omap_device.h"
  37. #include "gpmc.h"
  38. #include "gpmc-nand.h"
  39. #include "gpmc-onenand.h"
  40. #define DEVICE_NAME "omap-gpmc"
  41. /* GPMC register offsets */
  42. #define GPMC_REVISION 0x00
  43. #define GPMC_SYSCONFIG 0x10
  44. #define GPMC_SYSSTATUS 0x14
  45. #define GPMC_IRQSTATUS 0x18
  46. #define GPMC_IRQENABLE 0x1c
  47. #define GPMC_TIMEOUT_CONTROL 0x40
  48. #define GPMC_ERR_ADDRESS 0x44
  49. #define GPMC_ERR_TYPE 0x48
  50. #define GPMC_CONFIG 0x50
  51. #define GPMC_STATUS 0x54
  52. #define GPMC_PREFETCH_CONFIG1 0x1e0
  53. #define GPMC_PREFETCH_CONFIG2 0x1e4
  54. #define GPMC_PREFETCH_CONTROL 0x1ec
  55. #define GPMC_PREFETCH_STATUS 0x1f0
  56. #define GPMC_ECC_CONFIG 0x1f4
  57. #define GPMC_ECC_CONTROL 0x1f8
  58. #define GPMC_ECC_SIZE_CONFIG 0x1fc
  59. #define GPMC_ECC1_RESULT 0x200
  60. #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
  61. #define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */
  62. #define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */
  63. #define GPMC_ECC_BCH_RESULT_3 0x24c /* 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_CONFIG2_CSEXTRADELAY BIT(7)
  77. #define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
  78. #define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
  79. #define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
  80. #define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
  81. #define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
  82. #define GPMC_CS0_OFFSET 0x60
  83. #define GPMC_CS_SIZE 0x30
  84. #define GPMC_BCH_SIZE 0x10
  85. #define GPMC_MEM_START 0x00000000
  86. #define GPMC_MEM_END 0x3FFFFFFF
  87. #define BOOT_ROM_SPACE 0x100000 /* 1MB */
  88. #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
  89. #define GPMC_SECTION_SHIFT 28 /* 128 MB */
  90. #define CS_NUM_SHIFT 24
  91. #define ENABLE_PREFETCH (0x1 << 7)
  92. #define DMA_MPU_MODE 2
  93. #define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
  94. #define GPMC_REVISION_MINOR(l) (l & 0xf)
  95. #define GPMC_HAS_WR_ACCESS 0x1
  96. #define GPMC_HAS_WR_DATA_MUX_BUS 0x2
  97. #define GPMC_HAS_MUX_AAD 0x4
  98. #define GPMC_NR_WAITPINS 4
  99. /* XXX: Only NAND irq has been considered,currently these are the only ones used
  100. */
  101. #define GPMC_NR_IRQ 2
  102. struct gpmc_client_irq {
  103. unsigned irq;
  104. u32 bitmask;
  105. };
  106. /* Structure to save gpmc cs context */
  107. struct gpmc_cs_config {
  108. u32 config1;
  109. u32 config2;
  110. u32 config3;
  111. u32 config4;
  112. u32 config5;
  113. u32 config6;
  114. u32 config7;
  115. int is_valid;
  116. };
  117. /*
  118. * Structure to save/restore gpmc context
  119. * to support core off on OMAP3
  120. */
  121. struct omap3_gpmc_regs {
  122. u32 sysconfig;
  123. u32 irqenable;
  124. u32 timeout_ctrl;
  125. u32 config;
  126. u32 prefetch_config1;
  127. u32 prefetch_config2;
  128. u32 prefetch_control;
  129. struct gpmc_cs_config cs_context[GPMC_CS_NUM];
  130. };
  131. static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
  132. static struct irq_chip gpmc_irq_chip;
  133. static unsigned gpmc_irq_start;
  134. static struct resource gpmc_mem_root;
  135. static struct resource gpmc_cs_mem[GPMC_CS_NUM];
  136. static DEFINE_SPINLOCK(gpmc_mem_lock);
  137. /* Define chip-selects as reserved by default until probe completes */
  138. static unsigned int gpmc_cs_map = ((1 << GPMC_CS_NUM) - 1);
  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. return -ENODEV;
  439. gpmc_cs_get_memconf(cs, &old_base, &size);
  440. if (base == old_base)
  441. return 0;
  442. gpmc_cs_disable_mem(cs);
  443. ret = gpmc_cs_delete_mem(cs);
  444. if (ret < 0)
  445. return ret;
  446. ret = gpmc_cs_insert_mem(cs, base, size);
  447. if (ret < 0)
  448. return ret;
  449. ret = gpmc_cs_enable_mem(cs, base, size);
  450. if (ret < 0)
  451. return ret;
  452. return 0;
  453. }
  454. int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
  455. {
  456. struct resource *res = &gpmc_cs_mem[cs];
  457. int r = -1;
  458. if (cs > GPMC_CS_NUM)
  459. return -ENODEV;
  460. size = gpmc_mem_align(size);
  461. if (size > (1 << GPMC_SECTION_SHIFT))
  462. return -ENOMEM;
  463. spin_lock(&gpmc_mem_lock);
  464. if (gpmc_cs_reserved(cs)) {
  465. r = -EBUSY;
  466. goto out;
  467. }
  468. if (gpmc_cs_mem_enabled(cs))
  469. r = adjust_resource(res, res->start & ~(size - 1), size);
  470. if (r < 0)
  471. r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
  472. size, NULL, NULL);
  473. if (r < 0)
  474. goto out;
  475. r = gpmc_cs_enable_mem(cs, res->start, resource_size(res));
  476. if (r < 0) {
  477. release_resource(res);
  478. goto out;
  479. }
  480. *base = res->start;
  481. gpmc_cs_set_reserved(cs, 1);
  482. out:
  483. spin_unlock(&gpmc_mem_lock);
  484. return r;
  485. }
  486. EXPORT_SYMBOL(gpmc_cs_request);
  487. void gpmc_cs_free(int cs)
  488. {
  489. spin_lock(&gpmc_mem_lock);
  490. if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) {
  491. printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
  492. BUG();
  493. spin_unlock(&gpmc_mem_lock);
  494. return;
  495. }
  496. gpmc_cs_disable_mem(cs);
  497. release_resource(&gpmc_cs_mem[cs]);
  498. gpmc_cs_set_reserved(cs, 0);
  499. spin_unlock(&gpmc_mem_lock);
  500. }
  501. EXPORT_SYMBOL(gpmc_cs_free);
  502. /**
  503. * gpmc_configure - write request to configure gpmc
  504. * @cmd: command type
  505. * @wval: value to write
  506. * @return status of the operation
  507. */
  508. int gpmc_configure(int cmd, int wval)
  509. {
  510. u32 regval;
  511. switch (cmd) {
  512. case GPMC_ENABLE_IRQ:
  513. gpmc_write_reg(GPMC_IRQENABLE, wval);
  514. break;
  515. case GPMC_SET_IRQ_STATUS:
  516. gpmc_write_reg(GPMC_IRQSTATUS, wval);
  517. break;
  518. case GPMC_CONFIG_WP:
  519. regval = gpmc_read_reg(GPMC_CONFIG);
  520. if (wval)
  521. regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
  522. else
  523. regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
  524. gpmc_write_reg(GPMC_CONFIG, regval);
  525. break;
  526. default:
  527. pr_err("%s: command not supported\n", __func__);
  528. return -EINVAL;
  529. }
  530. return 0;
  531. }
  532. EXPORT_SYMBOL(gpmc_configure);
  533. void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
  534. {
  535. int i;
  536. reg->gpmc_status = gpmc_base + GPMC_STATUS;
  537. reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
  538. GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
  539. reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
  540. GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
  541. reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
  542. GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
  543. reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
  544. reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
  545. reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
  546. reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
  547. reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
  548. reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
  549. reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
  550. reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
  551. for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
  552. reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
  553. GPMC_BCH_SIZE * i;
  554. reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
  555. GPMC_BCH_SIZE * i;
  556. reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
  557. GPMC_BCH_SIZE * i;
  558. reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
  559. GPMC_BCH_SIZE * i;
  560. }
  561. }
  562. int gpmc_get_client_irq(unsigned irq_config)
  563. {
  564. int i;
  565. if (hweight32(irq_config) > 1)
  566. return 0;
  567. for (i = 0; i < GPMC_NR_IRQ; i++)
  568. if (gpmc_client_irq[i].bitmask & irq_config)
  569. return gpmc_client_irq[i].irq;
  570. return 0;
  571. }
  572. static int gpmc_irq_endis(unsigned irq, bool endis)
  573. {
  574. int i;
  575. u32 regval;
  576. for (i = 0; i < GPMC_NR_IRQ; i++)
  577. if (irq == gpmc_client_irq[i].irq) {
  578. regval = gpmc_read_reg(GPMC_IRQENABLE);
  579. if (endis)
  580. regval |= gpmc_client_irq[i].bitmask;
  581. else
  582. regval &= ~gpmc_client_irq[i].bitmask;
  583. gpmc_write_reg(GPMC_IRQENABLE, regval);
  584. break;
  585. }
  586. return 0;
  587. }
  588. static void gpmc_irq_disable(struct irq_data *p)
  589. {
  590. gpmc_irq_endis(p->irq, false);
  591. }
  592. static void gpmc_irq_enable(struct irq_data *p)
  593. {
  594. gpmc_irq_endis(p->irq, true);
  595. }
  596. static void gpmc_irq_noop(struct irq_data *data) { }
  597. static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
  598. static int gpmc_setup_irq(void)
  599. {
  600. int i;
  601. u32 regval;
  602. if (!gpmc_irq)
  603. return -EINVAL;
  604. gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
  605. if (gpmc_irq_start < 0) {
  606. pr_err("irq_alloc_descs failed\n");
  607. return gpmc_irq_start;
  608. }
  609. gpmc_irq_chip.name = "gpmc";
  610. gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
  611. gpmc_irq_chip.irq_enable = gpmc_irq_enable;
  612. gpmc_irq_chip.irq_disable = gpmc_irq_disable;
  613. gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
  614. gpmc_irq_chip.irq_ack = gpmc_irq_noop;
  615. gpmc_irq_chip.irq_mask = gpmc_irq_noop;
  616. gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
  617. gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
  618. gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
  619. for (i = 0; i < GPMC_NR_IRQ; i++) {
  620. gpmc_client_irq[i].irq = gpmc_irq_start + i;
  621. irq_set_chip_and_handler(gpmc_client_irq[i].irq,
  622. &gpmc_irq_chip, handle_simple_irq);
  623. set_irq_flags(gpmc_client_irq[i].irq,
  624. IRQF_VALID | IRQF_NOAUTOEN);
  625. }
  626. /* Disable interrupts */
  627. gpmc_write_reg(GPMC_IRQENABLE, 0);
  628. /* clear interrupts */
  629. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  630. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  631. return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
  632. }
  633. static int gpmc_free_irq(void)
  634. {
  635. int i;
  636. if (gpmc_irq)
  637. free_irq(gpmc_irq, NULL);
  638. for (i = 0; i < GPMC_NR_IRQ; i++) {
  639. irq_set_handler(gpmc_client_irq[i].irq, NULL);
  640. irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip);
  641. irq_modify_status(gpmc_client_irq[i].irq, 0, 0);
  642. }
  643. irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ);
  644. return 0;
  645. }
  646. static void gpmc_mem_exit(void)
  647. {
  648. int cs;
  649. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  650. if (!gpmc_cs_mem_enabled(cs))
  651. continue;
  652. gpmc_cs_delete_mem(cs);
  653. }
  654. }
  655. static int gpmc_mem_init(void)
  656. {
  657. int cs, rc;
  658. unsigned long boot_rom_space = 0;
  659. /* never allocate the first page, to facilitate bug detection;
  660. * even if we didn't boot from ROM.
  661. */
  662. boot_rom_space = BOOT_ROM_SPACE;
  663. gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
  664. gpmc_mem_root.end = GPMC_MEM_END;
  665. /* Reserve all regions that has been set up by bootloader */
  666. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  667. u32 base, size;
  668. if (!gpmc_cs_mem_enabled(cs))
  669. continue;
  670. gpmc_cs_get_memconf(cs, &base, &size);
  671. rc = gpmc_cs_insert_mem(cs, base, size);
  672. if (rc < 0) {
  673. while (--cs >= 0)
  674. if (gpmc_cs_mem_enabled(cs))
  675. gpmc_cs_delete_mem(cs);
  676. return rc;
  677. }
  678. }
  679. return 0;
  680. }
  681. static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
  682. {
  683. u32 temp;
  684. int div;
  685. div = gpmc_calc_divider(sync_clk);
  686. temp = gpmc_ps_to_ticks(time_ps);
  687. temp = (temp + div - 1) / div;
  688. return gpmc_ticks_to_ps(temp * div);
  689. }
  690. /* XXX: can the cycles be avoided ? */
  691. static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
  692. struct gpmc_device_timings *dev_t,
  693. bool mux)
  694. {
  695. u32 temp;
  696. /* adv_rd_off */
  697. temp = dev_t->t_avdp_r;
  698. /* XXX: mux check required ? */
  699. if (mux) {
  700. /* XXX: t_avdp not to be required for sync, only added for tusb
  701. * this indirectly necessitates requirement of t_avdp_r and
  702. * t_avdp_w instead of having a single t_avdp
  703. */
  704. temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
  705. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  706. }
  707. gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
  708. /* oe_on */
  709. temp = dev_t->t_oeasu; /* XXX: remove this ? */
  710. if (mux) {
  711. temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
  712. temp = max_t(u32, temp, gpmc_t->adv_rd_off +
  713. gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
  714. }
  715. gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
  716. /* access */
  717. /* XXX: any scope for improvement ?, by combining oe_on
  718. * and clk_activation, need to check whether
  719. * access = clk_activation + round to sync clk ?
  720. */
  721. temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
  722. temp += gpmc_t->clk_activation;
  723. if (dev_t->cyc_oe)
  724. temp = max_t(u32, temp, gpmc_t->oe_on +
  725. gpmc_ticks_to_ps(dev_t->cyc_oe));
  726. gpmc_t->access = gpmc_round_ps_to_ticks(temp);
  727. gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
  728. gpmc_t->cs_rd_off = gpmc_t->oe_off;
  729. /* rd_cycle */
  730. temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
  731. temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
  732. gpmc_t->access;
  733. /* XXX: barter t_ce_rdyz with t_cez_r ? */
  734. if (dev_t->t_ce_rdyz)
  735. temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
  736. gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
  737. return 0;
  738. }
  739. static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
  740. struct gpmc_device_timings *dev_t,
  741. bool mux)
  742. {
  743. u32 temp;
  744. /* adv_wr_off */
  745. temp = dev_t->t_avdp_w;
  746. if (mux) {
  747. temp = max_t(u32, temp,
  748. gpmc_t->clk_activation + dev_t->t_avdh);
  749. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  750. }
  751. gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
  752. /* wr_data_mux_bus */
  753. temp = max_t(u32, dev_t->t_weasu,
  754. gpmc_t->clk_activation + dev_t->t_rdyo);
  755. /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
  756. * and in that case remember to handle we_on properly
  757. */
  758. if (mux) {
  759. temp = max_t(u32, temp,
  760. gpmc_t->adv_wr_off + dev_t->t_aavdh);
  761. temp = max_t(u32, temp, gpmc_t->adv_wr_off +
  762. gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
  763. }
  764. gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
  765. /* we_on */
  766. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  767. gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
  768. else
  769. gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
  770. /* wr_access */
  771. /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
  772. gpmc_t->wr_access = gpmc_t->access;
  773. /* we_off */
  774. temp = gpmc_t->we_on + dev_t->t_wpl;
  775. temp = max_t(u32, temp,
  776. gpmc_t->wr_access + gpmc_ticks_to_ps(1));
  777. temp = max_t(u32, temp,
  778. gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
  779. gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
  780. gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
  781. dev_t->t_wph);
  782. /* wr_cycle */
  783. temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
  784. temp += gpmc_t->wr_access;
  785. /* XXX: barter t_ce_rdyz with t_cez_w ? */
  786. if (dev_t->t_ce_rdyz)
  787. temp = max_t(u32, temp,
  788. gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
  789. gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
  790. return 0;
  791. }
  792. static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
  793. struct gpmc_device_timings *dev_t,
  794. bool mux)
  795. {
  796. u32 temp;
  797. /* adv_rd_off */
  798. temp = dev_t->t_avdp_r;
  799. if (mux)
  800. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  801. gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
  802. /* oe_on */
  803. temp = dev_t->t_oeasu;
  804. if (mux)
  805. temp = max_t(u32, temp,
  806. gpmc_t->adv_rd_off + dev_t->t_aavdh);
  807. gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
  808. /* access */
  809. temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
  810. gpmc_t->oe_on + dev_t->t_oe);
  811. temp = max_t(u32, temp,
  812. gpmc_t->cs_on + dev_t->t_ce);
  813. temp = max_t(u32, temp,
  814. gpmc_t->adv_on + dev_t->t_aa);
  815. gpmc_t->access = gpmc_round_ps_to_ticks(temp);
  816. gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
  817. gpmc_t->cs_rd_off = gpmc_t->oe_off;
  818. /* rd_cycle */
  819. temp = max_t(u32, dev_t->t_rd_cycle,
  820. gpmc_t->cs_rd_off + dev_t->t_cez_r);
  821. temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
  822. gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
  823. return 0;
  824. }
  825. static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
  826. struct gpmc_device_timings *dev_t,
  827. bool mux)
  828. {
  829. u32 temp;
  830. /* adv_wr_off */
  831. temp = dev_t->t_avdp_w;
  832. if (mux)
  833. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  834. gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
  835. /* wr_data_mux_bus */
  836. temp = dev_t->t_weasu;
  837. if (mux) {
  838. temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
  839. temp = max_t(u32, temp, gpmc_t->adv_wr_off +
  840. gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
  841. }
  842. gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
  843. /* we_on */
  844. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  845. gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
  846. else
  847. gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
  848. /* we_off */
  849. temp = gpmc_t->we_on + dev_t->t_wpl;
  850. gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
  851. gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
  852. dev_t->t_wph);
  853. /* wr_cycle */
  854. temp = max_t(u32, dev_t->t_wr_cycle,
  855. gpmc_t->cs_wr_off + dev_t->t_cez_w);
  856. gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
  857. return 0;
  858. }
  859. static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
  860. struct gpmc_device_timings *dev_t)
  861. {
  862. u32 temp;
  863. gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
  864. gpmc_get_fclk_period();
  865. gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
  866. dev_t->t_bacc,
  867. gpmc_t->sync_clk);
  868. temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
  869. gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
  870. if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
  871. return 0;
  872. if (dev_t->ce_xdelay)
  873. gpmc_t->bool_timings.cs_extra_delay = true;
  874. if (dev_t->avd_xdelay)
  875. gpmc_t->bool_timings.adv_extra_delay = true;
  876. if (dev_t->oe_xdelay)
  877. gpmc_t->bool_timings.oe_extra_delay = true;
  878. if (dev_t->we_xdelay)
  879. gpmc_t->bool_timings.we_extra_delay = true;
  880. return 0;
  881. }
  882. static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
  883. struct gpmc_device_timings *dev_t,
  884. bool sync)
  885. {
  886. u32 temp;
  887. /* cs_on */
  888. gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
  889. /* adv_on */
  890. temp = dev_t->t_avdasu;
  891. if (dev_t->t_ce_avd)
  892. temp = max_t(u32, temp,
  893. gpmc_t->cs_on + dev_t->t_ce_avd);
  894. gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
  895. if (sync)
  896. gpmc_calc_sync_common_timings(gpmc_t, dev_t);
  897. return 0;
  898. }
  899. /* TODO: remove this function once all peripherals are confirmed to
  900. * work with generic timing. Simultaneously gpmc_cs_set_timings()
  901. * has to be modified to handle timings in ps instead of ns
  902. */
  903. static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
  904. {
  905. t->cs_on /= 1000;
  906. t->cs_rd_off /= 1000;
  907. t->cs_wr_off /= 1000;
  908. t->adv_on /= 1000;
  909. t->adv_rd_off /= 1000;
  910. t->adv_wr_off /= 1000;
  911. t->we_on /= 1000;
  912. t->we_off /= 1000;
  913. t->oe_on /= 1000;
  914. t->oe_off /= 1000;
  915. t->page_burst_access /= 1000;
  916. t->access /= 1000;
  917. t->rd_cycle /= 1000;
  918. t->wr_cycle /= 1000;
  919. t->bus_turnaround /= 1000;
  920. t->cycle2cycle_delay /= 1000;
  921. t->wait_monitoring /= 1000;
  922. t->clk_activation /= 1000;
  923. t->wr_access /= 1000;
  924. t->wr_data_mux_bus /= 1000;
  925. }
  926. int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
  927. struct gpmc_settings *gpmc_s,
  928. struct gpmc_device_timings *dev_t)
  929. {
  930. bool mux = false, sync = false;
  931. if (gpmc_s) {
  932. mux = gpmc_s->mux_add_data ? true : false;
  933. sync = (gpmc_s->sync_read || gpmc_s->sync_write);
  934. }
  935. memset(gpmc_t, 0, sizeof(*gpmc_t));
  936. gpmc_calc_common_timings(gpmc_t, dev_t, sync);
  937. if (gpmc_s && gpmc_s->sync_read)
  938. gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
  939. else
  940. gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
  941. if (gpmc_s && gpmc_s->sync_write)
  942. gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
  943. else
  944. gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
  945. /* TODO: remove, see function definition */
  946. gpmc_convert_ps_to_ns(gpmc_t);
  947. return 0;
  948. }
  949. /**
  950. * gpmc_cs_program_settings - programs non-timing related settings
  951. * @cs: GPMC chip-select to program
  952. * @p: pointer to GPMC settings structure
  953. *
  954. * Programs non-timing related settings for a GPMC chip-select, such as
  955. * bus-width, burst configuration, etc. Function should be called once
  956. * for each chip-select that is being used and must be called before
  957. * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
  958. * register will be initialised to zero by this function. Returns 0 on
  959. * success and appropriate negative error code on failure.
  960. */
  961. int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
  962. {
  963. u32 config1;
  964. if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
  965. pr_err("%s: invalid width %d!", __func__, p->device_width);
  966. return -EINVAL;
  967. }
  968. /* Address-data multiplexing not supported for NAND devices */
  969. if (p->device_nand && p->mux_add_data) {
  970. pr_err("%s: invalid configuration!\n", __func__);
  971. return -EINVAL;
  972. }
  973. if ((p->mux_add_data > GPMC_MUX_AD) ||
  974. ((p->mux_add_data == GPMC_MUX_AAD) &&
  975. !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
  976. pr_err("%s: invalid multiplex configuration!\n", __func__);
  977. return -EINVAL;
  978. }
  979. /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
  980. if (p->burst_read || p->burst_write) {
  981. switch (p->burst_len) {
  982. case GPMC_BURST_4:
  983. case GPMC_BURST_8:
  984. case GPMC_BURST_16:
  985. break;
  986. default:
  987. pr_err("%s: invalid page/burst-length (%d)\n",
  988. __func__, p->burst_len);
  989. return -EINVAL;
  990. }
  991. }
  992. if ((p->wait_on_read || p->wait_on_write) &&
  993. (p->wait_pin > gpmc_nr_waitpins)) {
  994. pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
  995. return -EINVAL;
  996. }
  997. config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
  998. if (p->sync_read)
  999. config1 |= GPMC_CONFIG1_READTYPE_SYNC;
  1000. if (p->sync_write)
  1001. config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
  1002. if (p->wait_on_read)
  1003. config1 |= GPMC_CONFIG1_WAIT_READ_MON;
  1004. if (p->wait_on_write)
  1005. config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
  1006. if (p->wait_on_read || p->wait_on_write)
  1007. config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
  1008. if (p->device_nand)
  1009. config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
  1010. if (p->mux_add_data)
  1011. config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
  1012. if (p->burst_read)
  1013. config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
  1014. if (p->burst_write)
  1015. config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
  1016. if (p->burst_read || p->burst_write) {
  1017. config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
  1018. config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
  1019. }
  1020. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
  1021. return 0;
  1022. }
  1023. #ifdef CONFIG_OF
  1024. static struct of_device_id gpmc_dt_ids[] = {
  1025. { .compatible = "ti,omap2420-gpmc" },
  1026. { .compatible = "ti,omap2430-gpmc" },
  1027. { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
  1028. { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
  1029. { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
  1030. { }
  1031. };
  1032. MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
  1033. /**
  1034. * gpmc_read_settings_dt - read gpmc settings from device-tree
  1035. * @np: pointer to device-tree node for a gpmc child device
  1036. * @p: pointer to gpmc settings structure
  1037. *
  1038. * Reads the GPMC settings for a GPMC child device from device-tree and
  1039. * stores them in the GPMC settings structure passed. The GPMC settings
  1040. * structure is initialised to zero by this function and so any
  1041. * previously stored settings will be cleared.
  1042. */
  1043. void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
  1044. {
  1045. memset(p, 0, sizeof(struct gpmc_settings));
  1046. p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
  1047. p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
  1048. p->device_nand = of_property_read_bool(np, "gpmc,device-nand");
  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_ecc_opts[] = {
  1125. [OMAP_ECC_HAMMING_CODE_DEFAULT] = "sw",
  1126. [OMAP_ECC_HAMMING_CODE_HW] = "hw",
  1127. [OMAP_ECC_HAMMING_CODE_HW_ROMCODE] = "hw-romcode",
  1128. [OMAP_ECC_BCH4_CODE_HW] = "bch4",
  1129. [OMAP_ECC_BCH8_CODE_HW] = "bch8",
  1130. };
  1131. static int gpmc_probe_nand_child(struct platform_device *pdev,
  1132. struct device_node *child)
  1133. {
  1134. u32 val;
  1135. const char *s;
  1136. struct gpmc_timings gpmc_t;
  1137. struct omap_nand_platform_data *gpmc_nand_data;
  1138. if (of_property_read_u32(child, "reg", &val) < 0) {
  1139. dev_err(&pdev->dev, "%s has no 'reg' property\n",
  1140. child->full_name);
  1141. return -ENODEV;
  1142. }
  1143. gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data),
  1144. GFP_KERNEL);
  1145. if (!gpmc_nand_data)
  1146. return -ENOMEM;
  1147. gpmc_nand_data->cs = val;
  1148. gpmc_nand_data->of_node = child;
  1149. if (!of_property_read_string(child, "ti,nand-ecc-opt", &s))
  1150. for (val = 0; val < ARRAY_SIZE(nand_ecc_opts); val++)
  1151. if (!strcasecmp(s, nand_ecc_opts[val])) {
  1152. gpmc_nand_data->ecc_opt = val;
  1153. break;
  1154. }
  1155. val = of_get_nand_bus_width(child);
  1156. if (val == 16)
  1157. gpmc_nand_data->devsize = NAND_BUSWIDTH_16;
  1158. gpmc_read_timings_dt(child, &gpmc_t);
  1159. gpmc_nand_init(gpmc_nand_data, &gpmc_t);
  1160. return 0;
  1161. }
  1162. #else
  1163. static int gpmc_probe_nand_child(struct platform_device *pdev,
  1164. struct device_node *child)
  1165. {
  1166. return 0;
  1167. }
  1168. #endif
  1169. #ifdef CONFIG_MTD_ONENAND
  1170. static int gpmc_probe_onenand_child(struct platform_device *pdev,
  1171. struct device_node *child)
  1172. {
  1173. u32 val;
  1174. struct omap_onenand_platform_data *gpmc_onenand_data;
  1175. if (of_property_read_u32(child, "reg", &val) < 0) {
  1176. dev_err(&pdev->dev, "%s has no 'reg' property\n",
  1177. child->full_name);
  1178. return -ENODEV;
  1179. }
  1180. gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
  1181. GFP_KERNEL);
  1182. if (!gpmc_onenand_data)
  1183. return -ENOMEM;
  1184. gpmc_onenand_data->cs = val;
  1185. gpmc_onenand_data->of_node = child;
  1186. gpmc_onenand_data->dma_channel = -1;
  1187. if (!of_property_read_u32(child, "dma-channel", &val))
  1188. gpmc_onenand_data->dma_channel = val;
  1189. gpmc_onenand_init(gpmc_onenand_data);
  1190. return 0;
  1191. }
  1192. #else
  1193. static int gpmc_probe_onenand_child(struct platform_device *pdev,
  1194. struct device_node *child)
  1195. {
  1196. return 0;
  1197. }
  1198. #endif
  1199. /**
  1200. * gpmc_probe_nor_child - configures the gpmc for a nor device
  1201. * @pdev: pointer to gpmc platform device
  1202. * @child: pointer to device-tree node for nor device
  1203. *
  1204. * Allocates and configures a GPMC chip-select for a NOR flash device.
  1205. * Returns 0 on success and appropriate negative error code on failure.
  1206. */
  1207. static int gpmc_probe_nor_child(struct platform_device *pdev,
  1208. struct device_node *child)
  1209. {
  1210. struct gpmc_settings gpmc_s;
  1211. struct gpmc_timings gpmc_t;
  1212. struct resource res;
  1213. unsigned long base;
  1214. int ret, cs;
  1215. if (of_property_read_u32(child, "reg", &cs) < 0) {
  1216. dev_err(&pdev->dev, "%s has no 'reg' property\n",
  1217. child->full_name);
  1218. return -ENODEV;
  1219. }
  1220. if (of_address_to_resource(child, 0, &res) < 0) {
  1221. dev_err(&pdev->dev, "%s has malformed 'reg' property\n",
  1222. child->full_name);
  1223. return -ENODEV;
  1224. }
  1225. ret = gpmc_cs_request(cs, resource_size(&res), &base);
  1226. if (ret < 0) {
  1227. dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
  1228. return ret;
  1229. }
  1230. /*
  1231. * FIXME: gpmc_cs_request() will map the CS to an arbitary
  1232. * location in the gpmc address space. When booting with
  1233. * device-tree we want the NOR flash to be mapped to the
  1234. * location specified in the device-tree blob. So remap the
  1235. * CS to this location. Once DT migration is complete should
  1236. * just make gpmc_cs_request() map a specific address.
  1237. */
  1238. ret = gpmc_cs_remap(cs, res.start);
  1239. if (ret < 0) {
  1240. dev_err(&pdev->dev, "cannot remap GPMC CS %d to 0x%x\n",
  1241. cs, res.start);
  1242. goto err;
  1243. }
  1244. gpmc_read_settings_dt(child, &gpmc_s);
  1245. ret = of_property_read_u32(child, "bank-width", &gpmc_s.device_width);
  1246. if (ret < 0)
  1247. goto err;
  1248. ret = gpmc_cs_program_settings(cs, &gpmc_s);
  1249. if (ret < 0)
  1250. goto err;
  1251. gpmc_read_timings_dt(child, &gpmc_t);
  1252. gpmc_cs_set_timings(cs, &gpmc_t);
  1253. if (of_platform_device_create(child, NULL, &pdev->dev))
  1254. return 0;
  1255. dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
  1256. err:
  1257. gpmc_cs_free(cs);
  1258. return ret;
  1259. }
  1260. static int gpmc_probe_dt(struct platform_device *pdev)
  1261. {
  1262. int ret;
  1263. struct device_node *child;
  1264. const struct of_device_id *of_id =
  1265. of_match_device(gpmc_dt_ids, &pdev->dev);
  1266. if (!of_id)
  1267. return 0;
  1268. ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
  1269. &gpmc_nr_waitpins);
  1270. if (ret < 0) {
  1271. pr_err("%s: number of wait pins not found!\n", __func__);
  1272. return ret;
  1273. }
  1274. for_each_node_by_name(child, "nand") {
  1275. ret = gpmc_probe_nand_child(pdev, child);
  1276. if (ret < 0) {
  1277. of_node_put(child);
  1278. return ret;
  1279. }
  1280. }
  1281. for_each_node_by_name(child, "onenand") {
  1282. ret = gpmc_probe_onenand_child(pdev, child);
  1283. if (ret < 0) {
  1284. of_node_put(child);
  1285. return ret;
  1286. }
  1287. }
  1288. for_each_node_by_name(child, "nor") {
  1289. ret = gpmc_probe_nor_child(pdev, child);
  1290. if (ret < 0) {
  1291. of_node_put(child);
  1292. return ret;
  1293. }
  1294. }
  1295. return 0;
  1296. }
  1297. #else
  1298. static int gpmc_probe_dt(struct platform_device *pdev)
  1299. {
  1300. return 0;
  1301. }
  1302. #endif
  1303. static int gpmc_probe(struct platform_device *pdev)
  1304. {
  1305. int rc;
  1306. u32 l;
  1307. struct resource *res;
  1308. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1309. if (res == NULL)
  1310. return -ENOENT;
  1311. phys_base = res->start;
  1312. mem_size = resource_size(res);
  1313. gpmc_base = devm_ioremap_resource(&pdev->dev, res);
  1314. if (IS_ERR(gpmc_base))
  1315. return PTR_ERR(gpmc_base);
  1316. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1317. if (res == NULL)
  1318. dev_warn(&pdev->dev, "Failed to get resource: irq\n");
  1319. else
  1320. gpmc_irq = res->start;
  1321. gpmc_l3_clk = clk_get(&pdev->dev, "fck");
  1322. if (IS_ERR(gpmc_l3_clk)) {
  1323. dev_err(&pdev->dev, "error: clk_get\n");
  1324. gpmc_irq = 0;
  1325. return PTR_ERR(gpmc_l3_clk);
  1326. }
  1327. clk_prepare_enable(gpmc_l3_clk);
  1328. gpmc_dev = &pdev->dev;
  1329. l = gpmc_read_reg(GPMC_REVISION);
  1330. /*
  1331. * FIXME: Once device-tree migration is complete the below flags
  1332. * should be populated based upon the device-tree compatible
  1333. * string. For now just use the IP revision. OMAP3+ devices have
  1334. * the wr_access and wr_data_mux_bus register fields. OMAP4+
  1335. * devices support the addr-addr-data multiplex protocol.
  1336. *
  1337. * GPMC IP revisions:
  1338. * - OMAP24xx = 2.0
  1339. * - OMAP3xxx = 5.0
  1340. * - OMAP44xx/54xx/AM335x = 6.0
  1341. */
  1342. if (GPMC_REVISION_MAJOR(l) > 0x4)
  1343. gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
  1344. if (GPMC_REVISION_MAJOR(l) > 0x5)
  1345. gpmc_capability |= GPMC_HAS_MUX_AAD;
  1346. dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
  1347. GPMC_REVISION_MINOR(l));
  1348. rc = gpmc_mem_init();
  1349. if (rc < 0) {
  1350. clk_disable_unprepare(gpmc_l3_clk);
  1351. clk_put(gpmc_l3_clk);
  1352. dev_err(gpmc_dev, "failed to reserve memory\n");
  1353. return rc;
  1354. }
  1355. if (gpmc_setup_irq() < 0)
  1356. dev_warn(gpmc_dev, "gpmc_setup_irq failed\n");
  1357. /* Now the GPMC is initialised, unreserve the chip-selects */
  1358. gpmc_cs_map = 0;
  1359. if (!pdev->dev.of_node)
  1360. gpmc_nr_waitpins = GPMC_NR_WAITPINS;
  1361. rc = gpmc_probe_dt(pdev);
  1362. if (rc < 0) {
  1363. clk_disable_unprepare(gpmc_l3_clk);
  1364. clk_put(gpmc_l3_clk);
  1365. dev_err(gpmc_dev, "failed to probe DT parameters\n");
  1366. return rc;
  1367. }
  1368. return 0;
  1369. }
  1370. static int gpmc_remove(struct platform_device *pdev)
  1371. {
  1372. gpmc_free_irq();
  1373. gpmc_mem_exit();
  1374. gpmc_dev = NULL;
  1375. return 0;
  1376. }
  1377. static struct platform_driver gpmc_driver = {
  1378. .probe = gpmc_probe,
  1379. .remove = gpmc_remove,
  1380. .driver = {
  1381. .name = DEVICE_NAME,
  1382. .owner = THIS_MODULE,
  1383. .of_match_table = of_match_ptr(gpmc_dt_ids),
  1384. },
  1385. };
  1386. static __init int gpmc_init(void)
  1387. {
  1388. return platform_driver_register(&gpmc_driver);
  1389. }
  1390. static __exit void gpmc_exit(void)
  1391. {
  1392. platform_driver_unregister(&gpmc_driver);
  1393. }
  1394. omap_postcore_initcall(gpmc_init);
  1395. module_exit(gpmc_exit);
  1396. static int __init omap_gpmc_init(void)
  1397. {
  1398. struct omap_hwmod *oh;
  1399. struct platform_device *pdev;
  1400. char *oh_name = "gpmc";
  1401. /*
  1402. * if the board boots up with a populated DT, do not
  1403. * manually add the device from this initcall
  1404. */
  1405. if (of_have_populated_dt())
  1406. return -ENODEV;
  1407. oh = omap_hwmod_lookup(oh_name);
  1408. if (!oh) {
  1409. pr_err("Could not look up %s\n", oh_name);
  1410. return -ENODEV;
  1411. }
  1412. pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0);
  1413. WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name);
  1414. return IS_ERR(pdev) ? PTR_ERR(pdev) : 0;
  1415. }
  1416. omap_postcore_initcall(omap_gpmc_init);
  1417. static irqreturn_t gpmc_handle_irq(int irq, void *dev)
  1418. {
  1419. int i;
  1420. u32 regval;
  1421. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  1422. if (!regval)
  1423. return IRQ_NONE;
  1424. for (i = 0; i < GPMC_NR_IRQ; i++)
  1425. if (regval & gpmc_client_irq[i].bitmask)
  1426. generic_handle_irq(gpmc_client_irq[i].irq);
  1427. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  1428. return IRQ_HANDLED;
  1429. }
  1430. #ifdef CONFIG_ARCH_OMAP3
  1431. static struct omap3_gpmc_regs gpmc_context;
  1432. void omap3_gpmc_save_context(void)
  1433. {
  1434. int i;
  1435. gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
  1436. gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
  1437. gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
  1438. gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
  1439. gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
  1440. gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
  1441. gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
  1442. for (i = 0; i < GPMC_CS_NUM; i++) {
  1443. gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
  1444. if (gpmc_context.cs_context[i].is_valid) {
  1445. gpmc_context.cs_context[i].config1 =
  1446. gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
  1447. gpmc_context.cs_context[i].config2 =
  1448. gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
  1449. gpmc_context.cs_context[i].config3 =
  1450. gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
  1451. gpmc_context.cs_context[i].config4 =
  1452. gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
  1453. gpmc_context.cs_context[i].config5 =
  1454. gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
  1455. gpmc_context.cs_context[i].config6 =
  1456. gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
  1457. gpmc_context.cs_context[i].config7 =
  1458. gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
  1459. }
  1460. }
  1461. }
  1462. void omap3_gpmc_restore_context(void)
  1463. {
  1464. int i;
  1465. gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
  1466. gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
  1467. gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
  1468. gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
  1469. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
  1470. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
  1471. gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
  1472. for (i = 0; i < GPMC_CS_NUM; i++) {
  1473. if (gpmc_context.cs_context[i].is_valid) {
  1474. gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
  1475. gpmc_context.cs_context[i].config1);
  1476. gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
  1477. gpmc_context.cs_context[i].config2);
  1478. gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
  1479. gpmc_context.cs_context[i].config3);
  1480. gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
  1481. gpmc_context.cs_context[i].config4);
  1482. gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
  1483. gpmc_context.cs_context[i].config5);
  1484. gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
  1485. gpmc_context.cs_context[i].config6);
  1486. gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
  1487. gpmc_context.cs_context[i].config7);
  1488. }
  1489. }
  1490. }
  1491. #endif /* CONFIG_ARCH_OMAP3 */