alarmDataOperation.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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/platform_data/mtd-nand-omap2.h>
  28. #include <asm/mach-types.h>
  29. #include "soc.h"
  30. #include "common.h"
  31. #include "omap_device.h"
  32. #include "gpmc.h"
  33. #define DEVICE_NAME "omap-gpmc"
  34. /* GPMC register offsets */
  35. #define GPMC_REVISION 0x00
  36. #define GPMC_SYSCONFIG 0x10
  37. #define GPMC_SYSSTATUS 0x14
  38. #define GPMC_IRQSTATUS 0x18
  39. #define GPMC_IRQENABLE 0x1c
  40. #define GPMC_TIMEOUT_CONTROL 0x40
  41. #define GPMC_ERR_ADDRESS 0x44
  42. #define GPMC_ERR_TYPE 0x48
  43. #define GPMC_CONFIG 0x50
  44. #define GPMC_STATUS 0x54
  45. #define GPMC_PREFETCH_CONFIG1 0x1e0
  46. #define GPMC_PREFETCH_CONFIG2 0x1e4
  47. #define GPMC_PREFETCH_CONTROL 0x1ec
  48. #define GPMC_PREFETCH_STATUS 0x1f0
  49. #define GPMC_ECC_CONFIG 0x1f4
  50. #define GPMC_ECC_CONTROL 0x1f8
  51. #define GPMC_ECC_SIZE_CONFIG 0x1fc
  52. #define GPMC_ECC1_RESULT 0x200
  53. #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
  54. #define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */
  55. #define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */
  56. #define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */
  57. /* GPMC ECC control settings */
  58. #define GPMC_ECC_CTRL_ECCCLEAR 0x100
  59. #define GPMC_ECC_CTRL_ECCDISABLE 0x000
  60. #define GPMC_ECC_CTRL_ECCREG1 0x001
  61. #define GPMC_ECC_CTRL_ECCREG2 0x002
  62. #define GPMC_ECC_CTRL_ECCREG3 0x003
  63. #define GPMC_ECC_CTRL_ECCREG4 0x004
  64. #define GPMC_ECC_CTRL_ECCREG5 0x005
  65. #define GPMC_ECC_CTRL_ECCREG6 0x006
  66. #define GPMC_ECC_CTRL_ECCREG7 0x007
  67. #define GPMC_ECC_CTRL_ECCREG8 0x008
  68. #define GPMC_ECC_CTRL_ECCREG9 0x009
  69. #define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
  70. #define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
  71. #define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
  72. #define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
  73. #define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
  74. #define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
  75. #define GPMC_CS0_OFFSET 0x60
  76. #define GPMC_CS_SIZE 0x30
  77. #define GPMC_BCH_SIZE 0x10
  78. #define GPMC_MEM_START 0x00000000
  79. #define GPMC_MEM_END 0x3FFFFFFF
  80. #define BOOT_ROM_SPACE 0x100000 /* 1MB */
  81. #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
  82. #define GPMC_SECTION_SHIFT 28 /* 128 MB */
  83. #define CS_NUM_SHIFT 24
  84. #define ENABLE_PREFETCH (0x1 << 7)
  85. #define DMA_MPU_MODE 2
  86. #define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
  87. #define GPMC_REVISION_MINOR(l) (l & 0xf)
  88. #define GPMC_HAS_WR_ACCESS 0x1
  89. #define GPMC_HAS_WR_DATA_MUX_BUS 0x2
  90. /* XXX: Only NAND irq has been considered,currently these are the only ones used
  91. */
  92. #define GPMC_NR_IRQ 2
  93. struct gpmc_client_irq {
  94. unsigned irq;
  95. u32 bitmask;
  96. };
  97. /* Structure to save gpmc cs context */
  98. struct gpmc_cs_config {
  99. u32 config1;
  100. u32 config2;
  101. u32 config3;
  102. u32 config4;
  103. u32 config5;
  104. u32 config6;
  105. u32 config7;
  106. int is_valid;
  107. };
  108. /*
  109. * Structure to save/restore gpmc context
  110. * to support core off on OMAP3
  111. */
  112. struct omap3_gpmc_regs {
  113. u32 sysconfig;
  114. u32 irqenable;
  115. u32 timeout_ctrl;
  116. u32 config;
  117. u32 prefetch_config1;
  118. u32 prefetch_config2;
  119. u32 prefetch_control;
  120. struct gpmc_cs_config cs_context[GPMC_CS_NUM];
  121. };
  122. static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
  123. static struct irq_chip gpmc_irq_chip;
  124. static unsigned gpmc_irq_start;
  125. static struct resource gpmc_mem_root;
  126. static struct resource gpmc_cs_mem[GPMC_CS_NUM];
  127. static DEFINE_SPINLOCK(gpmc_mem_lock);
  128. static unsigned int gpmc_cs_map; /* flag for cs which are initialized */
  129. static struct device *gpmc_dev;
  130. static int gpmc_irq;
  131. static resource_size_t phys_base, mem_size;
  132. static unsigned gpmc_capability;
  133. static void __iomem *gpmc_base;
  134. static struct clk *gpmc_l3_clk;
  135. static irqreturn_t gpmc_handle_irq(int irq, void *dev);
  136. static void gpmc_write_reg(int idx, u32 val)
  137. {
  138. __raw_writel(val, gpmc_base + idx);
  139. }
  140. static u32 gpmc_read_reg(int idx)
  141. {
  142. return __raw_readl(gpmc_base + idx);
  143. }
  144. void gpmc_cs_write_reg(int cs, int idx, u32 val)
  145. {
  146. void __iomem *reg_addr;
  147. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  148. __raw_writel(val, reg_addr);
  149. }
  150. u32 gpmc_cs_read_reg(int cs, int idx)
  151. {
  152. void __iomem *reg_addr;
  153. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  154. return __raw_readl(reg_addr);
  155. }
  156. /* TODO: Add support for gpmc_fck to clock framework and use it */
  157. unsigned long gpmc_get_fclk_period(void)
  158. {
  159. unsigned long rate = clk_get_rate(gpmc_l3_clk);
  160. if (rate == 0) {
  161. printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
  162. return 0;
  163. }
  164. rate /= 1000;
  165. rate = 1000000000 / rate; /* In picoseconds */
  166. return rate;
  167. }
  168. unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
  169. {
  170. unsigned long tick_ps;
  171. /* Calculate in picosecs to yield more exact results */
  172. tick_ps = gpmc_get_fclk_period();
  173. return (time_ns * 1000 + tick_ps - 1) / tick_ps;
  174. }
  175. unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
  176. {
  177. unsigned long tick_ps;
  178. /* Calculate in picosecs to yield more exact results */
  179. tick_ps = gpmc_get_fclk_period();
  180. return (time_ps + tick_ps - 1) / tick_ps;
  181. }
  182. unsigned int gpmc_ticks_to_ns(unsigned int ticks)
  183. {
  184. return ticks * gpmc_get_fclk_period() / 1000;
  185. }
  186. unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns)
  187. {
  188. unsigned long ticks = gpmc_ns_to_ticks(time_ns);
  189. return ticks * gpmc_get_fclk_period() / 1000;
  190. }
  191. static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
  192. {
  193. return ticks * gpmc_get_fclk_period();
  194. }
  195. static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
  196. {
  197. unsigned long ticks = gpmc_ps_to_ticks(time_ps);
  198. return ticks * gpmc_get_fclk_period();
  199. }
  200. static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
  201. {
  202. u32 l;
  203. l = gpmc_cs_read_reg(cs, reg);
  204. if (value)
  205. l |= mask;
  206. else
  207. l &= ~mask;
  208. gpmc_cs_write_reg(cs, reg, l);
  209. }
  210. static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
  211. {
  212. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
  213. GPMC_CONFIG1_TIME_PARA_GRAN,
  214. p->time_para_granularity);
  215. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
  216. GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
  217. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
  218. GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
  219. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
  220. GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
  221. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
  222. GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay);
  223. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
  224. GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
  225. p->cycle2cyclesamecsen);
  226. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
  227. GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
  228. p->cycle2cyclediffcsen);
  229. }
  230. #ifdef DEBUG
  231. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  232. int time, const char *name)
  233. #else
  234. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  235. int time)
  236. #endif
  237. {
  238. u32 l;
  239. int ticks, mask, nr_bits;
  240. if (time == 0)
  241. ticks = 0;
  242. else
  243. ticks = gpmc_ns_to_ticks(time);
  244. nr_bits = end_bit - st_bit + 1;
  245. if (ticks >= 1 << nr_bits) {
  246. #ifdef DEBUG
  247. printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
  248. cs, name, time, ticks, 1 << nr_bits);
  249. #endif
  250. return -1;
  251. }
  252. mask = (1 << nr_bits) - 1;
  253. l = gpmc_cs_read_reg(cs, reg);
  254. #ifdef DEBUG
  255. printk(KERN_INFO
  256. "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
  257. cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
  258. (l >> st_bit) & mask, time);
  259. #endif
  260. l &= ~(mask << st_bit);
  261. l |= ticks << st_bit;
  262. gpmc_cs_write_reg(cs, reg, l);
  263. return 0;
  264. }
  265. #ifdef DEBUG
  266. #define GPMC_SET_ONE(reg, st, end, field) \
  267. if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
  268. t->field, #field) < 0) \
  269. return -1
  270. #else
  271. #define GPMC_SET_ONE(reg, st, end, field) \
  272. if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
  273. return -1
  274. #endif
  275. int gpmc_calc_divider(unsigned int sync_clk)
  276. {
  277. int div;
  278. u32 l;
  279. l = sync_clk + (gpmc_get_fclk_period() - 1);
  280. div = l / gpmc_get_fclk_period();
  281. if (div > 4)
  282. return -1;
  283. if (div <= 0)
  284. div = 1;
  285. return div;
  286. }
  287. int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
  288. {
  289. int div;
  290. u32 l;
  291. div = gpmc_calc_divider(t->sync_clk);
  292. if (div < 0)
  293. return div;
  294. GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
  295. GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
  296. GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
  297. GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
  298. GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
  299. GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
  300. GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
  301. GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
  302. GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
  303. GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
  304. GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
  305. GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
  306. GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
  307. GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
  308. GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
  309. GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
  310. GPMC_SET_ONE(GPMC_CS_CONFIG1, 18, 19, wait_monitoring);
  311. GPMC_SET_ONE(GPMC_CS_CONFIG1, 25, 26, clk_activation);
  312. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  313. GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
  314. if (gpmc_capability & GPMC_HAS_WR_ACCESS)
  315. GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
  316. /* caller is expected to have initialized CONFIG1 to cover
  317. * at least sync vs async
  318. */
  319. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  320. if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
  321. #ifdef DEBUG
  322. printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
  323. cs, (div * gpmc_get_fclk_period()) / 1000, div);
  324. #endif
  325. l &= ~0x03;
  326. l |= (div - 1);
  327. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
  328. }
  329. gpmc_cs_bool_timings(cs, &t->bool_timings);
  330. return 0;
  331. }
  332. static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
  333. {
  334. u32 l;
  335. u32 mask;
  336. mask = (1 << GPMC_SECTION_SHIFT) - size;
  337. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  338. l &= ~0x3f;
  339. l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
  340. l &= ~(0x0f << 8);
  341. l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
  342. l |= GPMC_CONFIG7_CSVALID;
  343. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  344. }
  345. static void gpmc_cs_disable_mem(int cs)
  346. {
  347. u32 l;
  348. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  349. l &= ~GPMC_CONFIG7_CSVALID;
  350. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  351. }
  352. static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
  353. {
  354. u32 l;
  355. u32 mask;
  356. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  357. *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
  358. mask = (l >> 8) & 0x0f;
  359. *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
  360. }
  361. static int gpmc_cs_mem_enabled(int cs)
  362. {
  363. u32 l;
  364. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  365. return l & GPMC_CONFIG7_CSVALID;
  366. }
  367. int gpmc_cs_set_reserved(int cs, int reserved)
  368. {
  369. if (cs > GPMC_CS_NUM)
  370. return -ENODEV;
  371. gpmc_cs_map &= ~(1 << cs);
  372. gpmc_cs_map |= (reserved ? 1 : 0) << cs;
  373. return 0;
  374. }
  375. int gpmc_cs_reserved(int cs)
  376. {
  377. if (cs > GPMC_CS_NUM)
  378. return -ENODEV;
  379. return gpmc_cs_map & (1 << cs);
  380. }
  381. static unsigned long gpmc_mem_align(unsigned long size)
  382. {
  383. int order;
  384. size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
  385. order = GPMC_CHUNK_SHIFT - 1;
  386. do {
  387. size >>= 1;
  388. order++;
  389. } while (size);
  390. size = 1 << order;
  391. return size;
  392. }
  393. static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
  394. {
  395. struct resource *res = &gpmc_cs_mem[cs];
  396. int r;
  397. size = gpmc_mem_align(size);
  398. spin_lock(&gpmc_mem_lock);
  399. res->start = base;
  400. res->end = base + size - 1;
  401. r = request_resource(&gpmc_mem_root, res);
  402. spin_unlock(&gpmc_mem_lock);
  403. return r;
  404. }
  405. static int gpmc_cs_delete_mem(int cs)
  406. {
  407. struct resource *res = &gpmc_cs_mem[cs];
  408. int r;
  409. spin_lock(&gpmc_mem_lock);
  410. r = release_resource(&gpmc_cs_mem[cs]);
  411. res->start = 0;
  412. res->end = 0;
  413. spin_unlock(&gpmc_mem_lock);
  414. return r;
  415. }
  416. int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
  417. {
  418. struct resource *res = &gpmc_cs_mem[cs];
  419. int r = -1;
  420. if (cs > GPMC_CS_NUM)
  421. return -ENODEV;
  422. size = gpmc_mem_align(size);
  423. if (size > (1 << GPMC_SECTION_SHIFT))
  424. return -ENOMEM;
  425. spin_lock(&gpmc_mem_lock);
  426. if (gpmc_cs_reserved(cs)) {
  427. r = -EBUSY;
  428. goto out;
  429. }
  430. if (gpmc_cs_mem_enabled(cs))
  431. r = adjust_resource(res, res->start & ~(size - 1), size);
  432. if (r < 0)
  433. r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
  434. size, NULL, NULL);
  435. if (r < 0)
  436. goto out;
  437. gpmc_cs_enable_mem(cs, res->start, resource_size(res));
  438. *base = res->start;
  439. gpmc_cs_set_reserved(cs, 1);
  440. out:
  441. spin_unlock(&gpmc_mem_lock);
  442. return r;
  443. }
  444. EXPORT_SYMBOL(gpmc_cs_request);
  445. void gpmc_cs_free(int cs)
  446. {
  447. spin_lock(&gpmc_mem_lock);
  448. if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) {
  449. printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
  450. BUG();
  451. spin_unlock(&gpmc_mem_lock);
  452. return;
  453. }
  454. gpmc_cs_disable_mem(cs);
  455. release_resource(&gpmc_cs_mem[cs]);
  456. gpmc_cs_set_reserved(cs, 0);
  457. spin_unlock(&gpmc_mem_lock);
  458. }
  459. EXPORT_SYMBOL(gpmc_cs_free);
  460. /**
  461. * gpmc_cs_configure - write request to configure gpmc
  462. * @cs: chip select number
  463. * @cmd: command type
  464. * @wval: value to write
  465. * @return status of the operation
  466. */
  467. int gpmc_cs_configure(int cs, int cmd, int wval)
  468. {
  469. int err = 0;
  470. u32 regval = 0;
  471. switch (cmd) {
  472. case GPMC_ENABLE_IRQ:
  473. gpmc_write_reg(GPMC_IRQENABLE, wval);
  474. break;
  475. case GPMC_SET_IRQ_STATUS:
  476. gpmc_write_reg(GPMC_IRQSTATUS, wval);
  477. break;
  478. case GPMC_CONFIG_WP:
  479. regval = gpmc_read_reg(GPMC_CONFIG);
  480. if (wval)
  481. regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
  482. else
  483. regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
  484. gpmc_write_reg(GPMC_CONFIG, regval);
  485. break;
  486. case GPMC_CONFIG_RDY_BSY:
  487. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  488. if (wval)
  489. regval |= WR_RD_PIN_MONITORING;
  490. else
  491. regval &= ~WR_RD_PIN_MONITORING;
  492. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  493. break;
  494. case GPMC_CONFIG_DEV_SIZE:
  495. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  496. /* clear 2 target bits */
  497. regval &= ~GPMC_CONFIG1_DEVICESIZE(3);
  498. /* set the proper value */
  499. regval |= GPMC_CONFIG1_DEVICESIZE(wval);
  500. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  501. break;
  502. case GPMC_CONFIG_DEV_TYPE:
  503. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  504. regval |= GPMC_CONFIG1_DEVICETYPE(wval);
  505. if (wval == GPMC_DEVICETYPE_NOR)
  506. regval |= GPMC_CONFIG1_MUXADDDATA;
  507. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  508. break;
  509. default:
  510. printk(KERN_ERR "gpmc_configure_cs: Not supported\n");
  511. err = -EINVAL;
  512. }
  513. return err;
  514. }
  515. EXPORT_SYMBOL(gpmc_cs_configure);
  516. void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
  517. {
  518. int i;
  519. reg->gpmc_status = gpmc_base + GPMC_STATUS;
  520. reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
  521. GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
  522. reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
  523. GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
  524. reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
  525. GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
  526. reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
  527. reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
  528. reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
  529. reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
  530. reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
  531. reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
  532. reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
  533. reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
  534. for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
  535. reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
  536. GPMC_BCH_SIZE * i;
  537. reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
  538. GPMC_BCH_SIZE * i;
  539. reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
  540. GPMC_BCH_SIZE * i;
  541. reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
  542. GPMC_BCH_SIZE * i;
  543. }
  544. }
  545. int gpmc_get_client_irq(unsigned irq_config)
  546. {
  547. int i;
  548. if (hweight32(irq_config) > 1)
  549. return 0;
  550. for (i = 0; i < GPMC_NR_IRQ; i++)
  551. if (gpmc_client_irq[i].bitmask & irq_config)
  552. return gpmc_client_irq[i].irq;
  553. return 0;
  554. }
  555. static int gpmc_irq_endis(unsigned irq, bool endis)
  556. {
  557. int i;
  558. u32 regval;
  559. for (i = 0; i < GPMC_NR_IRQ; i++)
  560. if (irq == gpmc_client_irq[i].irq) {
  561. regval = gpmc_read_reg(GPMC_IRQENABLE);
  562. if (endis)
  563. regval |= gpmc_client_irq[i].bitmask;
  564. else
  565. regval &= ~gpmc_client_irq[i].bitmask;
  566. gpmc_write_reg(GPMC_IRQENABLE, regval);
  567. break;
  568. }
  569. return 0;
  570. }
  571. static void gpmc_irq_disable(struct irq_data *p)
  572. {
  573. gpmc_irq_endis(p->irq, false);
  574. }
  575. static void gpmc_irq_enable(struct irq_data *p)
  576. {
  577. gpmc_irq_endis(p->irq, true);
  578. }
  579. static void gpmc_irq_noop(struct irq_data *data) { }
  580. static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
  581. static int gpmc_setup_irq(void)
  582. {
  583. int i;
  584. u32 regval;
  585. if (!gpmc_irq)
  586. return -EINVAL;
  587. gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
  588. if (IS_ERR_VALUE(gpmc_irq_start)) {
  589. pr_err("irq_alloc_descs failed\n");
  590. return gpmc_irq_start;
  591. }
  592. gpmc_irq_chip.name = "gpmc";
  593. gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
  594. gpmc_irq_chip.irq_enable = gpmc_irq_enable;
  595. gpmc_irq_chip.irq_disable = gpmc_irq_disable;
  596. gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
  597. gpmc_irq_chip.irq_ack = gpmc_irq_noop;
  598. gpmc_irq_chip.irq_mask = gpmc_irq_noop;
  599. gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
  600. gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
  601. gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
  602. for (i = 0; i < GPMC_NR_IRQ; i++) {
  603. gpmc_client_irq[i].irq = gpmc_irq_start + i;
  604. irq_set_chip_and_handler(gpmc_client_irq[i].irq,
  605. &gpmc_irq_chip, handle_simple_irq);
  606. set_irq_flags(gpmc_client_irq[i].irq,
  607. IRQF_VALID | IRQF_NOAUTOEN);
  608. }
  609. /* Disable interrupts */
  610. gpmc_write_reg(GPMC_IRQENABLE, 0);
  611. /* clear interrupts */
  612. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  613. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  614. return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
  615. }
  616. static int gpmc_free_irq(void)
  617. {
  618. int i;
  619. if (gpmc_irq)
  620. free_irq(gpmc_irq, NULL);
  621. for (i = 0; i < GPMC_NR_IRQ; i++) {
  622. irq_set_handler(gpmc_client_irq[i].irq, NULL);
  623. irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip);
  624. irq_modify_status(gpmc_client_irq[i].irq, 0, 0);
  625. }
  626. irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ);
  627. return 0;
  628. }
  629. static void gpmc_mem_exit(void)
  630. {
  631. int cs;
  632. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  633. if (!gpmc_cs_mem_enabled(cs))
  634. continue;
  635. gpmc_cs_delete_mem(cs);
  636. }
  637. }
  638. static int gpmc_mem_init(void)
  639. {
  640. int cs, rc;
  641. unsigned long boot_rom_space = 0;
  642. /* never allocate the first page, to facilitate bug detection;
  643. * even if we didn't boot from ROM.
  644. */
  645. boot_rom_space = BOOT_ROM_SPACE;
  646. /* In apollon the CS0 is mapped as 0x0000 0000 */
  647. if (machine_is_omap_apollon())
  648. boot_rom_space = 0;
  649. gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
  650. gpmc_mem_root.end = GPMC_MEM_END;
  651. /* Reserve all regions that has been set up by bootloader */
  652. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  653. u32 base, size;
  654. if (!gpmc_cs_mem_enabled(cs))
  655. continue;
  656. gpmc_cs_get_memconf(cs, &base, &size);
  657. rc = gpmc_cs_insert_mem(cs, base, size);
  658. if (IS_ERR_VALUE(rc)) {
  659. while (--cs >= 0)
  660. if (gpmc_cs_mem_enabled(cs))
  661. gpmc_cs_delete_mem(cs);
  662. return rc;
  663. }
  664. }
  665. return 0;
  666. }
  667. static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
  668. {
  669. u32 temp;
  670. int div;
  671. div = gpmc_calc_divider(sync_clk);
  672. temp = gpmc_ps_to_ticks(time_ps);
  673. temp = (temp + div - 1) / div;
  674. return gpmc_ticks_to_ps(temp * div);
  675. }
  676. /* XXX: can the cycles be avoided ? */
  677. static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
  678. struct gpmc_device_timings *dev_t)
  679. {
  680. bool mux = dev_t->mux;
  681. u32 temp;
  682. /* adv_rd_off */
  683. temp = dev_t->t_avdp_r;
  684. /* XXX: mux check required ? */
  685. if (mux) {
  686. /* XXX: t_avdp not to be required for sync, only added for tusb
  687. * this indirectly necessitates requirement of t_avdp_r and
  688. * t_avdp_w instead of having a single t_avdp
  689. */
  690. temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
  691. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  692. }
  693. gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
  694. /* oe_on */
  695. temp = dev_t->t_oeasu; /* XXX: remove this ? */
  696. if (mux) {
  697. temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
  698. temp = max_t(u32, temp, gpmc_t->adv_rd_off +
  699. gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
  700. }
  701. gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
  702. /* access */
  703. /* XXX: any scope for improvement ?, by combining oe_on
  704. * and clk_activation, need to check whether
  705. * access = clk_activation + round to sync clk ?
  706. */
  707. temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
  708. temp += gpmc_t->clk_activation;
  709. if (dev_t->cyc_oe)
  710. temp = max_t(u32, temp, gpmc_t->oe_on +
  711. gpmc_ticks_to_ps(dev_t->cyc_oe));
  712. gpmc_t->access = gpmc_round_ps_to_ticks(temp);
  713. gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
  714. gpmc_t->cs_rd_off = gpmc_t->oe_off;
  715. /* rd_cycle */
  716. temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
  717. temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
  718. gpmc_t->access;
  719. /* XXX: barter t_ce_rdyz with t_cez_r ? */
  720. if (dev_t->t_ce_rdyz)
  721. temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
  722. gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
  723. return 0;
  724. }
  725. static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
  726. struct gpmc_device_timings *dev_t)
  727. {
  728. bool mux = dev_t->mux;
  729. u32 temp;
  730. /* adv_wr_off */
  731. temp = dev_t->t_avdp_w;
  732. if (mux) {
  733. temp = max_t(u32, temp,
  734. gpmc_t->clk_activation + dev_t->t_avdh);
  735. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  736. }
  737. gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
  738. /* wr_data_mux_bus */
  739. temp = max_t(u32, dev_t->t_weasu,
  740. gpmc_t->clk_activation + dev_t->t_rdyo);
  741. /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
  742. * and in that case remember to handle we_on properly
  743. */
  744. if (mux) {
  745. temp = max_t(u32, temp,
  746. gpmc_t->adv_wr_off + dev_t->t_aavdh);
  747. temp = max_t(u32, temp, gpmc_t->adv_wr_off +
  748. gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
  749. }
  750. gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
  751. /* we_on */
  752. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  753. gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
  754. else
  755. gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
  756. /* wr_access */
  757. /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
  758. gpmc_t->wr_access = gpmc_t->access;
  759. /* we_off */
  760. temp = gpmc_t->we_on + dev_t->t_wpl;
  761. temp = max_t(u32, temp,
  762. gpmc_t->wr_access + gpmc_ticks_to_ps(1));
  763. temp = max_t(u32, temp,
  764. gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
  765. gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
  766. gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
  767. dev_t->t_wph);
  768. /* wr_cycle */
  769. temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
  770. temp += gpmc_t->wr_access;
  771. /* XXX: barter t_ce_rdyz with t_cez_w ? */
  772. if (dev_t->t_ce_rdyz)
  773. temp = max_t(u32, temp,
  774. gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
  775. gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
  776. return 0;
  777. }
  778. static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
  779. struct gpmc_device_timings *dev_t)
  780. {
  781. bool mux = dev_t->mux;
  782. u32 temp;
  783. /* adv_rd_off */
  784. temp = dev_t->t_avdp_r;
  785. if (mux)
  786. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  787. gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
  788. /* oe_on */
  789. temp = dev_t->t_oeasu;
  790. if (mux)
  791. temp = max_t(u32, temp,
  792. gpmc_t->adv_rd_off + dev_t->t_aavdh);
  793. gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
  794. /* access */
  795. temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
  796. gpmc_t->oe_on + dev_t->t_oe);
  797. temp = max_t(u32, temp,
  798. gpmc_t->cs_on + dev_t->t_ce);
  799. temp = max_t(u32, temp,
  800. gpmc_t->adv_on + dev_t->t_aa);
  801. gpmc_t->access = gpmc_round_ps_to_ticks(temp);
  802. gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
  803. gpmc_t->cs_rd_off = gpmc_t->oe_off;
  804. /* rd_cycle */
  805. temp = max_t(u32, dev_t->t_rd_cycle,
  806. gpmc_t->cs_rd_off + dev_t->t_cez_r);
  807. temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
  808. gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
  809. return 0;
  810. }
  811. static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
  812. struct gpmc_device_timings *dev_t)
  813. {
  814. bool mux = dev_t->mux;
  815. u32 temp;
  816. /* adv_wr_off */
  817. temp = dev_t->t_avdp_w;
  818. if (mux)
  819. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  820. gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
  821. /* wr_data_mux_bus */
  822. temp = dev_t->t_weasu;
  823. if (mux) {
  824. temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
  825. temp = max_t(u32, temp, gpmc_t->adv_wr_off +
  826. gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
  827. }
  828. gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
  829. /* we_on */
  830. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  831. gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
  832. else
  833. gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
  834. /* we_off */
  835. temp = gpmc_t->we_on + dev_t->t_wpl;
  836. gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
  837. gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
  838. dev_t->t_wph);
  839. /* wr_cycle */
  840. temp = max_t(u32, dev_t->t_wr_cycle,
  841. gpmc_t->cs_wr_off + dev_t->t_cez_w);
  842. gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
  843. return 0;