gpmc.c 40 KB

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