gpmc.c 49 KB

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