gpmc.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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. #define DEVICE_NAME "omap-gpmc"
  39. /* GPMC register offsets */
  40. #define GPMC_REVISION 0x00
  41. #define GPMC_SYSCONFIG 0x10
  42. #define GPMC_SYSSTATUS 0x14
  43. #define GPMC_IRQSTATUS 0x18
  44. #define GPMC_IRQENABLE 0x1c
  45. #define GPMC_TIMEOUT_CONTROL 0x40
  46. #define GPMC_ERR_ADDRESS 0x44
  47. #define GPMC_ERR_TYPE 0x48
  48. #define GPMC_CONFIG 0x50
  49. #define GPMC_STATUS 0x54
  50. #define GPMC_PREFETCH_CONFIG1 0x1e0
  51. #define GPMC_PREFETCH_CONFIG2 0x1e4
  52. #define GPMC_PREFETCH_CONTROL 0x1ec
  53. #define GPMC_PREFETCH_STATUS 0x1f0
  54. #define GPMC_ECC_CONFIG 0x1f4
  55. #define GPMC_ECC_CONTROL 0x1f8
  56. #define GPMC_ECC_SIZE_CONFIG 0x1fc
  57. #define GPMC_ECC1_RESULT 0x200
  58. #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
  59. #define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */
  60. #define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */
  61. #define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */
  62. /* GPMC ECC control settings */
  63. #define GPMC_ECC_CTRL_ECCCLEAR 0x100
  64. #define GPMC_ECC_CTRL_ECCDISABLE 0x000
  65. #define GPMC_ECC_CTRL_ECCREG1 0x001
  66. #define GPMC_ECC_CTRL_ECCREG2 0x002
  67. #define GPMC_ECC_CTRL_ECCREG3 0x003
  68. #define GPMC_ECC_CTRL_ECCREG4 0x004
  69. #define GPMC_ECC_CTRL_ECCREG5 0x005
  70. #define GPMC_ECC_CTRL_ECCREG6 0x006
  71. #define GPMC_ECC_CTRL_ECCREG7 0x007
  72. #define GPMC_ECC_CTRL_ECCREG8 0x008
  73. #define GPMC_ECC_CTRL_ECCREG9 0x009
  74. #define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
  75. #define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
  76. #define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
  77. #define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
  78. #define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
  79. #define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
  80. #define GPMC_CS0_OFFSET 0x60
  81. #define GPMC_CS_SIZE 0x30
  82. #define GPMC_BCH_SIZE 0x10
  83. #define GPMC_MEM_START 0x00000000
  84. #define GPMC_MEM_END 0x3FFFFFFF
  85. #define BOOT_ROM_SPACE 0x100000 /* 1MB */
  86. #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
  87. #define GPMC_SECTION_SHIFT 28 /* 128 MB */
  88. #define CS_NUM_SHIFT 24
  89. #define ENABLE_PREFETCH (0x1 << 7)
  90. #define DMA_MPU_MODE 2
  91. #define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
  92. #define GPMC_REVISION_MINOR(l) (l & 0xf)
  93. #define GPMC_HAS_WR_ACCESS 0x1
  94. #define GPMC_HAS_WR_DATA_MUX_BUS 0x2
  95. /* XXX: Only NAND irq has been considered,currently these are the only ones used
  96. */
  97. #define GPMC_NR_IRQ 2
  98. struct gpmc_client_irq {
  99. unsigned irq;
  100. u32 bitmask;
  101. };
  102. /* Structure to save gpmc cs context */
  103. struct gpmc_cs_config {
  104. u32 config1;
  105. u32 config2;
  106. u32 config3;
  107. u32 config4;
  108. u32 config5;
  109. u32 config6;
  110. u32 config7;
  111. int is_valid;
  112. };
  113. /*
  114. * Structure to save/restore gpmc context
  115. * to support core off on OMAP3
  116. */
  117. struct omap3_gpmc_regs {
  118. u32 sysconfig;
  119. u32 irqenable;
  120. u32 timeout_ctrl;
  121. u32 config;
  122. u32 prefetch_config1;
  123. u32 prefetch_config2;
  124. u32 prefetch_control;
  125. struct gpmc_cs_config cs_context[GPMC_CS_NUM];
  126. };
  127. static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
  128. static struct irq_chip gpmc_irq_chip;
  129. static unsigned gpmc_irq_start;
  130. static struct resource gpmc_mem_root;
  131. static struct resource gpmc_cs_mem[GPMC_CS_NUM];
  132. static DEFINE_SPINLOCK(gpmc_mem_lock);
  133. /* Define chip-selects as reserved by default until probe completes */
  134. static unsigned int gpmc_cs_map = ((1 << GPMC_CS_NUM) - 1);
  135. static struct device *gpmc_dev;
  136. static int gpmc_irq;
  137. static resource_size_t phys_base, mem_size;
  138. static unsigned gpmc_capability;
  139. static void __iomem *gpmc_base;
  140. static struct clk *gpmc_l3_clk;
  141. static irqreturn_t gpmc_handle_irq(int irq, void *dev);
  142. static void gpmc_write_reg(int idx, u32 val)
  143. {
  144. __raw_writel(val, gpmc_base + idx);
  145. }
  146. static u32 gpmc_read_reg(int idx)
  147. {
  148. return __raw_readl(gpmc_base + idx);
  149. }
  150. void gpmc_cs_write_reg(int cs, int idx, u32 val)
  151. {
  152. void __iomem *reg_addr;
  153. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  154. __raw_writel(val, reg_addr);
  155. }
  156. u32 gpmc_cs_read_reg(int cs, int idx)
  157. {
  158. void __iomem *reg_addr;
  159. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  160. return __raw_readl(reg_addr);
  161. }
  162. /* TODO: Add support for gpmc_fck to clock framework and use it */
  163. unsigned long gpmc_get_fclk_period(void)
  164. {
  165. unsigned long rate = clk_get_rate(gpmc_l3_clk);
  166. if (rate == 0) {
  167. printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
  168. return 0;
  169. }
  170. rate /= 1000;
  171. rate = 1000000000 / rate; /* In picoseconds */
  172. return rate;
  173. }
  174. unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
  175. {
  176. unsigned long tick_ps;
  177. /* Calculate in picosecs to yield more exact results */
  178. tick_ps = gpmc_get_fclk_period();
  179. return (time_ns * 1000 + tick_ps - 1) / tick_ps;
  180. }
  181. unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
  182. {
  183. unsigned long tick_ps;
  184. /* Calculate in picosecs to yield more exact results */
  185. tick_ps = gpmc_get_fclk_period();
  186. return (time_ps + tick_ps - 1) / tick_ps;
  187. }
  188. unsigned int gpmc_ticks_to_ns(unsigned int ticks)
  189. {
  190. return ticks * gpmc_get_fclk_period() / 1000;
  191. }
  192. unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns)
  193. {
  194. unsigned long ticks = gpmc_ns_to_ticks(time_ns);
  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 void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
  339. {
  340. u32 l;
  341. u32 mask;
  342. mask = (1 << GPMC_SECTION_SHIFT) - size;
  343. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  344. l &= ~0x3f;
  345. l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
  346. l &= ~(0x0f << 8);
  347. l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
  348. l |= GPMC_CONFIG7_CSVALID;
  349. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  350. }
  351. static void gpmc_cs_disable_mem(int cs)
  352. {
  353. u32 l;
  354. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  355. l &= ~GPMC_CONFIG7_CSVALID;
  356. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  357. }
  358. static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
  359. {
  360. u32 l;
  361. u32 mask;
  362. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  363. *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
  364. mask = (l >> 8) & 0x0f;
  365. *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
  366. }
  367. static int gpmc_cs_mem_enabled(int cs)
  368. {
  369. u32 l;
  370. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  371. return l & GPMC_CONFIG7_CSVALID;
  372. }
  373. int gpmc_cs_set_reserved(int cs, int reserved)
  374. {
  375. if (cs > GPMC_CS_NUM)
  376. return -ENODEV;
  377. gpmc_cs_map &= ~(1 << cs);
  378. gpmc_cs_map |= (reserved ? 1 : 0) << cs;
  379. return 0;
  380. }
  381. int gpmc_cs_reserved(int cs)
  382. {
  383. if (cs > GPMC_CS_NUM)
  384. return -ENODEV;
  385. return gpmc_cs_map & (1 << cs);
  386. }
  387. static unsigned long gpmc_mem_align(unsigned long size)
  388. {
  389. int order;
  390. size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
  391. order = GPMC_CHUNK_SHIFT - 1;
  392. do {
  393. size >>= 1;
  394. order++;
  395. } while (size);
  396. size = 1 << order;
  397. return size;
  398. }
  399. static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
  400. {
  401. struct resource *res = &gpmc_cs_mem[cs];
  402. int r;
  403. size = gpmc_mem_align(size);
  404. spin_lock(&gpmc_mem_lock);
  405. res->start = base;
  406. res->end = base + size - 1;
  407. r = request_resource(&gpmc_mem_root, res);
  408. spin_unlock(&gpmc_mem_lock);
  409. return r;
  410. }
  411. static int gpmc_cs_delete_mem(int cs)
  412. {
  413. struct resource *res = &gpmc_cs_mem[cs];
  414. int r;
  415. spin_lock(&gpmc_mem_lock);
  416. r = release_resource(&gpmc_cs_mem[cs]);
  417. res->start = 0;
  418. res->end = 0;
  419. spin_unlock(&gpmc_mem_lock);
  420. return r;
  421. }
  422. int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
  423. {
  424. struct resource *res = &gpmc_cs_mem[cs];
  425. int r = -1;
  426. if (cs > GPMC_CS_NUM)
  427. return -ENODEV;
  428. size = gpmc_mem_align(size);
  429. if (size > (1 << GPMC_SECTION_SHIFT))
  430. return -ENOMEM;
  431. spin_lock(&gpmc_mem_lock);
  432. if (gpmc_cs_reserved(cs)) {
  433. r = -EBUSY;
  434. goto out;
  435. }
  436. if (gpmc_cs_mem_enabled(cs))
  437. r = adjust_resource(res, res->start & ~(size - 1), size);
  438. if (r < 0)
  439. r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
  440. size, NULL, NULL);
  441. if (r < 0)
  442. goto out;
  443. gpmc_cs_enable_mem(cs, res->start, resource_size(res));
  444. *base = res->start;
  445. gpmc_cs_set_reserved(cs, 1);
  446. out:
  447. spin_unlock(&gpmc_mem_lock);
  448. return r;
  449. }
  450. EXPORT_SYMBOL(gpmc_cs_request);
  451. void gpmc_cs_free(int cs)
  452. {
  453. spin_lock(&gpmc_mem_lock);
  454. if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) {
  455. printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
  456. BUG();
  457. spin_unlock(&gpmc_mem_lock);
  458. return;
  459. }
  460. gpmc_cs_disable_mem(cs);
  461. release_resource(&gpmc_cs_mem[cs]);
  462. gpmc_cs_set_reserved(cs, 0);
  463. spin_unlock(&gpmc_mem_lock);
  464. }
  465. EXPORT_SYMBOL(gpmc_cs_free);
  466. /**
  467. * gpmc_cs_configure - write request to configure gpmc
  468. * @cs: chip select number
  469. * @cmd: command type
  470. * @wval: value to write
  471. * @return status of the operation
  472. */
  473. int gpmc_cs_configure(int cs, int cmd, int wval)
  474. {
  475. int err = 0;
  476. u32 regval = 0;
  477. switch (cmd) {
  478. case GPMC_ENABLE_IRQ:
  479. gpmc_write_reg(GPMC_IRQENABLE, wval);
  480. break;
  481. case GPMC_SET_IRQ_STATUS:
  482. gpmc_write_reg(GPMC_IRQSTATUS, wval);
  483. break;
  484. case GPMC_CONFIG_WP:
  485. regval = gpmc_read_reg(GPMC_CONFIG);
  486. if (wval)
  487. regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
  488. else
  489. regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
  490. gpmc_write_reg(GPMC_CONFIG, regval);
  491. break;
  492. case GPMC_CONFIG_RDY_BSY:
  493. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  494. if (wval)
  495. regval |= WR_RD_PIN_MONITORING;
  496. else
  497. regval &= ~WR_RD_PIN_MONITORING;
  498. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  499. break;
  500. case GPMC_CONFIG_DEV_SIZE:
  501. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  502. /* clear 2 target bits */
  503. regval &= ~GPMC_CONFIG1_DEVICESIZE(3);
  504. /* set the proper value */
  505. regval |= GPMC_CONFIG1_DEVICESIZE(wval);
  506. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  507. break;
  508. case GPMC_CONFIG_DEV_TYPE:
  509. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  510. regval |= GPMC_CONFIG1_DEVICETYPE(wval);
  511. if (wval == GPMC_DEVICETYPE_NOR)
  512. regval |= GPMC_CONFIG1_MUXADDDATA;
  513. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  514. break;
  515. default:
  516. printk(KERN_ERR "gpmc_configure_cs: Not supported\n");
  517. err = -EINVAL;
  518. }
  519. return err;
  520. }
  521. EXPORT_SYMBOL(gpmc_cs_configure);
  522. void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
  523. {
  524. int i;
  525. reg->gpmc_status = gpmc_base + GPMC_STATUS;
  526. reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
  527. GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
  528. reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
  529. GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
  530. reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
  531. GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
  532. reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
  533. reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
  534. reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
  535. reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
  536. reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
  537. reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
  538. reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
  539. reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
  540. for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
  541. reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
  542. GPMC_BCH_SIZE * i;
  543. reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
  544. GPMC_BCH_SIZE * i;
  545. reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
  546. GPMC_BCH_SIZE * i;
  547. reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
  548. GPMC_BCH_SIZE * i;
  549. }
  550. }
  551. int gpmc_get_client_irq(unsigned irq_config)
  552. {
  553. int i;
  554. if (hweight32(irq_config) > 1)
  555. return 0;
  556. for (i = 0; i < GPMC_NR_IRQ; i++)
  557. if (gpmc_client_irq[i].bitmask & irq_config)
  558. return gpmc_client_irq[i].irq;
  559. return 0;
  560. }
  561. static int gpmc_irq_endis(unsigned irq, bool endis)
  562. {
  563. int i;
  564. u32 regval;
  565. for (i = 0; i < GPMC_NR_IRQ; i++)
  566. if (irq == gpmc_client_irq[i].irq) {
  567. regval = gpmc_read_reg(GPMC_IRQENABLE);
  568. if (endis)
  569. regval |= gpmc_client_irq[i].bitmask;
  570. else
  571. regval &= ~gpmc_client_irq[i].bitmask;
  572. gpmc_write_reg(GPMC_IRQENABLE, regval);
  573. break;
  574. }
  575. return 0;
  576. }
  577. static void gpmc_irq_disable(struct irq_data *p)
  578. {
  579. gpmc_irq_endis(p->irq, false);
  580. }
  581. static void gpmc_irq_enable(struct irq_data *p)
  582. {
  583. gpmc_irq_endis(p->irq, true);
  584. }
  585. static void gpmc_irq_noop(struct irq_data *data) { }
  586. static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
  587. static int gpmc_setup_irq(void)
  588. {
  589. int i;
  590. u32 regval;
  591. if (!gpmc_irq)
  592. return -EINVAL;
  593. gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
  594. if (IS_ERR_VALUE(gpmc_irq_start)) {
  595. pr_err("irq_alloc_descs failed\n");
  596. return gpmc_irq_start;
  597. }
  598. gpmc_irq_chip.name = "gpmc";
  599. gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
  600. gpmc_irq_chip.irq_enable = gpmc_irq_enable;
  601. gpmc_irq_chip.irq_disable = gpmc_irq_disable;
  602. gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
  603. gpmc_irq_chip.irq_ack = gpmc_irq_noop;
  604. gpmc_irq_chip.irq_mask = gpmc_irq_noop;
  605. gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
  606. gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
  607. gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
  608. for (i = 0; i < GPMC_NR_IRQ; i++) {
  609. gpmc_client_irq[i].irq = gpmc_irq_start + i;
  610. irq_set_chip_and_handler(gpmc_client_irq[i].irq,
  611. &gpmc_irq_chip, handle_simple_irq);
  612. set_irq_flags(gpmc_client_irq[i].irq,
  613. IRQF_VALID | IRQF_NOAUTOEN);
  614. }
  615. /* Disable interrupts */
  616. gpmc_write_reg(GPMC_IRQENABLE, 0);
  617. /* clear interrupts */
  618. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  619. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  620. return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
  621. }
  622. static int gpmc_free_irq(void)
  623. {
  624. int i;
  625. if (gpmc_irq)
  626. free_irq(gpmc_irq, NULL);
  627. for (i = 0; i < GPMC_NR_IRQ; i++) {
  628. irq_set_handler(gpmc_client_irq[i].irq, NULL);
  629. irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip);
  630. irq_modify_status(gpmc_client_irq[i].irq, 0, 0);
  631. }
  632. irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ);
  633. return 0;
  634. }
  635. static void gpmc_mem_exit(void)
  636. {
  637. int cs;
  638. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  639. if (!gpmc_cs_mem_enabled(cs))
  640. continue;
  641. gpmc_cs_delete_mem(cs);
  642. }
  643. }
  644. static int gpmc_mem_init(void)
  645. {
  646. int cs, rc;
  647. unsigned long boot_rom_space = 0;
  648. /* never allocate the first page, to facilitate bug detection;
  649. * even if we didn't boot from ROM.
  650. */
  651. boot_rom_space = BOOT_ROM_SPACE;
  652. /* In apollon the CS0 is mapped as 0x0000 0000 */
  653. if (machine_is_omap_apollon())
  654. boot_rom_space = 0;
  655. gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
  656. gpmc_mem_root.end = GPMC_MEM_END;
  657. /* Reserve all regions that has been set up by bootloader */
  658. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  659. u32 base, size;
  660. if (!gpmc_cs_mem_enabled(cs))
  661. continue;
  662. gpmc_cs_get_memconf(cs, &base, &size);
  663. rc = gpmc_cs_insert_mem(cs, base, size);
  664. if (IS_ERR_VALUE(rc)) {
  665. while (--cs >= 0)
  666. if (gpmc_cs_mem_enabled(cs))
  667. gpmc_cs_delete_mem(cs);
  668. return rc;
  669. }
  670. }
  671. return 0;
  672. }
  673. static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
  674. {
  675. u32 temp;
  676. int div;
  677. div = gpmc_calc_divider(sync_clk);
  678. temp = gpmc_ps_to_ticks(time_ps);
  679. temp = (temp + div - 1) / div;
  680. return gpmc_ticks_to_ps(temp * div);
  681. }
  682. /* XXX: can the cycles be avoided ? */
  683. static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
  684. struct gpmc_device_timings *dev_t)
  685. {
  686. bool mux = dev_t->mux;
  687. u32 temp;
  688. /* adv_rd_off */
  689. temp = dev_t->t_avdp_r;
  690. /* XXX: mux check required ? */
  691. if (mux) {
  692. /* XXX: t_avdp not to be required for sync, only added for tusb
  693. * this indirectly necessitates requirement of t_avdp_r and
  694. * t_avdp_w instead of having a single t_avdp
  695. */
  696. temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
  697. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  698. }
  699. gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
  700. /* oe_on */
  701. temp = dev_t->t_oeasu; /* XXX: remove this ? */
  702. if (mux) {
  703. temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
  704. temp = max_t(u32, temp, gpmc_t->adv_rd_off +
  705. gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
  706. }
  707. gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
  708. /* access */
  709. /* XXX: any scope for improvement ?, by combining oe_on
  710. * and clk_activation, need to check whether
  711. * access = clk_activation + round to sync clk ?
  712. */
  713. temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
  714. temp += gpmc_t->clk_activation;
  715. if (dev_t->cyc_oe)
  716. temp = max_t(u32, temp, gpmc_t->oe_on +
  717. gpmc_ticks_to_ps(dev_t->cyc_oe));
  718. gpmc_t->access = gpmc_round_ps_to_ticks(temp);
  719. gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
  720. gpmc_t->cs_rd_off = gpmc_t->oe_off;
  721. /* rd_cycle */
  722. temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
  723. temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
  724. gpmc_t->access;
  725. /* XXX: barter t_ce_rdyz with t_cez_r ? */
  726. if (dev_t->t_ce_rdyz)
  727. temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
  728. gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
  729. return 0;
  730. }
  731. static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
  732. struct gpmc_device_timings *dev_t)
  733. {
  734. bool mux = dev_t->mux;
  735. u32 temp;
  736. /* adv_wr_off */
  737. temp = dev_t->t_avdp_w;
  738. if (mux) {
  739. temp = max_t(u32, temp,
  740. gpmc_t->clk_activation + dev_t->t_avdh);
  741. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  742. }
  743. gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
  744. /* wr_data_mux_bus */
  745. temp = max_t(u32, dev_t->t_weasu,
  746. gpmc_t->clk_activation + dev_t->t_rdyo);
  747. /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
  748. * and in that case remember to handle we_on properly
  749. */
  750. if (mux) {
  751. temp = max_t(u32, temp,
  752. gpmc_t->adv_wr_off + dev_t->t_aavdh);
  753. temp = max_t(u32, temp, gpmc_t->adv_wr_off +
  754. gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
  755. }
  756. gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
  757. /* we_on */
  758. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  759. gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
  760. else
  761. gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
  762. /* wr_access */
  763. /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
  764. gpmc_t->wr_access = gpmc_t->access;
  765. /* we_off */
  766. temp = gpmc_t->we_on + dev_t->t_wpl;
  767. temp = max_t(u32, temp,
  768. gpmc_t->wr_access + gpmc_ticks_to_ps(1));
  769. temp = max_t(u32, temp,
  770. gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
  771. gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
  772. gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
  773. dev_t->t_wph);
  774. /* wr_cycle */
  775. temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
  776. temp += gpmc_t->wr_access;
  777. /* XXX: barter t_ce_rdyz with t_cez_w ? */
  778. if (dev_t->t_ce_rdyz)
  779. temp = max_t(u32, temp,
  780. gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
  781. gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
  782. return 0;
  783. }
  784. static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
  785. struct gpmc_device_timings *dev_t)
  786. {
  787. bool mux = dev_t->mux;
  788. u32 temp;
  789. /* adv_rd_off */
  790. temp = dev_t->t_avdp_r;
  791. if (mux)
  792. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  793. gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
  794. /* oe_on */
  795. temp = dev_t->t_oeasu;
  796. if (mux)
  797. temp = max_t(u32, temp,
  798. gpmc_t->adv_rd_off + dev_t->t_aavdh);
  799. gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
  800. /* access */
  801. temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
  802. gpmc_t->oe_on + dev_t->t_oe);
  803. temp = max_t(u32, temp,
  804. gpmc_t->cs_on + dev_t->t_ce);
  805. temp = max_t(u32, temp,
  806. gpmc_t->adv_on + dev_t->t_aa);
  807. gpmc_t->access = gpmc_round_ps_to_ticks(temp);
  808. gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
  809. gpmc_t->cs_rd_off = gpmc_t->oe_off;
  810. /* rd_cycle */
  811. temp = max_t(u32, dev_t->t_rd_cycle,
  812. gpmc_t->cs_rd_off + dev_t->t_cez_r);
  813. temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
  814. gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
  815. return 0;
  816. }
  817. static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
  818. struct gpmc_device_timings *dev_t)
  819. {
  820. bool mux = dev_t->mux;
  821. u32 temp;
  822. /* adv_wr_off */
  823. temp = dev_t->t_avdp_w;
  824. if (mux)
  825. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  826. gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
  827. /* wr_data_mux_bus */
  828. temp = dev_t->t_weasu;
  829. if (mux) {
  830. temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
  831. temp = max_t(u32, temp, gpmc_t->adv_wr_off +
  832. gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
  833. }
  834. gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
  835. /* we_on */
  836. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  837. gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
  838. else
  839. gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
  840. /* we_off */
  841. temp = gpmc_t->we_on + dev_t->t_wpl;
  842. gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
  843. gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
  844. dev_t->t_wph);
  845. /* wr_cycle */
  846. temp = max_t(u32, dev_t->t_wr_cycle,
  847. gpmc_t->cs_wr_off + dev_t->t_cez_w);
  848. gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
  849. return 0;
  850. }
  851. static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
  852. struct gpmc_device_timings *dev_t)
  853. {
  854. u32 temp;
  855. gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
  856. gpmc_get_fclk_period();
  857. gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
  858. dev_t->t_bacc,
  859. gpmc_t->sync_clk);
  860. temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
  861. gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
  862. if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
  863. return 0;
  864. if (dev_t->ce_xdelay)
  865. gpmc_t->bool_timings.cs_extra_delay = true;
  866. if (dev_t->avd_xdelay)
  867. gpmc_t->bool_timings.adv_extra_delay = true;
  868. if (dev_t->oe_xdelay)
  869. gpmc_t->bool_timings.oe_extra_delay = true;
  870. if (dev_t->we_xdelay)
  871. gpmc_t->bool_timings.we_extra_delay = true;
  872. return 0;
  873. }
  874. static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
  875. struct gpmc_device_timings *dev_t)
  876. {
  877. u32 temp;
  878. /* cs_on */
  879. gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
  880. /* adv_on */
  881. temp = dev_t->t_avdasu;
  882. if (dev_t->t_ce_avd)
  883. temp = max_t(u32, temp,
  884. gpmc_t->cs_on + dev_t->t_ce_avd);
  885. gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
  886. if (dev_t->sync_write || dev_t->sync_read)
  887. gpmc_calc_sync_common_timings(gpmc_t, dev_t);
  888. return 0;
  889. }
  890. /* TODO: remove this function once all peripherals are confirmed to
  891. * work with generic timing. Simultaneously gpmc_cs_set_timings()
  892. * has to be modified to handle timings in ps instead of ns
  893. */
  894. static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
  895. {
  896. t->cs_on /= 1000;
  897. t->cs_rd_off /= 1000;
  898. t->cs_wr_off /= 1000;
  899. t->adv_on /= 1000;
  900. t->adv_rd_off /= 1000;
  901. t->adv_wr_off /= 1000;
  902. t->we_on /= 1000;
  903. t->we_off /= 1000;
  904. t->oe_on /= 1000;
  905. t->oe_off /= 1000;
  906. t->page_burst_access /= 1000;
  907. t->access /= 1000;
  908. t->rd_cycle /= 1000;
  909. t->wr_cycle /= 1000;
  910. t->bus_turnaround /= 1000;
  911. t->cycle2cycle_delay /= 1000;
  912. t->wait_monitoring /= 1000;
  913. t->clk_activation /= 1000;
  914. t->wr_access /= 1000;
  915. t->wr_data_mux_bus /= 1000;
  916. }
  917. int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
  918. struct gpmc_device_timings *dev_t)
  919. {
  920. memset(gpmc_t, 0, sizeof(*gpmc_t));
  921. gpmc_calc_common_timings(gpmc_t, dev_t);
  922. if (dev_t->sync_read)
  923. gpmc_calc_sync_read_timings(gpmc_t, dev_t);
  924. else
  925. gpmc_calc_async_read_timings(gpmc_t, dev_t);
  926. if (dev_t->sync_write)
  927. gpmc_calc_sync_write_timings(gpmc_t, dev_t);
  928. else
  929. gpmc_calc_async_write_timings(gpmc_t, dev_t);
  930. /* TODO: remove, see function definition */
  931. gpmc_convert_ps_to_ns(gpmc_t);
  932. /* Now the GPMC is initialised, unreserve the chip-selects */
  933. gpmc_cs_map = 0;
  934. return 0;
  935. }
  936. #ifdef CONFIG_OF
  937. static struct of_device_id gpmc_dt_ids[] = {
  938. { .compatible = "ti,omap2420-gpmc" },
  939. { .compatible = "ti,omap2430-gpmc" },
  940. { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
  941. { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
  942. { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
  943. { }
  944. };
  945. MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
  946. static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
  947. struct gpmc_timings *gpmc_t)
  948. {
  949. u32 val;
  950. memset(gpmc_t, 0, sizeof(*gpmc_t));
  951. /* minimum clock period for syncronous mode */
  952. if (!of_property_read_u32(np, "gpmc,sync-clk", &val))
  953. gpmc_t->sync_clk = val;
  954. /* chip select timtings */
  955. if (!of_property_read_u32(np, "gpmc,cs-on", &val))
  956. gpmc_t->cs_on = val;
  957. if (!of_property_read_u32(np, "gpmc,cs-rd-off", &val))
  958. gpmc_t->cs_rd_off = val;
  959. if (!of_property_read_u32(np, "gpmc,cs-wr-off", &val))
  960. gpmc_t->cs_wr_off = val;
  961. /* ADV signal timings */
  962. if (!of_property_read_u32(np, "gpmc,adv-on", &val))
  963. gpmc_t->adv_on = val;
  964. if (!of_property_read_u32(np, "gpmc,adv-rd-off", &val))
  965. gpmc_t->adv_rd_off = val;
  966. if (!of_property_read_u32(np, "gpmc,adv-wr-off", &val))
  967. gpmc_t->adv_wr_off = val;
  968. /* WE signal timings */
  969. if (!of_property_read_u32(np, "gpmc,we-on", &val))
  970. gpmc_t->we_on = val;
  971. if (!of_property_read_u32(np, "gpmc,we-off", &val))
  972. gpmc_t->we_off = val;
  973. /* OE signal timings */
  974. if (!of_property_read_u32(np, "gpmc,oe-on", &val))
  975. gpmc_t->oe_on = val;
  976. if (!of_property_read_u32(np, "gpmc,oe-off", &val))
  977. gpmc_t->oe_off = val;
  978. /* access and cycle timings */
  979. if (!of_property_read_u32(np, "gpmc,page-burst-access", &val))
  980. gpmc_t->page_burst_access = val;
  981. if (!of_property_read_u32(np, "gpmc,access", &val))
  982. gpmc_t->access = val;
  983. if (!of_property_read_u32(np, "gpmc,rd-cycle", &val))
  984. gpmc_t->rd_cycle = val;
  985. if (!of_property_read_u32(np, "gpmc,wr-cycle", &val))
  986. gpmc_t->wr_cycle = val;
  987. /* only for OMAP3430 */
  988. if (!of_property_read_u32(np, "gpmc,wr-access", &val))
  989. gpmc_t->wr_access = val;
  990. if (!of_property_read_u32(np, "gpmc,wr-data-mux-bus", &val))
  991. gpmc_t->wr_data_mux_bus = val;
  992. }
  993. #ifdef CONFIG_MTD_NAND
  994. static const char * const nand_ecc_opts[] = {
  995. [OMAP_ECC_HAMMING_CODE_DEFAULT] = "sw",
  996. [OMAP_ECC_HAMMING_CODE_HW] = "hw",
  997. [OMAP_ECC_HAMMING_CODE_HW_ROMCODE] = "hw-romcode",
  998. [OMAP_ECC_BCH4_CODE_HW] = "bch4",
  999. [OMAP_ECC_BCH8_CODE_HW] = "bch8",
  1000. };
  1001. static int gpmc_probe_nand_child(struct platform_device *pdev,
  1002. struct device_node *child)
  1003. {
  1004. u32 val;
  1005. const char *s;
  1006. struct gpmc_timings gpmc_t;
  1007. struct omap_nand_platform_data *gpmc_nand_data;
  1008. if (of_property_read_u32(child, "reg", &val) < 0) {
  1009. dev_err(&pdev->dev, "%s has no 'reg' property\n",
  1010. child->full_name);
  1011. return -ENODEV;
  1012. }
  1013. gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data),
  1014. GFP_KERNEL);
  1015. if (!gpmc_nand_data)
  1016. return -ENOMEM;
  1017. gpmc_nand_data->cs = val;
  1018. gpmc_nand_data->of_node = child;
  1019. if (!of_property_read_string(child, "ti,nand-ecc-opt", &s))
  1020. for (val = 0; val < ARRAY_SIZE(nand_ecc_opts); val++)
  1021. if (!strcasecmp(s, nand_ecc_opts[val])) {
  1022. gpmc_nand_data->ecc_opt = val;
  1023. break;
  1024. }
  1025. val = of_get_nand_bus_width(child);
  1026. if (val == 16)
  1027. gpmc_nand_data->devsize = NAND_BUSWIDTH_16;
  1028. gpmc_read_timings_dt(child, &gpmc_t);
  1029. gpmc_nand_init(gpmc_nand_data, &gpmc_t);
  1030. return 0;
  1031. }
  1032. #else
  1033. static int gpmc_probe_nand_child(struct platform_device *pdev,
  1034. struct device_node *child)
  1035. {
  1036. return 0;
  1037. }
  1038. #endif
  1039. static int gpmc_probe_dt(struct platform_device *pdev)
  1040. {
  1041. int ret;
  1042. struct device_node *child;
  1043. const struct of_device_id *of_id =
  1044. of_match_device(gpmc_dt_ids, &pdev->dev);
  1045. if (!of_id)
  1046. return 0;
  1047. for_each_node_by_name(child, "nand") {
  1048. ret = gpmc_probe_nand_child(pdev, child);
  1049. if (ret < 0) {
  1050. of_node_put(child);
  1051. return ret;
  1052. }
  1053. }
  1054. return 0;
  1055. }
  1056. #else
  1057. static int gpmc_probe_dt(struct platform_device *pdev)
  1058. {
  1059. return 0;
  1060. }
  1061. #endif
  1062. static int gpmc_probe(struct platform_device *pdev)
  1063. {
  1064. int rc;
  1065. u32 l;
  1066. struct resource *res;
  1067. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1068. if (res == NULL)
  1069. return -ENOENT;
  1070. phys_base = res->start;
  1071. mem_size = resource_size(res);
  1072. gpmc_base = devm_request_and_ioremap(&pdev->dev, res);
  1073. if (!gpmc_base) {
  1074. dev_err(&pdev->dev, "error: request memory / ioremap\n");
  1075. return -EADDRNOTAVAIL;
  1076. }
  1077. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1078. if (res == NULL)
  1079. dev_warn(&pdev->dev, "Failed to get resource: irq\n");
  1080. else
  1081. gpmc_irq = res->start;
  1082. gpmc_l3_clk = clk_get(&pdev->dev, "fck");
  1083. if (IS_ERR(gpmc_l3_clk)) {
  1084. dev_err(&pdev->dev, "error: clk_get\n");
  1085. gpmc_irq = 0;
  1086. return PTR_ERR(gpmc_l3_clk);
  1087. }
  1088. clk_prepare_enable(gpmc_l3_clk);
  1089. gpmc_dev = &pdev->dev;
  1090. l = gpmc_read_reg(GPMC_REVISION);
  1091. if (GPMC_REVISION_MAJOR(l) > 0x4)
  1092. gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
  1093. dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
  1094. GPMC_REVISION_MINOR(l));
  1095. rc = gpmc_mem_init();
  1096. if (IS_ERR_VALUE(rc)) {
  1097. clk_disable_unprepare(gpmc_l3_clk);
  1098. clk_put(gpmc_l3_clk);
  1099. dev_err(gpmc_dev, "failed to reserve memory\n");
  1100. return rc;
  1101. }
  1102. if (IS_ERR_VALUE(gpmc_setup_irq()))
  1103. dev_warn(gpmc_dev, "gpmc_setup_irq failed\n");
  1104. rc = gpmc_probe_dt(pdev);
  1105. if (rc < 0) {
  1106. clk_disable_unprepare(gpmc_l3_clk);
  1107. clk_put(gpmc_l3_clk);
  1108. dev_err(gpmc_dev, "failed to probe DT parameters\n");
  1109. return rc;
  1110. }
  1111. return 0;
  1112. }
  1113. static int gpmc_remove(struct platform_device *pdev)
  1114. {
  1115. gpmc_free_irq();
  1116. gpmc_mem_exit();
  1117. gpmc_dev = NULL;
  1118. return 0;
  1119. }
  1120. static struct platform_driver gpmc_driver = {
  1121. .probe = gpmc_probe,
  1122. .remove = gpmc_remove,
  1123. .driver = {
  1124. .name = DEVICE_NAME,
  1125. .owner = THIS_MODULE,
  1126. .of_match_table = of_match_ptr(gpmc_dt_ids),
  1127. },
  1128. };
  1129. static __init int gpmc_init(void)
  1130. {
  1131. return platform_driver_register(&gpmc_driver);
  1132. }
  1133. static __exit void gpmc_exit(void)
  1134. {
  1135. platform_driver_unregister(&gpmc_driver);
  1136. }
  1137. postcore_initcall(gpmc_init);
  1138. module_exit(gpmc_exit);
  1139. static int __init omap_gpmc_init(void)
  1140. {
  1141. struct omap_hwmod *oh;
  1142. struct platform_device *pdev;
  1143. char *oh_name = "gpmc";
  1144. /*
  1145. * if the board boots up with a populated DT, do not
  1146. * manually add the device from this initcall
  1147. */
  1148. if (of_have_populated_dt())
  1149. return -ENODEV;
  1150. oh = omap_hwmod_lookup(oh_name);
  1151. if (!oh) {
  1152. pr_err("Could not look up %s\n", oh_name);
  1153. return -ENODEV;
  1154. }
  1155. pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0, NULL, 0, 0);
  1156. WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name);
  1157. return IS_ERR(pdev) ? PTR_ERR(pdev) : 0;
  1158. }
  1159. postcore_initcall(omap_gpmc_init);
  1160. static irqreturn_t gpmc_handle_irq(int irq, void *dev)
  1161. {
  1162. int i;
  1163. u32 regval;
  1164. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  1165. if (!regval)
  1166. return IRQ_NONE;
  1167. for (i = 0; i < GPMC_NR_IRQ; i++)
  1168. if (regval & gpmc_client_irq[i].bitmask)
  1169. generic_handle_irq(gpmc_client_irq[i].irq);
  1170. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  1171. return IRQ_HANDLED;
  1172. }
  1173. #ifdef CONFIG_ARCH_OMAP3
  1174. static struct omap3_gpmc_regs gpmc_context;
  1175. void omap3_gpmc_save_context(void)
  1176. {
  1177. int i;
  1178. gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
  1179. gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
  1180. gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
  1181. gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
  1182. gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
  1183. gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
  1184. gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
  1185. for (i = 0; i < GPMC_CS_NUM; i++) {
  1186. gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
  1187. if (gpmc_context.cs_context[i].is_valid) {
  1188. gpmc_context.cs_context[i].config1 =
  1189. gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
  1190. gpmc_context.cs_context[i].config2 =
  1191. gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
  1192. gpmc_context.cs_context[i].config3 =
  1193. gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
  1194. gpmc_context.cs_context[i].config4 =
  1195. gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
  1196. gpmc_context.cs_context[i].config5 =
  1197. gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
  1198. gpmc_context.cs_context[i].config6 =
  1199. gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
  1200. gpmc_context.cs_context[i].config7 =
  1201. gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
  1202. }
  1203. }
  1204. }
  1205. void omap3_gpmc_restore_context(void)
  1206. {
  1207. int i;
  1208. gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
  1209. gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
  1210. gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
  1211. gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
  1212. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
  1213. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
  1214. gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
  1215. for (i = 0; i < GPMC_CS_NUM; i++) {
  1216. if (gpmc_context.cs_context[i].is_valid) {
  1217. gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
  1218. gpmc_context.cs_context[i].config1);
  1219. gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
  1220. gpmc_context.cs_context[i].config2);
  1221. gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
  1222. gpmc_context.cs_context[i].config3);
  1223. gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
  1224. gpmc_context.cs_context[i].config4);
  1225. gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
  1226. gpmc_context.cs_context[i].config5);
  1227. gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
  1228. gpmc_context.cs_context[i].config6);
  1229. gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
  1230. gpmc_context.cs_context[i].config7);
  1231. }
  1232. }
  1233. }
  1234. #endif /* CONFIG_ARCH_OMAP3 */