gpmc.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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 <asm/mach-types.h>
  27. #include <plat/gpmc.h>
  28. #include <plat/sdrc.h>
  29. /* GPMC register offsets */
  30. #define GPMC_REVISION 0x00
  31. #define GPMC_SYSCONFIG 0x10
  32. #define GPMC_SYSSTATUS 0x14
  33. #define GPMC_IRQSTATUS 0x18
  34. #define GPMC_IRQENABLE 0x1c
  35. #define GPMC_TIMEOUT_CONTROL 0x40
  36. #define GPMC_ERR_ADDRESS 0x44
  37. #define GPMC_ERR_TYPE 0x48
  38. #define GPMC_CONFIG 0x50
  39. #define GPMC_STATUS 0x54
  40. #define GPMC_PREFETCH_CONFIG1 0x1e0
  41. #define GPMC_PREFETCH_CONFIG2 0x1e4
  42. #define GPMC_PREFETCH_CONTROL 0x1ec
  43. #define GPMC_PREFETCH_STATUS 0x1f0
  44. #define GPMC_ECC_CONFIG 0x1f4
  45. #define GPMC_ECC_CONTROL 0x1f8
  46. #define GPMC_ECC_SIZE_CONFIG 0x1fc
  47. #define GPMC_ECC1_RESULT 0x200
  48. #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
  49. /* GPMC ECC control settings */
  50. #define GPMC_ECC_CTRL_ECCCLEAR 0x100
  51. #define GPMC_ECC_CTRL_ECCDISABLE 0x000
  52. #define GPMC_ECC_CTRL_ECCREG1 0x001
  53. #define GPMC_ECC_CTRL_ECCREG2 0x002
  54. #define GPMC_ECC_CTRL_ECCREG3 0x003
  55. #define GPMC_ECC_CTRL_ECCREG4 0x004
  56. #define GPMC_ECC_CTRL_ECCREG5 0x005
  57. #define GPMC_ECC_CTRL_ECCREG6 0x006
  58. #define GPMC_ECC_CTRL_ECCREG7 0x007
  59. #define GPMC_ECC_CTRL_ECCREG8 0x008
  60. #define GPMC_ECC_CTRL_ECCREG9 0x009
  61. #define GPMC_CS0_OFFSET 0x60
  62. #define GPMC_CS_SIZE 0x30
  63. #define GPMC_MEM_START 0x00000000
  64. #define GPMC_MEM_END 0x3FFFFFFF
  65. #define BOOT_ROM_SPACE 0x100000 /* 1MB */
  66. #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
  67. #define GPMC_SECTION_SHIFT 28 /* 128 MB */
  68. #define CS_NUM_SHIFT 24
  69. #define ENABLE_PREFETCH (0x1 << 7)
  70. #define DMA_MPU_MODE 2
  71. /* Structure to save gpmc cs context */
  72. struct gpmc_cs_config {
  73. u32 config1;
  74. u32 config2;
  75. u32 config3;
  76. u32 config4;
  77. u32 config5;
  78. u32 config6;
  79. u32 config7;
  80. int is_valid;
  81. };
  82. /*
  83. * Structure to save/restore gpmc context
  84. * to support core off on OMAP3
  85. */
  86. struct omap3_gpmc_regs {
  87. u32 sysconfig;
  88. u32 irqenable;
  89. u32 timeout_ctrl;
  90. u32 config;
  91. u32 prefetch_config1;
  92. u32 prefetch_config2;
  93. u32 prefetch_control;
  94. struct gpmc_cs_config cs_context[GPMC_CS_NUM];
  95. };
  96. static struct resource gpmc_mem_root;
  97. static struct resource gpmc_cs_mem[GPMC_CS_NUM];
  98. static DEFINE_SPINLOCK(gpmc_mem_lock);
  99. static unsigned int gpmc_cs_map; /* flag for cs which are initialized */
  100. static int gpmc_ecc_used = -EINVAL; /* cs using ecc engine */
  101. static void __iomem *gpmc_base;
  102. static struct clk *gpmc_l3_clk;
  103. static irqreturn_t gpmc_handle_irq(int irq, void *dev);
  104. static void gpmc_write_reg(int idx, u32 val)
  105. {
  106. __raw_writel(val, gpmc_base + idx);
  107. }
  108. static u32 gpmc_read_reg(int idx)
  109. {
  110. return __raw_readl(gpmc_base + idx);
  111. }
  112. static void gpmc_cs_write_byte(int cs, int idx, u8 val)
  113. {
  114. void __iomem *reg_addr;
  115. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  116. __raw_writeb(val, reg_addr);
  117. }
  118. static u8 gpmc_cs_read_byte(int cs, int idx)
  119. {
  120. void __iomem *reg_addr;
  121. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  122. return __raw_readb(reg_addr);
  123. }
  124. void gpmc_cs_write_reg(int cs, int idx, u32 val)
  125. {
  126. void __iomem *reg_addr;
  127. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  128. __raw_writel(val, reg_addr);
  129. }
  130. u32 gpmc_cs_read_reg(int cs, int idx)
  131. {
  132. void __iomem *reg_addr;
  133. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  134. return __raw_readl(reg_addr);
  135. }
  136. /* TODO: Add support for gpmc_fck to clock framework and use it */
  137. unsigned long gpmc_get_fclk_period(void)
  138. {
  139. unsigned long rate = clk_get_rate(gpmc_l3_clk);
  140. if (rate == 0) {
  141. printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
  142. return 0;
  143. }
  144. rate /= 1000;
  145. rate = 1000000000 / rate; /* In picoseconds */
  146. return rate;
  147. }
  148. unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
  149. {
  150. unsigned long tick_ps;
  151. /* Calculate in picosecs to yield more exact results */
  152. tick_ps = gpmc_get_fclk_period();
  153. return (time_ns * 1000 + tick_ps - 1) / tick_ps;
  154. }
  155. unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
  156. {
  157. unsigned long tick_ps;
  158. /* Calculate in picosecs to yield more exact results */
  159. tick_ps = gpmc_get_fclk_period();
  160. return (time_ps + tick_ps - 1) / tick_ps;
  161. }
  162. unsigned int gpmc_ticks_to_ns(unsigned int ticks)
  163. {
  164. return ticks * gpmc_get_fclk_period() / 1000;
  165. }
  166. unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns)
  167. {
  168. unsigned long ticks = gpmc_ns_to_ticks(time_ns);
  169. return ticks * gpmc_get_fclk_period() / 1000;
  170. }
  171. #ifdef DEBUG
  172. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  173. int time, const char *name)
  174. #else
  175. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  176. int time)
  177. #endif
  178. {
  179. u32 l;
  180. int ticks, mask, nr_bits;
  181. if (time == 0)
  182. ticks = 0;
  183. else
  184. ticks = gpmc_ns_to_ticks(time);
  185. nr_bits = end_bit - st_bit + 1;
  186. if (ticks >= 1 << nr_bits) {
  187. #ifdef DEBUG
  188. printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
  189. cs, name, time, ticks, 1 << nr_bits);
  190. #endif
  191. return -1;
  192. }
  193. mask = (1 << nr_bits) - 1;
  194. l = gpmc_cs_read_reg(cs, reg);
  195. #ifdef DEBUG
  196. printk(KERN_INFO
  197. "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
  198. cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
  199. (l >> st_bit) & mask, time);
  200. #endif
  201. l &= ~(mask << st_bit);
  202. l |= ticks << st_bit;
  203. gpmc_cs_write_reg(cs, reg, l);
  204. return 0;
  205. }
  206. #ifdef DEBUG
  207. #define GPMC_SET_ONE(reg, st, end, field) \
  208. if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
  209. t->field, #field) < 0) \
  210. return -1
  211. #else
  212. #define GPMC_SET_ONE(reg, st, end, field) \
  213. if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
  214. return -1
  215. #endif
  216. int gpmc_cs_calc_divider(int cs, unsigned int sync_clk)
  217. {
  218. int div;
  219. u32 l;
  220. l = sync_clk + (gpmc_get_fclk_period() - 1);
  221. div = l / gpmc_get_fclk_period();
  222. if (div > 4)
  223. return -1;
  224. if (div <= 0)
  225. div = 1;
  226. return div;
  227. }
  228. int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
  229. {
  230. int div;
  231. u32 l;
  232. div = gpmc_cs_calc_divider(cs, t->sync_clk);
  233. if (div < 0)
  234. return -1;
  235. GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
  236. GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
  237. GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
  238. GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
  239. GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
  240. GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
  241. GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
  242. GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
  243. GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
  244. GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
  245. GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
  246. GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
  247. GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
  248. GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
  249. if (cpu_is_omap34xx()) {
  250. GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
  251. GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
  252. }
  253. /* caller is expected to have initialized CONFIG1 to cover
  254. * at least sync vs async
  255. */
  256. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  257. if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
  258. #ifdef DEBUG
  259. printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
  260. cs, (div * gpmc_get_fclk_period()) / 1000, div);
  261. #endif
  262. l &= ~0x03;
  263. l |= (div - 1);
  264. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
  265. }
  266. return 0;
  267. }
  268. static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
  269. {
  270. u32 l;
  271. u32 mask;
  272. mask = (1 << GPMC_SECTION_SHIFT) - size;
  273. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  274. l &= ~0x3f;
  275. l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
  276. l &= ~(0x0f << 8);
  277. l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
  278. l |= GPMC_CONFIG7_CSVALID;
  279. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  280. }
  281. static void gpmc_cs_disable_mem(int cs)
  282. {
  283. u32 l;
  284. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  285. l &= ~GPMC_CONFIG7_CSVALID;
  286. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  287. }
  288. static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
  289. {
  290. u32 l;
  291. u32 mask;
  292. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  293. *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
  294. mask = (l >> 8) & 0x0f;
  295. *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
  296. }
  297. static int gpmc_cs_mem_enabled(int cs)
  298. {
  299. u32 l;
  300. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  301. return l & GPMC_CONFIG7_CSVALID;
  302. }
  303. int gpmc_cs_set_reserved(int cs, int reserved)
  304. {
  305. if (cs > GPMC_CS_NUM)
  306. return -ENODEV;
  307. gpmc_cs_map &= ~(1 << cs);
  308. gpmc_cs_map |= (reserved ? 1 : 0) << cs;
  309. return 0;
  310. }
  311. int gpmc_cs_reserved(int cs)
  312. {
  313. if (cs > GPMC_CS_NUM)
  314. return -ENODEV;
  315. return gpmc_cs_map & (1 << cs);
  316. }
  317. static unsigned long gpmc_mem_align(unsigned long size)
  318. {
  319. int order;
  320. size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
  321. order = GPMC_CHUNK_SHIFT - 1;
  322. do {
  323. size >>= 1;
  324. order++;
  325. } while (size);
  326. size = 1 << order;
  327. return size;
  328. }
  329. static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
  330. {
  331. struct resource *res = &gpmc_cs_mem[cs];
  332. int r;
  333. size = gpmc_mem_align(size);
  334. spin_lock(&gpmc_mem_lock);
  335. res->start = base;
  336. res->end = base + size - 1;
  337. r = request_resource(&gpmc_mem_root, res);
  338. spin_unlock(&gpmc_mem_lock);
  339. return r;
  340. }
  341. int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
  342. {
  343. struct resource *res = &gpmc_cs_mem[cs];
  344. int r = -1;
  345. if (cs > GPMC_CS_NUM)
  346. return -ENODEV;
  347. size = gpmc_mem_align(size);
  348. if (size > (1 << GPMC_SECTION_SHIFT))
  349. return -ENOMEM;
  350. spin_lock(&gpmc_mem_lock);
  351. if (gpmc_cs_reserved(cs)) {
  352. r = -EBUSY;
  353. goto out;
  354. }
  355. if (gpmc_cs_mem_enabled(cs))
  356. r = adjust_resource(res, res->start & ~(size - 1), size);
  357. if (r < 0)
  358. r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
  359. size, NULL, NULL);
  360. if (r < 0)
  361. goto out;
  362. gpmc_cs_enable_mem(cs, res->start, resource_size(res));
  363. *base = res->start;
  364. gpmc_cs_set_reserved(cs, 1);
  365. out:
  366. spin_unlock(&gpmc_mem_lock);
  367. return r;
  368. }
  369. EXPORT_SYMBOL(gpmc_cs_request);
  370. void gpmc_cs_free(int cs)
  371. {
  372. spin_lock(&gpmc_mem_lock);
  373. if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) {
  374. printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
  375. BUG();
  376. spin_unlock(&gpmc_mem_lock);
  377. return;
  378. }
  379. gpmc_cs_disable_mem(cs);
  380. release_resource(&gpmc_cs_mem[cs]);
  381. gpmc_cs_set_reserved(cs, 0);
  382. spin_unlock(&gpmc_mem_lock);
  383. }
  384. EXPORT_SYMBOL(gpmc_cs_free);
  385. /**
  386. * gpmc_read_status - read access request to get the different gpmc status
  387. * @cmd: command type
  388. * @return status
  389. */
  390. int gpmc_read_status(int cmd)
  391. {
  392. int status = -EINVAL;
  393. u32 regval = 0;
  394. switch (cmd) {
  395. case GPMC_GET_IRQ_STATUS:
  396. status = gpmc_read_reg(GPMC_IRQSTATUS);
  397. break;
  398. case GPMC_PREFETCH_FIFO_CNT:
  399. regval = gpmc_read_reg(GPMC_PREFETCH_STATUS);
  400. status = GPMC_PREFETCH_STATUS_FIFO_CNT(regval);
  401. break;
  402. case GPMC_PREFETCH_COUNT:
  403. regval = gpmc_read_reg(GPMC_PREFETCH_STATUS);
  404. status = GPMC_PREFETCH_STATUS_COUNT(regval);
  405. break;
  406. case GPMC_STATUS_BUFFER:
  407. regval = gpmc_read_reg(GPMC_STATUS);
  408. /* 1 : buffer is available to write */
  409. status = regval & GPMC_STATUS_BUFF_EMPTY;
  410. break;
  411. default:
  412. printk(KERN_ERR "gpmc_read_status: Not supported\n");
  413. }
  414. return status;
  415. }
  416. EXPORT_SYMBOL(gpmc_read_status);
  417. /**
  418. * gpmc_cs_configure - write request to configure gpmc
  419. * @cs: chip select number
  420. * @cmd: command type
  421. * @wval: value to write
  422. * @return status of the operation
  423. */
  424. int gpmc_cs_configure(int cs, int cmd, int wval)
  425. {
  426. int err = 0;
  427. u32 regval = 0;
  428. switch (cmd) {
  429. case GPMC_ENABLE_IRQ:
  430. gpmc_write_reg(GPMC_IRQENABLE, wval);
  431. break;
  432. case GPMC_SET_IRQ_STATUS:
  433. gpmc_write_reg(GPMC_IRQSTATUS, wval);
  434. break;
  435. case GPMC_CONFIG_WP:
  436. regval = gpmc_read_reg(GPMC_CONFIG);
  437. if (wval)
  438. regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
  439. else
  440. regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
  441. gpmc_write_reg(GPMC_CONFIG, regval);
  442. break;
  443. case GPMC_CONFIG_RDY_BSY:
  444. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  445. if (wval)
  446. regval |= WR_RD_PIN_MONITORING;
  447. else
  448. regval &= ~WR_RD_PIN_MONITORING;
  449. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  450. break;
  451. case GPMC_CONFIG_DEV_SIZE:
  452. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  453. /* clear 2 target bits */
  454. regval &= ~GPMC_CONFIG1_DEVICESIZE(3);
  455. /* set the proper value */
  456. regval |= GPMC_CONFIG1_DEVICESIZE(wval);
  457. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  458. break;
  459. case GPMC_CONFIG_DEV_TYPE:
  460. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  461. regval |= GPMC_CONFIG1_DEVICETYPE(wval);
  462. if (wval == GPMC_DEVICETYPE_NOR)
  463. regval |= GPMC_CONFIG1_MUXADDDATA;
  464. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  465. break;
  466. default:
  467. printk(KERN_ERR "gpmc_configure_cs: Not supported\n");
  468. err = -EINVAL;
  469. }
  470. return err;
  471. }
  472. EXPORT_SYMBOL(gpmc_cs_configure);
  473. /**
  474. * gpmc_nand_read - nand specific read access request
  475. * @cs: chip select number
  476. * @cmd: command type
  477. */
  478. int gpmc_nand_read(int cs, int cmd)
  479. {
  480. int rval = -EINVAL;
  481. switch (cmd) {
  482. case GPMC_NAND_DATA:
  483. rval = gpmc_cs_read_byte(cs, GPMC_CS_NAND_DATA);
  484. break;
  485. default:
  486. printk(KERN_ERR "gpmc_read_nand_ctrl: Not supported\n");
  487. }
  488. return rval;
  489. }
  490. EXPORT_SYMBOL(gpmc_nand_read);
  491. /**
  492. * gpmc_nand_write - nand specific write request
  493. * @cs: chip select number
  494. * @cmd: command type
  495. * @wval: value to write
  496. */
  497. int gpmc_nand_write(int cs, int cmd, int wval)
  498. {
  499. int err = 0;
  500. switch (cmd) {
  501. case GPMC_NAND_COMMAND:
  502. gpmc_cs_write_byte(cs, GPMC_CS_NAND_COMMAND, wval);
  503. break;
  504. case GPMC_NAND_ADDRESS:
  505. gpmc_cs_write_byte(cs, GPMC_CS_NAND_ADDRESS, wval);
  506. break;
  507. case GPMC_NAND_DATA:
  508. gpmc_cs_write_byte(cs, GPMC_CS_NAND_DATA, wval);
  509. default:
  510. printk(KERN_ERR "gpmc_write_nand_ctrl: Not supported\n");
  511. err = -EINVAL;
  512. }
  513. return err;
  514. }
  515. EXPORT_SYMBOL(gpmc_nand_write);
  516. /**
  517. * gpmc_prefetch_enable - configures and starts prefetch transfer
  518. * @cs: cs (chip select) number
  519. * @fifo_th: fifo threshold to be used for read/ write
  520. * @dma_mode: dma mode enable (1) or disable (0)
  521. * @u32_count: number of bytes to be transferred
  522. * @is_write: prefetch read(0) or write post(1) mode
  523. */
  524. int gpmc_prefetch_enable(int cs, int fifo_th, int dma_mode,
  525. unsigned int u32_count, int is_write)
  526. {
  527. if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX) {
  528. pr_err("gpmc: fifo threshold is not supported\n");
  529. return -1;
  530. } else if (!(gpmc_read_reg(GPMC_PREFETCH_CONTROL))) {
  531. /* Set the amount of bytes to be prefetched */
  532. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, u32_count);
  533. /* Set dma/mpu mode, the prefetch read / post write and
  534. * enable the engine. Set which cs is has requested for.
  535. */
  536. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, ((cs << CS_NUM_SHIFT) |
  537. PREFETCH_FIFOTHRESHOLD(fifo_th) |
  538. ENABLE_PREFETCH |
  539. (dma_mode << DMA_MPU_MODE) |
  540. (0x1 & is_write)));
  541. /* Start the prefetch engine */
  542. gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x1);
  543. } else {
  544. return -EBUSY;
  545. }
  546. return 0;
  547. }
  548. EXPORT_SYMBOL(gpmc_prefetch_enable);
  549. /**
  550. * gpmc_prefetch_reset - disables and stops the prefetch engine
  551. */
  552. int gpmc_prefetch_reset(int cs)
  553. {
  554. u32 config1;
  555. /* check if the same module/cs is trying to reset */
  556. config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
  557. if (((config1 >> CS_NUM_SHIFT) & 0x7) != cs)
  558. return -EINVAL;
  559. /* Stop the PFPW engine */
  560. gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x0);
  561. /* Reset/disable the PFPW engine */
  562. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, 0x0);
  563. return 0;
  564. }
  565. EXPORT_SYMBOL(gpmc_prefetch_reset);
  566. static void __init gpmc_mem_init(void)
  567. {
  568. int cs;
  569. unsigned long boot_rom_space = 0;
  570. /* never allocate the first page, to facilitate bug detection;
  571. * even if we didn't boot from ROM.
  572. */
  573. boot_rom_space = BOOT_ROM_SPACE;
  574. /* In apollon the CS0 is mapped as 0x0000 0000 */
  575. if (machine_is_omap_apollon())
  576. boot_rom_space = 0;
  577. gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
  578. gpmc_mem_root.end = GPMC_MEM_END;
  579. /* Reserve all regions that has been set up by bootloader */
  580. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  581. u32 base, size;
  582. if (!gpmc_cs_mem_enabled(cs))
  583. continue;
  584. gpmc_cs_get_memconf(cs, &base, &size);
  585. if (gpmc_cs_insert_mem(cs, base, size) < 0)
  586. BUG();
  587. }
  588. }
  589. static int __init gpmc_init(void)
  590. {
  591. u32 l, irq;
  592. int cs, ret = -EINVAL;
  593. int gpmc_irq;
  594. char *ck = NULL;
  595. if (cpu_is_omap24xx()) {
  596. ck = "core_l3_ck";
  597. if (cpu_is_omap2420())
  598. l = OMAP2420_GPMC_BASE;
  599. else
  600. l = OMAP34XX_GPMC_BASE;
  601. gpmc_irq = INT_34XX_GPMC_IRQ;
  602. } else if (cpu_is_omap34xx()) {
  603. ck = "gpmc_fck";
  604. l = OMAP34XX_GPMC_BASE;
  605. gpmc_irq = INT_34XX_GPMC_IRQ;
  606. } else if (cpu_is_omap44xx()) {
  607. ck = "gpmc_ck";
  608. l = OMAP44XX_GPMC_BASE;
  609. gpmc_irq = OMAP44XX_IRQ_GPMC;
  610. }
  611. if (WARN_ON(!ck))
  612. return ret;
  613. gpmc_l3_clk = clk_get(NULL, ck);
  614. if (IS_ERR(gpmc_l3_clk)) {
  615. printk(KERN_ERR "Could not get GPMC clock %s\n", ck);
  616. BUG();
  617. }
  618. gpmc_base = ioremap(l, SZ_4K);
  619. if (!gpmc_base) {
  620. clk_put(gpmc_l3_clk);
  621. printk(KERN_ERR "Could not get GPMC register memory\n");
  622. BUG();
  623. }
  624. clk_enable(gpmc_l3_clk);
  625. l = gpmc_read_reg(GPMC_REVISION);
  626. printk(KERN_INFO "GPMC revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
  627. /* Set smart idle mode and automatic L3 clock gating */
  628. l = gpmc_read_reg(GPMC_SYSCONFIG);
  629. l &= 0x03 << 3;
  630. l |= (0x02 << 3) | (1 << 0);
  631. gpmc_write_reg(GPMC_SYSCONFIG, l);
  632. gpmc_mem_init();
  633. /* initalize the irq_chained */
  634. irq = OMAP_GPMC_IRQ_BASE;
  635. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  636. irq_set_chip_and_handler(irq, &dummy_irq_chip,
  637. handle_simple_irq);
  638. set_irq_flags(irq, IRQF_VALID);
  639. irq++;
  640. }
  641. ret = request_irq(gpmc_irq, gpmc_handle_irq, IRQF_SHARED, "gpmc", NULL);
  642. if (ret)
  643. pr_err("gpmc: irq-%d could not claim: err %d\n",
  644. gpmc_irq, ret);
  645. return ret;
  646. }
  647. postcore_initcall(gpmc_init);
  648. static irqreturn_t gpmc_handle_irq(int irq, void *dev)
  649. {
  650. u8 cs;
  651. /* check cs to invoke the irq */
  652. cs = ((gpmc_read_reg(GPMC_PREFETCH_CONFIG1)) >> CS_NUM_SHIFT) & 0x7;
  653. if (OMAP_GPMC_IRQ_BASE+cs <= OMAP_GPMC_IRQ_END)
  654. generic_handle_irq(OMAP_GPMC_IRQ_BASE+cs);
  655. return IRQ_HANDLED;
  656. }
  657. #ifdef CONFIG_ARCH_OMAP3
  658. static struct omap3_gpmc_regs gpmc_context;
  659. void omap3_gpmc_save_context(void)
  660. {
  661. int i;
  662. gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
  663. gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
  664. gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
  665. gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
  666. gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
  667. gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
  668. gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
  669. for (i = 0; i < GPMC_CS_NUM; i++) {
  670. gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
  671. if (gpmc_context.cs_context[i].is_valid) {
  672. gpmc_context.cs_context[i].config1 =
  673. gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
  674. gpmc_context.cs_context[i].config2 =
  675. gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
  676. gpmc_context.cs_context[i].config3 =
  677. gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
  678. gpmc_context.cs_context[i].config4 =
  679. gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
  680. gpmc_context.cs_context[i].config5 =
  681. gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
  682. gpmc_context.cs_context[i].config6 =
  683. gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
  684. gpmc_context.cs_context[i].config7 =
  685. gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
  686. }
  687. }
  688. }
  689. void omap3_gpmc_restore_context(void)
  690. {
  691. int i;
  692. gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
  693. gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
  694. gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
  695. gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
  696. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
  697. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
  698. gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
  699. for (i = 0; i < GPMC_CS_NUM; i++) {
  700. if (gpmc_context.cs_context[i].is_valid) {
  701. gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
  702. gpmc_context.cs_context[i].config1);
  703. gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
  704. gpmc_context.cs_context[i].config2);
  705. gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
  706. gpmc_context.cs_context[i].config3);
  707. gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
  708. gpmc_context.cs_context[i].config4);
  709. gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
  710. gpmc_context.cs_context[i].config5);
  711. gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
  712. gpmc_context.cs_context[i].config6);
  713. gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
  714. gpmc_context.cs_context[i].config7);
  715. }
  716. }
  717. }
  718. #endif /* CONFIG_ARCH_OMAP3 */
  719. /**
  720. * gpmc_enable_hwecc - enable hardware ecc functionality
  721. * @cs: chip select number
  722. * @mode: read/write mode
  723. * @dev_width: device bus width(1 for x16, 0 for x8)
  724. * @ecc_size: bytes for which ECC will be generated
  725. */
  726. int gpmc_enable_hwecc(int cs, int mode, int dev_width, int ecc_size)
  727. {
  728. unsigned int val;
  729. /* check if ecc module is in used */
  730. if (gpmc_ecc_used != -EINVAL)
  731. return -EINVAL;
  732. gpmc_ecc_used = cs;
  733. /* clear ecc and enable bits */
  734. gpmc_write_reg(GPMC_ECC_CONTROL,
  735. GPMC_ECC_CTRL_ECCCLEAR |
  736. GPMC_ECC_CTRL_ECCREG1);
  737. /* program ecc and result sizes */
  738. val = ((((ecc_size >> 1) - 1) << 22) | (0x0000000F));
  739. gpmc_write_reg(GPMC_ECC_SIZE_CONFIG, val);
  740. switch (mode) {
  741. case GPMC_ECC_READ:
  742. case GPMC_ECC_WRITE:
  743. gpmc_write_reg(GPMC_ECC_CONTROL,
  744. GPMC_ECC_CTRL_ECCCLEAR |
  745. GPMC_ECC_CTRL_ECCREG1);
  746. break;
  747. case GPMC_ECC_READSYN:
  748. gpmc_write_reg(GPMC_ECC_CONTROL,
  749. GPMC_ECC_CTRL_ECCCLEAR |
  750. GPMC_ECC_CTRL_ECCDISABLE);
  751. break;
  752. default:
  753. printk(KERN_INFO "Error: Unrecognized Mode[%d]!\n", mode);
  754. break;
  755. }
  756. /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
  757. val = (dev_width << 7) | (cs << 1) | (0x1);
  758. gpmc_write_reg(GPMC_ECC_CONFIG, val);
  759. return 0;
  760. }
  761. EXPORT_SYMBOL_GPL(gpmc_enable_hwecc);
  762. /**
  763. * gpmc_calculate_ecc - generate non-inverted ecc bytes
  764. * @cs: chip select number
  765. * @dat: data pointer over which ecc is computed
  766. * @ecc_code: ecc code buffer
  767. *
  768. * Using non-inverted ECC is considered ugly since writing a blank
  769. * page (padding) will clear the ECC bytes. This is not a problem as long
  770. * no one is trying to write data on the seemingly unused page. Reading
  771. * an erased page will produce an ECC mismatch between generated and read
  772. * ECC bytes that has to be dealt with separately.
  773. */
  774. int gpmc_calculate_ecc(int cs, const u_char *dat, u_char *ecc_code)
  775. {
  776. unsigned int val = 0x0;
  777. if (gpmc_ecc_used != cs)
  778. return -EINVAL;
  779. /* read ecc result */
  780. val = gpmc_read_reg(GPMC_ECC1_RESULT);
  781. *ecc_code++ = val; /* P128e, ..., P1e */
  782. *ecc_code++ = val >> 16; /* P128o, ..., P1o */
  783. /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
  784. *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
  785. gpmc_ecc_used = -EINVAL;
  786. return 0;
  787. }
  788. EXPORT_SYMBOL_GPL(gpmc_calculate_ecc);
  789. #ifdef CONFIG_ARCH_OMAP3
  790. /**
  791. * gpmc_init_hwecc_bch - initialize hardware BCH ecc functionality
  792. * @cs: chip select number
  793. * @nsectors: how many 512-byte sectors to process
  794. * @nerrors: how many errors to correct per sector (4 or 8)
  795. *
  796. * This function must be executed before any call to gpmc_enable_hwecc_bch.
  797. */
  798. int gpmc_init_hwecc_bch(int cs, int nsectors, int nerrors)
  799. {
  800. /* check if ecc module is in use */
  801. if (gpmc_ecc_used != -EINVAL)
  802. return -EINVAL;
  803. /* support only OMAP3 class */
  804. if (!cpu_is_omap34xx()) {
  805. printk(KERN_ERR "BCH ecc is not supported on this CPU\n");
  806. return -EINVAL;
  807. }
  808. /*
  809. * For now, assume 4-bit mode is only supported on OMAP3630 ES1.x, x>=1.
  810. * Other chips may be added if confirmed to work.
  811. */
  812. if ((nerrors == 4) &&
  813. (!cpu_is_omap3630() || (GET_OMAP_REVISION() == 0))) {
  814. printk(KERN_ERR "BCH 4-bit mode is not supported on this CPU\n");
  815. return -EINVAL;
  816. }
  817. /* sanity check */
  818. if (nsectors > 8) {
  819. printk(KERN_ERR "BCH cannot process %d sectors (max is 8)\n",
  820. nsectors);
  821. return -EINVAL;
  822. }
  823. return 0;
  824. }
  825. EXPORT_SYMBOL_GPL(gpmc_init_hwecc_bch);
  826. /**
  827. * gpmc_enable_hwecc_bch - enable hardware BCH ecc functionality
  828. * @cs: chip select number
  829. * @mode: read/write mode
  830. * @dev_width: device bus width(1 for x16, 0 for x8)
  831. * @nsectors: how many 512-byte sectors to process
  832. * @nerrors: how many errors to correct per sector (4 or 8)
  833. */
  834. int gpmc_enable_hwecc_bch(int cs, int mode, int dev_width, int nsectors,
  835. int nerrors)
  836. {
  837. unsigned int val;
  838. /* check if ecc module is in use */
  839. if (gpmc_ecc_used != -EINVAL)
  840. return -EINVAL;
  841. gpmc_ecc_used = cs;
  842. /* clear ecc and enable bits */
  843. gpmc_write_reg(GPMC_ECC_CONTROL, 0x1);
  844. /*
  845. * When using BCH, sector size is hardcoded to 512 bytes.
  846. * Here we are using wrapping mode 6 both for reading and writing, with:
  847. * size0 = 0 (no additional protected byte in spare area)
  848. * size1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
  849. */
  850. gpmc_write_reg(GPMC_ECC_SIZE_CONFIG, (32 << 22) | (0 << 12));
  851. /* BCH configuration */
  852. val = ((1 << 16) | /* enable BCH */
  853. (((nerrors == 8) ? 1 : 0) << 12) | /* 8 or 4 bits */
  854. (0x06 << 8) | /* wrap mode = 6 */
  855. (dev_width << 7) | /* bus width */
  856. (((nsectors-1) & 0x7) << 4) | /* number of sectors */
  857. (cs << 1) | /* ECC CS */
  858. (0x1)); /* enable ECC */
  859. gpmc_write_reg(GPMC_ECC_CONFIG, val);
  860. gpmc_write_reg(GPMC_ECC_CONTROL, 0x101);
  861. return 0;
  862. }
  863. EXPORT_SYMBOL_GPL(gpmc_enable_hwecc_bch);
  864. /**
  865. * gpmc_calculate_ecc_bch4 - Generate 7 ecc bytes per sector of 512 data bytes
  866. * @cs: chip select number
  867. * @dat: The pointer to data on which ecc is computed
  868. * @ecc: The ecc output buffer
  869. */
  870. int gpmc_calculate_ecc_bch4(int cs, const u_char *dat, u_char *ecc)
  871. {
  872. int i;
  873. unsigned long nsectors, reg, val1, val2;
  874. if (gpmc_ecc_used != cs)
  875. return -EINVAL;
  876. nsectors = ((gpmc_read_reg(GPMC_ECC_CONFIG) >> 4) & 0x7) + 1;
  877. for (i = 0; i < nsectors; i++) {
  878. reg = GPMC_ECC_BCH_RESULT_0 + 16*i;
  879. /* Read hw-computed remainder */
  880. val1 = gpmc_read_reg(reg + 0);
  881. val2 = gpmc_read_reg(reg + 4);
  882. /*
  883. * Add constant polynomial to remainder, in order to get an ecc
  884. * sequence of 0xFFs for a buffer filled with 0xFFs; and
  885. * left-justify the resulting polynomial.
  886. */
  887. *ecc++ = 0x28 ^ ((val2 >> 12) & 0xFF);
  888. *ecc++ = 0x13 ^ ((val2 >> 4) & 0xFF);
  889. *ecc++ = 0xcc ^ (((val2 & 0xF) << 4)|((val1 >> 28) & 0xF));
  890. *ecc++ = 0x39 ^ ((val1 >> 20) & 0xFF);
  891. *ecc++ = 0x96 ^ ((val1 >> 12) & 0xFF);
  892. *ecc++ = 0xac ^ ((val1 >> 4) & 0xFF);
  893. *ecc++ = 0x7f ^ ((val1 & 0xF) << 4);
  894. }
  895. gpmc_ecc_used = -EINVAL;
  896. return 0;
  897. }
  898. EXPORT_SYMBOL_GPL(gpmc_calculate_ecc_bch4);
  899. /**
  900. * gpmc_calculate_ecc_bch8 - Generate 13 ecc bytes per block of 512 data bytes
  901. * @cs: chip select number
  902. * @dat: The pointer to data on which ecc is computed
  903. * @ecc: The ecc output buffer
  904. */
  905. int gpmc_calculate_ecc_bch8(int cs, const u_char *dat, u_char *ecc)
  906. {
  907. int i;
  908. unsigned long nsectors, reg, val1, val2, val3, val4;
  909. if (gpmc_ecc_used != cs)
  910. return -EINVAL;
  911. nsectors = ((gpmc_read_reg(GPMC_ECC_CONFIG) >> 4) & 0x7) + 1;
  912. for (i = 0; i < nsectors; i++) {
  913. reg = GPMC_ECC_BCH_RESULT_0 + 16*i;
  914. /* Read hw-computed remainder */
  915. val1 = gpmc_read_reg(reg + 0);
  916. val2 = gpmc_read_reg(reg + 4);
  917. val3 = gpmc_read_reg(reg + 8);
  918. val4 = gpmc_read_reg(reg + 12);
  919. /*
  920. * Add constant polynomial to remainder, in order to get an ecc
  921. * sequence of 0xFFs for a buffer filled with 0xFFs.
  922. */
  923. *ecc++ = 0xef ^ (val4 & 0xFF);
  924. *ecc++ = 0x51 ^ ((val3 >> 24) & 0xFF);
  925. *ecc++ = 0x2e ^ ((val3 >> 16) & 0xFF);
  926. *ecc++ = 0x09 ^ ((val3 >> 8) & 0xFF);
  927. *ecc++ = 0xed ^ (val3 & 0xFF);
  928. *ecc++ = 0x93 ^ ((val2 >> 24) & 0xFF);
  929. *ecc++ = 0x9a ^ ((val2 >> 16) & 0xFF);
  930. *ecc++ = 0xc2 ^ ((val2 >> 8) & 0xFF);
  931. *ecc++ = 0x97 ^ (val2 & 0xFF);
  932. *ecc++ = 0x79 ^ ((val1 >> 24) & 0xFF);
  933. *ecc++ = 0xe5 ^ ((val1 >> 16) & 0xFF);
  934. *ecc++ = 0x24 ^ ((val1 >> 8) & 0xFF);
  935. *ecc++ = 0xb5 ^ (val1 & 0xFF);
  936. }
  937. gpmc_ecc_used = -EINVAL;
  938. return 0;
  939. }
  940. EXPORT_SYMBOL_GPL(gpmc_calculate_ecc_bch8);
  941. #endif /* CONFIG_ARCH_OMAP3 */