gpmc.c 47 KB

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